Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Added getNextNonceForUpdate #562

Merged
merged 1 commit into from
Apr 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions modules/protocol/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,27 @@ export const mergeAssetIds = (channel: FullChannelState): FullChannelState => {
defundNonces,
};
};


// Returns the first unused nonce for the given participant.
// Nonces alternate back and forth like so:
// 0: Alice
// 1: Alice
// 2: Bob
// 3: Bob
// 4: Alice
// 5: Alice
// 6: Bob
// 7: Bob
//
// Examples:
// (0, true) => 1
// (0, false) => 2
// (1, true) => 4
export function getNextNonceForUpdate(highestSeenNonce: number, isAlice: boolean): number {
let rotation = highestSeenNonce % 4;
let currentlyMe = rotation < 2 === isAlice;
let top = highestSeenNonce % 2 === 1;
let offset = currentlyMe ? (top ? 3 : 1) : (top ? 1 : 2);
return highestSeenNonce + offset;
}