2016-04-14 11:18:24 -05: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 11:55:18 -05:00
|
|
|
// This file contains some shares testing functionality, common to multiple
|
|
|
|
// different files and modules being tested.
|
|
|
|
|
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
2016-03-02 06:57:15 -06:00
|
|
|
"crypto/ecdsa"
|
2015-07-02 11:55:18 -05:00
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
2016-07-01 10:59:55 -05:00
|
|
|
"sort"
|
2015-07-02 11:55:18 -05:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-04-04 17:16:29 -05:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2015-07-02 11:55:18 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-01-17 05:19:50 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2015-07-02 11:55:18 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-04-12 09:27:23 -05:00
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2015-07-02 11:55:18 -05:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2016-10-20 06:36:29 -05:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2015-07-02 11:55:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-04-15 04:06:57 -05:00
|
|
|
testBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
2017-03-02 07:03:33 -06:00
|
|
|
testBank = crypto.PubkeyToAddress(testBankKey.PublicKey)
|
2015-07-02 11:55:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// newTestProtocolManager creates a new protocol manager for testing purposes,
|
|
|
|
// with the given number of blocks already known, and potential notification
|
|
|
|
// channels for different events.
|
2018-02-05 10:40:32 -06:00
|
|
|
func newTestProtocolManager(mode downloader.SyncMode, blocks int, generator func(int, *core.BlockGen), newtx chan<- []*types.Transaction) (*ProtocolManager, *ethdb.MemDatabase, error) {
|
2015-07-02 11:55:18 -05:00
|
|
|
var (
|
2017-04-04 17:16:29 -05:00
|
|
|
evmux = new(event.TypeMux)
|
|
|
|
engine = ethash.NewFaker()
|
2018-05-09 07:24:25 -05:00
|
|
|
db = ethdb.NewMemDatabase()
|
2017-04-04 17:16:29 -05:00
|
|
|
gspec = &core.Genesis{
|
2017-03-02 07:03:33 -06:00
|
|
|
Config: params.TestChainConfig,
|
|
|
|
Alloc: core.GenesisAlloc{testBank: {Balance: big.NewInt(1000000)}},
|
|
|
|
}
|
|
|
|
genesis = gspec.MustCommit(db)
|
2018-02-05 10:40:32 -06:00
|
|
|
blockchain, _ = core.NewBlockChain(db, nil, gspec.Config, engine, vm.Config{})
|
2015-07-02 11:55:18 -05:00
|
|
|
)
|
2017-12-22 06:37:50 -06:00
|
|
|
chain, _ := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, blocks, generator)
|
2015-10-06 09:35:55 -05:00
|
|
|
if _, err := blockchain.InsertChain(chain); err != nil {
|
2015-07-02 11:55:18 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
2016-03-01 16:32:43 -06:00
|
|
|
|
2017-09-05 11:18:28 -05:00
|
|
|
pm, err := NewProtocolManager(gspec.Config, mode, DefaultConfig.NetworkId, evmux, &testTxPool{added: newtx}, engine, blockchain, db)
|
2015-09-01 09:35:14 -05:00
|
|
|
if err != nil {
|
2018-02-05 10:40:32 -06:00
|
|
|
return nil, nil, err
|
2015-09-01 09:35:14 -05:00
|
|
|
}
|
2017-09-05 11:18:28 -05:00
|
|
|
pm.Start(1000)
|
2018-02-05 10:40:32 -06:00
|
|
|
return pm, db, nil
|
2015-09-01 09:35:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTestProtocolManagerMust creates a new protocol manager for testing purposes,
|
|
|
|
// with the given number of blocks already known, and potential notification
|
|
|
|
// channels for different events. In case of an error, the constructor force-
|
|
|
|
// fails the test.
|
2018-02-05 10:40:32 -06:00
|
|
|
func newTestProtocolManagerMust(t *testing.T, mode downloader.SyncMode, blocks int, generator func(int, *core.BlockGen), newtx chan<- []*types.Transaction) (*ProtocolManager, *ethdb.MemDatabase) {
|
|
|
|
pm, db, err := newTestProtocolManager(mode, blocks, generator, newtx)
|
2015-09-01 09:35:14 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create protocol manager: %v", err)
|
|
|
|
}
|
2018-02-05 10:40:32 -06:00
|
|
|
return pm, db
|
2015-07-02 11:55:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// testTxPool is a fake, helper transaction pool for testing purposes
|
|
|
|
type testTxPool struct {
|
2017-08-18 05:58:36 -05:00
|
|
|
txFeed event.Feed
|
|
|
|
pool []*types.Transaction // Collection of all transactions
|
|
|
|
added chan<- []*types.Transaction // Notification channel for new transactions
|
2015-07-02 11:55:18 -05:00
|
|
|
|
|
|
|
lock sync.RWMutex // Protects the transaction pool
|
|
|
|
}
|
|
|
|
|
2017-07-05 08:51:55 -05:00
|
|
|
// AddRemotes appends a batch of transactions to the pool, and notifies any
|
2015-07-02 11:55:18 -05:00
|
|
|
// listeners if the addition channel is non nil
|
2017-10-25 04:18:44 -05:00
|
|
|
func (p *testTxPool) AddRemotes(txs []*types.Transaction) []error {
|
2015-07-02 11:55:18 -05:00
|
|
|
p.lock.Lock()
|
|
|
|
defer p.lock.Unlock()
|
|
|
|
|
|
|
|
p.pool = append(p.pool, txs...)
|
|
|
|
if p.added != nil {
|
|
|
|
p.added <- txs
|
|
|
|
}
|
2017-10-25 04:18:44 -05:00
|
|
|
return make([]error, len(txs))
|
2015-07-02 11:55:18 -05:00
|
|
|
}
|
|
|
|
|
2016-07-01 10:59:55 -05:00
|
|
|
// Pending returns all the transactions known to the pool
|
2016-12-10 16:54:58 -06:00
|
|
|
func (p *testTxPool) Pending() (map[common.Address]types.Transactions, error) {
|
2015-07-02 11:55:18 -05:00
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
|
2016-07-01 10:59:55 -05:00
|
|
|
batches := make(map[common.Address]types.Transactions)
|
|
|
|
for _, tx := range p.pool {
|
2016-11-02 07:44:13 -05:00
|
|
|
from, _ := types.Sender(types.HomesteadSigner{}, tx)
|
2016-07-01 10:59:55 -05:00
|
|
|
batches[from] = append(batches[from], tx)
|
|
|
|
}
|
|
|
|
for _, batch := range batches {
|
|
|
|
sort.Sort(types.TxByNonce(batch))
|
|
|
|
}
|
2016-12-10 16:54:58 -06:00
|
|
|
return batches, nil
|
2015-07-02 11:55:18 -05:00
|
|
|
}
|
|
|
|
|
2017-08-18 05:58:36 -05:00
|
|
|
func (p *testTxPool) SubscribeTxPreEvent(ch chan<- core.TxPreEvent) event.Subscription {
|
|
|
|
return p.txFeed.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
2015-07-02 11:55:18 -05:00
|
|
|
// newTestTransaction create a new dummy transaction.
|
2016-03-02 06:57:15 -06:00
|
|
|
func newTestTransaction(from *ecdsa.PrivateKey, nonce uint64, datasize int) *types.Transaction {
|
2017-11-13 05:47:27 -06:00
|
|
|
tx := types.NewTransaction(nonce, common.Address{}, big.NewInt(0), 100000, big.NewInt(0), make([]byte, datasize))
|
2017-01-05 05:59:17 -06:00
|
|
|
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, from)
|
2015-07-02 11:55:18 -05:00
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
// testPeer is a simulated peer to allow testing direct network calls.
|
|
|
|
type testPeer struct {
|
|
|
|
net p2p.MsgReadWriter // Network layer reader/writer to simulate remote messaging
|
|
|
|
app *p2p.MsgPipeRW // Application layer reader/writer to simulate the local side
|
|
|
|
*peer
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestPeer creates a new peer registered at the given protocol manager.
|
|
|
|
func newTestPeer(name string, version int, pm *ProtocolManager, shake bool) (*testPeer, <-chan error) {
|
|
|
|
// Create a message pipe to communicate through
|
|
|
|
app, net := p2p.MsgPipe()
|
|
|
|
|
|
|
|
// Generate a random id and create the peer
|
|
|
|
var id discover.NodeID
|
|
|
|
rand.Read(id[:])
|
|
|
|
|
2015-10-27 08:10:30 -05:00
|
|
|
peer := pm.newPeer(version, p2p.NewPeer(id, name, nil), net)
|
2015-07-02 11:55:18 -05:00
|
|
|
|
|
|
|
// Start the peer on a new thread
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
2016-03-28 20:08:16 -05:00
|
|
|
select {
|
|
|
|
case pm.newPeerCh <- peer:
|
|
|
|
errc <- pm.handle(peer)
|
|
|
|
case <-pm.quitSync:
|
|
|
|
errc <- p2p.DiscQuitting
|
|
|
|
}
|
2015-07-02 11:55:18 -05:00
|
|
|
}()
|
2016-03-28 20:08:16 -05:00
|
|
|
tp := &testPeer{app: app, net: net, peer: peer}
|
2015-07-02 11:55:18 -05:00
|
|
|
// Execute any implicitly requested handshakes and return
|
|
|
|
if shake {
|
2018-01-30 10:39:32 -06:00
|
|
|
var (
|
|
|
|
genesis = pm.blockchain.Genesis()
|
|
|
|
head = pm.blockchain.CurrentHeader()
|
|
|
|
td = pm.blockchain.GetTd(head.Hash(), head.Number.Uint64())
|
|
|
|
)
|
|
|
|
tp.handshake(nil, td, head.Hash(), genesis.Hash())
|
2015-07-02 11:55:18 -05:00
|
|
|
}
|
|
|
|
return tp, errc
|
|
|
|
}
|
|
|
|
|
|
|
|
// handshake simulates a trivial handshake that expects the same state from the
|
|
|
|
// remote side as we are simulating locally.
|
|
|
|
func (p *testPeer) handshake(t *testing.T, td *big.Int, head common.Hash, genesis common.Hash) {
|
|
|
|
msg := &statusData{
|
|
|
|
ProtocolVersion: uint32(p.version),
|
2017-04-25 06:31:15 -05:00
|
|
|
NetworkId: DefaultConfig.NetworkId,
|
2015-07-02 11:55:18 -05:00
|
|
|
TD: td,
|
|
|
|
CurrentBlock: head,
|
|
|
|
GenesisBlock: genesis,
|
|
|
|
}
|
|
|
|
if err := p2p.ExpectMsg(p.app, StatusMsg, msg); err != nil {
|
|
|
|
t.Fatalf("status recv: %v", err)
|
|
|
|
}
|
|
|
|
if err := p2p.Send(p.app, StatusMsg, msg); err != nil {
|
|
|
|
t.Fatalf("status send: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close terminates the local side of the peer, notifying the remote protocol
|
|
|
|
// manager of termination.
|
|
|
|
func (p *testPeer) close() {
|
|
|
|
p.app.Close()
|
|
|
|
}
|