forked from cerc-io/plugeth
eth: add protocol tests
The protocol tests were commented out when eth/downloader was introduced.
This commit is contained in:
parent
6c73a59806
commit
2c24a73e25
@ -1,388 +1,242 @@
|
|||||||
package eth
|
package eth
|
||||||
|
|
||||||
/*
|
import (
|
||||||
TODO All of these tests need to be re-written
|
"crypto/rand"
|
||||||
|
"math/big"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
var logsys = ethlogger.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlogger.LogLevel(ethlogger.DebugDetailLevel))
|
"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"
|
||||||
|
)
|
||||||
|
|
||||||
var ini = false
|
func init() {
|
||||||
|
// glog.SetToStderr(true)
|
||||||
func logInit() {
|
// glog.SetV(6)
|
||||||
if !ini {
|
|
||||||
ethlogger.AddLogSystem(logsys)
|
|
||||||
ini = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type testTxPool struct {
|
var testAccount = crypto.NewKey(rand.Reader)
|
||||||
getTransactions func() []*types.Transaction
|
|
||||||
addTransactions func(txs []*types.Transaction)
|
|
||||||
}
|
|
||||||
|
|
||||||
type testChainManager struct {
|
|
||||||
getBlockHashes func(hash common.Hash, amount uint64) (hashes []common.Hash)
|
|
||||||
getBlock func(hash common.Hash) *types.Block
|
|
||||||
status func() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
type testBlockPool struct {
|
|
||||||
addBlockHashes func(next func() (common.Hash, bool), peerId string)
|
|
||||||
addBlock func(block *types.Block, peerId string) (err error)
|
|
||||||
addPeer func(td *big.Int, currentBlock common.Hash, peerId string, requestHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool, suspended bool)
|
|
||||||
removePeer func(peerId string)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testTxPool) AddTransactions(txs []*types.Transaction) {
|
|
||||||
if self.addTransactions != nil {
|
|
||||||
self.addTransactions(txs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testTxPool) GetTransactions() types.Transactions { return nil }
|
|
||||||
|
|
||||||
func (self *testChainManager) GetBlockHashesFromHash(hash common.Hash, amount uint64) (hashes []common.Hash) {
|
|
||||||
if self.getBlockHashes != nil {
|
|
||||||
hashes = self.getBlockHashes(hash, amount)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testChainManager) Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) {
|
|
||||||
if self.status != nil {
|
|
||||||
td, currentBlock, genesisBlock = self.status()
|
|
||||||
} else {
|
|
||||||
td = common.Big1
|
|
||||||
currentBlock = common.Hash{1}
|
|
||||||
genesisBlock = common.Hash{2}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testChainManager) GetBlock(hash common.Hash) (block *types.Block) {
|
|
||||||
if self.getBlock != nil {
|
|
||||||
block = self.getBlock(hash)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testBlockPool) AddBlockHashes(next func() (common.Hash, bool), peerId string) {
|
|
||||||
if self.addBlockHashes != nil {
|
|
||||||
self.addBlockHashes(next, peerId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testBlockPool) AddBlock(block *types.Block, peerId string) {
|
|
||||||
if self.addBlock != nil {
|
|
||||||
self.addBlock(block, peerId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testBlockPool) AddPeer(td *big.Int, currentBlock common.Hash, peerId string, requestBlockHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool, suspended bool) {
|
|
||||||
if self.addPeer != nil {
|
|
||||||
best, suspended = self.addPeer(td, currentBlock, peerId, requestBlockHashes, requestBlocks, peerError)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *testBlockPool) RemovePeer(peerId string) {
|
|
||||||
if self.removePeer != nil {
|
|
||||||
self.removePeer(peerId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testPeer() *p2p.Peer {
|
|
||||||
var id discover.NodeID
|
|
||||||
pk := crypto.GenerateNewKeyPair().PublicKey
|
|
||||||
copy(id[:], pk)
|
|
||||||
return p2p.NewPeer(id, "test peer", []p2p.Cap{})
|
|
||||||
}
|
|
||||||
|
|
||||||
type ethProtocolTester struct {
|
|
||||||
p2p.MsgReadWriter // writing to the tester feeds the protocol
|
|
||||||
|
|
||||||
quit chan error
|
|
||||||
pipe *p2p.MsgPipeRW // the protocol read/writes on this end
|
|
||||||
txPool *testTxPool // txPool
|
|
||||||
chainManager *testChainManager // chainManager
|
|
||||||
blockPool *testBlockPool // blockPool
|
|
||||||
t *testing.T
|
|
||||||
}
|
|
||||||
|
|
||||||
func newEth(t *testing.T) *ethProtocolTester {
|
|
||||||
p1, p2 := p2p.MsgPipe()
|
|
||||||
return ðProtocolTester{
|
|
||||||
MsgReadWriter: p1,
|
|
||||||
quit: make(chan error, 1),
|
|
||||||
pipe: p2,
|
|
||||||
txPool: &testTxPool{},
|
|
||||||
chainManager: &testChainManager{},
|
|
||||||
blockPool: &testBlockPool{},
|
|
||||||
t: t,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *ethProtocolTester) reset() {
|
|
||||||
self.pipe.Close()
|
|
||||||
|
|
||||||
p1, p2 := p2p.MsgPipe()
|
|
||||||
self.MsgReadWriter = p1
|
|
||||||
self.pipe = p2
|
|
||||||
self.quit = make(chan error, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *ethProtocolTester) checkError(expCode int, delay time.Duration) (err error) {
|
|
||||||
var timer = time.After(delay)
|
|
||||||
select {
|
|
||||||
case err = <-self.quit:
|
|
||||||
case <-timer:
|
|
||||||
self.t.Errorf("no error after %v, expected %v", delay, expCode)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
perr, ok := err.(*errs.Error)
|
|
||||||
if ok && perr != nil {
|
|
||||||
if code := perr.Code; code != expCode {
|
|
||||||
self.t.Errorf("expected protocol error (code %v), got %v (%v)", expCode, code, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self.t.Errorf("expected protocol error (code %v), got %v", expCode, err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *ethProtocolTester) run() {
|
|
||||||
err := runEthProtocol(ProtocolVersion, NetworkId, self.txPool, self.chainManager, self.blockPool, testPeer(), self.pipe)
|
|
||||||
self.quit <- err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *ethProtocolTester) handshake(t *testing.T, mock bool) {
|
|
||||||
td, currentBlock, genesis := self.chainManager.Status()
|
|
||||||
// first outgoing msg should be StatusMsg.
|
|
||||||
err := p2p.ExpectMsg(self, StatusMsg, &statusMsgData{
|
|
||||||
ProtocolVersion: ProtocolVersion,
|
|
||||||
NetworkId: NetworkId,
|
|
||||||
TD: td,
|
|
||||||
CurrentBlock: currentBlock,
|
|
||||||
GenesisBlock: genesis,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("incorrect outgoing status: %v", err)
|
|
||||||
}
|
|
||||||
if mock {
|
|
||||||
go p2p.Send(self, StatusMsg, &statusMsgData{ProtocolVersion, NetworkId, td, currentBlock, genesis})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStatusMsgErrors(t *testing.T) {
|
func TestStatusMsgErrors(t *testing.T) {
|
||||||
logInit()
|
pm := newProtocolManagerForTesting(nil)
|
||||||
eth := newEth(t)
|
td, currentBlock, genesis := pm.chainman.Status()
|
||||||
go eth.run()
|
defer pm.Stop()
|
||||||
td, currentBlock, genesis := eth.chainManager.Status()
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
code uint64
|
code uint64
|
||||||
data interface{}
|
data interface{}
|
||||||
wantErrorCode int
|
wantError error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
code: TxMsg, data: []interface{}{},
|
code: TxMsg, data: []interface{}{},
|
||||||
wantErrorCode: ErrNoStatusMsg,
|
wantError: errResp(ErrNoStatusMsg, "first msg has code 2 (!= 0)"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: StatusMsg, data: statusMsgData{10, NetworkId, td, currentBlock, genesis},
|
code: StatusMsg, data: statusMsgData{10, NetworkId, td, currentBlock, genesis},
|
||||||
wantErrorCode: ErrProtocolVersionMismatch,
|
wantError: errResp(ErrProtocolVersionMismatch, "10 (!= 0)"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: StatusMsg, data: statusMsgData{ProtocolVersion, 999, td, currentBlock, genesis},
|
code: StatusMsg, data: statusMsgData{ProtocolVersion, 999, td, currentBlock, genesis},
|
||||||
wantErrorCode: ErrNetworkIdMismatch,
|
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 0)"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: StatusMsg, data: statusMsgData{ProtocolVersion, NetworkId, td, currentBlock, common.Hash{3}},
|
code: StatusMsg, data: statusMsgData{ProtocolVersion, NetworkId, td, currentBlock, common.Hash{3}},
|
||||||
wantErrorCode: ErrGenesisBlockMismatch,
|
wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000000000000000000000000000000000000000000000000000 (!= %x)", genesis),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
|
||||||
eth.handshake(t, false)
|
for i, test := range tests {
|
||||||
// the send call might hang until reset because
|
p, errc := newTestPeer(pm)
|
||||||
|
// The send call might hang until reset because
|
||||||
// the protocol might not read the payload.
|
// the protocol might not read the payload.
|
||||||
go p2p.Send(eth, test.code, test.data)
|
go p2p.Send(p, test.code, test.data)
|
||||||
eth.checkError(test.wantErrorCode, 1*time.Second)
|
|
||||||
|
|
||||||
eth.reset()
|
|
||||||
go eth.run()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewBlockMsg(t *testing.T) {
|
|
||||||
// logInit()
|
|
||||||
eth := newEth(t)
|
|
||||||
|
|
||||||
var disconnected bool
|
|
||||||
eth.blockPool.removePeer = func(peerId string) {
|
|
||||||
disconnected = true
|
|
||||||
}
|
|
||||||
|
|
||||||
go eth.run()
|
|
||||||
|
|
||||||
eth.handshake(t, true)
|
|
||||||
err := p2p.ExpectMsg(eth, TxMsg, []interface{}{})
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("transactions expected, got %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var tds = make(chan *big.Int)
|
|
||||||
eth.blockPool.addPeer = func(td *big.Int, currentBlock common.Hash, peerId string, requestHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool, suspended bool) {
|
|
||||||
tds <- td
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var delay = 1 * time.Second
|
|
||||||
// eth.reset()
|
|
||||||
block := types.NewBlock(common.Hash{1}, common.Address{1}, common.Hash{1}, common.Big1, 1, []byte("extra"))
|
|
||||||
|
|
||||||
go p2p.Send(eth, NewBlockMsg, &newBlockMsgData{Block: block})
|
|
||||||
timer := time.After(delay)
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case td := <-tds:
|
case err := <-errc:
|
||||||
if td.Cmp(common.Big0) != 0 {
|
if err == nil {
|
||||||
t.Errorf("incorrect td %v, expected %v", td, common.Big0)
|
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()
|
||||||
}
|
}
|
||||||
case <-timer:
|
|
||||||
t.Errorf("no td recorded after %v", delay)
|
|
||||||
return
|
|
||||||
case err := <-eth.quit:
|
|
||||||
t.Errorf("no error expected, got %v", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go p2p.Send(eth, NewBlockMsg, &newBlockMsgData{block, common.Big2})
|
// This test checks that received transactions are added to the local pool.
|
||||||
timer = time.After(delay)
|
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)
|
||||||
|
|
||||||
|
tx := newtx(testAccount, 0, 0)
|
||||||
|
if err := p2p.Send(p, TxMsg, []interface{}{tx}); err != nil {
|
||||||
|
t.Fatalf("send error: %v", err)
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case td := <-tds:
|
case added := <-txAdded:
|
||||||
if td.Cmp(common.Big2) != 0 {
|
if len(added) != 1 {
|
||||||
t.Errorf("incorrect td %v, expected %v", td, common.Big2)
|
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())
|
||||||
|
}
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Errorf("no TxPreEvent received within 2 seconds")
|
||||||
}
|
}
|
||||||
case <-timer:
|
|
||||||
t.Errorf("no td recorded after %v", delay)
|
|
||||||
return
|
|
||||||
case err := <-eth.quit:
|
|
||||||
t.Errorf("no error expected, got %v", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go p2p.Send(eth, NewBlockMsg, []interface{}{})
|
// This test checks that pending transactions are sent.
|
||||||
// Block.DecodeRLP: validation failed: header is nil
|
func TestSendTransactions(t *testing.T) {
|
||||||
eth.checkError(ErrDecode, delay)
|
pm := newProtocolManagerForTesting(nil)
|
||||||
|
defer pm.Stop()
|
||||||
|
|
||||||
|
// 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)
|
||||||
}
|
}
|
||||||
|
pm.txpool.AddTransactions(alltxs)
|
||||||
|
|
||||||
func TestBlockMsg(t *testing.T) {
|
// Connect several peers. They should all receive the pending transactions.
|
||||||
// logInit()
|
var wg sync.WaitGroup
|
||||||
eth := newEth(t)
|
checktxs := func(p *testPeer) {
|
||||||
blocks := make(chan *types.Block)
|
defer wg.Done()
|
||||||
eth.blockPool.addBlock = func(block *types.Block, peerId string) (err error) {
|
defer p.close()
|
||||||
blocks <- block
|
seen := make(map[common.Hash]bool)
|
||||||
return
|
for _, tx := range alltxs {
|
||||||
|
seen[tx.Hash()] = false
|
||||||
}
|
}
|
||||||
|
for n := 0; n < len(alltxs) && !t.Failed(); {
|
||||||
var disconnected bool
|
var txs []*types.Transaction
|
||||||
eth.blockPool.removePeer = func(peerId string) {
|
msg, err := p.ReadMsg()
|
||||||
disconnected = true
|
|
||||||
}
|
|
||||||
|
|
||||||
go eth.run()
|
|
||||||
|
|
||||||
eth.handshake(t, true)
|
|
||||||
err := p2p.ExpectMsg(eth, TxMsg, []interface{}{})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("transactions expected, got %v", err)
|
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)
|
||||||
|
}
|
||||||
|
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++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
p, _ := newTestPeer(pm)
|
||||||
|
p.handshake(t)
|
||||||
|
wg.Add(1)
|
||||||
|
go checktxs(p)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
var delay = 3 * time.Second
|
// testPeer wraps all peer-related data for tests.
|
||||||
// eth.reset()
|
type testPeer struct {
|
||||||
newblock := func(i int64) *types.Block {
|
p2p.MsgReadWriter // writing to the test peer feeds the protocol
|
||||||
return types.NewBlock(common.Hash{byte(i)}, common.Address{byte(i)}, common.Hash{byte(i)}, big.NewInt(i), uint64(i), []byte{byte(i)})
|
pipe *p2p.MsgPipeRW // the protocol read/writes on this end
|
||||||
|
pm *ProtocolManager
|
||||||
|
*peer
|
||||||
}
|
}
|
||||||
b := newblock(0)
|
|
||||||
b.Header().Difficulty = nil // check if nil as *big.Int decodes as 0
|
func newProtocolManagerForTesting(txAdded chan<- []*types.Transaction) *ProtocolManager {
|
||||||
go p2p.Send(eth, BlocksMsg, types.Blocks{b, newblock(1), newblock(2)})
|
var (
|
||||||
timer := time.After(delay)
|
em = new(event.TypeMux)
|
||||||
for i := int64(0); i < 3; i++ {
|
db, _ = ethdb.NewMemDatabase()
|
||||||
select {
|
chain, _ = core.NewChainManager(core.GenesisBlock(0, db), db, db, core.FakePow{}, em)
|
||||||
case block := <-blocks:
|
txpool = &fakeTxPool{added: txAdded}
|
||||||
if (block.ParentHash() != common.Hash{byte(i)}) {
|
dl = downloader.New(em, chain.HasBlock, chain.GetBlock)
|
||||||
t.Errorf("incorrect block %v, expected %v", block.ParentHash(), common.Hash{byte(i)})
|
pm = NewProtocolManager(ProtocolVersion, 0, em, txpool, chain, dl)
|
||||||
|
)
|
||||||
|
pm.Start()
|
||||||
|
return pm
|
||||||
}
|
}
|
||||||
if block.Difficulty().Cmp(big.NewInt(i)) != 0 {
|
|
||||||
t.Errorf("incorrect block %v, expected %v", block.Difficulty(), big.NewInt(i))
|
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
|
||||||
}
|
}
|
||||||
case <-timer:
|
|
||||||
t.Errorf("no td recorded after %v", delay)
|
func (p *testPeer) handshake(t *testing.T) {
|
||||||
return
|
td, currentBlock, genesis := p.pm.chainman.Status()
|
||||||
case err := <-eth.quit:
|
msg := &statusMsgData{
|
||||||
t.Errorf("no error expected, got %v", err)
|
ProtocolVersion: uint32(p.pm.protVer),
|
||||||
return
|
NetworkId: uint32(p.pm.netId),
|
||||||
|
TD: td,
|
||||||
|
CurrentBlock: currentBlock,
|
||||||
|
GenesisBlock: genesis,
|
||||||
|
}
|
||||||
|
if err := p2p.ExpectMsg(p, StatusMsg, msg); err != nil {
|
||||||
|
t.Fatalf("status recv: %v", err)
|
||||||
|
}
|
||||||
|
if err := p2p.Send(p, StatusMsg, msg); err != nil {
|
||||||
|
t.Fatalf("status send: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go p2p.Send(eth, BlocksMsg, []interface{}{[]interface{}{}})
|
func (p *testPeer) close() {
|
||||||
eth.checkError(ErrDecode, delay)
|
p.pipe.Close()
|
||||||
if !disconnected {
|
|
||||||
t.Errorf("peer not disconnected after error")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// test empty transaction
|
type fakeTxPool struct {
|
||||||
eth.reset()
|
// all transactions are collected.
|
||||||
go eth.run()
|
mu sync.Mutex
|
||||||
eth.handshake(t, true)
|
all []*types.Transaction
|
||||||
err = p2p.ExpectMsg(eth, TxMsg, []interface{}{})
|
// if added is non-nil, it receives added transactions.
|
||||||
if err != nil {
|
added chan<- []*types.Transaction
|
||||||
t.Errorf("transactions expected, got %v", err)
|
|
||||||
}
|
|
||||||
b = newblock(0)
|
|
||||||
b.AddTransaction(nil)
|
|
||||||
go p2p.Send(eth, BlocksMsg, types.Blocks{b})
|
|
||||||
eth.checkError(ErrDecode, delay)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTransactionsMsg(t *testing.T) {
|
func (pool *fakeTxPool) AddTransactions(txs []*types.Transaction) {
|
||||||
logInit()
|
pool.mu.Lock()
|
||||||
eth := newEth(t)
|
defer pool.mu.Unlock()
|
||||||
txs := make(chan *types.Transaction)
|
pool.all = append(pool.all, txs...)
|
||||||
|
if pool.added != nil {
|
||||||
eth.txPool.addTransactions = func(t []*types.Transaction) {
|
pool.added <- txs
|
||||||
for _, tx := range t {
|
|
||||||
txs <- tx
|
|
||||||
}
|
|
||||||
}
|
|
||||||
go eth.run()
|
|
||||||
|
|
||||||
eth.handshake(t, true)
|
|
||||||
err := p2p.ExpectMsg(eth, TxMsg, []interface{}{})
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("transactions expected, got %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var delay = 3 * time.Second
|
|
||||||
tx := &types.Transaction{}
|
|
||||||
|
|
||||||
go p2p.Send(eth, TxMsg, []interface{}{tx, tx})
|
|
||||||
timer := time.After(delay)
|
|
||||||
for i := int64(0); i < 2; i++ {
|
|
||||||
select {
|
|
||||||
case <-txs:
|
|
||||||
case <-timer:
|
|
||||||
return
|
|
||||||
case err := <-eth.quit:
|
|
||||||
t.Errorf("no error expected, got %v", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go p2p.Send(eth, TxMsg, []interface{}{[]interface{}{}})
|
func (pool *fakeTxPool) GetTransactions() types.Transactions {
|
||||||
eth.checkError(ErrDecode, delay)
|
pool.mu.Lock()
|
||||||
|
defer pool.mu.Unlock()
|
||||||
|
txs := make([]*types.Transaction, len(pool.all))
|
||||||
|
copy(txs, pool.all)
|
||||||
|
return types.Transactions(txs)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user