tx_pool.go 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  1. // Copyright 2014 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. package core
  17. import (
  18. "errors"
  19. "math"
  20. "math/big"
  21. "sort"
  22. "sync"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/common/prque"
  26. "github.com/ethereum/go-ethereum/core/state"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/event"
  29. "github.com/ethereum/go-ethereum/log"
  30. "github.com/ethereum/go-ethereum/metrics"
  31. "github.com/ethereum/go-ethereum/params"
  32. )
  33. const (
  34. // chainHeadChanSize is the size of channel listening to ChainHeadEvent.
  35. // ChainHeadEvent 事件的监听通道的大小。
  36. chainHeadChanSize = 10
  37. // txSlotSize is used to calculate how many data slots a single transaction
  38. // takes up based on its size. The slots are used as DoS protection, ensuring
  39. // that validating a new transaction remains a constant operation (in reality
  40. // O(maxslots), where max slots are 4 currently).
  41. //根据交易的大小计算单个交易占用的数据插槽数量。
  42. //插槽用作防止拒绝服务(DoS)攻击的保护措施,确保验证新交易仍然是一个恒定的操作(实际上是 O(maxslots),其中 max slots 目前为 4)。
  43. txSlotSize = 32 * 1024
  44. // txMaxSize is the maximum size a single transaction can have. This field has
  45. // non-trivial consequences: larger transactions are significantly harder and
  46. // more expensive to propagate; larger transactions also take more resources
  47. // to validate whether they fit into the pool or not.
  48. //单个交易的最大大小。这个字段有着重要的影响:较大的交易在传播时更难且更昂贵;较大的交易也需要更多的资源来验证它们是否适合进入交易池。
  49. txMaxSize = 4 * txSlotSize // 128KB
  50. // txReannoMaxNum is the maximum number of transactions a reannounce action can include.
  51. //重新公告(reannounce)操作中可以包含的最大交易数量。
  52. txReannoMaxNum = 1024
  53. )
  54. var (
  55. // ErrAlreadyKnown is returned if the transactions is already contained
  56. // within the pool.
  57. ErrAlreadyKnown = errors.New("already known")
  58. // ErrInvalidSender is returned if the transaction contains an invalid signature.
  59. ErrInvalidSender = errors.New("invalid sender")
  60. // ErrUnderpriced is returned if a transaction's gas price is below the minimum
  61. // configured for the transaction pool.
  62. ErrUnderpriced = errors.New("transaction underpriced")
  63. // ErrTxPoolOverflow is returned if the transaction pool is full and can't accpet
  64. // another remote transaction.
  65. ErrTxPoolOverflow = errors.New("txpool is full")
  66. // ErrReplaceUnderpriced is returned if a transaction is attempted to be replaced
  67. // with a different one without the required price bump.
  68. ErrReplaceUnderpriced = errors.New("replacement transaction underpriced")
  69. // ErrGasLimit is returned if a transaction's requested gas limit exceeds the
  70. // maximum allowance of the current block.
  71. ErrGasLimit = errors.New("exceeds block gas limit")
  72. // ErrNegativeValue is a sanity error to ensure no one is able to specify a
  73. // transaction with a negative value.
  74. ErrNegativeValue = errors.New("negative value")
  75. // ErrOversizedData is returned if the input data of a transaction is greater
  76. // than some meaningful limit a user might use. This is not a consensus error
  77. // making the transaction invalid, rather a DOS protection.
  78. ErrOversizedData = errors.New("oversized data")
  79. )
  80. var (
  81. //在每个时间间隔内,交易池会检查是否有过期的交易,并进行驱逐操作。
  82. evictionInterval = time.Minute // Time interval to check for evictable transactions
  83. //交易池统计报告的时间间隔。在每个时间间隔内,交易池会生成并报告交易池的统计信息。
  84. statsReportInterval = 8 * time.Second // Time interval to report transaction pool stats
  85. //检查重新广播(reannounce)交易的时间间隔。在每个时间间隔内,交易池会检查是否有需要重新广播的交易,并进行相应的处理。
  86. reannounceInterval = time.Minute // Time interval to check for reannounce transactions
  87. )
  88. var (
  89. // Metrics for the pending pool
  90. // pendingDiscardMeter 计量已决交易被丢弃的数量
  91. pendingDiscardMeter = metrics.NewRegisteredMeter("txpool/pending/discard", nil)
  92. // pendingReplaceMeter 计量已决交易被替换的数量
  93. pendingReplaceMeter = metrics.NewRegisteredMeter("txpool/pending/replace", nil)
  94. // pendingRateLimitMeter 计量由于速率限制而被丢弃的已决交易的数量
  95. pendingRateLimitMeter = metrics.NewRegisteredMeter("txpool/pending/ratelimit", nil)
  96. // pendingNofundsMeter 计量由于资金不足而被丢弃的已决交易的数量
  97. pendingNofundsMeter = metrics.NewRegisteredMeter("txpool/pending/nofunds", nil)
  98. // Metrics for the queued pool
  99. // queuedDiscardMeter 计量排队交易被丢弃的数量
  100. queuedDiscardMeter = metrics.NewRegisteredMeter("txpool/queued/discard", nil)
  101. // queuedReplaceMeter 计量排队交易被替换的数量
  102. queuedReplaceMeter = metrics.NewRegisteredMeter("txpool/queued/replace", nil)
  103. // queuedRateLimitMeter 计量由于速率限制而被丢弃的排队交易的数量
  104. queuedRateLimitMeter = metrics.NewRegisteredMeter("txpool/queued/ratelimit", nil)
  105. // queuedNofundsMeter 计量由于资金不足而被丢弃的排队交易的数量
  106. queuedNofundsMeter = metrics.NewRegisteredMeter("txpool/queued/nofunds", nil)
  107. // queuedEvictionMeter 计量由于生命周期结束而被丢弃的排队交易的数量
  108. queuedEvictionMeter = metrics.NewRegisteredMeter("txpool/queued/eviction", nil)
  109. // General tx metrics
  110. // knownTxMeter 计量已知交易的数量
  111. knownTxMeter = metrics.NewRegisteredMeter("txpool/known", nil)
  112. // validTxMeter 计量有效交易的数量
  113. validTxMeter = metrics.NewRegisteredMeter("txpool/valid", nil)
  114. // invalidTxMeter 计量无效交易的数量
  115. invalidTxMeter = metrics.NewRegisteredMeter("txpool/invalid", nil)
  116. // underpricedTxMeter 计量低价交易的数量
  117. underpricedTxMeter = metrics.NewRegisteredMeter("txpool/underpriced", nil)
  118. // overflowedTxMeter 计量溢出交易的数量
  119. overflowedTxMeter = metrics.NewRegisteredMeter("txpool/overflowed", nil)
  120. // pendingGauge 计量当前已决交易的数量
  121. pendingGauge = metrics.NewRegisteredGauge("txpool/pending", nil)
  122. // queuedGauge 计量当前排队交易的数量
  123. queuedGauge = metrics.NewRegisteredGauge("txpool/queued", nil)
  124. // localGauge 计量当前本地交易的数量
  125. localGauge = metrics.NewRegisteredGauge("txpool/local", nil)
  126. // slotsGauge 计量当前交易占用的数据槽位数量
  127. slotsGauge = metrics.NewRegisteredGauge("txpool/slots", nil)
  128. )
  129. // TxStatus is the current status of a transaction as seen by the pool.
  130. type TxStatus uint
  131. const (
  132. TxStatusUnknown TxStatus = iota
  133. TxStatusQueued
  134. TxStatusPending
  135. TxStatusIncluded
  136. )
  137. // blockChain provides the state of blockchain and current gas limit to do
  138. // some pre checks in tx pool and event subscribers.
  139. type blockChain interface {
  140. CurrentBlock() *types.Block
  141. GetBlock(hash common.Hash, number uint64) *types.Block
  142. StateAt(root common.Hash) (*state.StateDB, error)
  143. SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription
  144. }
  145. // TxPoolConfig 是交易池的配置参数。
  146. type TxPoolConfig struct {
  147. Locals []common.Address // 默认情况下应被视为本地交易的地址列表
  148. NoLocals bool // 是否禁用本地交易处理
  149. Journal string // 本地交易日志,用于在节点重启后恢复
  150. Rejournal time.Duration // 重新生成本地交易日志的时间间隔
  151. PriceLimit uint64 // 最小的 gas 价格以确保交易被接受到交易池中
  152. PriceBump uint64 // 用于替换已存在交易的最小价格增幅百分比(按 nonce 排序)
  153. AccountSlots uint64 // 每个账户保证的可执行交易槽位数量
  154. GlobalSlots uint64 // 所有账户可执行交易槽位的最大数量
  155. AccountQueue uint64 // 每个账户允许的非可执行交易槽位的最大数量
  156. GlobalQueue uint64 // 所有账户非可执行交易槽位的最大数量
  157. Lifetime time.Duration // 非可执行交易在排队中的最大时间
  158. ReannounceTime time.Duration // 本地已决交易再次宣布的时间间隔
  159. }
  160. // DefaultTxPoolConfig contains the default configurations for the transaction
  161. // pool.
  162. var DefaultTxPoolConfig = TxPoolConfig{
  163. Journal: "transactions.rlp",
  164. Rejournal: time.Hour,
  165. PriceLimit: 1,
  166. PriceBump: 10,
  167. AccountSlots: 16,
  168. GlobalSlots: 4096,
  169. AccountQueue: 64,
  170. GlobalQueue: 1024,
  171. Lifetime: 3 * time.Hour,
  172. ReannounceTime: 10 * 365 * 24 * time.Hour,
  173. }
  174. // sanitize checks the provided user configurations and changes anything that's
  175. // unreasonable or unworkable.
  176. func (config *TxPoolConfig) sanitize() TxPoolConfig {
  177. conf := *config
  178. if conf.Rejournal < time.Second {
  179. log.Warn("Sanitizing invalid txpool journal time", "provided", conf.Rejournal, "updated", time.Second)
  180. conf.Rejournal = time.Second
  181. }
  182. if conf.PriceLimit < 1 {
  183. log.Warn("Sanitizing invalid txpool price limit", "provided", conf.PriceLimit, "updated", DefaultTxPoolConfig.PriceLimit)
  184. conf.PriceLimit = DefaultTxPoolConfig.PriceLimit
  185. }
  186. if conf.PriceBump < 1 {
  187. log.Warn("Sanitizing invalid txpool price bump", "provided", conf.PriceBump, "updated", DefaultTxPoolConfig.PriceBump)
  188. conf.PriceBump = DefaultTxPoolConfig.PriceBump
  189. }
  190. if conf.AccountSlots < 1 {
  191. log.Warn("Sanitizing invalid txpool account slots", "provided", conf.AccountSlots, "updated", DefaultTxPoolConfig.AccountSlots)
  192. conf.AccountSlots = DefaultTxPoolConfig.AccountSlots
  193. }
  194. if conf.GlobalSlots < 1 {
  195. log.Warn("Sanitizing invalid txpool global slots", "provided", conf.GlobalSlots, "updated", DefaultTxPoolConfig.GlobalSlots)
  196. conf.GlobalSlots = DefaultTxPoolConfig.GlobalSlots
  197. }
  198. if conf.AccountQueue < 1 {
  199. log.Warn("Sanitizing invalid txpool account queue", "provided", conf.AccountQueue, "updated", DefaultTxPoolConfig.AccountQueue)
  200. conf.AccountQueue = DefaultTxPoolConfig.AccountQueue
  201. }
  202. if conf.GlobalQueue < 1 {
  203. log.Warn("Sanitizing invalid txpool global queue", "provided", conf.GlobalQueue, "updated", DefaultTxPoolConfig.GlobalQueue)
  204. conf.GlobalQueue = DefaultTxPoolConfig.GlobalQueue
  205. }
  206. if conf.Lifetime < 1 {
  207. log.Warn("Sanitizing invalid txpool lifetime", "provided", conf.Lifetime, "updated", DefaultTxPoolConfig.Lifetime)
  208. conf.Lifetime = DefaultTxPoolConfig.Lifetime
  209. }
  210. if conf.ReannounceTime < time.Minute {
  211. log.Warn("Sanitizing invalid txpool reannounce time", "provided", conf.ReannounceTime, "updated", time.Minute)
  212. conf.ReannounceTime = time.Minute
  213. }
  214. return conf
  215. }
  216. // TxPool 包含了当前已知的所有交易。当交易从网络接收或本地提交时,它们会进入交易池。
  217. // 当交易被包含在区块链中时,它们会从交易池中移除。
  218. //
  219. // 交易池将交易分为可处理交易(可应用于当前状态)和未来交易。
  220. // 随着交易的接收和处理,交易会在这两种状态之间移动。
  221. type TxPool struct {
  222. config TxPoolConfig
  223. chainconfig *params.ChainConfig
  224. chain blockChain
  225. gasPrice *big.Int
  226. txFeed event.Feed
  227. reannoTxFeed event.Feed // 用于重新宣布交易的事件订阅
  228. scope event.SubscriptionScope
  229. signer types.Signer
  230. mu sync.RWMutex
  231. istanbul bool // 是否处于伊斯坦布尔阶段的标志。
  232. eip2718 bool // 是否使用 EIP-2718 类型的交易的标志。
  233. currentState *state.StateDB // 区块链头中的当前状态
  234. pendingNonces *txNoncer // 跟踪虚拟 nonce 的待处理状态
  235. currentMaxGas uint64 // 当前的 gas 限制,用于交易费用上限
  236. locals *accountSet // 免受淘汰规则限制的本地交易集合
  237. journal *txJournal // 本地交易日志,用于备份到磁盘
  238. pending map[common.Address]*txList // 当前所有可处理交易
  239. queue map[common.Address]*txList // 排队但无法处理的交易
  240. beats map[common.Address]time.Time // 每个已知账户的最后心跳时间
  241. all *txLookup // 所有交易,用于快速查找
  242. priced *txPricedList // 按价格排序的所有交易
  243. chainHeadCh chan ChainHeadEvent
  244. chainHeadSub event.Subscription
  245. reqResetCh chan *txpoolResetRequest
  246. reqPromoteCh chan *accountSet
  247. queueTxEventCh chan *types.Transaction
  248. reorgDoneCh chan chan struct{}
  249. reorgShutdownCh chan struct{} // 请求关闭 scheduleReorgLoop
  250. wg sync.WaitGroup // 追踪循环和 scheduleReorgLoop
  251. }
  252. type txpoolResetRequest struct {
  253. oldHead, newHead *types.Header
  254. }
  255. // NewTxPool creates a new transaction pool to gather, sort and filter inbound
  256. // transactions from the network.
  257. func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, chain blockChain) *TxPool {
  258. // Sanitize the input to ensure no vulnerable gas prices are set
  259. config = (&config).sanitize()
  260. // Create the transaction pool with its initial settings
  261. pool := &TxPool{
  262. config: config,
  263. chainconfig: chainconfig,
  264. chain: chain,
  265. signer: types.LatestSigner(chainconfig),
  266. pending: make(map[common.Address]*txList),
  267. queue: make(map[common.Address]*txList),
  268. beats: make(map[common.Address]time.Time),
  269. all: newTxLookup(),
  270. chainHeadCh: make(chan ChainHeadEvent, chainHeadChanSize),
  271. reqResetCh: make(chan *txpoolResetRequest),
  272. reqPromoteCh: make(chan *accountSet),
  273. queueTxEventCh: make(chan *types.Transaction),
  274. reorgDoneCh: make(chan chan struct{}),
  275. reorgShutdownCh: make(chan struct{}),
  276. gasPrice: new(big.Int).SetUint64(config.PriceLimit),
  277. }
  278. pool.locals = newAccountSet(pool.signer)
  279. for _, addr := range config.Locals {
  280. log.Info("Setting new local account", "address", addr)
  281. pool.locals.add(addr)
  282. }
  283. pool.priced = newTxPricedList(pool.all)
  284. pool.reset(nil, chain.CurrentBlock().Header())
  285. // Start the reorg loop early so it can handle requests generated during journal loading.
  286. pool.wg.Add(1)
  287. go pool.scheduleReorgLoop()
  288. // If local transactions and journaling is enabled, load from disk
  289. if !config.NoLocals && config.Journal != "" {
  290. pool.journal = newTxJournal(config.Journal)
  291. if err := pool.journal.load(pool.AddLocals); err != nil {
  292. log.Warn("Failed to load transaction journal", "err", err)
  293. }
  294. if err := pool.journal.rotate(pool.local()); err != nil {
  295. log.Warn("Failed to rotate transaction journal", "err", err)
  296. }
  297. }
  298. // Subscribe events from blockchain and start the main event loop.
  299. pool.chainHeadSub = pool.chain.SubscribeChainHeadEvent(pool.chainHeadCh)
  300. pool.wg.Add(1)
  301. go pool.loop()
  302. return pool
  303. }
  304. // loop is the transaction pool's main event loop, waiting for and reacting to
  305. // outside blockchain events as well as for various reporting and transaction
  306. // eviction events.
  307. func (pool *TxPool) loop() {
  308. defer pool.wg.Done()
  309. var (
  310. prevPending, prevQueued, prevStales int
  311. // Start the stats reporting and transaction eviction tickers
  312. report = time.NewTicker(statsReportInterval)
  313. evict = time.NewTicker(evictionInterval)
  314. reannounce = time.NewTicker(reannounceInterval)
  315. journal = time.NewTicker(pool.config.Rejournal)
  316. // Track the previous head headers for transaction reorgs
  317. head = pool.chain.CurrentBlock()
  318. )
  319. defer report.Stop()
  320. defer evict.Stop()
  321. defer reannounce.Stop()
  322. defer journal.Stop()
  323. for {
  324. select {
  325. // Handle ChainHeadEvent
  326. case ev := <-pool.chainHeadCh:
  327. if ev.Block != nil {
  328. pool.requestReset(head.Header(), ev.Block.Header())
  329. head = ev.Block
  330. }
  331. // System shutdown.
  332. case <-pool.chainHeadSub.Err():
  333. close(pool.reorgShutdownCh)
  334. return
  335. // Handle stats reporting ticks
  336. case <-report.C:
  337. pool.mu.RLock()
  338. pending, queued := pool.stats()
  339. stales := pool.priced.stales
  340. pool.mu.RUnlock()
  341. if pending != prevPending || queued != prevQueued || stales != prevStales {
  342. log.Debug("Transaction pool status report", "executable", pending, "queued", queued, "stales", stales)
  343. prevPending, prevQueued, prevStales = pending, queued, stales
  344. }
  345. // Handle inactive account transaction eviction
  346. case <-evict.C:
  347. pool.mu.Lock()
  348. for addr := range pool.queue {
  349. // Skip local transactions from the eviction mechanism
  350. if pool.locals.contains(addr) {
  351. continue
  352. }
  353. // Any non-locals old enough should be removed
  354. if time.Since(pool.beats[addr]) > pool.config.Lifetime {
  355. list := pool.queue[addr].Flatten()
  356. for _, tx := range list {
  357. pool.removeTx(tx.Hash(), true)
  358. }
  359. queuedEvictionMeter.Mark(int64(len(list)))
  360. }
  361. }
  362. pool.mu.Unlock()
  363. case <-reannounce.C:
  364. pool.mu.RLock()
  365. reannoTxs := func() []*types.Transaction {
  366. txs := make([]*types.Transaction, 0)
  367. for addr, list := range pool.pending {
  368. if !pool.locals.contains(addr) {
  369. continue
  370. }
  371. for _, tx := range list.Flatten() {
  372. // Default ReannounceTime is 10 years, won't announce by default.
  373. if time.Since(tx.Time()) < pool.config.ReannounceTime {
  374. break
  375. }
  376. txs = append(txs, tx)
  377. if len(txs) >= txReannoMaxNum {
  378. return txs
  379. }
  380. }
  381. }
  382. return txs
  383. }()
  384. pool.mu.RUnlock()
  385. if len(reannoTxs) > 0 {
  386. pool.reannoTxFeed.Send(ReannoTxsEvent{reannoTxs})
  387. }
  388. // Handle local transaction journal rotation
  389. case <-journal.C:
  390. if pool.journal != nil {
  391. pool.mu.Lock()
  392. if err := pool.journal.rotate(pool.local()); err != nil {
  393. log.Warn("Failed to rotate local tx journal", "err", err)
  394. }
  395. pool.mu.Unlock()
  396. }
  397. }
  398. }
  399. }
  400. // Stop terminates the transaction pool.
  401. func (pool *TxPool) Stop() {
  402. // Unsubscribe all subscriptions registered from txpool
  403. pool.scope.Close()
  404. // Unsubscribe subscriptions registered from blockchain
  405. pool.chainHeadSub.Unsubscribe()
  406. pool.wg.Wait()
  407. if pool.journal != nil {
  408. pool.journal.close()
  409. }
  410. log.Info("Transaction pool stopped")
  411. }
  412. // SubscribeNewTxsEvent registers a subscription of NewTxsEvent and
  413. // starts sending event to the given channel.
  414. func (pool *TxPool) SubscribeNewTxsEvent(ch chan<- NewTxsEvent) event.Subscription {
  415. return pool.scope.Track(pool.txFeed.Subscribe(ch))
  416. }
  417. // SubscribeReannoTxsEvent registers a subscription of ReannoTxsEvent and
  418. // starts sending event to the given channel.
  419. func (pool *TxPool) SubscribeReannoTxsEvent(ch chan<- ReannoTxsEvent) event.Subscription {
  420. return pool.scope.Track(pool.reannoTxFeed.Subscribe(ch))
  421. }
  422. // GasPrice returns the current gas price enforced by the transaction pool.
  423. func (pool *TxPool) GasPrice() *big.Int {
  424. pool.mu.RLock()
  425. defer pool.mu.RUnlock()
  426. return new(big.Int).Set(pool.gasPrice)
  427. }
  428. // SetGasPrice updates the minimum price required by the transaction pool for a
  429. // new transaction, and drops all transactions below this threshold.
  430. func (pool *TxPool) SetGasPrice(price *big.Int) {
  431. pool.mu.Lock()
  432. defer pool.mu.Unlock()
  433. pool.gasPrice = price
  434. for _, tx := range pool.priced.Cap(price) {
  435. pool.removeTx(tx.Hash(), false)
  436. }
  437. log.Info("Transaction pool price threshold updated", "price", price)
  438. }
  439. // Nonce returns the next nonce of an account, with all transactions executable
  440. // by the pool already applied on top.
  441. func (pool *TxPool) Nonce(addr common.Address) uint64 {
  442. pool.mu.RLock()
  443. defer pool.mu.RUnlock()
  444. return pool.pendingNonces.get(addr)
  445. }
  446. // Stats retrieves the current pool stats, namely the number of pending and the
  447. // number of queued (non-executable) transactions.
  448. func (pool *TxPool) Stats() (int, int) {
  449. pool.mu.RLock()
  450. defer pool.mu.RUnlock()
  451. return pool.stats()
  452. }
  453. // stats retrieves the current pool stats, namely the number of pending and the
  454. // number of queued (non-executable) transactions.
  455. func (pool *TxPool) stats() (int, int) {
  456. pending := 0
  457. for _, list := range pool.pending {
  458. pending += len(list.txs.items)
  459. }
  460. queued := 0
  461. for _, list := range pool.queue {
  462. queued += len(list.txs.items)
  463. }
  464. return pending, queued
  465. }
  466. // Content retrieves the data content of the transaction pool, returning all the
  467. // pending as well as queued transactions, grouped by account and sorted by nonce.
  468. func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
  469. pool.mu.Lock()
  470. defer pool.mu.Unlock()
  471. pending := make(map[common.Address]types.Transactions)
  472. for addr, list := range pool.pending {
  473. pending[addr] = list.Flatten()
  474. }
  475. queued := make(map[common.Address]types.Transactions)
  476. for addr, list := range pool.queue {
  477. queued[addr] = list.Flatten()
  478. }
  479. return pending, queued
  480. }
  481. // Pending retrieves all currently processable transactions, grouped by origin
  482. // account and sorted by nonce. The returned transaction set is a copy and can be
  483. // freely modified by calling code.
  484. func (pool *TxPool) Pending() (map[common.Address]types.Transactions, error) {
  485. pool.mu.Lock()
  486. defer pool.mu.Unlock()
  487. pending := make(map[common.Address]types.Transactions)
  488. for addr, list := range pool.pending {
  489. pending[addr] = list.Flatten()
  490. }
  491. return pending, nil
  492. }
  493. // Locals retrieves the accounts currently considered local by the pool.
  494. func (pool *TxPool) Locals() []common.Address {
  495. pool.mu.Lock()
  496. defer pool.mu.Unlock()
  497. return pool.locals.flatten()
  498. }
  499. // local retrieves all currently known local transactions, grouped by origin
  500. // account and sorted by nonce. The returned transaction set is a copy and can be
  501. // freely modified by calling code.
  502. func (pool *TxPool) local() map[common.Address]types.Transactions {
  503. txs := make(map[common.Address]types.Transactions)
  504. for addr := range pool.locals.accounts {
  505. if pending := pool.pending[addr]; pending != nil {
  506. txs[addr] = append(txs[addr], pending.Flatten()...)
  507. }
  508. if queued := pool.queue[addr]; queued != nil {
  509. txs[addr] = append(txs[addr], queued.Flatten()...)
  510. }
  511. }
  512. return txs
  513. }
  514. // validateTx checks whether a transaction is valid according to the consensus
  515. // rules and adheres to some heuristic limits of the local node (price and size).
  516. func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
  517. // Accept only legacy transactions until EIP-2718/2930 activates.
  518. if !pool.eip2718 && tx.Type() != types.LegacyTxType {
  519. return ErrTxTypeNotSupported
  520. }
  521. // Reject transactions over defined size to prevent DOS attacks
  522. if uint64(tx.Size()) > txMaxSize {
  523. return ErrOversizedData
  524. }
  525. // Transactions can't be negative. This may never happen using RLP decoded
  526. // transactions but may occur if you create a transaction using the RPC.
  527. if tx.Value().Sign() < 0 {
  528. return ErrNegativeValue
  529. }
  530. // Ensure the transaction doesn't exceed the current block limit gas.
  531. if pool.currentMaxGas < tx.Gas() {
  532. return ErrGasLimit
  533. }
  534. // Make sure the transaction is signed properly.
  535. from, err := types.Sender(pool.signer, tx)
  536. if err != nil {
  537. return ErrInvalidSender
  538. }
  539. // Drop non-local transactions under our own minimal accepted gas price
  540. if !local && tx.GasPriceIntCmp(pool.gasPrice) < 0 {
  541. return ErrUnderpriced
  542. }
  543. // Ensure the transaction adheres to nonce ordering
  544. if pool.currentState.GetNonce(from) > tx.Nonce() {
  545. return ErrNonceTooLow
  546. }
  547. // Transactor should have enough funds to cover the costs
  548. // cost == V + GP * GL
  549. if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
  550. return ErrInsufficientFunds
  551. }
  552. // Ensure the transaction has more gas than the basic tx fee.
  553. intrGas, err := IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, true, pool.istanbul)
  554. if err != nil {
  555. return err
  556. }
  557. if tx.Gas() < intrGas {
  558. return ErrIntrinsicGas
  559. }
  560. return nil
  561. }
  562. // add validates a transaction and inserts it into the non-executable queue for later
  563. // pending promotion and execution. If the transaction is a replacement for an already
  564. // pending or queued one, it overwrites the previous transaction if its price is higher.
  565. //
  566. // If a newly added transaction is marked as local, its sending account will be
  567. // whitelisted, preventing any associated transaction from being dropped out of the pool
  568. // due to pricing constraints.
  569. func (pool *TxPool) add(tx *types.Transaction, local bool) (replaced bool, err error) {
  570. // If the transaction is already known, discard it
  571. hash := tx.Hash()
  572. //log.Info("ZDY", "add", hash.Hex(), "local", local)
  573. if pool.all.Get(hash) != nil {
  574. //log.Trace("Discarding already known transaction", "hash", hash)
  575. knownTxMeter.Mark(1)
  576. return false, ErrAlreadyKnown
  577. }
  578. // Make the local flag. If it's from local source or it's from the network but
  579. // the sender is marked as local previously, treat it as the local transaction.
  580. isLocal := local || pool.locals.containsTx(tx)
  581. // If the transaction fails basic validation, discard it
  582. if err := pool.validateTx(tx, isLocal); err != nil {
  583. //log.Trace("Discarding invalid transaction", "hash", hash, "err", err)
  584. invalidTxMeter.Mark(1)
  585. return false, err
  586. }
  587. // If the transaction pool is full, discard underpriced transactions
  588. if uint64(pool.all.Count()+numSlots(tx)) > pool.config.GlobalSlots+pool.config.GlobalQueue {
  589. // If the new transaction is underpriced, don't accept it
  590. if !isLocal && pool.priced.Underpriced(tx) {
  591. //log.Trace("Discarding underpriced transaction", "hash", hash, "price", tx.GasPrice())
  592. underpricedTxMeter.Mark(1)
  593. return false, ErrUnderpriced
  594. }
  595. // New transaction is better than our worse ones, make room for it.
  596. // If it's a local transaction, forcibly discard all available transactions.
  597. // Otherwise if we can't make enough room for new one, abort the operation.
  598. drop, success := pool.priced.Discard(pool.all.Slots()-int(pool.config.GlobalSlots+pool.config.GlobalQueue)+numSlots(tx), isLocal)
  599. // Special case, we still can't make the room for the new remote one.
  600. if !isLocal && !success {
  601. //log.Trace("Discarding overflown transaction", "hash", hash)
  602. overflowedTxMeter.Mark(1)
  603. return false, ErrTxPoolOverflow
  604. }
  605. // Kick out the underpriced remote transactions.
  606. for _, tx := range drop {
  607. //log.Trace("Discarding freshly underpriced transaction", "hash", tx.Hash(), "price", tx.GasPrice())
  608. underpricedTxMeter.Mark(1)
  609. pool.removeTx(tx.Hash(), false)
  610. }
  611. }
  612. // Try to replace an existing transaction in the pending pool
  613. from, _ := types.Sender(pool.signer, tx) // already validated
  614. if list := pool.pending[from]; list != nil && list.Overlaps(tx) {
  615. // Nonce already pending, check if required price bump is met
  616. inserted, old := list.Add(tx, pool.config.PriceBump)
  617. if !inserted {
  618. pendingDiscardMeter.Mark(1)
  619. return false, ErrReplaceUnderpriced
  620. }
  621. // New transaction is better, replace old one
  622. if old != nil {
  623. pool.all.Remove(old.Hash())
  624. pool.priced.Removed(1)
  625. pendingReplaceMeter.Mark(1)
  626. }
  627. pool.all.Add(tx, isLocal)
  628. pool.priced.Put(tx, isLocal)
  629. pool.journalTx(from, tx)
  630. pool.queueTxEvent(tx)
  631. //log.Trace("Pooled new executable transaction", "hash", hash, "from", from, "to", tx.To())
  632. // Successful promotion, bump the heartbeat
  633. pool.beats[from] = time.Now()
  634. return old != nil, nil
  635. }
  636. // New transaction isn't replacing a pending one, push into queue
  637. replaced, err = pool.enqueueTx(hash, tx, isLocal, true)
  638. if err != nil {
  639. return false, err
  640. }
  641. // Mark local addresses and journal local transactions
  642. if local && !pool.locals.contains(from) {
  643. //log.Info("Setting new local account", "address", from)
  644. pool.locals.add(from)
  645. pool.priced.Removed(pool.all.RemoteToLocals(pool.locals)) // Migrate the remotes if it's marked as local first time.
  646. }
  647. if isLocal {
  648. localGauge.Inc(1)
  649. }
  650. pool.journalTx(from, tx)
  651. //log.Trace("Pooled new future transaction", "hash", hash, "from", from, "to", tx.To())
  652. return replaced, nil
  653. }
  654. // enqueueTx inserts a new transaction into the non-executable transaction queue.
  655. //
  656. // Note, this method assumes the pool lock is held!
  657. func (pool *TxPool) enqueueTx(hash common.Hash, tx *types.Transaction, local bool, addAll bool) (bool, error) {
  658. // Try to insert the transaction into the future queue
  659. from, _ := types.Sender(pool.signer, tx) // already validated
  660. if pool.queue[from] == nil {
  661. pool.queue[from] = newTxList(false)
  662. }
  663. inserted, old := pool.queue[from].Add(tx, pool.config.PriceBump)
  664. if !inserted {
  665. // An older transaction was better, discard this
  666. queuedDiscardMeter.Mark(1)
  667. return false, ErrReplaceUnderpriced
  668. }
  669. // Discard any previous transaction and mark this
  670. if old != nil {
  671. pool.all.Remove(old.Hash())
  672. pool.priced.Removed(1)
  673. queuedReplaceMeter.Mark(1)
  674. } else {
  675. // Nothing was replaced, bump the queued counter
  676. queuedGauge.Inc(1)
  677. }
  678. // If the transaction isn't in lookup set but it's expected to be there,
  679. // show the error log.
  680. if pool.all.Get(hash) == nil && !addAll {
  681. log.Error("Missing transaction in lookup set, please report the issue", "hash", hash)
  682. }
  683. if addAll {
  684. //核心添加TX方法,最后一步,可以直接到这里添加
  685. pool.all.Add(tx, local)
  686. pool.priced.Put(tx, local)
  687. }
  688. // If we never record the heartbeat, do it right now.
  689. if _, exist := pool.beats[from]; !exist {
  690. pool.beats[from] = time.Now()
  691. }
  692. return old != nil, nil
  693. }
  694. // journalTx adds the specified transaction to the local disk journal if it is
  695. // deemed to have been sent from a local account.
  696. func (pool *TxPool) journalTx(from common.Address, tx *types.Transaction) {
  697. // Only journal if it's enabled and the transaction is local
  698. if pool.journal == nil || !pool.locals.contains(from) {
  699. return
  700. }
  701. if err := pool.journal.insert(tx); err != nil {
  702. log.Warn("Failed to journal local transaction", "err", err)
  703. }
  704. }
  705. // promoteTx adds a transaction to the pending (processable) list of transactions
  706. // and returns whether it was inserted or an older was better.
  707. //
  708. // Note, this method assumes the pool lock is held!
  709. func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.Transaction) bool {
  710. // Try to insert the transaction into the pending queue
  711. if pool.pending[addr] == nil {
  712. pool.pending[addr] = newTxList(true)
  713. }
  714. list := pool.pending[addr]
  715. inserted, old := list.Add(tx, pool.config.PriceBump)
  716. if !inserted {
  717. // An older transaction was better, discard this
  718. pool.all.Remove(hash)
  719. pool.priced.Removed(1)
  720. pendingDiscardMeter.Mark(1)
  721. return false
  722. }
  723. // Otherwise discard any previous transaction and mark this
  724. if old != nil {
  725. pool.all.Remove(old.Hash())
  726. pool.priced.Removed(1)
  727. pendingReplaceMeter.Mark(1)
  728. } else {
  729. // Nothing was replaced, bump the pending counter
  730. pendingGauge.Inc(1)
  731. }
  732. // Set the potentially new pending nonce and notify any subsystems of the new tx
  733. pool.pendingNonces.set(addr, tx.Nonce()+1)
  734. // Successful promotion, bump the heartbeat
  735. pool.beats[addr] = time.Now()
  736. return true
  737. }
  738. // AddLocals enqueues a batch of transactions into the pool if they are valid, marking the
  739. // senders as a local ones, ensuring they go around the local pricing constraints.
  740. //
  741. // This method is used to add transactions from the RPC API and performs synchronous pool
  742. // reorganization and event propagation.
  743. func (pool *TxPool) AddLocals(txs []*types.Transaction) []error {
  744. //第二个参数改true,默认本地tx属性
  745. return pool.addTxs(txs, true, true)
  746. }
  747. // AddLocal enqueues a single local transaction into the pool if it is valid. This is
  748. // a convenience wrapper aroundd AddLocals.
  749. func (pool *TxPool) AddLocal(tx *types.Transaction) error {
  750. errs := pool.AddLocals([]*types.Transaction{tx})
  751. return errs[0]
  752. }
  753. func (pool *TxPool) AddLocalFast(tx *types.Transaction) error {
  754. //这里进行tx的添加插播
  755. pool.mu.Lock()
  756. pool.all.Add(tx, true)
  757. pool.txFeed.Send(NewTxsEvent{[]*types.Transaction{tx}})
  758. pool.mu.Unlock()
  759. return nil
  760. }
  761. // AddRemotes enqueues a batch of transactions into the pool if they are valid. If the
  762. // senders are not among the locally tracked ones, full pricing constraints will apply.
  763. //
  764. // This method is used to add transactions from the p2p network and does not wait for pool
  765. // reorganization and internal event propagation.
  766. func (pool *TxPool) AddRemotes(txs []*types.Transaction) []error {
  767. return pool.addTxs(txs, false, false)
  768. }
  769. // This is like AddRemotes, but waits for pool reorganization. Tests use this method.
  770. func (pool *TxPool) AddRemotesSync(txs []*types.Transaction) []error {
  771. return pool.addTxs(txs, false, true)
  772. }
  773. // This is like AddRemotes with a single transaction, but waits for pool reorganization. Tests use this method.
  774. func (pool *TxPool) addRemoteSync(tx *types.Transaction) error {
  775. errs := pool.AddRemotesSync([]*types.Transaction{tx})
  776. return errs[0]
  777. }
  778. // AddRemote enqueues a single transaction into the pool if it is valid. This is a convenience
  779. // wrapper around AddRemotes.
  780. //
  781. // Deprecated: use AddRemotes
  782. func (pool *TxPool) AddRemote(tx *types.Transaction) error {
  783. errs := pool.AddRemotes([]*types.Transaction{tx})
  784. return errs[0]
  785. }
  786. // addTxs attempts to queue a batch of transactions if they are valid.
  787. // 外部tx 和本地tx 都是放这里
  788. func (pool *TxPool) addTxs(txs []*types.Transaction, local, sync bool) []error {
  789. // Filter out known ones without obtaining the pool lock or recovering signatures
  790. var (
  791. errs = make([]error, len(txs))
  792. news = make([]*types.Transaction, 0, len(txs))
  793. )
  794. for i, tx := range txs {
  795. // If the transaction is known, pre-set the error slot
  796. if pool.all.Get(tx.Hash()) != nil {
  797. errs[i] = ErrAlreadyKnown
  798. knownTxMeter.Mark(1)
  799. continue
  800. }
  801. // Exclude transactions with invalid signatures as soon as
  802. // possible and cache senders in transactions before
  803. // obtaining lock
  804. _, err := types.Sender(pool.signer, tx)
  805. if err != nil {
  806. errs[i] = ErrInvalidSender
  807. invalidTxMeter.Mark(1)
  808. continue
  809. }
  810. // Accumulate all unknown transactions for deeper processing
  811. news = append(news, tx)
  812. //log.Info("ZDY", "addTxs", tx.Hash().Hex(), "local", local)
  813. }
  814. if len(news) == 0 {
  815. return errs
  816. }
  817. // Process all the new transaction and merge any errors into the original slice
  818. pool.mu.Lock()
  819. //这里进行tx的添加插播
  820. newErrs, dirtyAddrs := pool.addTxsLocked(news, local)
  821. pool.mu.Unlock()
  822. var nilSlot = 0
  823. for _, err := range newErrs {
  824. for errs[nilSlot] != nil {
  825. nilSlot++
  826. }
  827. errs[nilSlot] = err
  828. nilSlot++
  829. }
  830. // Reorg the pool internals if needed and return
  831. done := pool.requestPromoteExecutables(dirtyAddrs)
  832. if sync {
  833. <-done
  834. }
  835. return errs
  836. }
  837. // addTxsLocked attempts to queue a batch of transactions if they are valid.
  838. // The transaction pool lock must be held.
  839. func (pool *TxPool) addTxsLocked(txs []*types.Transaction, local bool) ([]error, *accountSet) {
  840. dirty := newAccountSet(pool.signer)
  841. errs := make([]error, len(txs))
  842. for i, tx := range txs {
  843. //这里进行tx的添加插播,
  844. //可以增加local判断,不传播别人的tx
  845. replaced, err := pool.add(tx, local)
  846. errs[i] = err
  847. if err == nil && !replaced {
  848. dirty.addTx(tx)
  849. }
  850. }
  851. validTxMeter.Mark(int64(len(dirty.accounts)))
  852. return errs, dirty
  853. }
  854. // Status returns the status (unknown/pending/queued) of a batch of transactions
  855. // identified by their hashes.
  856. func (pool *TxPool) Status(hashes []common.Hash) []TxStatus {
  857. status := make([]TxStatus, len(hashes))
  858. for i, hash := range hashes {
  859. tx := pool.Get(hash)
  860. if tx == nil {
  861. continue
  862. }
  863. from, _ := types.Sender(pool.signer, tx) // already validated
  864. pool.mu.RLock()
  865. if txList := pool.pending[from]; txList != nil && txList.txs.items[tx.Nonce()] != nil {
  866. status[i] = TxStatusPending
  867. } else if txList := pool.queue[from]; txList != nil && txList.txs.items[tx.Nonce()] != nil {
  868. status[i] = TxStatusQueued
  869. }
  870. // implicit else: the tx may have been included into a block between
  871. // checking pool.Get and obtaining the lock. In that case, TxStatusUnknown is correct
  872. pool.mu.RUnlock()
  873. }
  874. return status
  875. }
  876. // Get returns a transaction if it is contained in the pool and nil otherwise.
  877. func (pool *TxPool) Get(hash common.Hash) *types.Transaction {
  878. return pool.all.Get(hash)
  879. }
  880. // Has returns an indicator whether txpool has a transaction cached with the
  881. // given hash.
  882. func (pool *TxPool) Has(hash common.Hash) bool {
  883. return pool.all.Get(hash) != nil
  884. }
  885. // removeTx removes a single transaction from the queue, moving all subsequent
  886. // transactions back to the future queue.
  887. func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) {
  888. // Fetch the transaction we wish to delete
  889. tx := pool.all.Get(hash)
  890. if tx == nil {
  891. return
  892. }
  893. addr, _ := types.Sender(pool.signer, tx) // already validated during insertion
  894. // Remove it from the list of known transactions
  895. pool.all.Remove(hash)
  896. if outofbound {
  897. pool.priced.Removed(1)
  898. }
  899. if pool.locals.contains(addr) {
  900. localGauge.Dec(1)
  901. }
  902. // Remove the transaction from the pending lists and reset the account nonce
  903. if pending := pool.pending[addr]; pending != nil {
  904. if removed, invalids := pending.Remove(tx); removed {
  905. // If no more pending transactions are left, remove the list
  906. if pending.Empty() {
  907. delete(pool.pending, addr)
  908. }
  909. // Postpone any invalidated transactions
  910. for _, tx := range invalids {
  911. // Internal shuffle shouldn't touch the lookup set.
  912. pool.enqueueTx(tx.Hash(), tx, false, false)
  913. }
  914. // Update the account nonce if needed
  915. pool.pendingNonces.setIfLower(addr, tx.Nonce())
  916. // Reduce the pending counter
  917. pendingGauge.Dec(int64(1 + len(invalids)))
  918. return
  919. }
  920. }
  921. // Transaction is in the future queue
  922. if future := pool.queue[addr]; future != nil {
  923. if removed, _ := future.Remove(tx); removed {
  924. // Reduce the queued counter
  925. queuedGauge.Dec(1)
  926. }
  927. if future.Empty() {
  928. delete(pool.queue, addr)
  929. delete(pool.beats, addr)
  930. }
  931. }
  932. }
  933. // requestReset requests a pool reset to the new head block.
  934. // The returned channel is closed when the reset has occurred.
  935. func (pool *TxPool) requestReset(oldHead *types.Header, newHead *types.Header) chan struct{} {
  936. select {
  937. case pool.reqResetCh <- &txpoolResetRequest{oldHead, newHead}:
  938. return <-pool.reorgDoneCh
  939. case <-pool.reorgShutdownCh:
  940. return pool.reorgShutdownCh
  941. }
  942. }
  943. // requestPromoteExecutables requests transaction promotion checks for the given addresses.
  944. // The returned channel is closed when the promotion checks have occurred.
  945. func (pool *TxPool) requestPromoteExecutables(set *accountSet) chan struct{} {
  946. select {
  947. case pool.reqPromoteCh <- set:
  948. return <-pool.reorgDoneCh
  949. case <-pool.reorgShutdownCh:
  950. return pool.reorgShutdownCh
  951. }
  952. }
  953. // queueTxEvent enqueues a transaction event to be sent in the next reorg run.
  954. // 函数 queueTxEvent 用于将交易事件加入到下一次重新组织(reorg)运行中。
  955. // 在以太坊中,重新组织是指区块链的分叉和重组过程。当发生区块链重组时,之前加入交易池但尚未执行的交易可能会发生状态变化。
  956. // 因此,在重新组织过程中,需要重新处理这些交易。
  957. //
  958. // 具体而言,queueTxEvent 函数将交易对象 tx 加入到交易事件队列 pool.queueTxEventCh 中。
  959. // 该队列用于存储待处理的交易事件。当交易池进行重新组织时,会处理这些交易事件以确保交易状态的一致性。
  960. //
  961. // 在函数的实现中,使用了一个非阻塞的 select 语句,尝试将交易事件发送到 pool.queueTxEventCh。
  962. // 如果成功发送,则交易事件被加入到队列中。如果在发送时发现 pool.reorgShutdownCh 通道已关闭(即重新组织过程被关闭),则会立即退出函数。
  963. //
  964. // 总之,queueTxEvent 函数用于将待处理的交易事件加入到队列中,以便在重新组织过程中进行处理。这有助于保持交易池中交易状态的一致性。
  965. func (pool *TxPool) queueTxEvent(tx *types.Transaction) {
  966. select {
  967. case pool.queueTxEventCh <- tx:
  968. case <-pool.reorgShutdownCh:
  969. }
  970. }
  971. // scheduleReorgLoop schedules runs of reset and promoteExecutables. Code above should not
  972. // call those methods directly, but request them being run using requestReset and
  973. // requestPromoteExecutables instead.
  974. func (pool *TxPool) scheduleReorgLoop() {
  975. defer pool.wg.Done()
  976. var (
  977. curDone chan struct{} // non-nil while runReorg is active
  978. nextDone = make(chan struct{})
  979. launchNextRun bool
  980. reset *txpoolResetRequest
  981. dirtyAccounts *accountSet
  982. queuedEvents = make(map[common.Address]*txSortedMap)
  983. )
  984. for {
  985. // Launch next background reorg if needed
  986. if curDone == nil && launchNextRun {
  987. // Run the background reorg and announcements
  988. go pool.runReorg(nextDone, reset, dirtyAccounts, queuedEvents)
  989. // Prepare everything for the next round of reorg
  990. curDone, nextDone = nextDone, make(chan struct{})
  991. launchNextRun = false
  992. reset, dirtyAccounts = nil, nil
  993. queuedEvents = make(map[common.Address]*txSortedMap)
  994. }
  995. select {
  996. case req := <-pool.reqResetCh:
  997. // Reset request: update head if request is already pending.
  998. if reset == nil {
  999. reset = req
  1000. } else {
  1001. reset.newHead = req.newHead
  1002. }
  1003. launchNextRun = true
  1004. pool.reorgDoneCh <- nextDone
  1005. case req := <-pool.reqPromoteCh:
  1006. // Promote request: update address set if request is already pending.
  1007. if dirtyAccounts == nil {
  1008. dirtyAccounts = req
  1009. } else {
  1010. dirtyAccounts.merge(req)
  1011. }
  1012. launchNextRun = true
  1013. pool.reorgDoneCh <- nextDone
  1014. case tx := <-pool.queueTxEventCh:
  1015. // Queue up the event, but don't schedule a reorg. It's up to the caller to
  1016. // request one later if they want the events sent.
  1017. addr, _ := types.Sender(pool.signer, tx)
  1018. if _, ok := queuedEvents[addr]; !ok {
  1019. queuedEvents[addr] = newTxSortedMap()
  1020. }
  1021. queuedEvents[addr].Put(tx)
  1022. case <-curDone:
  1023. curDone = nil
  1024. case <-pool.reorgShutdownCh:
  1025. // Wait for current run to finish.
  1026. if curDone != nil {
  1027. <-curDone
  1028. }
  1029. close(nextDone)
  1030. return
  1031. }
  1032. }
  1033. }
  1034. // runReorg runs reset and promoteExecutables on behalf of scheduleReorgLoop.
  1035. func (pool *TxPool) runReorg(done chan struct{}, reset *txpoolResetRequest, dirtyAccounts *accountSet, events map[common.Address]*txSortedMap) {
  1036. defer close(done)
  1037. var promoteAddrs []common.Address
  1038. if dirtyAccounts != nil && reset == nil {
  1039. // Only dirty accounts need to be promoted, unless we're resetting.
  1040. // For resets, all addresses in the tx queue will be promoted and
  1041. // the flatten operation can be avoided.
  1042. promoteAddrs = dirtyAccounts.flatten()
  1043. }
  1044. pool.mu.Lock()
  1045. if reset != nil {
  1046. // Reset from the old head to the new, rescheduling any reorged transactions
  1047. pool.reset(reset.oldHead, reset.newHead)
  1048. // Nonces were reset, discard any events that became stale
  1049. for addr := range events {
  1050. events[addr].Forward(pool.pendingNonces.get(addr))
  1051. if len(events[addr].items) == 0 {
  1052. delete(events, addr)
  1053. }
  1054. }
  1055. // Reset needs promote for all addresses
  1056. promoteAddrs = make([]common.Address, 0, len(pool.queue))
  1057. for addr := range pool.queue {
  1058. promoteAddrs = append(promoteAddrs, addr)
  1059. }
  1060. }
  1061. // Check for pending transactions for every account that sent new ones
  1062. promoted := pool.promoteExecutables(promoteAddrs)
  1063. // If a new block appeared, validate the pool of pending transactions. This will
  1064. // remove any transaction that has been included in the block or was invalidated
  1065. // because of another transaction (e.g. higher gas price).
  1066. if reset != nil {
  1067. pool.demoteUnexecutables()
  1068. }
  1069. // Ensure pool.queue and pool.pending sizes stay within the configured limits.
  1070. pool.truncatePending()
  1071. pool.truncateQueue()
  1072. // Update all accounts to the latest known pending nonce
  1073. for addr, list := range pool.pending {
  1074. highestPending := list.LastElement()
  1075. pool.pendingNonces.set(addr, highestPending.Nonce()+1)
  1076. }
  1077. pool.mu.Unlock()
  1078. // Notify subsystems for newly added transactions
  1079. for _, tx := range promoted {
  1080. addr, _ := types.Sender(pool.signer, tx)
  1081. if _, ok := events[addr]; !ok {
  1082. events[addr] = newTxSortedMap()
  1083. }
  1084. events[addr].Put(tx)
  1085. }
  1086. if len(events) > 0 {
  1087. var txs []*types.Transaction
  1088. for _, set := range events {
  1089. txs = append(txs, set.Flatten()...)
  1090. }
  1091. pool.txFeed.Send(NewTxsEvent{txs})
  1092. }
  1093. }
  1094. // reset retrieves the current state of the blockchain and ensures the content
  1095. // of the transaction pool is valid with regard to the chain state.
  1096. func (pool *TxPool) reset(oldHead, newHead *types.Header) {
  1097. // If we're reorging an old state, reinject all dropped transactions
  1098. var reinject types.Transactions
  1099. if oldHead != nil && oldHead.Hash() != newHead.ParentHash {
  1100. // If the reorg is too deep, avoid doing it (will happen during fast sync)
  1101. oldNum := oldHead.Number.Uint64()
  1102. newNum := newHead.Number.Uint64()
  1103. if depth := uint64(math.Abs(float64(oldNum) - float64(newNum))); depth > 64 {
  1104. log.Debug("Skipping deep transaction reorg", "depth", depth)
  1105. } else {
  1106. // Reorg seems shallow enough to pull in all transactions into memory
  1107. var discarded, included types.Transactions
  1108. var (
  1109. rem = pool.chain.GetBlock(oldHead.Hash(), oldHead.Number.Uint64())
  1110. add = pool.chain.GetBlock(newHead.Hash(), newHead.Number.Uint64())
  1111. )
  1112. if rem == nil {
  1113. // This can happen if a setHead is performed, where we simply discard the old
  1114. // head from the chain.
  1115. // If that is the case, we don't have the lost transactions any more, and
  1116. // there's nothing to add
  1117. if newNum >= oldNum {
  1118. // If we reorged to a same or higher number, then it's not a case of setHead
  1119. log.Warn("Transaction pool reset with missing oldhead",
  1120. "old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum)
  1121. return
  1122. }
  1123. // If the reorg ended up on a lower number, it's indicative of setHead being the cause
  1124. log.Debug("Skipping transaction reset caused by setHead",
  1125. "old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum)
  1126. // We still need to update the current state s.th. the lost transactions can be readded by the user
  1127. } else {
  1128. for rem.NumberU64() > add.NumberU64() {
  1129. discarded = append(discarded, rem.Transactions()...)
  1130. if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
  1131. log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
  1132. return
  1133. }
  1134. }
  1135. for add.NumberU64() > rem.NumberU64() {
  1136. included = append(included, add.Transactions()...)
  1137. if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
  1138. log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
  1139. return
  1140. }
  1141. }
  1142. for rem.Hash() != add.Hash() {
  1143. discarded = append(discarded, rem.Transactions()...)
  1144. if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
  1145. log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
  1146. return
  1147. }
  1148. included = append(included, add.Transactions()...)
  1149. if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
  1150. log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
  1151. return
  1152. }
  1153. }
  1154. reinject = types.TxDifference(discarded, included)
  1155. }
  1156. }
  1157. }
  1158. // Initialize the internal state to the current head
  1159. if newHead == nil {
  1160. newHead = pool.chain.CurrentBlock().Header() // Special case during testing
  1161. }
  1162. statedb, err := pool.chain.StateAt(newHead.Root)
  1163. if err != nil {
  1164. log.Error("Failed to reset txpool state", "err", err)
  1165. return
  1166. }
  1167. pool.currentState = statedb
  1168. pool.pendingNonces = newTxNoncer(statedb)
  1169. pool.currentMaxGas = newHead.GasLimit
  1170. // Inject any transactions discarded due to reorgs
  1171. log.Debug("Reinjecting stale transactions", "count", len(reinject))
  1172. senderCacher.recover(pool.signer, reinject)
  1173. pool.addTxsLocked(reinject, false)
  1174. // Update all fork indicator by next pending block number.
  1175. next := new(big.Int).Add(newHead.Number, big.NewInt(1))
  1176. pool.istanbul = pool.chainconfig.IsIstanbul(next)
  1177. pool.eip2718 = pool.chainconfig.IsBerlin(next)
  1178. }
  1179. // promoteExecutables moves transactions that have become processable from the
  1180. // future queue to the set of pending transactions. During this process, all
  1181. // invalidated transactions (low nonce, low balance) are deleted.
  1182. func (pool *TxPool) promoteExecutables(accounts []common.Address) []*types.Transaction {
  1183. // Track the promoted transactions to broadcast them at once
  1184. var promoted []*types.Transaction
  1185. // Iterate over all accounts and promote any executable transactions
  1186. for _, addr := range accounts {
  1187. list := pool.queue[addr]
  1188. if list == nil {
  1189. continue // Just in case someone calls with a non existing account
  1190. }
  1191. // Drop all transactions that are deemed too old (low nonce)
  1192. forwards := list.Forward(pool.currentState.GetNonce(addr))
  1193. for _, tx := range forwards {
  1194. hash := tx.Hash()
  1195. pool.all.Remove(hash)
  1196. }
  1197. log.Trace("Removed old queued transactions", "count", len(forwards))
  1198. // Drop all transactions that are too costly (low balance or out of gas)
  1199. drops, _ := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas)
  1200. for _, tx := range drops {
  1201. hash := tx.Hash()
  1202. pool.all.Remove(hash)
  1203. }
  1204. log.Trace("Removed unpayable queued transactions", "count", len(drops))
  1205. queuedNofundsMeter.Mark(int64(len(drops)))
  1206. // Gather all executable transactions and promote them
  1207. readies := list.Ready(pool.pendingNonces.get(addr))
  1208. for _, tx := range readies {
  1209. hash := tx.Hash()
  1210. if pool.promoteTx(addr, hash, tx) {
  1211. promoted = append(promoted, tx)
  1212. }
  1213. }
  1214. log.Trace("Promoted queued transactions", "count", len(promoted))
  1215. queuedGauge.Dec(int64(len(readies)))
  1216. // Drop all transactions over the allowed limit
  1217. var caps types.Transactions
  1218. if !pool.locals.contains(addr) {
  1219. caps = list.Cap(int(pool.config.AccountQueue))
  1220. for _, tx := range caps {
  1221. hash := tx.Hash()
  1222. pool.all.Remove(hash)
  1223. log.Trace("Removed cap-exceeding queued transaction", "hash", hash)
  1224. }
  1225. queuedRateLimitMeter.Mark(int64(len(caps)))
  1226. }
  1227. // Mark all the items dropped as removed
  1228. pool.priced.Removed(len(forwards) + len(drops) + len(caps))
  1229. queuedGauge.Dec(int64(len(forwards) + len(drops) + len(caps)))
  1230. if pool.locals.contains(addr) {
  1231. localGauge.Dec(int64(len(forwards) + len(drops) + len(caps)))
  1232. }
  1233. // Delete the entire queue entry if it became empty.
  1234. if list.Empty() {
  1235. delete(pool.queue, addr)
  1236. delete(pool.beats, addr)
  1237. }
  1238. }
  1239. return promoted
  1240. }
  1241. // truncatePending removes transactions from the pending queue if the pool is above the
  1242. // pending limit. The algorithm tries to reduce transaction counts by an approximately
  1243. // equal number for all for accounts with many pending transactions.
  1244. func (pool *TxPool) truncatePending() {
  1245. pending := uint64(0)
  1246. for _, list := range pool.pending {
  1247. pending += uint64(len(list.txs.items))
  1248. }
  1249. if pending <= pool.config.GlobalSlots {
  1250. return
  1251. }
  1252. pendingBeforeCap := pending
  1253. // Assemble a spam order to penalize large transactors first
  1254. spammers := prque.New(nil)
  1255. for addr, list := range pool.pending {
  1256. // Only evict transactions from high rollers
  1257. if !pool.locals.contains(addr) && uint64(len(list.txs.items)) > pool.config.AccountSlots {
  1258. spammers.Push(addr, int64(len(list.txs.items)))
  1259. }
  1260. }
  1261. // Gradually drop transactions from offenders
  1262. offenders := []common.Address{}
  1263. for pending > pool.config.GlobalSlots && !spammers.Empty() {
  1264. // Retrieve the next offender if not local address
  1265. offender, _ := spammers.Pop()
  1266. offenders = append(offenders, offender.(common.Address))
  1267. // Equalize balances until all the same or below threshold
  1268. if len(offenders) > 1 {
  1269. // Calculate the equalization threshold for all current offenders
  1270. threshold := len(pool.pending[offender.(common.Address)].txs.items)
  1271. // Iteratively reduce all offenders until below limit or threshold reached
  1272. for pending > pool.config.GlobalSlots && len(pool.pending[offenders[len(offenders)-2]].txs.items) > threshold {
  1273. for i := 0; i < len(offenders)-1; i++ {
  1274. list := pool.pending[offenders[i]]
  1275. caps := list.Cap(len(list.txs.items) - 1)
  1276. for _, tx := range caps {
  1277. // Drop the transaction from the global pools too
  1278. hash := tx.Hash()
  1279. pool.all.Remove(hash)
  1280. // Update the account nonce to the dropped transaction
  1281. pool.pendingNonces.setIfLower(offenders[i], tx.Nonce())
  1282. log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
  1283. }
  1284. pool.priced.Removed(len(caps))
  1285. pendingGauge.Dec(int64(len(caps)))
  1286. if pool.locals.contains(offenders[i]) {
  1287. localGauge.Dec(int64(len(caps)))
  1288. }
  1289. pending--
  1290. }
  1291. }
  1292. }
  1293. }
  1294. // If still above threshold, reduce to limit or min allowance
  1295. if pending > pool.config.GlobalSlots && len(offenders) > 0 {
  1296. for pending > pool.config.GlobalSlots && uint64(len(pool.pending[offenders[len(offenders)-1]].txs.items)) > pool.config.AccountSlots {
  1297. for _, addr := range offenders {
  1298. list := pool.pending[addr]
  1299. caps := list.Cap(len(list.txs.items) - 1)
  1300. for _, tx := range caps {
  1301. // Drop the transaction from the global pools too
  1302. hash := tx.Hash()
  1303. pool.all.Remove(hash)
  1304. // Update the account nonce to the dropped transaction
  1305. pool.pendingNonces.setIfLower(addr, tx.Nonce())
  1306. log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
  1307. }
  1308. pool.priced.Removed(len(caps))
  1309. pendingGauge.Dec(int64(len(caps)))
  1310. if pool.locals.contains(addr) {
  1311. localGauge.Dec(int64(len(caps)))
  1312. }
  1313. pending--
  1314. }
  1315. }
  1316. }
  1317. pendingRateLimitMeter.Mark(int64(pendingBeforeCap - pending))
  1318. }
  1319. // truncateQueue drops the oldes transactions in the queue if the pool is above the global queue limit.
  1320. func (pool *TxPool) truncateQueue() {
  1321. queued := uint64(0)
  1322. for _, list := range pool.queue {
  1323. queued += uint64(len(list.txs.items))
  1324. }
  1325. if queued <= pool.config.GlobalQueue {
  1326. return
  1327. }
  1328. // Sort all accounts with queued transactions by heartbeat
  1329. addresses := make(addressesByHeartbeat, 0, len(pool.queue))
  1330. for addr := range pool.queue {
  1331. if !pool.locals.contains(addr) { // don't drop locals
  1332. addresses = append(addresses, addressByHeartbeat{addr, pool.beats[addr]})
  1333. }
  1334. }
  1335. sort.Sort(addresses)
  1336. // Drop transactions until the total is below the limit or only locals remain
  1337. for drop := queued - pool.config.GlobalQueue; drop > 0 && len(addresses) > 0; {
  1338. addr := addresses[len(addresses)-1]
  1339. list := pool.queue[addr.address]
  1340. addresses = addresses[:len(addresses)-1]
  1341. // Drop all transactions if they are less than the overflow
  1342. if size := uint64(len(list.txs.items)); size <= drop {
  1343. for _, tx := range list.Flatten() {
  1344. pool.removeTx(tx.Hash(), true)
  1345. }
  1346. drop -= size
  1347. queuedRateLimitMeter.Mark(int64(size))
  1348. continue
  1349. }
  1350. // Otherwise drop only last few transactions
  1351. txs := list.Flatten()
  1352. for i := len(txs) - 1; i >= 0 && drop > 0; i-- {
  1353. pool.removeTx(txs[i].Hash(), true)
  1354. drop--
  1355. queuedRateLimitMeter.Mark(1)
  1356. }
  1357. }
  1358. }
  1359. // demoteUnexecutables removes invalid and processed transactions from the pools
  1360. // executable/pending queue and any subsequent transactions that become unexecutable
  1361. // are moved back into the future queue.
  1362. func (pool *TxPool) demoteUnexecutables() {
  1363. // Iterate over all accounts and demote any non-executable transactions
  1364. for addr, list := range pool.pending {
  1365. nonce := pool.currentState.GetNonce(addr)
  1366. // Drop all transactions that are deemed too old (low nonce)
  1367. olds := list.Forward(nonce)
  1368. for _, tx := range olds {
  1369. hash := tx.Hash()
  1370. pool.all.Remove(hash)
  1371. log.Trace("Removed old pending transaction", "hash", hash)
  1372. }
  1373. // Drop all transactions that are too costly (low balance or out of gas), and queue any invalids back for later
  1374. drops, invalids := list.Filter(pool.currentState.GetBalance(addr), pool.currentMaxGas)
  1375. for _, tx := range drops {
  1376. hash := tx.Hash()
  1377. log.Trace("Removed unpayable pending transaction", "hash", hash)
  1378. pool.all.Remove(hash)
  1379. }
  1380. pool.priced.Removed(len(olds) + len(drops))
  1381. pendingNofundsMeter.Mark(int64(len(drops)))
  1382. for _, tx := range invalids {
  1383. hash := tx.Hash()
  1384. log.Trace("Demoting pending transaction", "hash", hash)
  1385. // Internal shuffle shouldn't touch the lookup set.
  1386. pool.enqueueTx(hash, tx, false, false)
  1387. }
  1388. pendingGauge.Dec(int64(len(olds) + len(drops) + len(invalids)))
  1389. if pool.locals.contains(addr) {
  1390. localGauge.Dec(int64(len(olds) + len(drops) + len(invalids)))
  1391. }
  1392. // If there's a gap in front, alert (should never happen) and postpone all transactions
  1393. if len(list.txs.items) > 0 && list.txs.Get(nonce) == nil {
  1394. gapped := list.Cap(0)
  1395. for _, tx := range gapped {
  1396. hash := tx.Hash()
  1397. log.Error("Demoting invalidated transaction", "hash", hash)
  1398. // Internal shuffle shouldn't touch the lookup set.
  1399. pool.enqueueTx(hash, tx, false, false)
  1400. }
  1401. pendingGauge.Dec(int64(len(gapped)))
  1402. // This might happen in a reorg, so log it to the metering
  1403. blockReorgInvalidatedTx.Mark(int64(len(gapped)))
  1404. }
  1405. // Delete the entire pending entry if it became empty.
  1406. if list.Empty() {
  1407. delete(pool.pending, addr)
  1408. }
  1409. }
  1410. }
  1411. // addressByHeartbeat is an account address tagged with its last activity timestamp.
  1412. type addressByHeartbeat struct {
  1413. address common.Address
  1414. heartbeat time.Time
  1415. }
  1416. type addressesByHeartbeat []addressByHeartbeat
  1417. func (a addressesByHeartbeat) Len() int { return len(a) }
  1418. func (a addressesByHeartbeat) Less(i, j int) bool { return a[i].heartbeat.Before(a[j].heartbeat) }
  1419. func (a addressesByHeartbeat) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  1420. // accountSet is simply a set of addresses to check for existence, and a signer
  1421. // capable of deriving addresses from transactions.
  1422. type accountSet struct {
  1423. accounts map[common.Address]struct{}
  1424. signer types.Signer
  1425. cache *[]common.Address
  1426. }
  1427. // newAccountSet creates a new address set with an associated signer for sender
  1428. // derivations.
  1429. func newAccountSet(signer types.Signer, addrs ...common.Address) *accountSet {
  1430. as := &accountSet{
  1431. accounts: make(map[common.Address]struct{}),
  1432. signer: signer,
  1433. }
  1434. for _, addr := range addrs {
  1435. as.add(addr)
  1436. }
  1437. return as
  1438. }
  1439. // contains checks if a given address is contained within the set.
  1440. func (as *accountSet) contains(addr common.Address) bool {
  1441. _, exist := as.accounts[addr]
  1442. return exist
  1443. }
  1444. func (as *accountSet) empty() bool {
  1445. return len(as.accounts) == 0
  1446. }
  1447. // containsTx checks if the sender of a given tx is within the set. If the sender
  1448. // cannot be derived, this method returns false.
  1449. func (as *accountSet) containsTx(tx *types.Transaction) bool {
  1450. if addr, err := types.Sender(as.signer, tx); err == nil {
  1451. return as.contains(addr)
  1452. }
  1453. return false
  1454. }
  1455. // add inserts a new address into the set to track.
  1456. func (as *accountSet) add(addr common.Address) {
  1457. as.accounts[addr] = struct{}{}
  1458. as.cache = nil
  1459. }
  1460. // addTx adds the sender of tx into the set.
  1461. func (as *accountSet) addTx(tx *types.Transaction) {
  1462. if addr, err := types.Sender(as.signer, tx); err == nil {
  1463. as.add(addr)
  1464. }
  1465. }
  1466. // flatten returns the list of addresses within this set, also caching it for later
  1467. // reuse. The returned slice should not be changed!
  1468. func (as *accountSet) flatten() []common.Address {
  1469. if as.cache == nil {
  1470. accounts := make([]common.Address, 0, len(as.accounts))
  1471. for account := range as.accounts {
  1472. accounts = append(accounts, account)
  1473. }
  1474. as.cache = &accounts
  1475. }
  1476. return *as.cache
  1477. }
  1478. // merge adds all addresses from the 'other' set into 'as'.
  1479. func (as *accountSet) merge(other *accountSet) {
  1480. for addr := range other.accounts {
  1481. as.accounts[addr] = struct{}{}
  1482. }
  1483. as.cache = nil
  1484. }
  1485. // txLookup is used internally by TxPool to track transactions while allowing
  1486. // lookup without mutex contention.
  1487. //
  1488. // Note, although this type is properly protected against concurrent access, it
  1489. // is **not** a type that should ever be mutated or even exposed outside of the
  1490. // transaction pool, since its internal state is tightly coupled with the pools
  1491. // internal mechanisms. The sole purpose of the type is to permit out-of-bound
  1492. // peeking into the pool in TxPool.Get without having to acquire the widely scoped
  1493. // TxPool.mu mutex.
  1494. //
  1495. // This lookup set combines the notion of "local transactions", which is useful
  1496. // to build upper-level structure.
  1497. // txLookup 被 TxPool 内部用于在不使用互斥锁竞争的情况下跟踪交易并允许查找。
  1498. //
  1499. // 注意,尽管这个类型在并发访问方面受到适当的保护,但它**不是**一种应该被修改甚至在事务池之外暴露的类型,因为它的内部状态与池的内部机制紧密耦合。
  1500. // 这个类型的唯一目的是允许在不必获取广泛范围的 TxPool.mu 互斥锁的情况下,在 TxPool.Get 中对池进行超出边界的查看。
  1501. //
  1502. // 这个查找集合结合了“本地交易”的概念,对于构建上层结构非常有用。
  1503. type txLookup struct {
  1504. slots int
  1505. lock sync.RWMutex
  1506. locals map[common.Hash]*types.Transaction
  1507. remotes map[common.Hash]*types.Transaction
  1508. }
  1509. // newTxLookup returns a new txLookup structure.
  1510. func newTxLookup() *txLookup {
  1511. return &txLookup{
  1512. locals: make(map[common.Hash]*types.Transaction),
  1513. remotes: make(map[common.Hash]*types.Transaction),
  1514. }
  1515. }
  1516. // Range calls f on each key and value present in the map. The callback passed
  1517. // should return the indicator whether the iteration needs to be continued.
  1518. // Callers need to specify which set (or both) to be iterated.
  1519. func (t *txLookup) Range(f func(hash common.Hash, tx *types.Transaction, local bool) bool, local bool, remote bool) {
  1520. t.lock.RLock()
  1521. defer t.lock.RUnlock()
  1522. if local {
  1523. for key, value := range t.locals {
  1524. if !f(key, value, true) {
  1525. return
  1526. }
  1527. }
  1528. }
  1529. if remote {
  1530. for key, value := range t.remotes {
  1531. if !f(key, value, false) {
  1532. return
  1533. }
  1534. }
  1535. }
  1536. }
  1537. // Get returns a transaction if it exists in the lookup, or nil if not found.
  1538. func (t *txLookup) Get(hash common.Hash) *types.Transaction {
  1539. t.lock.RLock()
  1540. defer t.lock.RUnlock()
  1541. if tx := t.locals[hash]; tx != nil {
  1542. return tx
  1543. }
  1544. return t.remotes[hash]
  1545. }
  1546. // GetLocal returns a transaction if it exists in the lookup, or nil if not found.
  1547. func (t *txLookup) GetLocal(hash common.Hash) *types.Transaction {
  1548. t.lock.RLock()
  1549. defer t.lock.RUnlock()
  1550. return t.locals[hash]
  1551. }
  1552. // GetRemote returns a transaction if it exists in the lookup, or nil if not found.
  1553. func (t *txLookup) GetRemote(hash common.Hash) *types.Transaction {
  1554. t.lock.RLock()
  1555. defer t.lock.RUnlock()
  1556. return t.remotes[hash]
  1557. }
  1558. // Count returns the current number of transactions in the lookup.
  1559. func (t *txLookup) Count() int {
  1560. t.lock.RLock()
  1561. defer t.lock.RUnlock()
  1562. return len(t.locals) + len(t.remotes)
  1563. }
  1564. // LocalCount returns the current number of local transactions in the lookup.
  1565. func (t *txLookup) LocalCount() int {
  1566. t.lock.RLock()
  1567. defer t.lock.RUnlock()
  1568. return len(t.locals)
  1569. }
  1570. // RemoteCount returns the current number of remote transactions in the lookup.
  1571. func (t *txLookup) RemoteCount() int {
  1572. t.lock.RLock()
  1573. defer t.lock.RUnlock()
  1574. return len(t.remotes)
  1575. }
  1576. // Slots returns the current number of slots used in the lookup.
  1577. func (t *txLookup) Slots() int {
  1578. t.lock.RLock()
  1579. defer t.lock.RUnlock()
  1580. return t.slots
  1581. }
  1582. // Add adds a transaction to the lookup.
  1583. // 函数 `Add` 用于向查找表(txLookup)中添加一个交易。
  1584. //
  1585. // 具体而言,该函数执行以下操作:
  1586. // 1. 获取查找表的互斥锁(t.lock)以确保并发安全。
  1587. // 2. 根据交易的类型(local 或 remote),将交易添加到相应的映射表中。
  1588. // - 如果交易被标记为 local,则将其添加到 `t.locals` 映射表中,使用交易的哈希(tx.Hash())作为键,交易对象(tx)作为值。
  1589. // - 如果交易被标记为 remote,则将其添加到 `t.remotes` 映射表中,使用交易的哈希作为键,交易对象作为值。
  1590. // 3. 更新查找表的插槽计数(t.slots),增加交易的插槽数(numSlots(tx))。
  1591. // 4. 更新插槽计数的度量指标(slotsGauge)。
  1592. //
  1593. // 通过这些操作,函数将交易添加到查找表中,并相应地更新计数和度量指标。查找表可用于高效地查找和管理交易,并根据交易类型进行分类。
  1594. func (t *txLookup) Add(tx *types.Transaction, local bool) {
  1595. t.lock.Lock()
  1596. defer t.lock.Unlock()
  1597. t.slots += numSlots(tx)
  1598. slotsGauge.Update(int64(t.slots))
  1599. if local {
  1600. t.locals[tx.Hash()] = tx
  1601. } else {
  1602. t.remotes[tx.Hash()] = tx
  1603. }
  1604. }
  1605. // Remove removes a transaction from the lookup.
  1606. func (t *txLookup) Remove(hash common.Hash) {
  1607. t.lock.Lock()
  1608. defer t.lock.Unlock()
  1609. tx, ok := t.locals[hash]
  1610. if !ok {
  1611. tx, ok = t.remotes[hash]
  1612. }
  1613. if !ok {
  1614. log.Error("No transaction found to be deleted", "hash", hash)
  1615. return
  1616. }
  1617. t.slots -= numSlots(tx)
  1618. slotsGauge.Update(int64(t.slots))
  1619. delete(t.locals, hash)
  1620. delete(t.remotes, hash)
  1621. }
  1622. // RemoteToLocals migrates the transactions belongs to the given locals to locals
  1623. // set. The assumption is held the locals set is thread-safe to be used.
  1624. func (t *txLookup) RemoteToLocals(locals *accountSet) int {
  1625. t.lock.Lock()
  1626. defer t.lock.Unlock()
  1627. var migrated int
  1628. for hash, tx := range t.remotes {
  1629. if locals.containsTx(tx) {
  1630. t.locals[hash] = tx
  1631. delete(t.remotes, hash)
  1632. migrated += 1
  1633. }
  1634. }
  1635. return migrated
  1636. }
  1637. // numSlots calculates the number of slots needed for a single transaction.
  1638. func numSlots(tx *types.Transaction) int {
  1639. return int((tx.Size() + txSlotSize - 1) / txSlotSize)
  1640. }