2018-01-18 16:55:17 +00:00
|
|
|
// copy of go-ethereum/internal/ethapi/addrlock.go
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
package transactions
|
2018-01-18 16:55:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-01-18 16:55:17 +00:00
|
|
|
)
|
|
|
|
|
2018-01-24 08:25:28 +00:00
|
|
|
// AddrLocker provides locks for addresses
|
2018-01-18 16:55:17 +00:00
|
|
|
type AddrLocker struct {
|
|
|
|
mu sync.Mutex
|
2019-12-19 18:27:27 +00:00
|
|
|
locks map[types.Address]*sync.Mutex
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lock returns the lock of the given address.
|
2019-12-19 18:27:27 +00:00
|
|
|
func (l *AddrLocker) lock(address types.Address) *sync.Mutex {
|
2018-01-18 16:55:17 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
if l.locks == nil {
|
2019-12-19 18:27:27 +00:00
|
|
|
l.locks = make(map[types.Address]*sync.Mutex)
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
|
|
|
if _, ok := l.locks[address]; !ok {
|
|
|
|
l.locks[address] = new(sync.Mutex)
|
|
|
|
}
|
|
|
|
return l.locks[address]
|
|
|
|
}
|
|
|
|
|
|
|
|
// LockAddr locks an account's mutex. This is used to prevent another tx getting the
|
|
|
|
// same nonce until the lock is released. The mutex prevents the (an identical nonce) from
|
|
|
|
// being read again during the time that the first transaction is being signed.
|
2019-12-19 18:27:27 +00:00
|
|
|
func (l *AddrLocker) LockAddr(address types.Address) {
|
2018-01-18 16:55:17 +00:00
|
|
|
l.lock(address).Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnlockAddr unlocks the mutex of the given account.
|
2019-12-19 18:27:27 +00:00
|
|
|
func (l *AddrLocker) UnlockAddr(address types.Address) {
|
2018-01-18 16:55:17 +00:00
|
|
|
l.lock(address).Unlock()
|
|
|
|
}
|