github.com/dim4egster/coreth@v0.10.2/core/tx_noncer.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 2019 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 core
    28  
    29  import (
    30  	"sync"
    31  
    32  	"github.com/dim4egster/coreth/core/state"
    33  	"github.com/ethereum/go-ethereum/common"
    34  )
    35  
    36  // txNoncer is a tiny virtual state database to manage the executable nonces of
    37  // accounts in the pool, falling back to reading from a real state database if
    38  // an account is unknown.
    39  type txNoncer struct {
    40  	fallback *state.StateDB
    41  	nonces   map[common.Address]uint64
    42  	lock     sync.Mutex
    43  }
    44  
    45  // newTxNoncer creates a new virtual state database to track the pool nonces.
    46  func newTxNoncer(statedb *state.StateDB) *txNoncer {
    47  	return &txNoncer{
    48  		fallback: statedb.Copy(),
    49  		nonces:   make(map[common.Address]uint64),
    50  	}
    51  }
    52  
    53  // get returns the current nonce of an account, falling back to a real state
    54  // database if the account is unknown.
    55  func (txn *txNoncer) get(addr common.Address) uint64 {
    56  	// We use mutex for get operation is the underlying
    57  	// state will mutate db even for read access.
    58  	txn.lock.Lock()
    59  	defer txn.lock.Unlock()
    60  
    61  	if _, ok := txn.nonces[addr]; !ok {
    62  		txn.nonces[addr] = txn.fallback.GetNonce(addr)
    63  	}
    64  	return txn.nonces[addr]
    65  }
    66  
    67  // set inserts a new virtual nonce into the virtual state database to be returned
    68  // whenever the pool requests it instead of reaching into the real state database.
    69  func (txn *txNoncer) set(addr common.Address, nonce uint64) {
    70  	txn.lock.Lock()
    71  	defer txn.lock.Unlock()
    72  
    73  	txn.nonces[addr] = nonce
    74  }
    75  
    76  // setIfLower updates a new virtual nonce into the virtual state database if the
    77  // the new one is lower.
    78  func (txn *txNoncer) setIfLower(addr common.Address, nonce uint64) {
    79  	txn.lock.Lock()
    80  	defer txn.lock.Unlock()
    81  
    82  	if _, ok := txn.nonces[addr]; !ok {
    83  		txn.nonces[addr] = txn.fallback.GetNonce(addr)
    84  	}
    85  	if txn.nonces[addr] <= nonce {
    86  		return
    87  	}
    88  	txn.nonces[addr] = nonce
    89  }
    90  
    91  // setAll sets the nonces for all accounts to the given map.
    92  func (txn *txNoncer) setAll(all map[common.Address]uint64) {
    93  	txn.lock.Lock()
    94  	defer txn.lock.Unlock()
    95  
    96  	txn.nonces = all
    97  }