github.com/core-coin/go-core/v2@v2.1.9/core/tx_noncer.go (about)

     1  // Copyright 2019 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"sync"
    21  
    22  	"github.com/core-coin/go-core/v2/common"
    23  	"github.com/core-coin/go-core/v2/core/state"
    24  )
    25  
    26  // txNoncer 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 txNoncer struct {
    30  	fallback *state.StateDB
    31  	nonces   map[common.Address]uint64
    32  	lock     sync.Mutex
    33  }
    34  
    35  // newTxNoncer creates a new virtual state database to track the pool nonces.
    36  func newTxNoncer(statedb *state.StateDB) *txNoncer {
    37  	return &txNoncer{
    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 *txNoncer) 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  		txn.nonces[addr] = txn.fallback.GetNonce(addr)
    53  	}
    54  	return txn.nonces[addr]
    55  }
    56  
    57  // set inserts a new virtual nonce into the virtual state database to be returned
    58  // whenever the pool requests it instead of reaching into the real state database.
    59  func (txn *txNoncer) set(addr common.Address, nonce uint64) {
    60  	txn.lock.Lock()
    61  	defer txn.lock.Unlock()
    62  
    63  	txn.nonces[addr] = nonce
    64  }
    65  
    66  // setIfLower updates a new virtual nonce into the virtual state database if the
    67  // the new one is lower.
    68  func (txn *txNoncer) setIfLower(addr common.Address, nonce uint64) {
    69  	txn.lock.Lock()
    70  	defer txn.lock.Unlock()
    71  
    72  	if _, ok := txn.nonces[addr]; !ok {
    73  		txn.nonces[addr] = txn.fallback.GetNonce(addr)
    74  	}
    75  	if txn.nonces[addr] <= nonce {
    76  		return
    77  	}
    78  	txn.nonces[addr] = nonce
    79  }