Preserve ACK ownership across contact relocation and retirement

This commit is contained in:
mikecarper committed 2026-10-03 21:10:49 -07:00
1 parent d29cba046b
commit ea938e8e84
15 files changed
+907 -50

No files matched your search

+18 -13
View File
@@ -1060,7 +1060,15 @@ MyMesh::AckTableEntry* MyMesh::findPendingTextMessage(
return NULL;
}
ContactInfo* MyMesh::processAck(const uint8_t *data) {
void MyMesh::onContactReferenceChanged(const ContactInfo* previous, ContactInfo* replacement) {
for (AckTableEntry* entry = expected_ack_table;
entry != expected_ack_table + EXPECTED_ACK_TABLE_SIZE; ++entry) {
if (entry->ack != 0 && entry->contact == previous) entry->contact = replacement;
}
}
bool MyMesh::processAck(const uint8_t *data, ContactInfo*& peer) {
peer = NULL;
expireExpectedAcks();
uint32_t incoming;
memcpy(&incoming, data, sizeof(incoming));
@@ -1090,10 +1098,11 @@ ContactInfo* MyMesh::processAck(const uint8_t *data) {
if (contact != NULL && contact->type == ADV_TYPE_CHAT) rememberOneKeyAck(*contact);
#endif
expireExpectedAcks();
return contact;
peer = contact;
return true;
}
}
return checkConnectionsAck(data);
return checkConnectionsAck(data, peer);
}
#if MESH_ENABLE_ONE_KEY_DM
@@ -1260,7 +1269,7 @@ void MyMesh::onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret,
// A prior anonymous service request may occupy a transient slot for this
// key. Remove it so regular text lookup finds the durable contact first.
ContactInfo* transient = lookupTransientContactByPubKey(sender.pub_key, PUB_KEY_SIZE);
if (transient != NULL) clearTransientContact(*transient);
if (transient != NULL) clearTransientContact(*transient, added);
onDiscoveredContact(*added, true, OUT_PATH_UNKNOWN, NULL);
}
@@ -5583,7 +5592,7 @@ void MyMesh::handleCmdFrame(size_t len) {
if (scheduleContactWrite(*recipient)) {
ContactInfo* transient = lookupTransientContactByPubKey(
pub_key, PUB_KEY_SIZE);
if (transient != NULL) clearTransientContact(*transient);
if (transient != NULL) clearTransientContact(*transient, recipient);
writeOKFrame();
} else {
recipient->out_path_len = previous_out_path_len;
@@ -5613,8 +5622,6 @@ void MyMesh::handleCmdFrame(size_t len) {
ContactInfo* transient = lookupTransientContactByPubKey(
pub_key, PUB_KEY_SIZE);
ContactInfo previous_transient = {};
if (transient != NULL) previous_transient = *transient;
if (recipient != NULL) {
const ContactInfo previous = *recipient;
@@ -5622,7 +5629,7 @@ void MyMesh::handleCmdFrame(size_t len) {
if (scheduleContactWrite(*recipient)) {
// A prior anonymous request can leave a same-key entry in the
// reserved prefix. Retire it in place; never compact that prefix.
if (transient != NULL) clearTransientContact(*transient);
if (transient != NULL) clearTransientContact(*transient, recipient);
updateGpsTelemetryPolicy();
writeOKFrame();
} else {
@@ -5630,22 +5637,20 @@ void MyMesh::handleCmdFrame(size_t len) {
writeErrFrame(ERR_CODE_FILE_IO_ERROR);
}
} else {
// Promotion must free the exact transient prefix slot before lookup
// of the newly-added persistent record. Restore it on every failure.
if (transient != NULL) clearTransientContact(*transient);
// Preserve the transient and its pending ACK references until the
// persistent destination is accepted. Failure preserves the transient.
if (addContact(candidate)) {
ContactInfo* added = lookupPersistentContactByPubKey(
candidate.id.pub_key, PUB_KEY_SIZE);
if (added != NULL && scheduleContactWrite(*added)) {
if (transient != NULL) clearTransientContact(*transient, added);
updateGpsTelemetryPolicy();
writeOKFrame();
} else {
if (added != NULL) removeContact(*added);
if (transient != NULL) *transient = previous_transient;
writeErrFrame(ERR_CODE_FILE_IO_ERROR);
}
} else {
if (transient != NULL) *transient = previous_transient;
writeErrFrame(ERR_CODE_TABLE_FULL);
}
}
+2 -1
View File
@@ -370,13 +370,14 @@ protected:
bool canMutateContacts() const override;
void onContactsFull() override;
bool onContactOverwrite(const ContactInfo& contact) override;
void onContactReferenceChanged(const ContactInfo* previous, ContactInfo* replacement) override;
bool onContactPathRecv(ContactInfo& from, uint8_t* in_path, uint8_t in_path_len, uint8_t* out_path, uint8_t out_path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
void onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) override;
void onContactPathUpdated(const ContactInfo &contact) override;
#if COMPANION_FEATURE_TEXT_TERMINAL
void onContactVisit(const ContactInfo& contact) override;
#endif
ContactInfo* processAck(const uint8_t *data) override;
bool processAck(const uint8_t *data, ContactInfo*& peer) override;
#if MESH_ENABLE_ONE_KEY_DM
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret,
const mesh::Identity& sender, uint8_t* data,
+5 -4
View File
@@ -240,18 +240,19 @@ protected:
saveContacts();
}
ContactInfo* processAck(const uint8_t *data) override {
if (memcmp(data, &expected_ack_crc, 4) == 0) { // got an ACK from recipient
bool processAck(const uint8_t *data, ContactInfo*& peer) override {
peer = NULL;
if (expected_ack_crc != 0 && memcmp(data, &expected_ack_crc, 4) == 0) { // got an ACK from recipient
Serial.printf(" Got ACK! (round trip: %d millis)\n", _ms->getMillis() - last_msg_sent);
// NOTE: the same ACK can be received multiple times!
expected_ack_crc = 0; // reset our expected hash, now that we have received ACK
return NULL; // TODO: really should return ContactInfo pointer
return true; // owned, even without a retained contact pointer
}
//uint32_t crc;
//memcpy(&crc, data, 4);
//MESH_DEBUG_PRINTLN("unknown ACK received: %08X (expected: %08X)", crc, expected_ack_crc);
return NULL;
return false;
}
void onMessageRecv(const ContactInfo& from, mesh::Packet* pkt, uint32_t sender_timestamp, const char *text) override {