Skip to content

Commit 4811f46

Browse files
committed
p2p: export ExpectMsg (for eth protocol testing)
1 parent e13c673 commit 4811f46

2 files changed

Lines changed: 32 additions & 32 deletions

File tree

‎p2p/message.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,35 @@ func (p *MsgPipeRW) Close() error {
208208
close(p.closing)
209209
return nil
210210
}
211+
212+
// ExpectMsg reads a message from r and verifies that its
213+
// code and encoded RLP content match the provided values.
214+
// If content is nil, the payload is discarded and not verified.
215+
func ExpectMsg(r MsgReader, code uint64, content interface{}) error {
216+
msg, err := r.ReadMsg()
217+
if err != nil {
218+
return err
219+
}
220+
if msg.Code != code {
221+
return fmt.Errorf("message code mismatch: got %d, expected %d", msg.Code, code)
222+
}
223+
if content == nil {
224+
return msg.Discard()
225+
} else {
226+
contentEnc, err := rlp.EncodeToBytes(content)
227+
if err != nil {
228+
panic("content encode error: " + err.Error())
229+
}
230+
if int(msg.Size) != len(contentEnc) {
231+
return fmt.Errorf("message size mismatch: got %d, want %d", msg.Size, len(contentEnc))
232+
}
233+
actualContent, err := ioutil.ReadAll(msg.Payload)
234+
if err != nil {
235+
return err
236+
}
237+
if !bytes.Equal(actualContent, contentEnc) {
238+
return fmt.Errorf("message payload mismatch:\ngot: %x\nwant: %x", actualContent, contentEnc)
239+
}
240+
}
241+
return nil
242+
}

‎p2p/peer_test.go‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -192,35 +192,3 @@ func TestNewPeer(t *testing.T) {
192192

193193
p.Disconnect(DiscAlreadyConnected) // Should not hang
194194
}
195-
196-
// expectMsg reads a message from r and verifies that its
197-
// code and encoded RLP content match the provided values.
198-
// If content is nil, the payload is discarded and not verified.
199-
func expectMsg(r MsgReader, code uint64, content interface{}) error {
200-
msg, err := r.ReadMsg()
201-
if err != nil {
202-
return err
203-
}
204-
if msg.Code != code {
205-
return fmt.Errorf("message code mismatch: got %d, expected %d", msg.Code, code)
206-
}
207-
if content == nil {
208-
return msg.Discard()
209-
} else {
210-
contentEnc, err := rlp.EncodeToBytes(content)
211-
if err != nil {
212-
panic("content encode error: " + err.Error())
213-
}
214-
if int(msg.Size) != len(contentEnc) {
215-
return fmt.Errorf("message size mismatch: got %d, want %d", msg.Size, len(contentEnc))
216-
}
217-
actualContent, err := ioutil.ReadAll(msg.Payload)
218-
if err != nil {
219-
return err
220-
}
221-
if !bytes.Equal(actualContent, contentEnc) {
222-
return fmt.Errorf("message payload mismatch:\ngot: %x\nwant: %x", actualContent, contentEnc)
223-
}
224-
}
225-
return nil
226-
}

0 commit comments

Comments
 (0)