github.com/MetalBlockchain/subnet-evm@v0.4.9/internal/ethapi/addrlock.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2017 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package ethapi 28 29 import ( 30 "sync" 31 32 "github.com/ethereum/go-ethereum/common" 33 ) 34 35 type AddrLocker struct { 36 mu sync.Mutex 37 locks map[common.Address]*sync.Mutex 38 } 39 40 // lock returns the lock of the given address. 41 func (l *AddrLocker) lock(address common.Address) *sync.Mutex { 42 l.mu.Lock() 43 defer l.mu.Unlock() 44 if l.locks == nil { 45 l.locks = make(map[common.Address]*sync.Mutex) 46 } 47 if _, ok := l.locks[address]; !ok { 48 l.locks[address] = new(sync.Mutex) 49 } 50 return l.locks[address] 51 } 52 53 // LockAddr locks an account's mutex. This is used to prevent another tx getting the 54 // same nonce until the lock is released. The mutex prevents the (an identical nonce) from 55 // being read again during the time that the first transaction is being signed. 56 func (l *AddrLocker) LockAddr(address common.Address) { 57 l.lock(address).Lock() 58 } 59 60 // UnlockAddr unlocks the mutex of the given account. 61 func (l *AddrLocker) UnlockAddr(address common.Address) { 62 l.lock(address).Unlock() 63 }