2016-04-14 19:18:24 +03:00
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-07-02 19:55:18 +03:00
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
2020-12-14 12:27:15 +03:00
|
|
|
"sort"
|
|
|
|
"sync"
|
2015-07-02 19:55:18 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-04-05 01:16:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2015-07-02 19:55:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-09-24 15:57:49 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2015-07-02 19:55:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-01-17 14:19:50 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2015-07-02 19:55:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2020-12-14 12:27:15 +03:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2016-07-08 20:59:11 +03:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2015-07-02 19:55:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
var (
|
|
|
|
// testKey is a private key to use for funding a tester account.
|
|
|
|
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
2015-07-02 19:55:18 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// testAddr is the Ethereum address of the tester account.
|
|
|
|
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
|
|
|
)
|
2015-07-02 19:55:18 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// 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
|
2015-12-15 19:22:48 +02:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
txFeed event.Feed // Notification feed to allow waiting for inclusion
|
|
|
|
lock sync.RWMutex // Protects the transaction pool
|
2015-07-02 19:55:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// newTestTxPool creates a mock transaction pool.
|
|
|
|
func newTestTxPool() *testTxPool {
|
|
|
|
return &testTxPool{
|
|
|
|
pool: make(map[common.Hash]*types.Transaction),
|
2015-07-02 19:55:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// 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()
|
2015-07-02 19:55:18 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
return p.pool[hash] != nil
|
2015-07-02 19:55:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// 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()
|
2015-07-02 19:55:18 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
return p.pool[hash]
|
2015-07-02 19:55:18 +03:00
|
|
|
}
|
2016-07-08 20:59:11 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// 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()
|
2019-04-16 13:20:38 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
for _, tx := range txs {
|
|
|
|
p.pool[tx.Hash()] = tx
|
2016-07-08 20:59:11 +03:00
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
p.txFeed.Send(core.NewTxsEvent{Txs: txs})
|
|
|
|
return make([]error, len(txs))
|
2019-04-16 13:20:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// Pending returns all the transactions known to the pool
|
2021-06-04 09:55:00 +03:00
|
|
|
func (p *testTxPool) Pending(enforceTips bool) (map[common.Address]types.Transactions, error) {
|
2020-12-14 12:27:15 +03:00
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
2019-04-16 13:20:38 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
batches := make(map[common.Address]types.Transactions)
|
|
|
|
for _, tx := range p.pool {
|
|
|
|
from, _ := types.Sender(types.HomesteadSigner{}, tx)
|
|
|
|
batches[from] = append(batches[from], tx)
|
2019-04-16 13:20:38 +03:00
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
for _, batch := range batches {
|
|
|
|
sort.Sort(types.TxByNonce(batch))
|
2016-07-08 20:59:11 +03:00
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
return batches, nil
|
2016-07-08 20:59:11 +03:00
|
|
|
}
|
2018-09-29 23:17:06 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// 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)
|
2018-09-29 23:17:06 +03:00
|
|
|
}
|
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// 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
|
|
|
|
chain *core.BlockChain
|
|
|
|
txpool *testTxPool
|
|
|
|
handler *handler
|
|
|
|
}
|
2020-02-17 13:01:03 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// newTestHandler creates a new handler for testing purposes with no blocks.
|
|
|
|
func newTestHandler() *testHandler {
|
|
|
|
return newTestHandlerWithBlocks(0)
|
2018-09-29 23:17:06 +03:00
|
|
|
}
|
2020-01-13 15:23:54 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// 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 := rawdb.NewMemoryDatabase()
|
|
|
|
(&core.Genesis{
|
|
|
|
Config: params.TestChainConfig,
|
|
|
|
Alloc: core.GenesisAlloc{testAddr: {Balance: big.NewInt(1000000)}},
|
|
|
|
}).MustCommit(db)
|
|
|
|
|
|
|
|
chain, _ := core.NewBlockChain(db, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{}, nil, nil)
|
|
|
|
|
|
|
|
bs, _ := core.GenerateChain(params.TestChainConfig, chain.Genesis(), ethash.NewFaker(), db, blocks, nil)
|
|
|
|
if _, err := chain.InsertChain(bs); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
txpool := newTestTxPool()
|
|
|
|
|
|
|
|
handler, _ := newHandler(&handlerConfig{
|
|
|
|
Database: db,
|
|
|
|
Chain: chain,
|
|
|
|
TxPool: txpool,
|
|
|
|
Network: 1,
|
|
|
|
Sync: downloader.FastSync,
|
|
|
|
BloomCache: 1,
|
|
|
|
})
|
|
|
|
handler.Start(1000)
|
|
|
|
|
|
|
|
return &testHandler{
|
|
|
|
db: db,
|
|
|
|
chain: chain,
|
|
|
|
txpool: txpool,
|
|
|
|
handler: handler,
|
2020-01-13 15:23:54 +03:00
|
|
|
}
|
2020-12-14 12:27:15 +03:00
|
|
|
}
|
2020-01-13 15:23:54 +03:00
|
|
|
|
2020-12-14 12:27:15 +03:00
|
|
|
// close tears down the handler and all its internal constructs.
|
|
|
|
func (b *testHandler) close() {
|
|
|
|
b.handler.Stop()
|
|
|
|
b.chain.Stop()
|
2020-01-13 15:23:54 +03:00
|
|
|
}
|