github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/eth/handler_test.go (about)

     1  // Copyright 2015 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 eth
    18  
    19  import (
    20  	"math/big"
    21  	"sort"
    22  	"sync"
    23  
    24  	"github.com/ethw3/go-ethereuma/common"
    25  	"github.com/ethw3/go-ethereuma/consensus"
    26  	"github.com/ethw3/go-ethereuma/consensus/ethash"
    27  	"github.com/ethw3/go-ethereuma/core"
    28  	"github.com/ethw3/go-ethereuma/core/rawdb"
    29  	"github.com/ethw3/go-ethereuma/core/types"
    30  	"github.com/ethw3/go-ethereuma/core/vm"
    31  	"github.com/ethw3/go-ethereuma/crypto"
    32  	"github.com/ethw3/go-ethereuma/eth/downloader"
    33  	"github.com/ethw3/go-ethereuma/ethdb"
    34  	"github.com/ethw3/go-ethereuma/event"
    35  	"github.com/ethw3/go-ethereuma/params"
    36  )
    37  
    38  var (
    39  	// testKey is a private key to use for funding a tester account.
    40  	testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    41  
    42  	// testAddr is the Ethereum address of the tester account.
    43  	testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
    44  )
    45  
    46  // testTxPool is a mock transaction pool that blindly accepts all transactions.
    47  // Its goal is to get around setting up a valid statedb for the balance and nonce
    48  // checks.
    49  type testTxPool struct {
    50  	pool map[common.Hash]*types.Transaction // Hash map of collected transactions
    51  
    52  	txFeed event.Feed   // Notification feed to allow waiting for inclusion
    53  	lock   sync.RWMutex // Protects the transaction pool
    54  }
    55  
    56  // newTestTxPool creates a mock transaction pool.
    57  func newTestTxPool() *testTxPool {
    58  	return &testTxPool{
    59  		pool: make(map[common.Hash]*types.Transaction),
    60  	}
    61  }
    62  
    63  // Has returns an indicator whether txpool has a transaction
    64  // cached with the given hash.
    65  func (p *testTxPool) Has(hash common.Hash) bool {
    66  	p.lock.Lock()
    67  	defer p.lock.Unlock()
    68  
    69  	return p.pool[hash] != nil
    70  }
    71  
    72  // Get retrieves the transaction from local txpool with given
    73  // tx hash.
    74  func (p *testTxPool) Get(hash common.Hash) *types.Transaction {
    75  	p.lock.Lock()
    76  	defer p.lock.Unlock()
    77  
    78  	return p.pool[hash]
    79  }
    80  
    81  // AddRemotes appends a batch of transactions to the pool, and notifies any
    82  // listeners if the addition channel is non nil
    83  func (p *testTxPool) AddRemotes(txs []*types.Transaction) []error {
    84  	p.lock.Lock()
    85  	defer p.lock.Unlock()
    86  
    87  	for _, tx := range txs {
    88  		p.pool[tx.Hash()] = tx
    89  	}
    90  	p.txFeed.Send(core.NewTxsEvent{Txs: txs})
    91  	return make([]error, len(txs))
    92  }
    93  
    94  // Pending returns all the transactions known to the pool
    95  func (p *testTxPool) Pending(enforceTips bool) map[common.Address]types.Transactions {
    96  	p.lock.RLock()
    97  	defer p.lock.RUnlock()
    98  
    99  	batches := make(map[common.Address]types.Transactions)
   100  	for _, tx := range p.pool {
   101  		from, _ := types.Sender(types.HomesteadSigner{}, tx)
   102  		batches[from] = append(batches[from], tx)
   103  	}
   104  	for _, batch := range batches {
   105  		sort.Sort(types.TxByNonce(batch))
   106  	}
   107  	return batches
   108  }
   109  
   110  // SubscribeNewTxsEvent should return an event subscription of NewTxsEvent and
   111  // send events to the given channel.
   112  func (p *testTxPool) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
   113  	return p.txFeed.Subscribe(ch)
   114  }
   115  
   116  // testHandler is a live implementation of the Ethereum protocol handler, just
   117  // preinitialized with some sane testing defaults and the transaction pool mocked
   118  // out.
   119  type testHandler struct {
   120  	db      ethdb.Database
   121  	chain   *core.BlockChain
   122  	txpool  *testTxPool
   123  	handler *handler
   124  }
   125  
   126  // newTestHandler creates a new handler for testing purposes with no blocks.
   127  func newTestHandler() *testHandler {
   128  	return newTestHandlerWithBlocks(0)
   129  }
   130  
   131  // newTestHandlerWithBlocks creates a new handler for testing purposes, with a
   132  // given number of initial blocks.
   133  func newTestHandlerWithBlocks(blocks int) *testHandler {
   134  	// Create a database pre-initialize with a genesis block
   135  	db := rawdb.NewMemoryDatabase()
   136  	(&core.Genesis{
   137  		Config: params.TestChainConfig,
   138  		Alloc:  core.GenesisAlloc{testAddr: {Balance: big.NewInt(1000000)}},
   139  	}).MustCommit(db)
   140  
   141  	chain, _ := core.NewBlockChain(db, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil)
   142  
   143  	bs, _ := core.GenerateChain(params.TestChainConfig, chain.Genesis(), ethash.NewFaker(), db, blocks, nil)
   144  	if _, err := chain.InsertChain(bs); err != nil {
   145  		panic(err)
   146  	}
   147  	txpool := newTestTxPool()
   148  
   149  	handler, _ := newHandler(&handlerConfig{
   150  		Database:   db,
   151  		Chain:      chain,
   152  		TxPool:     txpool,
   153  		Merger:     consensus.NewMerger(rawdb.NewMemoryDatabase()),
   154  		Network:    1,
   155  		Sync:       downloader.SnapSync,
   156  		BloomCache: 1,
   157  	})
   158  	handler.Start(1000)
   159  
   160  	return &testHandler{
   161  		db:      db,
   162  		chain:   chain,
   163  		txpool:  txpool,
   164  		handler: handler,
   165  	}
   166  }
   167  
   168  // close tears down the handler and all its internal constructs.
   169  func (b *testHandler) close() {
   170  	b.handler.Stop()
   171  	b.chain.Stop()
   172  }