-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonce_manager.go
More file actions
46 lines (40 loc) · 959 Bytes
/
nonce_manager.go
File metadata and controls
46 lines (40 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"math/big"
"sync"
)
type NonceManager struct {
lock sync.Mutex
nonceMemCache map[string]*big.Int
}
func NewNonceManager() *NonceManager {
return &NonceManager{
lock: sync.Mutex{},
}
}
func (m *NonceManager) SetNonce(address string, nonce *big.Int) {
if m.nonceMemCache == nil {
m.nonceMemCache = make(map[string]*big.Int)
}
m.lock.Lock()
defer m.lock.Unlock()
m.nonceMemCache[address] = nonce
}
func (m *NonceManager) GetNonce(address string) *big.Int {
if m.nonceMemCache == nil {
m.nonceMemCache = make(map[string]*big.Int)
}
m.lock.Lock()
defer m.lock.Unlock()
return m.nonceMemCache[address]
}
func (m *NonceManager) PlusNonce(address string) {
if m.nonceMemCache == nil {
m.nonceMemCache = make(map[string]*big.Int)
}
m.lock.Lock()
defer m.lock.Unlock()
oldNonce := m.nonceMemCache[address]
newNonce := oldNonce.Add(oldNonce, big.NewInt(int64(1)))
m.nonceMemCache[address] = newNonce
}