2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2014-12-05 21:14:55 +00:00
|
|
|
package eth
|
|
|
|
|
2015-06-09 10:10:19 +00:00
|
|
|
import (
|
2015-07-02 16:55:18 +00:00
|
|
|
"fmt"
|
2015-06-09 10:10:19 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2015-07-02 16:55:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2015-06-09 10:10:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-02-22 12:10:07 +00:00
|
|
|
// log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat())))
|
2015-06-09 10:10:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 12:57:15 +00:00
|
|
|
var testAccount, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
2015-03-28 00:48:37 +00:00
|
|
|
|
2015-07-02 16:55:18 +00:00
|
|
|
// Tests that handshake failures are detected and reported correctly.
|
|
|
|
func TestStatusMsgErrors62(t *testing.T) { testStatusMsgErrors(t, 62) }
|
|
|
|
func TestStatusMsgErrors63(t *testing.T) { testStatusMsgErrors(t, 63) }
|
|
|
|
|
|
|
|
func testStatusMsgErrors(t *testing.T, protocol int) {
|
2015-10-09 15:36:31 +00:00
|
|
|
pm := newTestProtocolManagerMust(t, false, 0, nil, nil)
|
2015-08-31 15:09:50 +00:00
|
|
|
td, currentBlock, genesis := pm.blockchain.Status()
|
2015-06-09 10:10:19 +00:00
|
|
|
defer pm.Stop()
|
2015-01-05 16:10:42 +00:00
|
|
|
|
2015-03-19 14:18:31 +00:00
|
|
|
tests := []struct {
|
2015-06-09 10:10:19 +00:00
|
|
|
code uint64
|
|
|
|
data interface{}
|
|
|
|
wantError error
|
2015-03-19 14:18:31 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
code: TxMsg, data: []interface{}{},
|
2015-06-09 10:10:19 +00:00
|
|
|
wantError: errResp(ErrNoStatusMsg, "first msg has code 2 (!= 0)"),
|
2015-03-19 14:18:31 +00:00
|
|
|
},
|
|
|
|
{
|
2015-06-29 14:32:14 +00:00
|
|
|
code: StatusMsg, data: statusData{10, NetworkId, td, currentBlock, genesis},
|
2015-07-02 16:55:18 +00:00
|
|
|
wantError: errResp(ErrProtocolVersionMismatch, "10 (!= %d)", protocol),
|
2015-03-19 14:18:31 +00:00
|
|
|
},
|
|
|
|
{
|
2015-07-02 16:55:18 +00:00
|
|
|
code: StatusMsg, data: statusData{uint32(protocol), 999, td, currentBlock, genesis},
|
2015-07-22 11:41:31 +00:00
|
|
|
wantError: errResp(ErrNetworkIdMismatch, "999 (!= 1)"),
|
2015-03-19 14:18:31 +00:00
|
|
|
},
|
|
|
|
{
|
2015-07-02 16:55:18 +00:00
|
|
|
code: StatusMsg, data: statusData{uint32(protocol), NetworkId, td, currentBlock, common.Hash{3}},
|
2015-06-09 10:10:19 +00:00
|
|
|
wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000000000000000000000000000000000000000000000000000 (!= %x)", genesis),
|
2015-03-19 14:18:31 +00:00
|
|
|
},
|
|
|
|
}
|
2015-06-09 10:10:19 +00:00
|
|
|
|
|
|
|
for i, test := range tests {
|
2015-07-02 16:55:18 +00:00
|
|
|
p, errc := newTestPeer("peer", protocol, pm, false)
|
2015-06-09 10:10:19 +00:00
|
|
|
// The send call might hang until reset because
|
2015-03-19 14:18:31 +00:00
|
|
|
// the protocol might not read the payload.
|
2015-07-02 16:55:18 +00:00
|
|
|
go p2p.Send(p.app, test.code, test.data)
|
2015-01-05 16:10:42 +00:00
|
|
|
|
2015-06-09 10:10:19 +00:00
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if err == nil {
|
2016-04-15 09:06:57 +00:00
|
|
|
t.Errorf("test %d: protocol returned nil error, want %q", i, test.wantError)
|
2015-06-09 10:10:19 +00:00
|
|
|
} 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):
|
2017-01-06 17:44:35 +00:00
|
|
|
t.Errorf("protocol did not shut down within 2 seconds")
|
2015-06-09 10:10:19 +00:00
|
|
|
}
|
|
|
|
p.close()
|
2015-03-19 14:18:31 +00:00
|
|
|
}
|
2014-12-05 21:14:55 +00:00
|
|
|
}
|
2015-03-28 00:48:37 +00:00
|
|
|
|
2015-06-09 10:10:19 +00:00
|
|
|
// This test checks that received transactions are added to the local pool.
|
2015-07-02 16:55:18 +00:00
|
|
|
func TestRecvTransactions62(t *testing.T) { testRecvTransactions(t, 62) }
|
|
|
|
func TestRecvTransactions63(t *testing.T) { testRecvTransactions(t, 63) }
|
|
|
|
|
|
|
|
func testRecvTransactions(t *testing.T, protocol int) {
|
2015-06-09 10:10:19 +00:00
|
|
|
txAdded := make(chan []*types.Transaction)
|
2015-10-09 15:36:31 +00:00
|
|
|
pm := newTestProtocolManagerMust(t, false, 0, nil, txAdded)
|
2016-06-02 12:54:07 +00:00
|
|
|
pm.synced = 1 // mark synced to accept transactions
|
2015-07-02 16:55:18 +00:00
|
|
|
p, _ := newTestPeer("peer", protocol, pm, true)
|
2015-06-09 10:10:19 +00:00
|
|
|
defer pm.Stop()
|
|
|
|
defer p.close()
|
2015-03-28 00:48:37 +00:00
|
|
|
|
2015-07-02 16:55:18 +00:00
|
|
|
tx := newTestTransaction(testAccount, 0, 0)
|
|
|
|
if err := p2p.Send(p.app, TxMsg, []interface{}{tx}); err != nil {
|
2015-06-09 10:10:19 +00:00
|
|
|
t.Fatalf("send error: %v", err)
|
2015-03-28 00:48:37 +00:00
|
|
|
}
|
|
|
|
select {
|
2015-06-09 10:10:19 +00: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-28 00:48:37 +00:00
|
|
|
}
|
2015-06-09 10:10:19 +00:00
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Errorf("no TxPreEvent received within 2 seconds")
|
2015-03-28 00:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-28 01:54:23 +00:00
|
|
|
|
2015-06-09 10:10:19 +00:00
|
|
|
// This test checks that pending transactions are sent.
|
2015-07-02 16:55:18 +00:00
|
|
|
func TestSendTransactions62(t *testing.T) { testSendTransactions(t, 62) }
|
|
|
|
func TestSendTransactions63(t *testing.T) { testSendTransactions(t, 63) }
|
|
|
|
|
|
|
|
func testSendTransactions(t *testing.T, protocol int) {
|
2015-10-09 15:36:31 +00:00
|
|
|
pm := newTestProtocolManagerMust(t, false, 0, nil, nil)
|
2015-06-09 10:10:19 +00:00
|
|
|
defer pm.Stop()
|
2015-03-28 01:54:23 +00:00
|
|
|
|
2015-06-09 10:10:19 +00:00
|
|
|
// Fill the pool with big transactions.
|
|
|
|
const txsize = txsyncPackSize / 10
|
|
|
|
alltxs := make([]*types.Transaction, 100)
|
|
|
|
for nonce := range alltxs {
|
2015-07-02 16:55:18 +00:00
|
|
|
alltxs[nonce] = newTestTransaction(testAccount, uint64(nonce), txsize)
|
2015-03-28 01:54:23 +00:00
|
|
|
}
|
2016-07-01 15:59:55 +00:00
|
|
|
pm.txpool.AddBatch(alltxs)
|
2015-03-28 01:54:23 +00:00
|
|
|
|
2015-06-09 10:10:19 +00: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
|
2015-07-02 16:55:18 +00:00
|
|
|
msg, err := p.app.ReadMsg()
|
2015-06-09 10:10:19 +00:00
|
|
|
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-28 01:54:23 +00:00
|
|
|
}
|
2015-06-09 10:10:19 +00: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 12:48:07 +00:00
|
|
|
}
|
2015-03-28 01:54:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 10:10:19 +00:00
|
|
|
for i := 0; i < 3; i++ {
|
2015-07-02 16:55:18 +00:00
|
|
|
p, _ := newTestPeer(fmt.Sprintf("peer #%d", i), protocol, pm, true)
|
2015-06-09 10:10:19 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go checktxs(p)
|
2015-03-30 12:48:07 +00:00
|
|
|
}
|
2015-06-09 10:10:19 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|
2015-03-30 12:48:07 +00:00
|
|
|
|
2015-07-02 16:55:18 +00:00
|
|
|
// Tests that the custom union field encoder and decoder works correctly.
|
|
|
|
func TestGetBlockHeadersDataEncodeDecode(t *testing.T) {
|
|
|
|
// Create a "random" hash for testing
|
|
|
|
var hash common.Hash
|
2017-01-06 14:52:03 +00:00
|
|
|
for i := range hash {
|
2015-07-02 16:55:18 +00:00
|
|
|
hash[i] = byte(i)
|
2015-06-09 10:10:19 +00:00
|
|
|
}
|
2015-07-02 16:55:18 +00:00
|
|
|
// Assemble some table driven tests
|
|
|
|
tests := []struct {
|
|
|
|
packet *getBlockHeadersData
|
|
|
|
fail bool
|
|
|
|
}{
|
|
|
|
// Providing the origin as either a hash or a number should both work
|
|
|
|
{fail: false, packet: &getBlockHeadersData{Origin: hashOrNumber{Number: 314}}},
|
|
|
|
{fail: false, packet: &getBlockHeadersData{Origin: hashOrNumber{Hash: hash}}},
|
2015-03-30 14:21:41 +00:00
|
|
|
|
2015-07-02 16:55:18 +00:00
|
|
|
// Providing arbitrary query field should also work
|
|
|
|
{fail: false, packet: &getBlockHeadersData{Origin: hashOrNumber{Number: 314}, Amount: 314, Skip: 1, Reverse: true}},
|
|
|
|
{fail: false, packet: &getBlockHeadersData{Origin: hashOrNumber{Hash: hash}, Amount: 314, Skip: 1, Reverse: true}},
|
2015-06-09 10:10:19 +00:00
|
|
|
|
2015-07-02 16:55:18 +00:00
|
|
|
// Providing both the origin hash and origin number must fail
|
|
|
|
{fail: true, packet: &getBlockHeadersData{Origin: hashOrNumber{Hash: hash, Number: 314}}},
|
|
|
|
}
|
|
|
|
// Iterate over each of the tests and try to encode and then decode
|
|
|
|
for i, tt := range tests {
|
|
|
|
bytes, err := rlp.EncodeToBytes(tt.packet)
|
|
|
|
if err != nil && !tt.fail {
|
|
|
|
t.Fatalf("test %d: failed to encode packet: %v", i, err)
|
|
|
|
} else if err == nil && tt.fail {
|
|
|
|
t.Fatalf("test %d: encode should have failed", i)
|
|
|
|
}
|
|
|
|
if !tt.fail {
|
|
|
|
packet := new(getBlockHeadersData)
|
|
|
|
if err := rlp.DecodeBytes(bytes, packet); err != nil {
|
|
|
|
t.Fatalf("test %d: failed to decode packet: %v", i, err)
|
|
|
|
}
|
|
|
|
if packet.Origin.Hash != tt.packet.Origin.Hash || packet.Origin.Number != tt.packet.Origin.Number || packet.Amount != tt.packet.Amount ||
|
|
|
|
packet.Skip != tt.packet.Skip || packet.Reverse != tt.packet.Reverse {
|
|
|
|
t.Fatalf("test %d: encode decode mismatch: have %+v, want %+v", i, packet, tt.packet)
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 14:21:41 +00:00
|
|
|
}
|
|
|
|
}
|