// Copyright 2015 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // The go-ethereum library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . package eth import ( "bytes" "context" "math/big" "sort" "sync" "github.com/ledgerwatch/turbo-geth/common" "github.com/ledgerwatch/turbo-geth/consensus" "github.com/ledgerwatch/turbo-geth/consensus/ethash" "github.com/ledgerwatch/turbo-geth/core" "github.com/ledgerwatch/turbo-geth/core/types" "github.com/ledgerwatch/turbo-geth/core/vm" "github.com/ledgerwatch/turbo-geth/crypto" "github.com/ledgerwatch/turbo-geth/eth/stagedsync" "github.com/ledgerwatch/turbo-geth/ethdb" "github.com/ledgerwatch/turbo-geth/event" "github.com/ledgerwatch/turbo-geth/params" "github.com/ledgerwatch/turbo-geth/rlp" ) var ( // testKey is a private key to use for funding a tester account. testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") // testAddr is the Ethereum address of the tester account. testAddr = crypto.PubkeyToAddress(testKey.PublicKey) ) // testTxPool is a mock transaction pool that blindly accepts all transactions. // Its goal is to get around setting up a valid statedb for the balance and nonce // checks. type testTxPool struct { pool map[common.Hash]*types.Transaction // Hash map of collected transactions txFeed event.Feed // Notification feed to allow waiting for inclusion lock sync.RWMutex // Protects the transaction pool } // newTestTxPool creates a mock transaction pool. func newTestTxPool() *testTxPool { return &testTxPool{ pool: make(map[common.Hash]*types.Transaction), } } // Has returns an indicator whether txpool has a transaction // cached with the given hash. func (p *testTxPool) Has(hash common.Hash) bool { p.lock.Lock() defer p.lock.Unlock() return p.pool[hash] != nil } // Get retrieves the transaction from local txpool with given // tx hash. func (p *testTxPool) Get(hash common.Hash) *types.Transaction { p.lock.Lock() defer p.lock.Unlock() return p.pool[hash] } func (p *testTxPool) GetSerializedTransactions(ctx context.Context, hashes common.Hashes) ([]rlp.RawValue, error) { reply := []rlp.RawValue{} var size uint64 buf := bytes.NewBuffer(nil) for i := range hashes { txn := p.Get(hashes[i]) if txn == nil { reply = append(reply, nil) continue } buf.Reset() if err := txn.EncodeRLP(buf); err != nil { return nil, err } reply = append(reply, common.CopyBytes(buf.Bytes())) size += uint64(buf.Len()) } return reply, nil } // AddRemotes appends a batch of transactions to the pool, and notifies any // listeners if the addition channel is non nil func (p *testTxPool) AddRemotes(txs []*types.Transaction) []error { p.lock.Lock() defer p.lock.Unlock() for _, tx := range txs { p.pool[tx.Hash()] = tx } p.txFeed.Send(core.NewTxsEvent{Txs: txs}) return make([]error, len(txs)) } // Pending returns all the transactions known to the pool func (p *testTxPool) Pending() (types.TransactionsGroupedBySender, error) { p.lock.RLock() defer p.lock.RUnlock() batches := make(map[common.Address]types.Transactions) for _, tx := range p.pool { from, _ := types.Sender(types.HomesteadSigner{}, tx) batches[from] = append(batches[from], tx) } groups := types.TransactionsGroupedBySender{} for _, batch := range batches { sort.Sort(types.TxByNonce(batch)) groups = append(groups, batch) } return groups, nil } // SubscribeNewTxsEvent should return an event subscription of NewTxsEvent and // send events to the given channel. func (p *testTxPool) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { return p.txFeed.Subscribe(ch) } // testHandler is a live implementation of the Ethereum protocol handler, just // preinitialized with some sane testing defaults and the transaction pool mocked // out. type testHandler struct { db ethdb.Database ChainConfig *params.ChainConfig vmConfig *vm.Config genesis *types.Block engine consensus.Engine txpool *testTxPool handler *handler headBlock *types.Block } // newTestHandler creates a new handler for testing purposes with no blocks. func newTestHandler() *testHandler { return newTestHandlerWithBlocks(0) } // newTestHandlerWithBlocks creates a new handler for testing purposes, with a // given number of initial blocks. func newTestHandlerWithBlocks(blocks int) *testHandler { // Create a database pre-initialize with a genesis block db := ethdb.NewMemoryDatabase() genesis := (&core.Genesis{ Config: params.TestChainConfig, Alloc: core.GenesisAlloc{testAddr: {Balance: big.NewInt(1000000)}}, }).MustCommit(db) headBlock := genesis if blocks > 0 { bs, _, _ := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, blocks, nil, false) if _, err := stagedsync.InsertBlocksInStages(db, ethdb.DefaultStorageMode, params.TestChainConfig, &vm.Config{}, ethash.NewFaker(), bs, true /* checkRoot */); err != nil { panic(err) } headBlock = bs[len(bs)-1] } txpool := newTestTxPool() handler, _ := newHandler(&handlerConfig{ Database: db, ChainConfig: params.TestChainConfig, genesis: genesis, vmConfig: &vm.Config{}, engine: ethash.NewFaker(), TxPool: txpool, Network: 1, BloomCache: 1, }) handler.Start(1000) return &testHandler{ db: db, ChainConfig: params.TestChainConfig, genesis: genesis, vmConfig: &vm.Config{}, engine: ethash.NewFaker(), txpool: txpool, handler: handler, headBlock: headBlock, } } // close tears down the handler and all its internal constructs. func (b *testHandler) close() { b.handler.Stop() }