good morning!!!!

Skip to content
Snippets Groups Projects
protocol_test.go 7.37 KiB
Newer Older
  • Learn to ignore specific revisions
  • // Copyright 2014 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/>.
    
    Felix Lange's avatar
    Felix Lange committed
    import (
    
    Felix Lange's avatar
    Felix Lange committed
    	"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"
    
    	"github.com/ethereum/go-ethereum/rlp"
    
    Felix Lange's avatar
    Felix Lange committed
    )
    
    func init() {
    	// glog.SetToStderr(true)
    	// glog.SetV(6)
    }
    
    
    var testAccount, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    
    // 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) {
    
    	pm := newTestProtocolManagerMust(t, false, 0, nil, nil)
    
    	td, currentBlock, genesis := pm.blockchain.Status()
    
    Felix Lange's avatar
    Felix Lange committed
    	defer pm.Stop()
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    
    
    Felix Lange's avatar
    Felix Lange committed
    		code      uint64
    		data      interface{}
    		wantError error
    
    	}{
    		{
    			code: TxMsg, data: []interface{}{},
    
    Felix Lange's avatar
    Felix Lange committed
    			wantError: errResp(ErrNoStatusMsg, "first msg has code 2 (!= 0)"),
    
    			code: StatusMsg, data: statusData{10, NetworkId, td, currentBlock, genesis},
    
    			wantError: errResp(ErrProtocolVersionMismatch, "10 (!= %d)", protocol),
    
    			code: StatusMsg, data: statusData{uint32(protocol), 999, td, currentBlock, genesis},
    
    			wantError: errResp(ErrNetworkIdMismatch, "999 (!= 1)"),
    
    			code: StatusMsg, data: statusData{uint32(protocol), NetworkId, td, currentBlock, common.Hash{3}},
    
    Felix Lange's avatar
    Felix Lange committed
    			wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000000000000000000000000000000000000000000000000000 (!= %x)", genesis),
    
    Felix Lange's avatar
    Felix Lange committed
    
    	for i, test := range tests {
    
    		p, errc := newTestPeer("peer", protocol, pm, false)
    
    Felix Lange's avatar
    Felix Lange committed
    		// The send call might hang until reset because
    
    		// the protocol might not read the payload.
    
    		go p2p.Send(p.app, test.code, test.data)
    
    Jeffrey Wilcke's avatar
    Jeffrey Wilcke committed
    
    
    Felix Lange's avatar
    Felix Lange committed
    		select {
    		case err := <-errc:
    			if err == nil {
    
    Felix Lange's avatar
    Felix Lange committed
    				t.Errorf("test %d: protocol returned nil error, want %q", i, test.wantError)
    
    Felix Lange's avatar
    Felix Lange committed
    			} 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()
    
    Felix Lange's avatar
    Felix Lange committed
    // This test checks that received transactions are added to the local pool.
    
    func TestRecvTransactions62(t *testing.T) { testRecvTransactions(t, 62) }
    func TestRecvTransactions63(t *testing.T) { testRecvTransactions(t, 63) }
    
    func testRecvTransactions(t *testing.T, protocol int) {
    
    Felix Lange's avatar
    Felix Lange committed
    	txAdded := make(chan []*types.Transaction)
    
    	pm := newTestProtocolManagerMust(t, false, 0, nil, txAdded)
    
    	pm.synced = 1 // mark synced to accept transactions
    
    	p, _ := newTestPeer("peer", protocol, pm, true)
    
    Felix Lange's avatar
    Felix Lange committed
    	defer pm.Stop()
    	defer p.close()
    
    	tx := newTestTransaction(testAccount, 0, 0)
    	if err := p2p.Send(p.app, TxMsg, []interface{}{tx}); err != nil {
    
    Felix Lange's avatar
    Felix Lange committed
    		t.Fatalf("send error: %v", err)
    
    Felix Lange's avatar
    Felix Lange committed
    	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())
    
    Felix Lange's avatar
    Felix Lange committed
    	case <-time.After(2 * time.Second):
    		t.Errorf("no TxPreEvent received within 2 seconds")
    
    Felix Lange's avatar
    Felix Lange committed
    // This test checks that pending transactions are sent.
    
    func TestSendTransactions62(t *testing.T) { testSendTransactions(t, 62) }
    func TestSendTransactions63(t *testing.T) { testSendTransactions(t, 63) }
    
    func testSendTransactions(t *testing.T, protocol int) {
    
    	pm := newTestProtocolManagerMust(t, false, 0, nil, nil)
    
    Felix Lange's avatar
    Felix Lange committed
    	defer pm.Stop()
    
    Felix Lange's avatar
    Felix Lange committed
    	// Fill the pool with big transactions.
    	const txsize = txsyncPackSize / 10
    	alltxs := make([]*types.Transaction, 100)
    	for nonce := range alltxs {
    
    		alltxs[nonce] = newTestTransaction(testAccount, uint64(nonce), txsize)
    
    Felix Lange's avatar
    Felix Lange committed
    	// 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
    
    Felix Lange's avatar
    Felix Lange committed
    			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)
    
    Felix Lange's avatar
    Felix Lange committed
    			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++
    
    Felix Lange's avatar
    Felix Lange committed
    	for i := 0; i < 3; i++ {
    
    		p, _ := newTestPeer(fmt.Sprintf("peer #%d", i), protocol, pm, true)
    
    Felix Lange's avatar
    Felix Lange committed
    		wg.Add(1)
    		go checktxs(p)
    
    Felix Lange's avatar
    Felix Lange committed
    	wg.Wait()
    }
    
    // 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
    	for i, _ := range hash {
    		hash[i] = byte(i)
    
    	// 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}}},
    
    Viktor Trón's avatar
    Viktor Trón committed
    
    
    		// 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}},
    
    		// 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)
    			}
    		}
    
    Viktor Trón's avatar
    Viktor Trón committed
    	}
    }