github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/core/txpool/noncer.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package txpool
    18  
    19  import (
    20  	"sync"
    21  
    22  	"github.com/tacshi/go-ethereum/common"
    23  	"github.com/tacshi/go-ethereum/core/state"
    24  )
    25  
    26  // noncer is a tiny virtual state database to manage the executable nonces of
    27  // accounts in the pool, falling back to reading from a real state database if
    28  // an account is unknown.
    29  type noncer struct {
    30  	fallback *state.StateDB
    31  	nonces   map[common.Address]uint64
    32  	lock     sync.Mutex
    33  }
    34  
    35  // newNoncer creates a new virtual state database to track the pool nonces.
    36  func newNoncer(statedb *state.StateDB) *noncer {
    37  	return &noncer{
    38  		fallback: statedb.Copy(),
    39  		nonces:   make(map[common.Address]uint64),
    40  	}
    41  }
    42  
    43  // get returns the current nonce of an account, falling back to a real state
    44  // database if the account is unknown.
    45  func (txn *noncer) get(addr common.Address) uint64 {
    46  	// We use mutex for get operation is the underlying
    47  	// state will mutate db even for read access.
    48  	txn.lock.Lock()
    49  	defer txn.lock.Unlock()
    50  
    51  	if _, ok := txn.nonces[addr]; !ok {
    52  		if nonce := txn.fallback.GetNonce(addr); nonce != 0 {
    53  			txn.nonces[addr] = nonce
    54  		}
    55  	}
    56  	return txn.nonces[addr]
    57  }
    58  
    59  // set inserts a new virtual nonce into the virtual state database to be returned
    60  // whenever the pool requests it instead of reaching into the real state database.
    61  func (txn *noncer) set(addr common.Address, nonce uint64) {
    62  	txn.lock.Lock()
    63  	defer txn.lock.Unlock()
    64  
    65  	txn.nonces[addr] = nonce
    66  }
    67  
    68  // setIfLower updates a new virtual nonce into the virtual state database if the
    69  // new one is lower.
    70  func (txn *noncer) setIfLower(addr common.Address, nonce uint64) {
    71  	txn.lock.Lock()
    72  	defer txn.lock.Unlock()
    73  
    74  	if _, ok := txn.nonces[addr]; !ok {
    75  		if nonce := txn.fallback.GetNonce(addr); nonce != 0 {
    76  			txn.nonces[addr] = nonce
    77  		}
    78  	}
    79  	if txn.nonces[addr] <= nonce {
    80  		return
    81  	}
    82  	txn.nonces[addr] = nonce
    83  }
    84  
    85  // setAll sets the nonces for all accounts to the given map.
    86  func (txn *noncer) setAll(all map[common.Address]uint64) {
    87  	txn.lock.Lock()
    88  	defer txn.lock.Unlock()
    89  
    90  	txn.nonces = all
    91  }