2014-12-05 15:14:55 -06:00
|
|
|
package eth
|
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// glog.SetToStderr(true)
|
|
|
|
// glog.SetV(6)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccount = crypto.NewKey(rand.Reader)
|
2015-03-27 19:48:37 -05:00
|
|
|
|
2015-01-05 10:10:42 -06:00
|
|
|
func TestStatusMsgErrors(t *testing.T) {
|
2015-06-09 05:10:19 -05:00
|
|
|
pm := newProtocolManagerForTesting(nil)
|
|
|
|
td, currentBlock, genesis := pm.chainman.Status()
|
|
|
|
defer pm.Stop()
|
2015-01-05 10:10:42 -06:00
|
|
|
|
2015-03-19 09:18:31 -05:00
|
|
|
tests := []struct {
|
2015-06-09 05:10:19 -05:00
|
|
|
code uint64
|
|
|
|
data interface{}
|
|
|
|
wantError error
|
2015-03-19 09:18:31 -05:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
code: TxMsg, data: []interface{}{},
|
2015-06-09 05:10:19 -05:00
|
|
|
wantError: errResp(ErrNoStatusMsg, "first msg has code 2 (!= 0)"),
|
2015-03-19 09:18:31 -05:00
|
|
|
},
|
|
|
|
{
|
2015-04-01 06:36:49 -05:00
|
|
|
code: StatusMsg, data: statusMsgData{10, NetworkId, td, currentBlock, genesis},
|
2015-06-09 05:10:19 -05:00
|
|
|
wantError: errResp(ErrProtocolVersionMismatch, "10 (!= 0)"),
|
2015-03-19 09:18:31 -05:00
|
|
|
},
|
|
|
|
{
|
2015-04-01 06:36:49 -05:00
|
|
|
code: StatusMsg, data: statusMsgData{ProtocolVersion, 999, td, currentBlock, genesis},
|
2015-06-09 05:10:19 -05:00
|
|
|
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 0)"),
|
2015-03-19 09:18:31 -05:00
|
|
|
},
|
|
|
|
{
|
2015-04-01 06:36:49 -05:00
|
|
|
code: StatusMsg, data: statusMsgData{ProtocolVersion, NetworkId, td, currentBlock, common.Hash{3}},
|
2015-06-09 05:10:19 -05:00
|
|
|
wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000000000000000000000000000000000000000000000000000 (!= %x)", genesis),
|
2015-03-19 09:18:31 -05:00
|
|
|
},
|
|
|
|
}
|
2015-06-09 05:10:19 -05:00
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
p, errc := newTestPeer(pm)
|
|
|
|
// The send call might hang until reset because
|
2015-03-19 09:18:31 -05:00
|
|
|
// the protocol might not read the payload.
|
2015-06-09 05:10:19 -05:00
|
|
|
go p2p.Send(p, test.code, test.data)
|
2015-01-05 10:10:42 -06:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("test %d: protocol returned nil error, want %q", test.wantError)
|
|
|
|
} else if err.Error() != test.wantError.Error() {
|
|
|
|
t.Errorf("test %d: wrong error: got %q, want %q", i, err, test.wantError)
|
|
|
|
}
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Errorf("protocol did not shut down withing 2 seconds")
|
|
|
|
}
|
|
|
|
p.close()
|
2015-03-19 09:18:31 -05:00
|
|
|
}
|
2014-12-05 15:14:55 -06:00
|
|
|
}
|
2015-03-27 19:48:37 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
// This test checks that received transactions are added to the local pool.
|
|
|
|
func TestRecvTransactions(t *testing.T) {
|
|
|
|
txAdded := make(chan []*types.Transaction)
|
|
|
|
pm := newProtocolManagerForTesting(txAdded)
|
|
|
|
p, _ := newTestPeer(pm)
|
|
|
|
defer pm.Stop()
|
|
|
|
defer p.close()
|
|
|
|
p.handshake(t)
|
2015-03-27 19:48:37 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
tx := newtx(testAccount, 0, 0)
|
|
|
|
if err := p2p.Send(p, TxMsg, []interface{}{tx}); err != nil {
|
|
|
|
t.Fatalf("send error: %v", err)
|
2015-03-27 19:48:37 -05:00
|
|
|
}
|
|
|
|
select {
|
2015-06-09 05:10:19 -05:00
|
|
|
case added := <-txAdded:
|
|
|
|
if len(added) != 1 {
|
|
|
|
t.Errorf("wrong number of added transactions: got %d, want 1", len(added))
|
|
|
|
} else if added[0].Hash() != tx.Hash() {
|
|
|
|
t.Errorf("added wrong tx hash: got %v, want %v", added[0].Hash(), tx.Hash())
|
2015-03-27 19:48:37 -05:00
|
|
|
}
|
2015-06-09 05:10:19 -05:00
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Errorf("no TxPreEvent received within 2 seconds")
|
2015-03-27 19:48:37 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 20:54:23 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
// This test checks that pending transactions are sent.
|
|
|
|
func TestSendTransactions(t *testing.T) {
|
|
|
|
pm := newProtocolManagerForTesting(nil)
|
|
|
|
defer pm.Stop()
|
2015-03-27 20:54:23 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
// Fill the pool with big transactions.
|
|
|
|
const txsize = txsyncPackSize / 10
|
|
|
|
alltxs := make([]*types.Transaction, 100)
|
|
|
|
for nonce := range alltxs {
|
|
|
|
alltxs[nonce] = newtx(testAccount, uint64(nonce), txsize)
|
2015-03-27 20:54:23 -05:00
|
|
|
}
|
2015-06-09 05:10:19 -05:00
|
|
|
pm.txpool.AddTransactions(alltxs)
|
2015-03-27 20:54:23 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
// Connect several peers. They should all receive the pending transactions.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
checktxs := func(p *testPeer) {
|
|
|
|
defer wg.Done()
|
|
|
|
defer p.close()
|
|
|
|
seen := make(map[common.Hash]bool)
|
|
|
|
for _, tx := range alltxs {
|
|
|
|
seen[tx.Hash()] = false
|
|
|
|
}
|
|
|
|
for n := 0; n < len(alltxs) && !t.Failed(); {
|
|
|
|
var txs []*types.Transaction
|
|
|
|
msg, err := p.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v: read error: %v", p.Peer, err)
|
|
|
|
} else if msg.Code != TxMsg {
|
|
|
|
t.Errorf("%v: got code %d, want TxMsg", p.Peer, msg.Code)
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&txs); err != nil {
|
|
|
|
t.Errorf("%v: %v", p.Peer, err)
|
2015-03-27 20:54:23 -05:00
|
|
|
}
|
2015-06-09 05:10:19 -05:00
|
|
|
for _, tx := range txs {
|
|
|
|
hash := tx.Hash()
|
|
|
|
seentx, want := seen[hash]
|
|
|
|
if seentx {
|
|
|
|
t.Errorf("%v: got tx more than once: %x", p.Peer, hash)
|
|
|
|
}
|
|
|
|
if !want {
|
|
|
|
t.Errorf("%v: got unexpected tx: %x", p.Peer, hash)
|
|
|
|
}
|
|
|
|
seen[hash] = true
|
|
|
|
n++
|
2015-03-30 07:48:07 -05:00
|
|
|
}
|
2015-03-27 20:54:23 -05:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 05:10:19 -05:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
p, _ := newTestPeer(pm)
|
|
|
|
p.handshake(t)
|
|
|
|
wg.Add(1)
|
|
|
|
go checktxs(p)
|
2015-03-30 07:48:07 -05:00
|
|
|
}
|
2015-06-09 05:10:19 -05:00
|
|
|
wg.Wait()
|
|
|
|
}
|
2015-03-30 07:48:07 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
// testPeer wraps all peer-related data for tests.
|
|
|
|
type testPeer struct {
|
|
|
|
p2p.MsgReadWriter // writing to the test peer feeds the protocol
|
|
|
|
pipe *p2p.MsgPipeRW // the protocol read/writes on this end
|
|
|
|
pm *ProtocolManager
|
|
|
|
*peer
|
2015-03-27 20:54:23 -05:00
|
|
|
}
|
2015-03-30 09:21:41 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
func newProtocolManagerForTesting(txAdded chan<- []*types.Transaction) *ProtocolManager {
|
|
|
|
var (
|
|
|
|
em = new(event.TypeMux)
|
|
|
|
db, _ = ethdb.NewMemDatabase()
|
|
|
|
chain, _ = core.NewChainManager(core.GenesisBlock(0, db), db, db, core.FakePow{}, em)
|
|
|
|
txpool = &fakeTxPool{added: txAdded}
|
|
|
|
dl = downloader.New(em, chain.HasBlock, chain.GetBlock)
|
|
|
|
pm = NewProtocolManager(ProtocolVersion, 0, em, txpool, chain, dl)
|
|
|
|
)
|
|
|
|
pm.Start()
|
|
|
|
return pm
|
|
|
|
}
|
2015-03-30 09:21:41 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
func newTestPeer(pm *ProtocolManager) (*testPeer, <-chan error) {
|
|
|
|
var id discover.NodeID
|
|
|
|
rand.Read(id[:])
|
|
|
|
rw1, rw2 := p2p.MsgPipe()
|
|
|
|
peer := pm.newPeer(pm.protVer, pm.netId, p2p.NewPeer(id, "test peer", nil), rw2)
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
pm.newPeerCh <- peer
|
|
|
|
errc <- pm.handle(peer)
|
|
|
|
}()
|
|
|
|
return &testPeer{rw1, rw2, pm, peer}, errc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testPeer) handshake(t *testing.T) {
|
|
|
|
td, currentBlock, genesis := p.pm.chainman.Status()
|
|
|
|
msg := &statusMsgData{
|
|
|
|
ProtocolVersion: uint32(p.pm.protVer),
|
|
|
|
NetworkId: uint32(p.pm.netId),
|
|
|
|
TD: td,
|
|
|
|
CurrentBlock: currentBlock,
|
|
|
|
GenesisBlock: genesis,
|
2015-03-30 09:21:41 -05:00
|
|
|
}
|
2015-06-09 05:10:19 -05:00
|
|
|
if err := p2p.ExpectMsg(p, StatusMsg, msg); err != nil {
|
|
|
|
t.Fatalf("status recv: %v", err)
|
2015-03-30 09:21:41 -05:00
|
|
|
}
|
2015-06-09 05:10:19 -05:00
|
|
|
if err := p2p.Send(p, StatusMsg, msg); err != nil {
|
|
|
|
t.Fatalf("status send: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 09:21:41 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
func (p *testPeer) close() {
|
|
|
|
p.pipe.Close()
|
|
|
|
}
|
2015-03-30 09:21:41 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
type fakeTxPool struct {
|
|
|
|
// all transactions are collected.
|
|
|
|
mu sync.Mutex
|
|
|
|
all []*types.Transaction
|
|
|
|
// if added is non-nil, it receives added transactions.
|
|
|
|
added chan<- []*types.Transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pool *fakeTxPool) AddTransactions(txs []*types.Transaction) {
|
|
|
|
pool.mu.Lock()
|
|
|
|
defer pool.mu.Unlock()
|
|
|
|
pool.all = append(pool.all, txs...)
|
|
|
|
if pool.added != nil {
|
|
|
|
pool.added <- txs
|
2015-03-30 09:21:41 -05:00
|
|
|
}
|
2015-06-09 05:10:19 -05:00
|
|
|
}
|
2015-03-30 09:21:41 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
func (pool *fakeTxPool) GetTransactions() types.Transactions {
|
|
|
|
pool.mu.Lock()
|
|
|
|
defer pool.mu.Unlock()
|
|
|
|
txs := make([]*types.Transaction, len(pool.all))
|
|
|
|
copy(txs, pool.all)
|
|
|
|
return types.Transactions(txs)
|
|
|
|
}
|
2015-03-30 09:21:41 -05:00
|
|
|
|
2015-06-09 05:10:19 -05:00
|
|
|
func newtx(from *crypto.Key, nonce uint64, datasize int) *types.Transaction {
|
|
|
|
data := make([]byte, datasize)
|
|
|
|
tx := types.NewTransactionMessage(common.Address{}, big.NewInt(0), big.NewInt(100000), big.NewInt(0), data)
|
|
|
|
tx.SetNonce(nonce)
|
|
|
|
return tx
|
2015-03-30 09:21:41 -05:00
|
|
|
}
|