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
17package light
18
19import (
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)
36
37const (
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
44var 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.
51type 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	istanbul bool // Fork indicator whether we are in the istanbul stage.
71	eip2718  bool // Fork indicator whether we are in the eip2718 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
83type 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
90func NewTxPool(config *params.ChainConfig, chain *LightChain, relay TxRelayBackend) *TxPool {
91	pool := &TxPool{
92		config:      config,
93		signer:      types.LatestSigner(config),
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
114func (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.
121func (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
139type txStateChanges map[common.Hash]bool
140
141// setState sets the status of a tx to either recently mined or recently rolled back
142func (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
152func (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.
166func (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.WriteTxLookupEntriesByBlock(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.
202func (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
222func (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.
283const 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
287func (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
303func (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	pool.eip2718 = pool.config.IsBerlin(next)
318}
319
320// Stop stops the light transaction pool
321func (pool *TxPool) Stop() {
322	// Unsubscribe all subscriptions registered from txpool
323	pool.scope.Close()
324	// Unsubscribe subscriptions registered from blockchain
325	pool.chainHeadSub.Unsubscribe()
326	close(pool.quit)
327	log.Info("Transaction pool stopped")
328}
329
330// SubscribeNewTxsEvent registers a subscription of core.NewTxsEvent and
331// starts sending event to the given channel.
332func (pool *TxPool) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
333	return pool.scope.Track(pool.txFeed.Subscribe(ch))
334}
335
336// Stats returns the number of currently pending (locally created) transactions
337func (pool *TxPool) Stats() (pending int) {
338	pool.mu.RLock()
339	defer pool.mu.RUnlock()
340
341	pending = len(pool.pending)
342	return
343}
344
345// validateTx checks whether a transaction is valid according to the consensus rules.
346func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error {
347	// Validate sender
348	var (
349		from common.Address
350		err  error
351	)
352
353	// Validate the transaction sender and it's sig. Throw
354	// if the from fields is invalid.
355	if from, err = types.Sender(pool.signer, tx); err != nil {
356		return core.ErrInvalidSender
357	}
358	// Last but not least check for nonce errors
359	currentState := pool.currentState(ctx)
360	if n := currentState.GetNonce(from); n > tx.Nonce() {
361		return core.ErrNonceTooLow
362	}
363
364	// Check the transaction doesn't exceed the current
365	// block limit gas.
366	header := pool.chain.GetHeaderByHash(pool.head)
367	if header.GasLimit < tx.Gas() {
368		return core.ErrGasLimit
369	}
370
371	// Transactions can't be negative. This may never happen
372	// using RLP decoded transactions but may occur if you create
373	// a transaction using the RPC for example.
374	if tx.Value().Sign() < 0 {
375		return core.ErrNegativeValue
376	}
377
378	// Transactor should have enough funds to cover the costs
379	// cost == V + GP * GL
380	if b := currentState.GetBalance(from); b.Cmp(tx.Cost()) < 0 {
381		return core.ErrInsufficientFunds
382	}
383
384	// Should supply enough intrinsic gas
385	gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, true, pool.istanbul)
386	if err != nil {
387		return err
388	}
389	if tx.Gas() < gas {
390		return core.ErrIntrinsicGas
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.
397func (pool *TxPool) add(ctx context.Context, tx *types.Transaction) error {
398	hash := tx.Hash()
399
400	if pool.pending[hash] != nil {
401		return fmt.Errorf("Known transaction (%x)", hash[:4])
402	}
403	err := pool.validateTx(ctx, tx)
404	if err != nil {
405		return err
406	}
407
408	if _, ok := pool.pending[hash]; !ok {
409		pool.pending[hash] = tx
410
411		nonce := tx.Nonce() + 1
412
413		addr, _ := types.Sender(pool.signer, tx)
414		if nonce > pool.nonce[addr] {
415			pool.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 pool.txFeed.Send(core.NewTxsEvent{Txs: types.Transactions{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(pool.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
431func (pool *TxPool) Add(ctx context.Context, tx *types.Transaction) error {
432	pool.mu.Lock()
433	defer pool.mu.Unlock()
434	data, err := tx.MarshalBinary()
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
451func (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.
468func (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.
478func (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.
493func (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// ContentFrom retrieves the data content of the transaction pool, returning the
509// pending as well as queued transactions of this address, grouped by nonce.
510func (pool *TxPool) ContentFrom(addr common.Address) (types.Transactions, types.Transactions) {
511	pool.mu.RLock()
512	defer pool.mu.RUnlock()
513
514	// Retrieve the pending transactions and sort by nonce
515	var pending types.Transactions
516	for _, tx := range pool.pending {
517		account, _ := types.Sender(pool.signer, tx)
518		if account != addr {
519			continue
520		}
521		pending = append(pending, tx)
522	}
523	// There are no queued transactions in a light pool, just return an empty map
524	return pending, types.Transactions{}
525}
526
527// RemoveTransactions removes all given transactions from the pool.
528func (pool *TxPool) RemoveTransactions(txs types.Transactions) {
529	pool.mu.Lock()
530	defer pool.mu.Unlock()
531
532	var hashes []common.Hash
533	batch := pool.chainDb.NewBatch()
534	for _, tx := range txs {
535		hash := tx.Hash()
536		delete(pool.pending, hash)
537		batch.Delete(hash.Bytes())
538		hashes = append(hashes, hash)
539	}
540	batch.Write()
541	pool.relay.Discard(hashes)
542}
543
544// RemoveTx removes the transaction with the given hash from the pool.
545func (pool *TxPool) RemoveTx(hash common.Hash) {
546	pool.mu.Lock()
547	defer pool.mu.Unlock()
548	// delete from pending pool
549	delete(pool.pending, hash)
550	pool.chainDb.Delete(hash[:])
551	pool.relay.Discard([]common.Hash{hash})
552}
553