2014-12-05 23:14:55 +02:00
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
2015-01-05 18:10:42 +02:00
|
|
|
"bytes"
|
2014-12-05 23:14:55 +02:00
|
|
|
"io"
|
2015-01-05 18:10:42 +02:00
|
|
|
"log"
|
2014-12-05 23:14:55 +02:00
|
|
|
"math/big"
|
2015-01-05 18:10:42 +02:00
|
|
|
"os"
|
2014-12-05 23:14:55 +02:00
|
|
|
"testing"
|
2015-01-05 18:10:42 +02:00
|
|
|
"time"
|
2014-12-05 23:14:55 +02:00
|
|
|
|
2015-03-06 21:07:35 +02:00
|
|
|
ethlogger "github.com/ethereum/go-ethereu/logger"
|
2014-12-05 23:14:55 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2014-12-10 01:55:50 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-02-25 15:06:59 +02:00
|
|
|
"github.com/ethereum/go-ethereum/errs"
|
2015-01-05 18:10:42 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2014-12-05 23:14:55 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2015-02-05 04:16:16 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2014-12-05 23:14:55 +02:00
|
|
|
)
|
|
|
|
|
2015-02-25 15:06:59 +02:00
|
|
|
var logsys = ethlogger.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlogger.LogLevel(ethlogger.DebugDetailLevel))
|
|
|
|
|
|
|
|
var ini = false
|
|
|
|
|
|
|
|
func logInit() {
|
|
|
|
if !ini {
|
|
|
|
ethlogger.AddLogSystem(logsys)
|
|
|
|
ini = true
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 18:10:42 +02:00
|
|
|
|
2014-12-05 23:14:55 +02:00
|
|
|
type testMsgReadWriter struct {
|
|
|
|
in chan p2p.Msg
|
2015-01-05 18:10:42 +02:00
|
|
|
out []p2p.Msg
|
2014-12-05 23:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *testMsgReadWriter) In(msg p2p.Msg) {
|
|
|
|
self.in <- msg
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
func (self *testMsgReadWriter) Out() (msg p2p.Msg, ok bool) {
|
|
|
|
if len(self.out) > 0 {
|
|
|
|
msg = self.out[0]
|
|
|
|
self.out = self.out[1:]
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
return
|
2014-12-05 23:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *testMsgReadWriter) WriteMsg(msg p2p.Msg) error {
|
2015-01-05 18:10:42 +02:00
|
|
|
self.out = append(self.out, msg)
|
2014-12-05 23:14:55 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *testMsgReadWriter) ReadMsg() (p2p.Msg, error) {
|
|
|
|
msg, ok := <-self.in
|
|
|
|
if !ok {
|
|
|
|
return msg, io.EOF
|
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
type testTxPool struct {
|
2014-12-05 23:14:55 +02:00
|
|
|
getTransactions func() []*types.Transaction
|
|
|
|
addTransactions func(txs []*types.Transaction)
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
type testChainManager struct {
|
|
|
|
getBlockHashes func(hash []byte, amount uint64) (hashes [][]byte)
|
|
|
|
getBlock func(hash []byte) *types.Block
|
|
|
|
status func() (td *big.Int, currentBlock []byte, genesisBlock []byte)
|
2014-12-05 23:14:55 +02:00
|
|
|
}
|
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
type testBlockPool struct {
|
|
|
|
addBlockHashes func(next func() ([]byte, bool), peerId string)
|
|
|
|
addBlock func(block *types.Block, peerId string) (err error)
|
2015-02-25 15:06:59 +02:00
|
|
|
addPeer func(td *big.Int, currentBlock []byte, peerId string, requestHashes func([]byte) error, requestBlocks func([][]byte) error, peerError func(*errs.Error)) (best bool)
|
2015-01-05 18:10:42 +02:00
|
|
|
removePeer func(peerId string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// func (self *testTxPool) GetTransactions() (txs []*types.Transaction) {
|
|
|
|
// if self.getTransactions != nil {
|
|
|
|
// txs = self.getTransactions()
|
|
|
|
// }
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (self *testTxPool) AddTransactions(txs []*types.Transaction) {
|
2014-12-05 23:14:55 +02:00
|
|
|
if self.addTransactions != nil {
|
|
|
|
self.addTransactions(txs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 03:35:49 +02:00
|
|
|
func (self *testTxPool) GetTransactions() types.Transactions { return nil }
|
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
func (self *testChainManager) GetBlockHashesFromHash(hash []byte, amount uint64) (hashes [][]byte) {
|
2014-12-05 23:14:55 +02:00
|
|
|
if self.getBlockHashes != nil {
|
|
|
|
hashes = self.getBlockHashes(hash, amount)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
func (self *testChainManager) Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) {
|
|
|
|
if self.status != nil {
|
|
|
|
td, currentBlock, genesisBlock = self.status()
|
2014-12-05 23:14:55 +02:00
|
|
|
}
|
2015-01-05 18:10:42 +02:00
|
|
|
return
|
2014-12-05 23:14:55 +02:00
|
|
|
}
|
2014-12-10 01:55:50 +02:00
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
func (self *testChainManager) GetBlock(hash []byte) (block *types.Block) {
|
2014-12-05 23:14:55 +02:00
|
|
|
if self.getBlock != nil {
|
|
|
|
block = self.getBlock(hash)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
func (self *testBlockPool) AddBlockHashes(next func() ([]byte, bool), peerId string) {
|
|
|
|
if self.addBlockHashes != nil {
|
|
|
|
self.addBlockHashes(next, peerId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *testBlockPool) AddBlock(block *types.Block, peerId string) {
|
2014-12-10 01:55:50 +02:00
|
|
|
if self.addBlock != nil {
|
2015-01-05 18:10:42 +02:00
|
|
|
self.addBlock(block, peerId)
|
2014-12-05 23:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 15:06:59 +02:00
|
|
|
func (self *testBlockPool) AddPeer(td *big.Int, currentBlock []byte, peerId string, requestBlockHashes func([]byte) error, requestBlocks func([][]byte) error, peerError func(*errs.Error)) (best bool) {
|
2014-12-05 23:14:55 +02:00
|
|
|
if self.addPeer != nil {
|
2015-01-05 18:10:42 +02:00
|
|
|
best = self.addPeer(td, currentBlock, peerId, requestBlockHashes, requestBlocks, peerError)
|
2014-12-05 23:14:55 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
func (self *testBlockPool) RemovePeer(peerId string) {
|
2014-12-10 01:55:50 +02:00
|
|
|
if self.removePeer != nil {
|
|
|
|
self.removePeer(peerId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPeer() *p2p.Peer {
|
2015-02-05 04:16:16 +02:00
|
|
|
var id discover.NodeID
|
|
|
|
pk := crypto.GenerateNewKeyPair().PublicKey
|
|
|
|
copy(id[:], pk)
|
|
|
|
return p2p.NewPeer(id, "test peer", []p2p.Cap{})
|
2014-12-10 01:55:50 +02:00
|
|
|
}
|
|
|
|
|
2015-01-05 18:10:42 +02:00
|
|
|
type ethProtocolTester struct {
|
|
|
|
quit chan error
|
|
|
|
rw *testMsgReadWriter // p2p.MsgReadWriter
|
|
|
|
txPool *testTxPool // txPool
|
|
|
|
chainManager *testChainManager // chainManager
|
|
|
|
blockPool *testBlockPool // blockPool
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEth(t *testing.T) *ethProtocolTester {
|
|
|
|
return ðProtocolTester{
|
|
|
|
quit: make(chan error),
|
|
|
|
rw: &testMsgReadWriter{in: make(chan p2p.Msg, 10)},
|
|
|
|
txPool: &testTxPool{},
|
|
|
|
chainManager: &testChainManager{},
|
|
|
|
blockPool: &testBlockPool{},
|
|
|
|
t: t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *ethProtocolTester) reset() {
|
|
|
|
self.rw = &testMsgReadWriter{in: make(chan p2p.Msg, 10)}
|
|
|
|
self.quit = make(chan error)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-02-25 15:06:59 +02:00
|
|
|
perr, ok := err.(*errs.Error)
|
2015-01-05 18:10:42 +02:00
|
|
|
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) In(msg p2p.Msg) {
|
|
|
|
self.rw.In(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *ethProtocolTester) Out() (p2p.Msg, bool) {
|
|
|
|
return self.rw.Out()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *ethProtocolTester) checkMsg(i int, code uint64, val interface{}) (msg p2p.Msg) {
|
|
|
|
if i >= len(self.rw.out) {
|
|
|
|
self.t.Errorf("expected at least %v msgs, got %v", i, len(self.rw.out))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg = self.rw.out[i]
|
|
|
|
if msg.Code != code {
|
|
|
|
self.t.Errorf("expected msg code %v, got %v", code, msg.Code)
|
|
|
|
}
|
|
|
|
if val != nil {
|
|
|
|
if err := msg.Decode(val); err != nil {
|
|
|
|
self.t.Errorf("rlp encoding error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *ethProtocolTester) run() {
|
|
|
|
err := runEthProtocol(self.txPool, self.chainManager, self.blockPool, testPeer(), self.rw)
|
|
|
|
self.quit <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusMsgErrors(t *testing.T) {
|
|
|
|
logInit()
|
|
|
|
eth := newEth(t)
|
|
|
|
td := ethutil.Big1
|
|
|
|
currentBlock := []byte{1}
|
|
|
|
genesis := []byte{2}
|
|
|
|
eth.chainManager.status = func() (*big.Int, []byte, []byte) { return td, currentBlock, genesis }
|
|
|
|
go eth.run()
|
2014-12-05 23:14:55 +02:00
|
|
|
statusMsg := p2p.NewMsg(4)
|
2015-01-05 18:10:42 +02:00
|
|
|
eth.In(statusMsg)
|
|
|
|
delay := 1 * time.Second
|
|
|
|
eth.checkError(ErrNoStatusMsg, delay)
|
|
|
|
var status statusMsgData
|
|
|
|
eth.checkMsg(0, StatusMsg, &status) // first outgoing msg should be StatusMsg
|
|
|
|
if status.TD.Cmp(td) != 0 ||
|
|
|
|
status.ProtocolVersion != ProtocolVersion ||
|
|
|
|
status.NetworkId != NetworkId ||
|
|
|
|
status.TD.Cmp(td) != 0 ||
|
|
|
|
bytes.Compare(status.CurrentBlock, currentBlock) != 0 ||
|
|
|
|
bytes.Compare(status.GenesisBlock, genesis) != 0 {
|
|
|
|
t.Errorf("incorrect outgoing status")
|
|
|
|
}
|
|
|
|
|
|
|
|
eth.reset()
|
|
|
|
go eth.run()
|
|
|
|
statusMsg = p2p.NewMsg(0, uint32(48), uint32(0), td, currentBlock, genesis)
|
|
|
|
eth.In(statusMsg)
|
|
|
|
eth.checkError(ErrProtocolVersionMismatch, delay)
|
|
|
|
|
|
|
|
eth.reset()
|
|
|
|
go eth.run()
|
|
|
|
statusMsg = p2p.NewMsg(0, uint32(49), uint32(1), td, currentBlock, genesis)
|
|
|
|
eth.In(statusMsg)
|
|
|
|
eth.checkError(ErrNetworkIdMismatch, delay)
|
|
|
|
|
|
|
|
eth.reset()
|
|
|
|
go eth.run()
|
|
|
|
statusMsg = p2p.NewMsg(0, uint32(49), uint32(0), td, currentBlock, []byte{3})
|
|
|
|
eth.In(statusMsg)
|
|
|
|
eth.checkError(ErrGenesisBlockMismatch, delay)
|
|
|
|
|
2014-12-05 23:14:55 +02:00
|
|
|
}
|