github.com/nnlgsakib/mind-dpos@v0.0.0-20230606105614-f3c8ca06f808/light/txpool.go (about) 1 // Copyright 2016 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 light 18 19 import ( 20 "context" 21 "fmt" 22 "sync" 23 "time" 24 25 "github.com/TTCECO/gttc/common" 26 "github.com/TTCECO/gttc/core" 27 "github.com/TTCECO/gttc/core/rawdb" 28 "github.com/TTCECO/gttc/core/state" 29 "github.com/TTCECO/gttc/core/types" 30 "github.com/TTCECO/gttc/ethdb" 31 "github.com/TTCECO/gttc/event" 32 "github.com/TTCECO/gttc/log" 33 "github.com/TTCECO/gttc/params" 34 "github.com/TTCECO/gttc/rlp" 35 ) 36 37 const ( 38 // chainHeadChanSize is the size of channel listening to ChainHeadEvent. 39 chainHeadChanSize = 10 40 ) 41 42 // txPermanent is the number of mined blocks after a mined transaction is 43 // considered permanent and no rollback is expected 44 var txPermanent = uint64(500) 45 46 // TxPool implements the transaction pool for light clients, which keeps track 47 // of the status of locally created transactions, detecting if they are included 48 // in a block (mined) or rolled back. There are no queued transactions since we 49 // always receive all locally signed transactions in the same order as they are 50 // created. 51 type TxPool struct { 52 config *params.ChainConfig 53 signer types.Signer 54 quit chan bool 55 txFeed event.Feed 56 scope event.SubscriptionScope 57 chainHeadCh chan core.ChainHeadEvent 58 chainHeadSub event.Subscription 59 mu sync.RWMutex 60 chain *LightChain 61 odr OdrBackend 62 chainDb ethdb.Database 63 relay TxRelayBackend 64 head common.Hash 65 nonce map[common.Address]uint64 // "pending" nonce 66 pending map[common.Hash]*types.Transaction // pending transactions by tx hash 67 mined map[common.Hash][]*types.Transaction // mined transactions by block hash 68 clearIdx uint64 // earliest block nr that can contain mined tx info 69 70 homestead bool 71 } 72 73 // TxRelayBackend provides an interface to the mechanism that forwards transacions 74 // to the ETH network. The implementations of the functions should be non-blocking. 75 // 76 // Send instructs backend to forward new transactions 77 // NewHead notifies backend about a new head after processed by the tx pool, 78 // including mined and rolled back transactions since the last event 79 // Discard notifies backend about transactions that should be discarded either 80 // because they have been replaced by a re-send or because they have been mined 81 // long ago and no rollback is expected 82 type TxRelayBackend interface { 83 Send(txs types.Transactions) 84 NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) 85 Discard(hashes []common.Hash) 86 } 87 88 // NewTxPool creates a new light transaction pool 89 func NewTxPool(config *params.ChainConfig, chain *LightChain, relay TxRelayBackend) *TxPool { 90 pool := &TxPool{ 91 config: config, 92 signer: types.NewEIP155Signer(config.ChainId), 93 nonce: make(map[common.Address]uint64), 94 pending: make(map[common.Hash]*types.Transaction), 95 mined: make(map[common.Hash][]*types.Transaction), 96 quit: make(chan bool), 97 chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), 98 chain: chain, 99 relay: relay, 100 odr: chain.Odr(), 101 chainDb: chain.Odr().Database(), 102 head: chain.CurrentHeader().Hash(), 103 clearIdx: chain.CurrentHeader().Number.Uint64(), 104 } 105 // Subscribe events from blockchain 106 pool.chainHeadSub = pool.chain.SubscribeChainHeadEvent(pool.chainHeadCh) 107 go pool.eventLoop() 108 109 return pool 110 } 111 112 // currentState returns the light state of the current head header 113 func (pool *TxPool) currentState(ctx context.Context) *state.StateDB { 114 return NewState(ctx, pool.chain.CurrentHeader(), pool.odr) 115 } 116 117 // GetNonce returns the "pending" nonce of a given address. It always queries 118 // the nonce belonging to the latest header too in order to detect if another 119 // client using the same key sent a transaction. 120 func (pool *TxPool) GetNonce(ctx context.Context, addr common.Address) (uint64, error) { 121 state := pool.currentState(ctx) 122 nonce := state.GetNonce(addr) 123 if state.Error() != nil { 124 return 0, state.Error() 125 } 126 sn, ok := pool.nonce[addr] 127 if ok && sn > nonce { 128 nonce = sn 129 } 130 if !ok || sn < nonce { 131 pool.nonce[addr] = nonce 132 } 133 return nonce, nil 134 } 135 136 // txStateChanges stores the recent changes between pending/mined states of 137 // transactions. True means mined, false means rolled back, no entry means no change 138 type txStateChanges map[common.Hash]bool 139 140 // setState sets the status of a tx to either recently mined or recently rolled back 141 func (txc txStateChanges) setState(txHash common.Hash, mined bool) { 142 val, ent := txc[txHash] 143 if ent && (val != mined) { 144 delete(txc, txHash) 145 } else { 146 txc[txHash] = mined 147 } 148 } 149 150 // getLists creates lists of mined and rolled back tx hashes 151 func (txc txStateChanges) getLists() (mined []common.Hash, rollback []common.Hash) { 152 for hash, val := range txc { 153 if val { 154 mined = append(mined, hash) 155 } else { 156 rollback = append(rollback, hash) 157 } 158 } 159 return 160 } 161 162 // checkMinedTxs checks newly added blocks for the currently pending transactions 163 // and marks them as mined if necessary. It also stores block position in the db 164 // and adds them to the received txStateChanges map. 165 func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number uint64, txc txStateChanges) error { 166 // If no transactions are pending, we don't care about anything 167 if len(pool.pending) == 0 { 168 return nil 169 } 170 block, err := GetBlock(ctx, pool.odr, hash, number) 171 if err != nil { 172 return err 173 } 174 // Gather all the local transaction mined in this block 175 list := pool.mined[hash] 176 for _, tx := range block.Transactions() { 177 if _, ok := pool.pending[tx.Hash()]; ok { 178 list = append(list, tx) 179 } 180 } 181 // If some transactions have been mined, write the needed data to disk and update 182 if list != nil { 183 // Retrieve all the receipts belonging to this block and write the loopup table 184 if _, err := GetBlockReceipts(ctx, pool.odr, hash, number); err != nil { // ODR caches, ignore results 185 return err 186 } 187 rawdb.WriteTxLookupEntries(pool.chainDb, block) 188 189 // Update the transaction pool's state 190 for _, tx := range list { 191 delete(pool.pending, tx.Hash()) 192 txc.setState(tx.Hash(), true) 193 } 194 pool.mined[hash] = list 195 } 196 return nil 197 } 198 199 // rollbackTxs marks the transactions contained in recently rolled back blocks 200 // as rolled back. It also removes any positional lookup entries. 201 func (pool *TxPool) rollbackTxs(hash common.Hash, txc txStateChanges) { 202 if list, ok := pool.mined[hash]; ok { 203 for _, tx := range list { 204 txHash := tx.Hash() 205 rawdb.DeleteTxLookupEntry(pool.chainDb, txHash) 206 pool.pending[txHash] = tx 207 txc.setState(txHash, false) 208 } 209 delete(pool.mined, hash) 210 } 211 } 212 213 // reorgOnNewHead sets a new head header, processing (and rolling back if necessary) 214 // the blocks since the last known head and returns a txStateChanges map containing 215 // the recently mined and rolled back transaction hashes. If an error (context 216 // timeout) occurs during checking new blocks, it leaves the locally known head 217 // at the latest checked block and still returns a valid txStateChanges, making it 218 // possible to continue checking the missing blocks at the next chain head event 219 func (pool *TxPool) reorgOnNewHead(ctx context.Context, newHeader *types.Header) (txStateChanges, error) { 220 txc := make(txStateChanges) 221 oldh := pool.chain.GetHeaderByHash(pool.head) 222 newh := newHeader 223 // find common ancestor, create list of rolled back and new block hashes 224 var oldHashes, newHashes []common.Hash 225 for oldh.Hash() != newh.Hash() { 226 if oldh.Number.Uint64() >= newh.Number.Uint64() { 227 oldHashes = append(oldHashes, oldh.Hash()) 228 oldh = pool.chain.GetHeader(oldh.ParentHash, oldh.Number.Uint64()-1) 229 } 230 if oldh.Number.Uint64() < newh.Number.Uint64() { 231 newHashes = append(newHashes, newh.Hash()) 232 newh = pool.chain.GetHeader(newh.ParentHash, newh.Number.Uint64()-1) 233 if newh == nil { 234 // happens when CHT syncing, nothing to do 235 newh = oldh 236 } 237 } 238 } 239 if oldh.Number.Uint64() < pool.clearIdx { 240 pool.clearIdx = oldh.Number.Uint64() 241 } 242 // roll back old blocks 243 for _, hash := range oldHashes { 244 pool.rollbackTxs(hash, txc) 245 } 246 pool.head = oldh.Hash() 247 // check mined txs of new blocks (array is in reversed order) 248 for i := len(newHashes) - 1; i >= 0; i-- { 249 hash := newHashes[i] 250 if err := pool.checkMinedTxs(ctx, hash, newHeader.Number.Uint64()-uint64(i), txc); err != nil { 251 return txc, err 252 } 253 pool.head = hash 254 } 255 256 // clear old mined tx entries of old blocks 257 if idx := newHeader.Number.Uint64(); idx > pool.clearIdx+txPermanent { 258 idx2 := idx - txPermanent 259 if len(pool.mined) > 0 { 260 for i := pool.clearIdx; i < idx2; i++ { 261 hash := rawdb.ReadCanonicalHash(pool.chainDb, i) 262 if list, ok := pool.mined[hash]; ok { 263 hashes := make([]common.Hash, len(list)) 264 for i, tx := range list { 265 hashes[i] = tx.Hash() 266 } 267 pool.relay.Discard(hashes) 268 delete(pool.mined, hash) 269 } 270 } 271 } 272 pool.clearIdx = idx2 273 } 274 275 return txc, nil 276 } 277 278 // blockCheckTimeout is the time limit for checking new blocks for mined 279 // transactions. Checking resumes at the next chain head event if timed out. 280 const blockCheckTimeout = time.Second * 3 281 282 // eventLoop processes chain head events and also notifies the tx relay backend 283 // about the new head hash and tx state changes 284 func (pool *TxPool) eventLoop() { 285 for { 286 select { 287 case ev := <-pool.chainHeadCh: 288 pool.setNewHead(ev.Block.Header()) 289 // hack in order to avoid hogging the lock; this part will 290 // be replaced by a subsequent PR. 291 time.Sleep(time.Millisecond) 292 293 // System stopped 294 case <-pool.chainHeadSub.Err(): 295 return 296 } 297 } 298 } 299 300 func (pool *TxPool) setNewHead(head *types.Header) { 301 pool.mu.Lock() 302 defer pool.mu.Unlock() 303 304 ctx, cancel := context.WithTimeout(context.Background(), blockCheckTimeout) 305 defer cancel() 306 307 txc, _ := pool.reorgOnNewHead(ctx, head) 308 m, r := txc.getLists() 309 pool.relay.NewHead(pool.head, m, r) 310 pool.homestead = pool.config.IsHomestead(head.Number) 311 pool.signer = types.MakeSigner(pool.config, head.Number) 312 } 313 314 // Stop stops the light transaction pool 315 func (pool *TxPool) Stop() { 316 // Unsubscribe all subscriptions registered from txpool 317 pool.scope.Close() 318 // Unsubscribe subscriptions registered from blockchain 319 pool.chainHeadSub.Unsubscribe() 320 close(pool.quit) 321 log.Info("Transaction pool stopped") 322 } 323 324 // SubscribeNewTxsEvent registers a subscription of core.NewTxsEvent and 325 // starts sending event to the given channel. 326 func (pool *TxPool) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 327 return pool.scope.Track(pool.txFeed.Subscribe(ch)) 328 } 329 330 // Stats returns the number of currently pending (locally created) transactions 331 func (pool *TxPool) Stats() (pending int) { 332 pool.mu.RLock() 333 defer pool.mu.RUnlock() 334 335 pending = len(pool.pending) 336 return 337 } 338 339 // validateTx checks whether a transaction is valid according to the consensus rules. 340 func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error { 341 // Validate sender 342 var ( 343 from common.Address 344 err error 345 ) 346 347 // Validate the transaction sender and it's sig. Throw 348 // if the from fields is invalid. 349 if from, err = types.Sender(pool.signer, tx); err != nil { 350 return core.ErrInvalidSender 351 } 352 // Last but not least check for nonce errors 353 currentState := pool.currentState(ctx) 354 if n := currentState.GetNonce(from); n > tx.Nonce() { 355 return core.ErrNonceTooLow 356 } 357 358 // Check the transaction doesn't exceed the current 359 // block limit gas. 360 header := pool.chain.GetHeaderByHash(pool.head) 361 if header.GasLimit < tx.Gas() { 362 return core.ErrGasLimit 363 } 364 365 // Transactions can't be negative. This may never happen 366 // using RLP decoded transactions but may occur if you create 367 // a transaction using the RPC for example. 368 if tx.Value().Sign() < 0 { 369 return core.ErrNegativeValue 370 } 371 372 // Transactor should have enough funds to cover the costs 373 // cost == V + GP * GL 374 if b := currentState.GetBalance(from); b.Cmp(tx.Cost()) < 0 { 375 return core.ErrInsufficientFunds 376 } 377 378 // Should supply enough intrinsic gas 379 gas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead) 380 if err != nil { 381 return err 382 } 383 if tx.Gas() < gas { 384 return core.ErrIntrinsicGas 385 } 386 return currentState.Error() 387 } 388 389 // add validates a new transaction and sets its state pending if processable. 390 // It also updates the locally stored nonce if necessary. 391 func (self *TxPool) add(ctx context.Context, tx *types.Transaction) error { 392 hash := tx.Hash() 393 394 if self.pending[hash] != nil { 395 return fmt.Errorf("Known transaction (%x)", hash[:4]) 396 } 397 err := self.validateTx(ctx, tx) 398 if err != nil { 399 return err 400 } 401 402 if _, ok := self.pending[hash]; !ok { 403 self.pending[hash] = tx 404 405 nonce := tx.Nonce() + 1 406 407 addr, _ := types.Sender(self.signer, tx) 408 if nonce > self.nonce[addr] { 409 self.nonce[addr] = nonce 410 } 411 412 // Notify the subscribers. This event is posted in a goroutine 413 // because it's possible that somewhere during the post "Remove transaction" 414 // gets called which will then wait for the global tx pool lock and deadlock. 415 go self.txFeed.Send(core.NewTxsEvent{Txs: types.Transactions{tx}}) 416 } 417 418 // Print a log message if low enough level is set 419 log.Debug("Pooled new transaction", "hash", hash, "from", log.Lazy{Fn: func() common.Address { from, _ := types.Sender(self.signer, tx); return from }}, "to", tx.To()) 420 return nil 421 } 422 423 // Add adds a transaction to the pool if valid and passes it to the tx relay 424 // backend 425 func (self *TxPool) Add(ctx context.Context, tx *types.Transaction) error { 426 self.mu.Lock() 427 defer self.mu.Unlock() 428 429 data, err := rlp.EncodeToBytes(tx) 430 if err != nil { 431 return err 432 } 433 434 if err := self.add(ctx, tx); err != nil { 435 return err 436 } 437 //fmt.Println("Send", tx.Hash()) 438 self.relay.Send(types.Transactions{tx}) 439 440 self.chainDb.Put(tx.Hash().Bytes(), data) 441 return nil 442 } 443 444 // AddTransactions adds all valid transactions to the pool and passes them to 445 // the tx relay backend 446 func (self *TxPool) AddBatch(ctx context.Context, txs []*types.Transaction) { 447 self.mu.Lock() 448 defer self.mu.Unlock() 449 var sendTx types.Transactions 450 451 for _, tx := range txs { 452 if err := self.add(ctx, tx); err == nil { 453 sendTx = append(sendTx, tx) 454 } 455 } 456 if len(sendTx) > 0 { 457 self.relay.Send(sendTx) 458 } 459 } 460 461 // GetTransaction returns a transaction if it is contained in the pool 462 // and nil otherwise. 463 func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction { 464 // check the txs first 465 if tx, ok := tp.pending[hash]; ok { 466 return tx 467 } 468 return nil 469 } 470 471 // GetTransactions returns all currently processable transactions. 472 // The returned slice may be modified by the caller. 473 func (self *TxPool) GetTransactions() (txs types.Transactions, err error) { 474 self.mu.RLock() 475 defer self.mu.RUnlock() 476 477 txs = make(types.Transactions, len(self.pending)) 478 i := 0 479 for _, tx := range self.pending { 480 txs[i] = tx 481 i++ 482 } 483 return txs, nil 484 } 485 486 // Content retrieves the data content of the transaction pool, returning all the 487 // pending as well as queued transactions, grouped by account and nonce. 488 func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 489 self.mu.RLock() 490 defer self.mu.RUnlock() 491 492 // Retrieve all the pending transactions and sort by account and by nonce 493 pending := make(map[common.Address]types.Transactions) 494 for _, tx := range self.pending { 495 account, _ := types.Sender(self.signer, tx) 496 pending[account] = append(pending[account], tx) 497 } 498 // There are no queued transactions in a light pool, just return an empty map 499 queued := make(map[common.Address]types.Transactions) 500 return pending, queued 501 } 502 503 // RemoveTransactions removes all given transactions from the pool. 504 func (self *TxPool) RemoveTransactions(txs types.Transactions) { 505 self.mu.Lock() 506 defer self.mu.Unlock() 507 var hashes []common.Hash 508 for _, tx := range txs { 509 //self.RemoveTx(tx.Hash()) 510 hash := tx.Hash() 511 delete(self.pending, hash) 512 self.chainDb.Delete(hash[:]) 513 hashes = append(hashes, hash) 514 } 515 self.relay.Discard(hashes) 516 } 517 518 // RemoveTx removes the transaction with the given hash from the pool. 519 func (pool *TxPool) RemoveTx(hash common.Hash) { 520 pool.mu.Lock() 521 defer pool.mu.Unlock() 522 // delete from pending pool 523 delete(pool.pending, hash) 524 pool.chainDb.Delete(hash[:]) 525 pool.relay.Discard([]common.Hash{hash}) 526 }