github.com/status-im/status-go@v1.1.0/transactions/addrlock.go (about) 1 // copy of go-ethereum/internal/ethapi/addrlock.go 2 3 package transactions 4 5 import ( 6 "sync" 7 8 "github.com/status-im/status-go/eth-node/types" 9 ) 10 11 // AddrLocker provides locks for addresses 12 type AddrLocker struct { 13 mu sync.Mutex 14 locks map[types.Address]*sync.Mutex 15 } 16 17 // lock returns the lock of the given address. 18 func (l *AddrLocker) lock(address types.Address) *sync.Mutex { 19 l.mu.Lock() 20 defer l.mu.Unlock() 21 if l.locks == nil { 22 l.locks = make(map[types.Address]*sync.Mutex) 23 } 24 if _, ok := l.locks[address]; !ok { 25 l.locks[address] = new(sync.Mutex) 26 } 27 return l.locks[address] 28 } 29 30 // LockAddr locks an account's mutex. This is used to prevent another tx getting the 31 // same nonce until the lock is released. The mutex prevents the (an identical nonce) from 32 // being read again during the time that the first transaction is being signed. 33 func (l *AddrLocker) LockAddr(address types.Address) { 34 l.lock(address).Lock() 35 } 36 37 // UnlockAddr unlocks the mutex of the given account. 38 func (l *AddrLocker) UnlockAddr(address types.Address) { 39 l.lock(address).Unlock() 40 }