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