github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/light/txpool.go (about) 1 // Copyright 2019 The ebakus/go-ebakus Authors 2 // This file is part of the ebakus/go-ebakus library. 3 // 4 // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus 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/ebakus/go-ebakus/common" 26 "github.com/ebakus/go-ebakus/core" 27 "github.com/ebakus/go-ebakus/core/rawdb" 28 "github.com/ebakus/go-ebakus/core/state" 29 "github.com/ebakus/go-ebakus/core/types" 30 "github.com/ebakus/go-ebakus/ethdb" 31 "github.com/ebakus/go-ebakus/event" 32 "github.com/ebakus/go-ebakus/log" 33 "github.com/ebakus/go-ebakus/params" 34 "github.com/ebakus/go-ebakus/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 71 // TxRelayBackend provides an interface to the mechanism that forwards transacions 72 // to the ETH network. The implementations of the functions should be non-blocking. 73 // 74 // Send instructs backend to forward new transactions 75 // NewHead notifies backend about a new head after processed by the tx pool, 76 // including mined and rolled back transactions since the last event 77 // Discard notifies backend about transactions that should be discarded either 78 // because they have been replaced by a re-send or because they have been mined 79 // long ago and no rollback is expected 80 type TxRelayBackend interface { 81 Send(txs types.Transactions) 82 NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) 83 Discard(hashes []common.Hash) 84 } 85 86 // NewTxPool creates a new light transaction pool 87 func NewTxPool(config *params.ChainConfig, chain *LightChain, relay TxRelayBackend) *TxPool { 88 pool := &TxPool{ 89 config: config, 90 signer: types.NewEIP155Signer(config.ChainID), 91 nonce: make(map[common.Address]uint64), 92 pending: make(map[common.Hash]*types.Transaction), 93 mined: make(map[common.Hash][]*types.Transaction), 94 quit: make(chan bool), 95 chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), 96 chain: chain, 97 relay: relay, 98 odr: chain.Odr(), 99 chainDb: chain.Odr().Database(), 100 head: chain.CurrentHeader().Hash(), 101 clearIdx: chain.CurrentHeader().Number.Uint64(), 102 } 103 // Subscribe events from blockchain 104 pool.chainHeadSub = pool.chain.SubscribeChainHeadEvent(pool.chainHeadCh) 105 go pool.eventLoop() 106 107 return pool 108 } 109 110 // currentState returns the light state of the current head header 111 func (pool *TxPool) currentState(ctx context.Context) *state.StateDB { 112 return NewState(ctx, pool.chain.CurrentHeader(), pool.odr) 113 } 114 115 // GetNonce returns the "pending" nonce of a given address. It always queries 116 // the nonce belonging to the latest header too in order to detect if another 117 // client using the same key sent a transaction. 118 func (pool *TxPool) GetNonce(ctx context.Context, addr common.Address) (uint64, error) { 119 state := pool.currentState(ctx) 120 nonce := state.GetNonce(addr) 121 if state.Error() != nil { 122 return 0, state.Error() 123 } 124 sn, ok := pool.nonce[addr] 125 if ok && sn > nonce { 126 nonce = sn 127 } 128 if !ok || sn < nonce { 129 pool.nonce[addr] = nonce 130 } 131 return nonce, nil 132 } 133 134 // txStateChanges stores the recent changes between pending/mined states of 135 // transactions. True means mined, false means rolled back, no entry means no change 136 type txStateChanges map[common.Hash]bool 137 138 // setState sets the status of a tx to either recently mined or recently rolled back 139 func (txc txStateChanges) setState(txHash common.Hash, mined bool) { 140 val, ent := txc[txHash] 141 if ent && (val != mined) { 142 delete(txc, txHash) 143 } else { 144 txc[txHash] = mined 145 } 146 } 147 148 // getLists creates lists of mined and rolled back tx hashes 149 func (txc txStateChanges) getLists() (mined []common.Hash, rollback []common.Hash) { 150 for hash, val := range txc { 151 if val { 152 mined = append(mined, hash) 153 } else { 154 rollback = append(rollback, hash) 155 } 156 } 157 return 158 } 159 160 // checkMinedTxs checks newly added blocks for the currently pending transactions 161 // and marks them as mined if necessary. It also stores block position in the db 162 // and adds them to the received txStateChanges map. 163 func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number uint64, txc txStateChanges) error { 164 // If no transactions are pending, we don't care about anything 165 if len(pool.pending) == 0 { 166 return nil 167 } 168 block, err := GetBlock(ctx, pool.odr, hash, number) 169 if err != nil { 170 return err 171 } 172 // Gather all the local transaction mined in this block 173 list := pool.mined[hash] 174 for _, tx := range block.Transactions() { 175 if _, ok := pool.pending[tx.Hash()]; ok { 176 list = append(list, tx) 177 } 178 } 179 // If some transactions have been mined, write the needed data to disk and update 180 if list != nil { 181 // Retrieve all the receipts belonging to this block and write the loopup table 182 if _, err := GetBlockReceipts(ctx, pool.odr, hash, number); err != nil { // ODR caches, ignore results 183 return err 184 } 185 rawdb.WriteTxLookupEntries(pool.chainDb, block) 186 187 // Update the transaction pool's state 188 for _, tx := range list { 189 delete(pool.pending, tx.Hash()) 190 txc.setState(tx.Hash(), true) 191 } 192 pool.mined[hash] = list 193 } 194 return nil 195 } 196 197 // rollbackTxs marks the transactions contained in recently rolled back blocks 198 // as rolled back. It also removes any positional lookup entries. 199 func (pool *TxPool) rollbackTxs(hash common.Hash, txc txStateChanges) { 200 batch := pool.chainDb.NewBatch() 201 if list, ok := pool.mined[hash]; ok { 202 for _, tx := range list { 203 txHash := tx.Hash() 204 rawdb.DeleteTxLookupEntry(batch, txHash) 205 pool.pending[txHash] = tx 206 txc.setState(txHash, false) 207 } 208 delete(pool.mined, hash) 209 } 210 batch.Write() 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.signer = types.MakeSigner(pool.config) 311 } 312 313 // Stop stops the light transaction pool 314 func (pool *TxPool) Stop() { 315 // Unsubscribe all subscriptions registered from txpool 316 pool.scope.Close() 317 // Unsubscribe subscriptions registered from blockchain 318 pool.chainHeadSub.Unsubscribe() 319 close(pool.quit) 320 log.Info("Transaction pool stopped") 321 } 322 323 // SubscribeNewTxsEvent registers a subscription of core.NewTxsEvent and 324 // starts sending event to the given channel. 325 func (pool *TxPool) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 326 return pool.scope.Track(pool.txFeed.Subscribe(ch)) 327 } 328 329 // Stats returns the number of currently pending (locally created) transactions 330 func (pool *TxPool) Stats() (pending int) { 331 pool.mu.RLock() 332 defer pool.mu.RUnlock() 333 334 pending = len(pool.pending) 335 return 336 } 337 338 // validateTx checks whether a transaction is valid according to the consensus rules. 339 func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error { 340 // Validate sender 341 var ( 342 from common.Address 343 err error 344 ) 345 346 // Validate the transaction sender and it's sig. Throw 347 // if the from fields is invalid. 348 if from, err = types.Sender(pool.signer, tx); err != nil { 349 return core.ErrInvalidSender 350 } 351 // Last but not least check for nonce errors 352 currentState := pool.currentState(ctx) 353 if n := currentState.GetNonce(from); n > tx.Nonce() { 354 return core.ErrNonceTooLow 355 } 356 357 // Check the transaction doesn't exceed the current 358 // block limit gas. 359 header := pool.chain.GetHeaderByHash(pool.head) 360 if header.GasLimit < tx.Gas() { 361 return core.ErrGasLimit 362 } 363 364 // Transactions can't be negative. This may never happen 365 // using RLP decoded transactions but may occur if you create 366 // a transaction using the RPC for example. 367 if tx.Value().Sign() < 0 { 368 return core.ErrNegativeValue 369 } 370 371 // Should supply enough intrinsic gas 372 gas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil) 373 if err != nil { 374 return err 375 } 376 if tx.Gas() < gas { 377 return core.ErrIntrinsicGas 378 } 379 return currentState.Error() 380 } 381 382 // add validates a new transaction and sets its state pending if processable. 383 // It also updates the locally stored nonce if necessary. 384 func (pool *TxPool) add(ctx context.Context, tx *types.Transaction) error { 385 hash := tx.Hash() 386 387 if pool.pending[hash] != nil { 388 return fmt.Errorf("Known transaction (%x)", hash[:4]) 389 } 390 err := pool.validateTx(ctx, tx) 391 if err != nil { 392 return err 393 } 394 395 if _, ok := pool.pending[hash]; !ok { 396 pool.pending[hash] = tx 397 398 nonce := tx.Nonce() + 1 399 400 addr, _ := types.Sender(pool.signer, tx) 401 if nonce > pool.nonce[addr] { 402 pool.nonce[addr] = nonce 403 } 404 405 // Notify the subscribers. This event is posted in a goroutine 406 // because it's possible that somewhere during the post "Remove transaction" 407 // gets called which will then wait for the global tx pool lock and deadlock. 408 go pool.txFeed.Send(core.NewTxsEvent{Txs: types.Transactions{tx}}) 409 } 410 411 // Print a log message if low enough level is set 412 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()) 413 return nil 414 } 415 416 // Add adds a transaction to the pool if valid and passes it to the tx relay 417 // backend 418 func (pool *TxPool) Add(ctx context.Context, tx *types.Transaction) error { 419 pool.mu.Lock() 420 defer pool.mu.Unlock() 421 422 data, err := rlp.EncodeToBytes(tx) 423 if err != nil { 424 return err 425 } 426 427 if err := pool.add(ctx, tx); err != nil { 428 return err 429 } 430 //fmt.Println("Send", tx.Hash()) 431 pool.relay.Send(types.Transactions{tx}) 432 433 pool.chainDb.Put(tx.Hash().Bytes(), data) 434 return nil 435 } 436 437 // AddTransactions adds all valid transactions to the pool and passes them to 438 // the tx relay backend 439 func (pool *TxPool) AddBatch(ctx context.Context, txs []*types.Transaction) { 440 pool.mu.Lock() 441 defer pool.mu.Unlock() 442 var sendTx types.Transactions 443 444 for _, tx := range txs { 445 if err := pool.add(ctx, tx); err == nil { 446 sendTx = append(sendTx, tx) 447 } 448 } 449 if len(sendTx) > 0 { 450 pool.relay.Send(sendTx) 451 } 452 } 453 454 // GetTransaction returns a transaction if it is contained in the pool 455 // and nil otherwise. 456 func (pool *TxPool) GetTransaction(hash common.Hash) *types.Transaction { 457 // check the txs first 458 if tx, ok := pool.pending[hash]; ok { 459 return tx 460 } 461 return nil 462 } 463 464 // GetTransactions returns all currently processable transactions. 465 // The returned slice may be modified by the caller. 466 func (pool *TxPool) GetTransactions() (txs types.Transactions, err error) { 467 pool.mu.RLock() 468 defer pool.mu.RUnlock() 469 470 txs = make(types.Transactions, len(pool.pending)) 471 i := 0 472 for _, tx := range pool.pending { 473 txs[i] = tx 474 i++ 475 } 476 return txs, nil 477 } 478 479 // Content retrieves the data content of the transaction pool, returning all the 480 // pending as well as queued transactions, grouped by account and nonce. 481 func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 482 pool.mu.RLock() 483 defer pool.mu.RUnlock() 484 485 // Retrieve all the pending transactions and sort by account and by nonce 486 pending := make(map[common.Address]types.Transactions) 487 for _, tx := range pool.pending { 488 account, _ := types.Sender(pool.signer, tx) 489 pending[account] = append(pending[account], tx) 490 } 491 // There are no queued transactions in a light pool, just return an empty map 492 queued := make(map[common.Address]types.Transactions) 493 return pending, queued 494 } 495 496 // RemoveTransactions removes all given transactions from the pool. 497 func (pool *TxPool) RemoveTransactions(txs types.Transactions) { 498 pool.mu.Lock() 499 defer pool.mu.Unlock() 500 501 var hashes []common.Hash 502 batch := pool.chainDb.NewBatch() 503 for _, tx := range txs { 504 hash := tx.Hash() 505 delete(pool.pending, hash) 506 batch.Delete(hash.Bytes()) 507 hashes = append(hashes, hash) 508 } 509 batch.Write() 510 pool.relay.Discard(hashes) 511 } 512 513 // RemoveTx removes the transaction with the given hash from the pool. 514 func (pool *TxPool) RemoveTx(hash common.Hash) { 515 pool.mu.Lock() 516 defer pool.mu.Unlock() 517 // delete from pending pool 518 delete(pool.pending, hash) 519 pool.chainDb.Delete(hash[:]) 520 pool.relay.Discard([]common.Hash{hash}) 521 }