Skip to content

Commit ed7bf32

Browse files
renaynayatif-konasl
authored andcommitted
cmd/devp2p/internal/ethtest: run test suite as Go unit test (ethereum#22698)
This change adds a Go unit test that runs the protocol test suite against the go-ethereum implementation of the eth protocol.
1 parent 02796b9 commit ed7bf32

File tree

7 files changed

+269
-74
lines changed

7 files changed

+269
-74
lines changed

cmd/devp2p/internal/ethtest/chain.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
)
3535

3636
type Chain struct {
37+
genesis core.Genesis
3738
blocks []*types.Block
3839
chainConfig *params.ChainConfig
3940
}
@@ -124,16 +125,34 @@ func (c *Chain) GetHeaders(req GetBlockHeaders) (BlockHeaders, error) {
124125
// loadChain takes the given chain.rlp file, and decodes and returns
125126
// the blocks from the file.
126127
func loadChain(chainfile string, genesis string) (*Chain, error) {
127-
chainConfig, err := ioutil.ReadFile(genesis)
128+
gen, err := loadGenesis(genesis)
128129
if err != nil {
129130
return nil, err
130131
}
132+
gblock := gen.ToBlock(nil)
133+
134+
blocks, err := blocksFromFile(chainfile, gblock)
135+
if err != nil {
136+
return nil, err
137+
}
138+
139+
c := &Chain{genesis: gen, blocks: blocks, chainConfig: gen.Config}
140+
return c, nil
141+
}
142+
143+
func loadGenesis(genesisFile string) (core.Genesis, error) {
144+
chainConfig, err := ioutil.ReadFile(genesisFile)
145+
if err != nil {
146+
return core.Genesis{}, err
147+
}
131148
var gen core.Genesis
132149
if err := json.Unmarshal(chainConfig, &gen); err != nil {
133-
return nil, err
150+
return core.Genesis{}, err
134151
}
135-
gblock := gen.ToBlock(nil)
152+
return gen, nil
153+
}
136154

155+
func blocksFromFile(chainfile string, gblock *types.Block) ([]*types.Block, error) {
137156
// Load chain.rlp.
138157
fh, err := os.Open(chainfile)
139158
if err != nil {
@@ -161,7 +180,5 @@ func loadChain(chainfile string, genesis string) (*Chain, error) {
161180
}
162181
blocks = append(blocks, &b)
163182
}
164-
165-
c := &Chain{blocks: blocks, chainConfig: gen.Config}
166-
return c, nil
183+
return blocks, nil
167184
}

cmd/devp2p/internal/ethtest/eth66_suite.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (s *Suite) Is_66(t *utesting.T) {
4141
// make sure the chain head is correct.
4242
func (s *Suite) TestStatus_66(t *utesting.T) {
4343
conn := s.dial66(t)
44+
defer conn.Close()
4445
// get protoHandshake
4546
conn.handshake(t)
4647
// get status
@@ -60,6 +61,7 @@ func (s *Suite) TestStatus_66(t *utesting.T) {
6061
// an eth66 `GetBlockHeaders` request and that the response is accurate.
6162
func (s *Suite) TestGetBlockHeaders_66(t *utesting.T) {
6263
conn := s.setupConnection66(t)
64+
defer conn.Close()
6365
// get block headers
6466
req := &eth.GetBlockHeadersPacket66{
6567
RequestId: 3,
@@ -84,6 +86,8 @@ func (s *Suite) TestGetBlockHeaders_66(t *utesting.T) {
8486
func (s *Suite) TestSimultaneousRequests_66(t *utesting.T) {
8587
// create two connections
8688
conn1, conn2 := s.setupConnection66(t), s.setupConnection66(t)
89+
defer conn1.Close()
90+
defer conn2.Close()
8791
// create two requests
8892
req1 := &eth.GetBlockHeadersPacket66{
8993
RequestId: 111,
@@ -122,6 +126,9 @@ func (s *Suite) TestSimultaneousRequests_66(t *utesting.T) {
122126
// propagated to the given node's peer(s) on the eth66 protocol.
123127
func (s *Suite) TestBroadcast_66(t *utesting.T) {
124128
sendConn, receiveConn := s.setupConnection66(t), s.setupConnection66(t)
129+
defer sendConn.Close()
130+
defer receiveConn.Close()
131+
125132
nextBlock := len(s.chain.blocks)
126133
blockAnnouncement := &NewBlock{
127134
Block: s.fullChain.blocks[nextBlock],
@@ -141,6 +148,7 @@ func (s *Suite) TestBroadcast_66(t *utesting.T) {
141148
// the eth66 protocol.
142149
func (s *Suite) TestGetBlockBodies_66(t *utesting.T) {
143150
conn := s.setupConnection66(t)
151+
defer conn.Close()
144152
// create block bodies request
145153
id := uint64(55)
146154
req := &eth.GetBlockBodiesPacket66{
@@ -195,17 +203,20 @@ func (s *Suite) TestLargeAnnounce_66(t *utesting.T) {
195203
t.Fatalf("could not write to connection: %v", err)
196204
}
197205
// Invalid announcement, check that peer disconnected
198-
switch msg := sendConn.ReadAndServe(s.chain, timeout).(type) {
206+
switch msg := sendConn.ReadAndServe(s.chain, time.Second*8).(type) {
199207
case *Disconnect:
200208
case *Error:
201209
break
202210
default:
203211
t.Fatalf("unexpected: %s wanted disconnect", pretty.Sdump(msg))
204212
}
213+
sendConn.Close()
205214
}
206215
// Test the last block as a valid block
207-
sendConn := s.setupConnection66(t)
208-
receiveConn := s.setupConnection66(t)
216+
sendConn, receiveConn := s.setupConnection66(t), s.setupConnection66(t)
217+
defer sendConn.Close()
218+
defer receiveConn.Close()
219+
209220
s.testAnnounce66(t, sendConn, receiveConn, blocks[3])
210221
// update test suite chain
211222
s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
@@ -216,12 +227,17 @@ func (s *Suite) TestLargeAnnounce_66(t *utesting.T) {
216227
}
217228

218229
func (s *Suite) TestOldAnnounce_66(t *utesting.T) {
219-
s.oldAnnounce(t, s.setupConnection66(t), s.setupConnection66(t))
230+
sendConn, recvConn := s.setupConnection66(t), s.setupConnection66(t)
231+
defer sendConn.Close()
232+
defer recvConn.Close()
233+
234+
s.oldAnnounce(t, sendConn, recvConn)
220235
}
221236

222237
// TestMaliciousHandshake_66 tries to send malicious data during the handshake.
223238
func (s *Suite) TestMaliciousHandshake_66(t *utesting.T) {
224239
conn := s.dial66(t)
240+
defer conn.Close()
225241
// write hello to client
226242
pub0 := crypto.FromECDSAPub(&conn.ourKey.PublicKey)[1:]
227243
handshakes := []*Hello{
@@ -295,6 +311,7 @@ func (s *Suite) TestMaliciousHandshake_66(t *utesting.T) {
295311
// TestMaliciousStatus_66 sends a status package with a large total difficulty.
296312
func (s *Suite) TestMaliciousStatus_66(t *utesting.T) {
297313
conn := s.dial66(t)
314+
defer conn.Close()
298315
// get protoHandshake
299316
conn.handshake(t)
300317
status := &Status{
@@ -334,23 +351,37 @@ func (s *Suite) TestTransaction_66(t *utesting.T) {
334351
}
335352

336353
func (s *Suite) TestMaliciousTx_66(t *utesting.T) {
337-
tests := []*types.Transaction{
354+
badTxs := []*types.Transaction{
338355
getOldTxFromChain(t, s),
339356
invalidNonceTx(t, s),
340357
hugeAmount(t, s),
341358
hugeGasPrice(t, s),
342359
hugeData(t, s),
343360
}
344-
for i, tx := range tests {
361+
sendConn := s.setupConnection66(t)
362+
defer sendConn.Close()
363+
// set up receiving connection before sending txs to make sure
364+
// no announcements are missed
365+
recvConn := s.setupConnection66(t)
366+
defer recvConn.Close()
367+
368+
for i, tx := range badTxs {
345369
t.Logf("Testing malicious tx propagation: %v\n", i)
346-
sendFailingTx66(t, s, tx)
370+
if err := sendConn.Write(&Transactions{tx}); err != nil {
371+
t.Fatalf("could not write to connection: %v", err)
372+
}
373+
347374
}
375+
// check to make sure bad txs aren't propagated
376+
waitForTxPropagation(t, s, badTxs, recvConn)
348377
}
349378

350379
// TestZeroRequestID_66 checks that a request ID of zero is still handled
351380
// by the node.
352381
func (s *Suite) TestZeroRequestID_66(t *utesting.T) {
353382
conn := s.setupConnection66(t)
383+
defer conn.Close()
384+
354385
req := &eth.GetBlockHeadersPacket66{
355386
RequestId: 0,
356387
GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
@@ -367,6 +398,7 @@ func (s *Suite) TestZeroRequestID_66(t *utesting.T) {
367398
// concurrently to a single node.
368399
func (s *Suite) TestSameRequestID_66(t *utesting.T) {
369400
conn := s.setupConnection66(t)
401+
defer conn.Close()
370402
// create two separate requests with same ID
371403
reqID := uint64(1234)
372404
req1 := &eth.GetBlockHeadersPacket66{

cmd/devp2p/internal/ethtest/eth66_suiteHelpers.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,10 @@ func (c *Conn) waitForBlock66(block *types.Block) error {
238238

239239
func sendSuccessfulTx66(t *utesting.T, s *Suite, tx *types.Transaction) {
240240
sendConn := s.setupConnection66(t)
241+
defer sendConn.Close()
241242
sendSuccessfulTxWithConn(t, s, tx, sendConn)
242243
}
243244

244-
func sendFailingTx66(t *utesting.T, s *Suite, tx *types.Transaction) {
245-
sendConn, recvConn := s.setupConnection66(t), s.setupConnection66(t)
246-
sendFailingTxWithConns(t, s, tx, sendConn, recvConn)
247-
}
248-
249245
func (s *Suite) getBlockHeaders66(t *utesting.T, conn *Conn, req eth.Packet, expectedID uint64) BlockHeaders {
250246
if err := conn.write66(req, GetBlockHeaders{}.Code()); err != nil {
251247
t.Fatalf("could not write to connection: %v", err)

0 commit comments

Comments
 (0)