github.com/theQRL/go-zond@v0.2.1/core/txpool/subpool.go (about) 1 // Copyright 2023 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 "math/big" 21 "time" 22 23 "github.com/theQRL/go-zond/common" 24 "github.com/theQRL/go-zond/core" 25 "github.com/theQRL/go-zond/core/types" 26 "github.com/theQRL/go-zond/event" 27 ) 28 29 // LazyTransaction contains a small subset of the transaction properties that is 30 // enough for the miner and other APIs to handle large batches of transactions; 31 // and supports pulling up the entire transaction when really needed. 32 type LazyTransaction struct { 33 Pool LazyResolver // Transaction resolver to pull the real transaction up 34 Hash common.Hash // Transaction hash to pull up if needed 35 Tx *types.Transaction // Transaction if already resolved 36 37 Time time.Time // Time when the transaction was first seen 38 GasFeeCap *big.Int // Maximum fee per gas the transaction may consume 39 GasTipCap *big.Int // Maximum miner tip per gas the transaction can pay 40 41 Gas uint64 // Amount of gas required by the transaction 42 } 43 44 // Resolve retrieves the full transaction belonging to a lazy handle if it is still 45 // maintained by the transaction pool. 46 // 47 // Note, the method will *not* cache the retrieved transaction if the original 48 // pool has not cached it. The idea being, that if the tx was too big to insert 49 // originally, silently saving it will cause more trouble down the line (and 50 // indeed seems to have caused a memory bloat in the original implementation 51 // which did just that). 52 func (ltx *LazyTransaction) Resolve() *types.Transaction { 53 if ltx.Tx != nil { 54 return ltx.Tx 55 } 56 return ltx.Pool.Get(ltx.Hash) 57 } 58 59 // LazyResolver is a minimal interface needed for a transaction pool to satisfy 60 // resolving lazy transactions. It's mostly a helper to avoid the entire sub- 61 // pool being injected into the lazy transaction. 62 type LazyResolver interface { 63 // Get returns a transaction if it is contained in the pool, or nil otherwise. 64 Get(hash common.Hash) *types.Transaction 65 } 66 67 // AddressReserver is passed by the main transaction pool to subpools, so they 68 // may request (and relinquish) exclusive access to certain addresses. 69 type AddressReserver func(addr common.Address, reserve bool) error 70 71 // PendingFilter is a collection of filter rules to allow retrieving a subset 72 // of transactions for announcement or mining. 73 // 74 // Note, the entries here are not arbitrary useful filters, rather each one has 75 // a very specific call site in mind and each one can be evaluated very cheaply 76 // by the pool implementations. Only add new ones that satisfy those constraints. 77 type PendingFilter struct { 78 MinTip *big.Int // Minimum miner tip required to include a transaction 79 BaseFee *big.Int // Minimum 1559 basefee needed to include a transaction 80 } 81 82 // SubPool represents a specialized transaction pool that lives on its own (e.g. 83 // blob pool). Since independent of how many specialized pools we have, they do 84 // need to be updated in lockstep and assemble into one coherent view for block 85 // production, this interface defines the common methods that allow the primary 86 // transaction pool to manage the subpools. 87 type SubPool interface { 88 // Filter is a selector used to decide whether a transaction would be added 89 // to this particular subpool. 90 Filter(tx *types.Transaction) bool 91 92 // Init sets the base parameters of the subpool, allowing it to load any saved 93 // transactions from disk and also permitting internal maintenance routines to 94 // start up. 95 // 96 // These should not be passed as a constructor argument - nor should the pools 97 // start by themselves - in order to keep multiple subpools in lockstep with 98 // one another. 99 Init(gasTip *big.Int, head *types.Header, reserve AddressReserver) error 100 101 // Close terminates any background processing threads and releases any held 102 // resources. 103 Close() error 104 105 // Reset retrieves the current state of the blockchain and ensures the content 106 // of the transaction pool is valid with regard to the chain state. 107 Reset(oldHead, newHead *types.Header) 108 109 // SetGasTip updates the minimum price required by the subpool for a new 110 // transaction, and drops all transactions below this threshold. 111 SetGasTip(tip *big.Int) 112 113 // Has returns an indicator whether subpool has a transaction cached with the 114 // given hash. 115 Has(hash common.Hash) bool 116 117 // Get returns a transaction if it is contained in the pool, or nil otherwise. 118 Get(hash common.Hash) *types.Transaction 119 120 // Add enqueues a batch of transactions into the pool if they are valid. Due 121 // to the large transaction churn, add may postpone fully integrating the tx 122 // to a later point to batch multiple ones together. 123 Add(txs []*types.Transaction, local bool, sync bool) []error 124 125 // Pending retrieves all currently processable transactions, grouped by origin 126 // account and sorted by nonce. 127 // 128 // The transactions can also be pre-filtered by the dynamic fee components to 129 // reduce allocations and load on downstream subsystems. 130 Pending(filter PendingFilter) map[common.Address][]*LazyTransaction 131 132 // SubscribeTransactions subscribes to new transaction events. The subscriber 133 // can decide whether to receive notifications only for newly seen transactions 134 // or also for reorged out ones. 135 SubscribeTransactions(ch chan<- core.NewTxsEvent) event.Subscription 136 137 // Nonce returns the next nonce of an account, with all transactions executable 138 // by the pool already applied on top. 139 Nonce(addr common.Address) uint64 140 141 // Stats retrieves the current pool stats, namely the number of pending and the 142 // number of queued (non-executable) transactions. 143 Stats() (int, int) 144 145 // Content retrieves the data content of the transaction pool, returning all the 146 // pending as well as queued transactions, grouped by account and sorted by nonce. 147 Content() (map[common.Address][]*types.Transaction, map[common.Address][]*types.Transaction) 148 149 // ContentFrom retrieves the data content of the transaction pool, returning the 150 // pending as well as queued transactions of this address, grouped by nonce. 151 ContentFrom(addr common.Address) ([]*types.Transaction, []*types.Transaction) 152 153 // Locals retrieves the accounts currently considered local by the pool. 154 Locals() []common.Address 155 156 // Status returns the known status (unknown/pending/queued) of a transaction 157 // identified by their hashes. 158 Status(hash common.Hash) TxStatus 159 }