github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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/vntchain/go-vnt/common" 26 "github.com/vntchain/go-vnt/core" 27 "github.com/vntchain/go-vnt/core/rawdb" 28 "github.com/vntchain/go-vnt/core/state" 29 "github.com/vntchain/go-vnt/core/types" 30 "github.com/vntchain/go-vnt/event" 31 "github.com/vntchain/go-vnt/log" 32 "github.com/vntchain/go-vnt/params" 33 "github.com/vntchain/go-vnt/rlp" 34 "github.com/vntchain/go-vnt/vntdb" 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 produced blocks after a produced 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 (produced) 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 vntdb.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 produced map[common.Hash][]*types.Transaction // produced transactions by block hash 68 clearIdx uint64 // earliest block nr that can contain produced tx info 69 } 70 71 // TxRelayBackend provides an interface to the mechanism that forwards transacions 72 // to the VNT 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 produced 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 produced 79 // long ago and no rollback is expected 80 type TxRelayBackend interface { 81 Send(txs types.Transactions) 82 NewHead(head common.Hash, produced []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.NewHubbleSigner(config.ChainID), 91 nonce: make(map[common.Address]uint64), 92 pending: make(map[common.Hash]*types.Transaction), 93 produced: 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/produced states of 135 // transactions. True means produced, 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 produced or recently rolled back 139 func (txc txStateChanges) setState(txHash common.Hash, produced bool) { 140 val, ent := txc[txHash] 141 if ent && (val != produced) { 142 delete(txc, txHash) 143 } else { 144 txc[txHash] = produced 145 } 146 } 147 148 // getLists creates lists of produced and rolled back tx hashes 149 func (txc txStateChanges) getLists() (produced []common.Hash, rollback []common.Hash) { 150 for hash, val := range txc { 151 if val { 152 produced = append(produced, hash) 153 } else { 154 rollback = append(rollback, hash) 155 } 156 } 157 return 158 } 159 160 // checkProducedTxs checks newly added blocks for the currently pending transactions 161 // and marks them as produced if necessary. It also stores block position in the db 162 // and adds them to the received txStateChanges map. 163 func (pool *TxPool) checkProducedTxs(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 produced in this block 173 list := pool.produced[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 produced, 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.produced[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 if list, ok := pool.produced[hash]; ok { 201 for _, tx := range list { 202 txHash := tx.Hash() 203 rawdb.DeleteTxLookupEntry(pool.chainDb, txHash) 204 pool.pending[txHash] = tx 205 txc.setState(txHash, false) 206 } 207 delete(pool.produced, hash) 208 } 209 } 210 211 // reorgOnNewHead sets a new head header, processing (and rolling back if necessary) 212 // the blocks since the last known head and returns a txStateChanges map containing 213 // the recently produced and rolled back transaction hashes. If an error (context 214 // timeout) occurs during checking new blocks, it leaves the locally known head 215 // at the latest checked block and still returns a valid txStateChanges, making it 216 // possible to continue checking the missing blocks at the next chain head event 217 func (pool *TxPool) reorgOnNewHead(ctx context.Context, newHeader *types.Header) (txStateChanges, error) { 218 txc := make(txStateChanges) 219 oldh := pool.chain.GetHeaderByHash(pool.head) 220 newh := newHeader 221 // find common ancestor, create list of rolled back and new block hashes 222 var oldHashes, newHashes []common.Hash 223 for oldh.Hash() != newh.Hash() { 224 if oldh.Number.Uint64() >= newh.Number.Uint64() { 225 oldHashes = append(oldHashes, oldh.Hash()) 226 oldh = pool.chain.GetHeader(oldh.ParentHash, oldh.Number.Uint64()-1) 227 } 228 if oldh.Number.Uint64() < newh.Number.Uint64() { 229 newHashes = append(newHashes, newh.Hash()) 230 newh = pool.chain.GetHeader(newh.ParentHash, newh.Number.Uint64()-1) 231 if newh == nil { 232 // happens when CHT syncing, nothing to do 233 newh = oldh 234 } 235 } 236 } 237 if oldh.Number.Uint64() < pool.clearIdx { 238 pool.clearIdx = oldh.Number.Uint64() 239 } 240 // roll back old blocks 241 for _, hash := range oldHashes { 242 pool.rollbackTxs(hash, txc) 243 } 244 pool.head = oldh.Hash() 245 // check produced txs of new blocks (array is in reversed order) 246 for i := len(newHashes) - 1; i >= 0; i-- { 247 hash := newHashes[i] 248 if err := pool.checkProducedTxs(ctx, hash, newHeader.Number.Uint64()-uint64(i), txc); err != nil { 249 return txc, err 250 } 251 pool.head = hash 252 } 253 254 // clear old produced tx entries of old blocks 255 if idx := newHeader.Number.Uint64(); idx > pool.clearIdx+txPermanent { 256 idx2 := idx - txPermanent 257 if len(pool.produced) > 0 { 258 for i := pool.clearIdx; i < idx2; i++ { 259 hash := rawdb.ReadCanonicalHash(pool.chainDb, i) 260 if list, ok := pool.produced[hash]; ok { 261 hashes := make([]common.Hash, len(list)) 262 for i, tx := range list { 263 hashes[i] = tx.Hash() 264 } 265 pool.relay.Discard(hashes) 266 delete(pool.produced, hash) 267 } 268 } 269 } 270 pool.clearIdx = idx2 271 } 272 273 return txc, nil 274 } 275 276 // blockCheckTimeout is the time limit for checking new blocks for produced 277 // transactions. Checking resumes at the next chain head event if timed out. 278 const blockCheckTimeout = time.Second * 3 279 280 // eventLoop processes chain head events and also notifies the tx relay backend 281 // about the new head hash and tx state changes 282 func (pool *TxPool) eventLoop() { 283 for { 284 select { 285 case ev := <-pool.chainHeadCh: 286 pool.setNewHead(ev.Block.Header()) 287 // hack in order to avoid hogging the lock; this part will 288 // be replaced by a subsequent PR. 289 time.Sleep(time.Millisecond) 290 291 // System stopped 292 case <-pool.chainHeadSub.Err(): 293 return 294 } 295 } 296 } 297 298 func (pool *TxPool) setNewHead(head *types.Header) { 299 pool.mu.Lock() 300 defer pool.mu.Unlock() 301 302 ctx, cancel := context.WithTimeout(context.Background(), blockCheckTimeout) 303 defer cancel() 304 305 txc, _ := pool.reorgOnNewHead(ctx, head) 306 m, r := txc.getLists() 307 pool.relay.NewHead(pool.head, m, r) 308 pool.signer = types.MakeSigner(pool.config, head.Number) 309 } 310 311 // Stop stops the light transaction pool 312 func (pool *TxPool) Stop() { 313 // Unsubscribe all subscriptions registered from txpool 314 pool.scope.Close() 315 // Unsubscribe subscriptions registered from blockchain 316 pool.chainHeadSub.Unsubscribe() 317 close(pool.quit) 318 log.Info("Transaction pool stopped") 319 } 320 321 // SubscribeNewTxsEvent registers a subscription of core.NewTxsEvent and 322 // starts sending event to the given channel. 323 func (pool *TxPool) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { 324 return pool.scope.Track(pool.txFeed.Subscribe(ch)) 325 } 326 327 // Stats returns the number of currently pending (locally created) transactions 328 func (pool *TxPool) Stats() (pending int) { 329 pool.mu.RLock() 330 defer pool.mu.RUnlock() 331 332 pending = len(pool.pending) 333 return 334 } 335 336 // validateTx checks whether a transaction is valid according to the consensus rules. 337 func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error { 338 // Validate sender 339 var ( 340 from common.Address 341 err error 342 ) 343 344 // Validate the transaction sender and it's sig. Throw 345 // if the from fields is invalid. 346 if from, err = types.Sender(pool.signer, tx); err != nil { 347 return core.ErrInvalidSender 348 } 349 // Last but not least check for nonce errors 350 currentState := pool.currentState(ctx) 351 if n := currentState.GetNonce(from); n > tx.Nonce() { 352 return core.ErrNonceTooLow 353 } 354 355 // Check the transaction doesn't exceed the current 356 // block limit gas. 357 header := pool.chain.GetHeaderByHash(pool.head) 358 if header.GasLimit < tx.Gas() { 359 return core.ErrGasLimit 360 } 361 362 // Transactions can't be negative. This may never happen 363 // using RLP decoded transactions but may occur if you create 364 // a transaction using the RPC for example. 365 if tx.Value().Sign() < 0 { 366 return core.ErrNegativeValue 367 } 368 369 // Transactor should have enough funds to cover the costs 370 // cost == V + GP * GL 371 if b := currentState.GetBalance(from); b.Cmp(tx.Cost()) < 0 { 372 return core.ErrInsufficientFunds 373 } 374 375 // Should supply enough intrinsic gas 376 gas, err := core.IntrinsicGas(tx.Data(), tx.To() == nil) 377 if err != nil { 378 return err 379 } 380 if tx.Gas() < gas { 381 return core.ErrIntrinsicGas 382 } 383 return currentState.Error() 384 } 385 386 // add validates a new transaction and sets its state pending if processable. 387 // It also updates the locally stored nonce if necessary. 388 func (self *TxPool) add(ctx context.Context, tx *types.Transaction) error { 389 hash := tx.Hash() 390 391 if self.pending[hash] != nil { 392 return fmt.Errorf("Known transaction (%x)", hash[:4]) 393 } 394 err := self.validateTx(ctx, tx) 395 if err != nil { 396 return err 397 } 398 399 if _, ok := self.pending[hash]; !ok { 400 self.pending[hash] = tx 401 402 nonce := tx.Nonce() + 1 403 404 addr, _ := types.Sender(self.signer, tx) 405 if nonce > self.nonce[addr] { 406 self.nonce[addr] = nonce 407 } 408 409 // Notify the subscribers. This event is posted in a goroutine 410 // because it's possible that somewhere during the post "Remove transaction" 411 // gets called which will then wait for the global tx pool lock and deadlock. 412 go self.txFeed.Send(core.NewTxsEvent{Txs: types.Transactions{tx}}) 413 } 414 415 // Print a log message if low enough level is set 416 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()) 417 return nil 418 } 419 420 // Add adds a transaction to the pool if valid and passes it to the tx relay 421 // backend 422 func (self *TxPool) Add(ctx context.Context, tx *types.Transaction) error { 423 self.mu.Lock() 424 defer self.mu.Unlock() 425 426 data, err := rlp.EncodeToBytes(tx) 427 if err != nil { 428 return err 429 } 430 431 if err := self.add(ctx, tx); err != nil { 432 return err 433 } 434 //fmt.Println("Send", tx.Hash()) 435 self.relay.Send(types.Transactions{tx}) 436 437 self.chainDb.Put(tx.Hash().Bytes(), data) 438 return nil 439 } 440 441 // AddTransactions adds all valid transactions to the pool and passes them to 442 // the tx relay backend 443 func (self *TxPool) AddBatch(ctx context.Context, txs []*types.Transaction) { 444 self.mu.Lock() 445 defer self.mu.Unlock() 446 var sendTx types.Transactions 447 448 for _, tx := range txs { 449 if err := self.add(ctx, tx); err == nil { 450 sendTx = append(sendTx, tx) 451 } 452 } 453 if len(sendTx) > 0 { 454 self.relay.Send(sendTx) 455 } 456 } 457 458 // GetTransaction returns a transaction if it is contained in the pool 459 // and nil otherwise. 460 func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction { 461 // check the txs first 462 if tx, ok := tp.pending[hash]; ok { 463 return tx 464 } 465 return nil 466 } 467 468 // GetTransactions returns all currently processable transactions. 469 // The returned slice may be modified by the caller. 470 func (self *TxPool) GetTransactions() (txs types.Transactions, err error) { 471 self.mu.RLock() 472 defer self.mu.RUnlock() 473 474 txs = make(types.Transactions, len(self.pending)) 475 i := 0 476 for _, tx := range self.pending { 477 txs[i] = tx 478 i++ 479 } 480 return txs, nil 481 } 482 483 // Content retrieves the data content of the transaction pool, returning all the 484 // pending as well as queued transactions, grouped by account and nonce. 485 func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) { 486 self.mu.RLock() 487 defer self.mu.RUnlock() 488 489 // Retrieve all the pending transactions and sort by account and by nonce 490 pending := make(map[common.Address]types.Transactions) 491 for _, tx := range self.pending { 492 account, _ := types.Sender(self.signer, tx) 493 pending[account] = append(pending[account], tx) 494 } 495 // There are no queued transactions in a light pool, just return an empty map 496 queued := make(map[common.Address]types.Transactions) 497 return pending, queued 498 } 499 500 // RemoveTransactions removes all given transactions from the pool. 501 func (self *TxPool) RemoveTransactions(txs types.Transactions) { 502 self.mu.Lock() 503 defer self.mu.Unlock() 504 var hashes []common.Hash 505 for _, tx := range txs { 506 //self.RemoveTx(tx.Hash()) 507 hash := tx.Hash() 508 delete(self.pending, hash) 509 self.chainDb.Delete(hash[:]) 510 hashes = append(hashes, hash) 511 } 512 self.relay.Discard(hashes) 513 } 514 515 // RemoveTx removes the transaction with the given hash from the pool. 516 func (pool *TxPool) RemoveTx(hash common.Hash) { 517 pool.mu.Lock() 518 defer pool.mu.Unlock() 519 // delete from pending pool 520 delete(pool.pending, hash) 521 pool.chainDb.Delete(hash[:]) 522 pool.relay.Discard([]common.Hash{hash}) 523 }