github.com/core-coin/go-core/v2@v2.1.9/light/txpool.go (about) 1 // Copyright 2016 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core 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/core-coin/go-core/v2/xcbdb" 26 27 "github.com/core-coin/go-core/v2/common" 28 "github.com/core-coin/go-core/v2/core" 29 "github.com/core-coin/go-core/v2/core/rawdb" 30 "github.com/core-coin/go-core/v2/core/state" 31 "github.com/core-coin/go-core/v2/core/types" 32 "github.com/core-coin/go-core/v2/event" 33 "github.com/core-coin/go-core/v2/log" 34 "github.com/core-coin/go-core/v2/params" 35 "github.com/core-coin/go-core/v2/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 xcbdb.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 72 // TxRelayBackend provides an interface to the mechanism that forwards transacions 73 // to the XCB network. The implementations of the functions should be non-blocking. 74 // 75 // Send instructs backend to forward new transactions 76 // NewHead notifies backend about a new head after processed by the tx pool, 77 // 78 // including mined and rolled back transactions since the last event 79 // 80 // Discard notifies backend about transactions that should be discarded either 81 // 82 // because they have been replaced by a re-send or because they have been mined 83 // long ago and no rollback is expected 84 type TxRelayBackend interface { 85 Send(txs types.Transactions) 86 NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) 87 Discard(hashes []common.Hash) 88 } 89 90 // NewTxPool creates a new light transaction pool 91 func NewTxPool(config *params.ChainConfig, chain *LightChain, relay TxRelayBackend) *TxPool { 92 pool := &TxPool{ 93 config: config, 94 signer: types.NewNucleusSigner(config.NetworkID), 95 nonce: make(map[common.Address]uint64), 96 pending: make(map[common.Hash]*types.Transaction), 97 mined: make(map[common.Hash][]*types.Transaction), 98 quit: make(chan bool), 99 chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), 100 chain: chain, 101 relay: relay, 102 odr: chain.Odr(), 103 chainDb: chain.Odr().Database(), 104 head: chain.CurrentHeader().Hash(), 105 clearIdx: chain.CurrentHeader().Number.Uint64(), 106 } 107 // Subscribe events from blockchain 108 pool.chainHeadSub = pool.chain.SubscribeChainHeadEvent(pool.chainHeadCh) 109 go pool.eventLoop() 110 111 return pool 112 } 113 114 // currentState returns the light state of the current head header 115 func (pool *TxPool) currentState(ctx context.Context) *state.StateDB { 116 return NewState(ctx, pool.chain.CurrentHeader(), pool.odr) 117 } 118 119 // GetNonce returns the "pending" nonce of a given address. It always queries 120 // the nonce belonging to the latest header too in order to detect if another 121 // client using the same key sent a transaction. 122 func (pool *TxPool) GetNonce(ctx context.Context, addr common.Address) (uint64, error) { 123 state := pool.currentState(ctx) 124 nonce := state.GetNonce(addr) 125 if state.Error() != nil { 126 return 0, state.Error() 127 } 128 sn, ok := pool.nonce[addr] 129 if ok && sn > nonce { 130 nonce = sn 131 } 132 if !ok || sn < nonce { 133 pool.nonce[addr] = nonce 134 } 135 return nonce, nil 136 } 137 138 // txStateChanges stores the recent changes between pending/mined states of 139 // transactions. True means mined, false means rolled back, no entry means no change 140 type txStateChanges map[common.Hash]bool 141 142 // setState sets the status of a tx to either recently mined or recently rolled back 143 func (txc txStateChanges) setState(txHash common.Hash, mined bool) { 144 val, ent := txc[txHash] 145 if ent && (val != mined) { 146 delete(txc, txHash) 147 } else { 148 txc[txHash] = mined 149 } 150 } 151 152 // getLists creates lists of mined and rolled back tx hashes 153 func (txc txStateChanges) getLists() (mined []common.Hash, rollback []common.Hash) { 154 for hash, val := range txc { 155 if val { 156 mined = append(mined, hash) 157 } else { 158 rollback = append(rollback, hash) 159 } 160 } 161 return 162 } 163 164 // checkMinedTxs checks newly added blocks for the currently pending transactions 165 // and marks them as mined if necessary. It also stores block position in the db 166 // and adds them to the received txStateChanges map. 167 func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number uint64, txc txStateChanges) error { 168 // If no transactions are pending, we don't care about anything 169 if len(pool.pending) == 0 { 170 return nil 171 } 172 block, err := GetBlock(ctx, pool.odr, hash, number) 173 if err != nil { 174 return err 175 } 176 // Gather all the local transaction mined in this block 177 list := pool.mined[hash] 178 for _, tx := range block.Transactions() { 179 if _, ok := pool.pending[tx.Hash()]; ok { 180 list = append(list, tx) 181 } 182 } 183 // If some transactions have been mined, write the needed data to disk and update 184 if list != nil { 185 // Retrieve all the receipts belonging to this block and write the loopup table 186 if _, err := GetBlockReceipts(ctx, pool.odr, hash, number); err != nil { // ODR caches, ignore results 187 return err 188 } 189 rawdb.WriteTxLookupEntriesByBlock(pool.chainDb, block) 190 191 // Update the transaction pool's state 192 for _, tx := range list { 193 delete(pool.pending, tx.Hash()) 194 txc.setState(tx.Hash(), true) 195 } 196 pool.mined[hash] = list 197 } 198 return nil 199 } 200 201 // rollbackTxs marks the transactions contained in recently rolled back blocks 202 // as rolled back. It also removes any positional lookup entries. 203 func (pool *TxPool) rollbackTxs(hash common.Hash, txc txStateChanges) { 204 batch := pool.chainDb.NewBatch() 205 if list, ok := pool.mined[hash]; ok { 206 for _, tx := range list { 207 txHash := tx.Hash() 208 rawdb.DeleteTxLookupEntry(batch, txHash) 209 pool.pending[txHash] = tx 210 txc.setState(txHash, false) 211 } 212 delete(pool.mined, hash) 213 } 214 batch.Write() 215 } 216 217 // reorgOnNewHead sets a new head header, processing (and rolling back if necessary) 218 // the blocks since the last known head and returns a txStateChanges map containing 219 // the recently mined and rolled back transaction hashes. If an error (context 220 // timeout) occurs during checking new blocks, it leaves the locally known head 221 // at the latest checked block and still returns a valid txStateChanges, making it 222 // possible to continue checking the missing blocks at the next chain head event 223 func (pool *TxPool) reorgOnNewHead(ctx context.Context, newHeader *types.Header) (txStateChanges, error) { 224 txc := make(txStateChanges) 225 oldh := pool.chain.GetHeaderByHash(pool.head) 226 newh := newHeader 227 // find common ancestor, create list of rolled back and new block hashes 228 var oldHashes, newHashes []common.Hash 229 for oldh.Hash() != newh.Hash() { 230 if oldh.Number.Uint64() >= newh.Number.Uint64() { 231 oldHashes = append(oldHashes, oldh.Hash()) 232 oldh = pool.chain.GetHeader(oldh.ParentHash, oldh.Number.Uint64()-1) 233 } 234 if oldh.Number.Uint64() < newh.Number.Uint64() { 235 newHashes = append(newHashes, newh.Hash()) 236 newh = pool.chain.GetHeader(newh.ParentHash, newh.Number.Uint64()-1) 237 if newh == nil { 238 // happens when CHT syncing, nothing to do 239 newh = oldh 240 } 241 } 242 } 243 if oldh.Number.Uint64() < pool.clearIdx { 244 pool.clearIdx = oldh.Number.Uint64() 245 } 246 // roll back old blocks 247 for _, hash := range oldHashes { 248 pool.rollbackTxs(hash, txc) 249 } 250 pool.head = oldh.Hash() 251 // check mined txs of new blocks (array is in reversed order) 252 for i := len(newHashes) - 1; i >= 0; i-- { 253 hash := newHashes[i] 254 if err := pool.checkMinedTxs(ctx, hash, newHeader.Number.Uint64()-uint64(i), txc); err != nil { 255 return txc, err 256 } 257 pool.head = hash 258 } 259 260 // clear old mined tx entries of old blocks 261 if idx := newHeader.Number.Uint64(); idx > pool.clearIdx+txPermanent { 262 idx2 := idx - txPermanent 263 if len(pool.mined) > 0 { 264 for i := pool.clearIdx; i < idx2; i++ { 265 hash := rawdb.ReadCanonicalHash(pool.chainDb, i) 266 if list, ok := pool.mined[hash]; ok { 267 hashes := make([]common.Hash, len(list)) 268 for i, tx := range list { 269 hashes[i] = tx.Hash() 270 } 271 pool.relay.Discard(hashes) 272 delete(pool.mined, hash) 273 } 274 } 275 } 276 pool.clearIdx = idx2 277 } 278 279 return txc, nil 280 } 281 282 // blockCheckTimeout is the time limit for checking new blocks for mined 283 // transactions. Checking resumes at the next chain head event if timed out. 284 const blockCheckTimeout = time.Second * 3 285 286 // eventLoop processes chain head events and also notifies the tx relay backend 287 // about the new head hash and tx state changes 288 func (pool *TxPool) eventLoop() { 289 for { 290 select { 291 case ev := <-pool.chainHeadCh: 292 pool.setNewHead(ev.Block.Header()) 293 // hack in order to avoid hogging the lock; this part will 294 // be replaced by a subsequent PR. 295 time.Sleep(time.Millisecond) 296 297 // System stopped 298 case <-pool.chainHeadSub.Err(): 299 return 300 } 301 } 302 } 303 304 func (pool *TxPool) setNewHead(head *types.Header) { 305 pool.mu.Lock() 306 defer pool.mu.Unlock() 307 308 ctx, cancel := context.WithTimeout(context.Background(), blockCheckTimeout) 309 defer cancel() 310 311 txc, _ := pool.reorgOnNewHead(ctx, head) 312 m, r := txc.getLists() 313 pool.relay.NewHead(pool.head, m, r) 314 } 315 316 // Stop stops the light transaction pool 317 func (pool *TxPool) Stop() { 318 // Unsubscribe all subscriptions registered from txpool 319 pool.scope.Close() 320 // Unsubscribe subscriptions registered from blockchain 321 pool.chainHeadSub.Unsubscribe() 322 close(pool.quit) 323 log.Info("Transaction pool stopped") 324 } 325 326 // SubscribeNewTxsEvent registers a subscription of core.NewTxsEvent and 327 // starts sending event to the given channel. 328 func (pool *TxPool) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 329 return pool.scope.Track(pool.txFeed.Subscribe(ch)) 330 } 331 332 // Stats returns the number of currently pending (locally created) transactions 333 func (pool *TxPool) Stats() (pending int) { 334 pool.mu.RLock() 335 defer pool.mu.RUnlock() 336 337 pending = len(pool.pending) 338 return 339 } 340 341 // validateTx checks whether a transaction is valid according to the consensus rules. 342 func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error { 343 // Validate sender 344 var ( 345 from common.Address 346 err error 347 ) 348 349 // Validate the transaction recipient and it's sig. Throw 350 // if the from fields is invalid. 351 if from, err = types.Sender(pool.signer, tx); err != nil { 352 return core.ErrInvalidRecipientOrSig 353 } 354 // Last but not least check for nonce errors 355 currentState := pool.currentState(ctx) 356 if n := currentState.GetNonce(from); n > tx.Nonce() { 357 return core.ErrNonceTooLow 358 } 359 360 // Check the transaction doesn't exceed the current 361 // block limit energy. 362 header := pool.chain.GetHeaderByHash(pool.head) 363 if header.EnergyLimit < tx.Energy() { 364 return core.ErrEnergyLimit 365 } 366 367 // Transactions can't be negative. This may never happen 368 // using RLP decoded transactions but may occur if you create 369 // a transaction using the RPC for example. 370 if tx.Value().Sign() < 0 { 371 return core.ErrNegativeValue 372 } 373 374 // Transactor should have enough funds to cover the costs 375 // cost == V + GP * GL 376 if b := currentState.GetBalance(from); b.Cmp(tx.Cost()) < 0 { 377 return core.ErrInsufficientFunds 378 } 379 380 // Should supply enough intrinsic energy 381 energy, err := core.IntrinsicEnergy(tx.Data(), tx.To() == nil) 382 if err != nil { 383 return err 384 } 385 if tx.Energy() < energy { 386 return core.ErrIntrinsicEnergy 387 } 388 return currentState.Error() 389 } 390 391 // add validates a new transaction and sets its state pending if processable. 392 // It also updates the locally stored nonce if necessary. 393 func (pool *TxPool) add(ctx context.Context, tx *types.Transaction) error { 394 hash := tx.Hash() 395 396 if pool.pending[hash] != nil { 397 return fmt.Errorf("Known transaction (%x)", hash[:4]) 398 } 399 err := pool.validateTx(ctx, tx) 400 if err != nil { 401 return err 402 } 403 404 if _, ok := pool.pending[hash]; !ok { 405 pool.pending[hash] = tx 406 407 nonce := tx.Nonce() + 1 408 409 addr, err := types.Sender(pool.signer, tx) 410 if err != nil { 411 return err 412 } 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 }