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