Skip to content

Commit

Permalink
cmd/devp2p/internal/ethtest: run test suite as Go unit test (#22698)
Browse files Browse the repository at this point in the history
This change adds a Go unit test that runs the protocol test suite
against the go-ethereum implementation of the eth protocol.
  • Loading branch information
renaynay authored Apr 23, 2021
1 parent 1fb9a6d commit ea54c58
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 74 deletions.
29 changes: 23 additions & 6 deletions cmd/devp2p/internal/ethtest/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
)

type Chain struct {
genesis core.Genesis
blocks []*types.Block
chainConfig *params.ChainConfig
}
Expand Down Expand Up @@ -124,16 +125,34 @@ func (c *Chain) GetHeaders(req GetBlockHeaders) (BlockHeaders, error) {
// loadChain takes the given chain.rlp file, and decodes and returns
// the blocks from the file.
func loadChain(chainfile string, genesis string) (*Chain, error) {
chainConfig, err := ioutil.ReadFile(genesis)
gen, err := loadGenesis(genesis)
if err != nil {
return nil, err
}
gblock := gen.ToBlock(nil)

blocks, err := blocksFromFile(chainfile, gblock)
if err != nil {
return nil, err
}

c := &Chain{genesis: gen, blocks: blocks, chainConfig: gen.Config}
return c, nil
}

func loadGenesis(genesisFile string) (core.Genesis, error) {
chainConfig, err := ioutil.ReadFile(genesisFile)
if err != nil {
return core.Genesis{}, err
}
var gen core.Genesis
if err := json.Unmarshal(chainConfig, &gen); err != nil {
return nil, err
return core.Genesis{}, err
}
gblock := gen.ToBlock(nil)
return gen, nil
}

func blocksFromFile(chainfile string, gblock *types.Block) ([]*types.Block, error) {
// Load chain.rlp.
fh, err := os.Open(chainfile)
if err != nil {
Expand Down Expand Up @@ -161,7 +180,5 @@ func loadChain(chainfile string, genesis string) (*Chain, error) {
}
blocks = append(blocks, &b)
}

c := &Chain{blocks: blocks, chainConfig: gen.Config}
return c, nil
return blocks, nil
}
46 changes: 39 additions & 7 deletions cmd/devp2p/internal/ethtest/eth66_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (s *Suite) Is_66(t *utesting.T) {
// make sure the chain head is correct.
func (s *Suite) TestStatus_66(t *utesting.T) {
conn := s.dial66(t)
defer conn.Close()
// get protoHandshake
conn.handshake(t)
// get status
Expand All @@ -60,6 +61,7 @@ func (s *Suite) TestStatus_66(t *utesting.T) {
// an eth66 `GetBlockHeaders` request and that the response is accurate.
func (s *Suite) TestGetBlockHeaders_66(t *utesting.T) {
conn := s.setupConnection66(t)
defer conn.Close()
// get block headers
req := &eth.GetBlockHeadersPacket66{
RequestId: 3,
Expand All @@ -84,6 +86,8 @@ func (s *Suite) TestGetBlockHeaders_66(t *utesting.T) {
func (s *Suite) TestSimultaneousRequests_66(t *utesting.T) {
// create two connections
conn1, conn2 := s.setupConnection66(t), s.setupConnection66(t)
defer conn1.Close()
defer conn2.Close()
// create two requests
req1 := &eth.GetBlockHeadersPacket66{
RequestId: 111,
Expand Down Expand Up @@ -122,6 +126,9 @@ func (s *Suite) TestSimultaneousRequests_66(t *utesting.T) {
// propagated to the given node's peer(s) on the eth66 protocol.
func (s *Suite) TestBroadcast_66(t *utesting.T) {
sendConn, receiveConn := s.setupConnection66(t), s.setupConnection66(t)
defer sendConn.Close()
defer receiveConn.Close()

nextBlock := len(s.chain.blocks)
blockAnnouncement := &NewBlock{
Block: s.fullChain.blocks[nextBlock],
Expand All @@ -141,6 +148,7 @@ func (s *Suite) TestBroadcast_66(t *utesting.T) {
// the eth66 protocol.
func (s *Suite) TestGetBlockBodies_66(t *utesting.T) {
conn := s.setupConnection66(t)
defer conn.Close()
// create block bodies request
id := uint64(55)
req := &eth.GetBlockBodiesPacket66{
Expand Down Expand Up @@ -195,17 +203,20 @@ func (s *Suite) TestLargeAnnounce_66(t *utesting.T) {
t.Fatalf("could not write to connection: %v", err)
}
// Invalid announcement, check that peer disconnected
switch msg := sendConn.ReadAndServe(s.chain, timeout).(type) {
switch msg := sendConn.ReadAndServe(s.chain, time.Second*8).(type) {
case *Disconnect:
case *Error:
break
default:
t.Fatalf("unexpected: %s wanted disconnect", pretty.Sdump(msg))
}
sendConn.Close()
}
// Test the last block as a valid block
sendConn := s.setupConnection66(t)
receiveConn := s.setupConnection66(t)
sendConn, receiveConn := s.setupConnection66(t), s.setupConnection66(t)
defer sendConn.Close()
defer receiveConn.Close()

s.testAnnounce66(t, sendConn, receiveConn, blocks[3])
// update test suite chain
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
Expand All @@ -216,12 +227,17 @@ func (s *Suite) TestLargeAnnounce_66(t *utesting.T) {
}

func (s *Suite) TestOldAnnounce_66(t *utesting.T) {
s.oldAnnounce(t, s.setupConnection66(t), s.setupConnection66(t))
sendConn, recvConn := s.setupConnection66(t), s.setupConnection66(t)
defer sendConn.Close()
defer recvConn.Close()

s.oldAnnounce(t, sendConn, recvConn)
}

// TestMaliciousHandshake_66 tries to send malicious data during the handshake.
func (s *Suite) TestMaliciousHandshake_66(t *utesting.T) {
conn := s.dial66(t)
defer conn.Close()
// write hello to client
pub0 := crypto.FromECDSAPub(&conn.ourKey.PublicKey)[1:]
handshakes := []*Hello{
Expand Down Expand Up @@ -295,6 +311,7 @@ func (s *Suite) TestMaliciousHandshake_66(t *utesting.T) {
// TestMaliciousStatus_66 sends a status package with a large total difficulty.
func (s *Suite) TestMaliciousStatus_66(t *utesting.T) {
conn := s.dial66(t)
defer conn.Close()
// get protoHandshake
conn.handshake(t)
status := &Status{
Expand Down Expand Up @@ -334,23 +351,37 @@ func (s *Suite) TestTransaction_66(t *utesting.T) {
}

func (s *Suite) TestMaliciousTx_66(t *utesting.T) {
tests := []*types.Transaction{
badTxs := []*types.Transaction{
getOldTxFromChain(t, s),
invalidNonceTx(t, s),
hugeAmount(t, s),
hugeGasPrice(t, s),
hugeData(t, s),
}
for i, tx := range tests {
sendConn := s.setupConnection66(t)
defer sendConn.Close()
// set up receiving connection before sending txs to make sure
// no announcements are missed
recvConn := s.setupConnection66(t)
defer recvConn.Close()

for i, tx := range badTxs {
t.Logf("Testing malicious tx propagation: %v\n", i)
sendFailingTx66(t, s, tx)
if err := sendConn.Write(&Transactions{tx}); err != nil {
t.Fatalf("could not write to connection: %v", err)
}

}
// check to make sure bad txs aren't propagated
waitForTxPropagation(t, s, badTxs, recvConn)
}

// TestZeroRequestID_66 checks that a request ID of zero is still handled
// by the node.
func (s *Suite) TestZeroRequestID_66(t *utesting.T) {
conn := s.setupConnection66(t)
defer conn.Close()

req := &eth.GetBlockHeadersPacket66{
RequestId: 0,
GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
Expand All @@ -367,6 +398,7 @@ func (s *Suite) TestZeroRequestID_66(t *utesting.T) {
// concurrently to a single node.
func (s *Suite) TestSameRequestID_66(t *utesting.T) {
conn := s.setupConnection66(t)
defer conn.Close()
// create two separate requests with same ID
reqID := uint64(1234)
req1 := &eth.GetBlockHeadersPacket66{
Expand Down
6 changes: 1 addition & 5 deletions cmd/devp2p/internal/ethtest/eth66_suiteHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,10 @@ func (c *Conn) waitForBlock66(block *types.Block) error {

func sendSuccessfulTx66(t *utesting.T, s *Suite, tx *types.Transaction) {
sendConn := s.setupConnection66(t)
defer sendConn.Close()
sendSuccessfulTxWithConn(t, s, tx, sendConn)
}

func sendFailingTx66(t *utesting.T, s *Suite, tx *types.Transaction) {
sendConn, recvConn := s.setupConnection66(t), s.setupConnection66(t)
sendFailingTxWithConns(t, s, tx, sendConn, recvConn)
}

func (s *Suite) getBlockHeaders66(t *utesting.T, conn *Conn, req eth.Packet, expectedID uint64) BlockHeaders {
if err := conn.write66(req, GetBlockHeaders{}.Code()); err != nil {
t.Fatalf("could not write to connection: %v", err)
Expand Down
Loading

0 comments on commit ea54c58

Please sign in to comment.