bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.83k stars 2.08k forks source link

Draft: Nonce Manager #3548

Closed leolambo closed 1 year ago

leolambo commented 1 year ago

Create a NonceManager and NonceService that is designed to correctly assign nonces to TXs.

A NonceManager(NM) is related to a wallet, address, and chain, Each NonceManager has a slotBuffer that represents a circular queue.

The NonceService interacts with the NM and will do the following based on the txp's current state.

Create: (changing this to the point of signing) Gets the appropriate NonceManager, creates a new NM is one doesnt exist. assigns the txp to a NonceSlot on the NM. returns the associated nonce from NM to the txp

Sign: Checks if the current txp being signed matches the head of the NM slotBuffer if not, throws error.

Broadcast: Remove the head NonceSlot of the NM's slotBuffer.

Remove: Updates the NM slotBuffer by deleting the removed txp then updating all TXP's with a higher nonce by decrementing their nonce value by 1 then and removing any signatures thus requiring them to be reaccepted

Error or Reject: Free's the slot associated with the txp found in the NM. Now the freed nonce slot will be available to any TXP attempting to assign.