mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-02 13:43:06 +00:00
There's a new byteutils in newer versions of stew - also, remove upraises and disable windows testing which requires SSL library install
35 lines
939 B
Nim
35 lines
939 B
Nim
import std/tables
|
|
import std/sets
|
|
import std/hashes
|
|
import ../basics
|
|
|
|
{.push raises: [].}
|
|
|
|
type
|
|
Nonces* = object
|
|
next: Table[NonceKey, UInt48]
|
|
NonceKey = object
|
|
chainId: UInt256
|
|
participants: HashSet[EthAddress]
|
|
|
|
func hash(key: NonceKey): Hash =
|
|
var h: Hash
|
|
h = h !& key.chainId.hash
|
|
h = h !& key.participants.hash
|
|
!$h
|
|
|
|
func key(chainId: UInt256, participants: openArray[EthAddress]): NonceKey =
|
|
NonceKey(chainId: chainId, participants: participants.toHashSet)
|
|
|
|
func getNonce*(nonces: var Nonces,
|
|
chainId: UInt256,
|
|
participants: varargs[EthAddress]): UInt48 =
|
|
nonces.next.?[key(chainId, participants)] |? 0
|
|
|
|
func incNonce*(nonces: var Nonces,
|
|
oldNonce: UInt48,
|
|
chainId: UInt256,
|
|
participants: varargs[EthAddress]) =
|
|
let next = max(oldNonce, nonces.getNonce(chainId, participants)) + 1
|
|
nonces.next[key(chainId, participants)] = next
|