From 059c971ee2501befae0fc493f8ab52d1e45e3856 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 28 Aug 2024 12:28:58 +1000 Subject: [PATCH] updateGroupViaLogs: Save new endorsements only if at latest revision --- ts/groups.ts | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/ts/groups.ts b/ts/groups.ts index 369e9a548b65..7e72a5cd624b 100644 --- a/ts/groups.ts +++ b/ts/groups.ts @@ -3908,6 +3908,41 @@ async function updateGroupViaSingleChange({ return singleChangeResult; } +function getLastRevisionFromChanges( + changes: ReadonlyArray +): number | undefined { + for (let i = changes.length - 1; i >= 0; i -= 1) { + const change = changes[i]; + if (!change) { + continue; + } + + const { groupChanges } = change; + if (!groupChanges) { + continue; + } + + for (let j = groupChanges.length - 1; j >= 0; j -= 1) { + const groupChange = groupChanges[j]; + if (!groupChange) { + continue; + } + + const { groupState } = groupChange; + if (!groupState) { + continue; + } + + const { version } = groupState; + if (isNumber(version)) { + return version; + } + } + } + + return undefined; +} + async function updateGroupViaLogs({ group, newRevision, @@ -4008,8 +4043,14 @@ async function updateGroupViaLogs({ newRevision, }); - // If we're not in the group, we won't receive endorsements - if (Bytes.isNotEmpty(groupSendEndorsementResponse)) { + const currentVersion = response.paginated + ? response.currentRevision + : getLastRevisionFromChanges(changes); + const isAtLatestVersion = + isNumber(currentVersion) && + updates.newAttributes.revision === currentVersion; + + if (isAtLatestVersion && Bytes.isNotEmpty(groupSendEndorsementResponse)) { try { log.info(`updateGroupViaLogs/${logId}: Saving group endorsements`); // Use the latest state of the group after applying changes