updateGroupViaLogs: Save new endorsements only if at latest revision

This commit is contained in:
Scott Nonnenberg 2024-08-28 12:28:58 +10:00 committed by GitHub
parent 8ef149e3a8
commit 059c971ee2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3908,6 +3908,41 @@ async function updateGroupViaSingleChange({
return singleChangeResult;
}
function getLastRevisionFromChanges(
changes: ReadonlyArray<Proto.IGroupChanges>
): 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