Skip to content

Commit 7964f30

Browse files
committed
p2p: msg.Payload contains list data
With RLPx frames, the message code is contained in the frame and is no longer part of the encoded data. EncodeMsg, Msg.Decode have been updated to match. Code that decodes RLP directly from Msg.Payload will need to change.
1 parent 2164910 commit 7964f30

7 files changed

Lines changed: 25 additions & 75 deletions

File tree

‎p2p/message.go‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,16 @@ type Msg struct {
5151

5252
// NewMsg creates an RLP-encoded message with the given code.
5353
func NewMsg(code uint64, params ...interface{}) Msg {
54-
buf := new(bytes.Buffer)
55-
for _, p := range params {
56-
buf.Write(ethutil.Encode(p))
57-
}
58-
return Msg{Code: code, Size: uint32(buf.Len()), Payload: buf}
59-
}
60-
61-
func encodePayload(params ...interface{}) []byte {
62-
buf := new(bytes.Buffer)
63-
for _, p := range params {
64-
buf.Write(ethutil.Encode(p))
65-
}
66-
return buf.Bytes()
54+
p := bytes.NewReader(ethutil.Encode(params))
55+
return Msg{Code: code, Size: uint32(p.Len()), Payload: p}
6756
}
6857

6958
// Decode parse the RLP content of a message into
7059
// the given value, which must be a pointer.
7160
//
7261
// For the decoding rules, please see package rlp.
7362
func (msg Msg) Decode(val interface{}) error {
74-
s := rlp.NewListStream(msg.Payload, uint64(msg.Size))
75-
if err := s.Decode(val); err != nil {
63+
if err := rlp.Decode(msg.Payload, val); err != nil {
7664
return newPeerError(errInvalidMsg, "(code %#x) (size %d) %v", msg.Code, msg.Size, err)
7765
}
7866
return nil

‎p2p/message_test.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package p2p
22

33
import (
44
"bytes"
5+
"encoding/hex"
56
"fmt"
67
"io"
78
"io/ioutil"
89
"runtime"
10+
"strings"
911
"testing"
1012
"time"
1113
)
@@ -15,11 +17,11 @@ func TestNewMsg(t *testing.T) {
1517
if msg.Code != 3 {
1618
t.Errorf("incorrect code %d, want %d", msg.Code)
1719
}
18-
if msg.Size != 5 {
19-
t.Errorf("incorrect size %d, want %d", msg.Size, 5)
20+
expect := unhex("c50183303030")
21+
if msg.Size != uint32(len(expect)) {
22+
t.Errorf("incorrect size %d, want %d", msg.Size, len(expect))
2023
}
2124
pl, _ := ioutil.ReadAll(msg.Payload)
22-
expect := []byte{0x01, 0x83, 0x30, 0x30, 0x30}
2325
if !bytes.Equal(pl, expect) {
2426
t.Errorf("incorrect payload content, got %x, want %x", pl, expect)
2527
}
@@ -139,3 +141,11 @@ func TestEOFSignal(t *testing.T) {
139141
default:
140142
}
141143
}
144+
145+
func unhex(str string) []byte {
146+
b, err := hex.DecodeString(strings.Replace(str, "\n", "", -1))
147+
if err != nil {
148+
panic(fmt.Sprintf("invalid hex string: %q", str))
149+
}
150+
return b
151+
}

‎p2p/peer.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ func (p *Peer) handle(msg Msg) error {
193193
msg.Discard()
194194
go EncodeMsg(p.rw, pongMsg)
195195
case msg.Code == discMsg:
196-
var reason DiscReason
196+
var reason [1]DiscReason
197197
// no need to discard or for error checking, we'll close the
198198
// connection after this.
199199
rlp.Decode(msg.Payload, &reason)
200200
p.Disconnect(DiscRequested)
201-
return discRequestedError(reason)
201+
return discRequestedError(reason[0])
202202
case msg.Code < baseProtocolLength:
203203
// ignore other base protocol messages
204204
return msg.Discard()

‎p2p/peer_test.go‎

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -85,41 +85,6 @@ func TestPeerProtoReadMsg(t *testing.T) {
8585
}
8686
}
8787

88-
func TestPeerProtoReadLargeMsg(t *testing.T) {
89-
defer testlog(t).detach()
90-
91-
msgsize := uint32(10 * 1024 * 1024)
92-
done := make(chan struct{})
93-
proto := Protocol{
94-
Name: "a",
95-
Length: 5,
96-
Run: func(peer *Peer, rw MsgReadWriter) error {
97-
msg, err := rw.ReadMsg()
98-
if err != nil {
99-
t.Errorf("read error: %v", err)
100-
}
101-
if msg.Size != msgsize+4 {
102-
t.Errorf("incorrect msg.Size, got %d, expected %d", msg.Size, msgsize)
103-
}
104-
msg.Discard()
105-
close(done)
106-
return nil
107-
},
108-
}
109-
110-
closer, rw, _, errc := testPeer([]Protocol{proto})
111-
defer closer.Close()
112-
113-
EncodeMsg(rw, 18, make([]byte, msgsize))
114-
select {
115-
case <-done:
116-
case err := <-errc:
117-
t.Errorf("peer returned: %v", err)
118-
case <-time.After(2 * time.Second):
119-
t.Errorf("receive timeout")
120-
}
121-
}
122-
12388
func TestPeerProtoEncodeMsg(t *testing.T) {
12489
defer testlog(t).detach()
12590

@@ -246,13 +211,9 @@ func expectMsg(r MsgReader, code uint64, content interface{}) error {
246211
if err != nil {
247212
panic("content encode error: " + err.Error())
248213
}
249-
// skip over list header in encoded value. this is temporary.
250-
contentEncR := bytes.NewReader(contentEnc)
251-
if k, _, err := rlp.NewStream(contentEncR).Kind(); k != rlp.List || err != nil {
252-
panic("content must encode as RLP list")
214+
if int(msg.Size) != len(contentEnc) {
215+
return fmt.Errorf("message size mismatch: got %d, want %d", msg.Size, len(contentEnc))
253216
}
254-
contentEnc = contentEnc[len(contentEnc)-contentEncR.Len():]
255-
256217
actualContent, err := ioutil.ReadAll(msg.Payload)
257218
if err != nil {
258219
return err

‎p2p/rlpx_test.go‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package p2p
33
import (
44
"bytes"
55
"crypto/rand"
6-
"encoding/hex"
7-
"fmt"
86
"io/ioutil"
97
"strings"
108
"testing"
@@ -32,7 +30,7 @@ ba628a4ba590cb43f7848f41c4382885
3230
`)
3331

3432
// Check WriteMsg. This puts a message into the buffer.
35-
if err := EncodeMsg(rw, 8, []interface{}{1, 2, 3, 4}); err != nil {
33+
if err := EncodeMsg(rw, 8, 1, 2, 3, 4); err != nil {
3634
t.Fatalf("WriteMsg error: %v", err)
3735
}
3836
written := buf.Bytes()
@@ -68,14 +66,6 @@ func (fakeHash) BlockSize() int { return 0 }
6866
func (h fakeHash) Size() int { return len(h) }
6967
func (h fakeHash) Sum(b []byte) []byte { return append(b, h...) }
7068

71-
func unhex(str string) []byte {
72-
b, err := hex.DecodeString(strings.Replace(str, "\n", "", -1))
73-
if err != nil {
74-
panic(fmt.Sprintf("invalid hex string: %q", str))
75-
}
76-
return b
77-
}
78-
7969
func TestRlpxFrameRW(t *testing.T) {
8070
var (
8171
aesSecret = make([]byte, 16)
@@ -112,7 +102,7 @@ func TestRlpxFrameRW(t *testing.T) {
112102
for i := 0; i < 10; i++ {
113103
// write message into conn buffer
114104
wmsg := []interface{}{"foo", "bar", strings.Repeat("test", i)}
115-
err := EncodeMsg(rw1, uint64(i), wmsg)
105+
err := EncodeMsg(rw1, uint64(i), wmsg...)
116106
if err != nil {
117107
t.Fatalf("WriteMsg error (i=%d): %v", i, err)
118108
}

‎p2p/server.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111
"time"
1212

13+
"github.com/ethereum/go-ethereum/ethutil"
1314
"github.com/ethereum/go-ethereum/logger"
1415
"github.com/ethereum/go-ethereum/p2p/discover"
1516
"github.com/ethereum/go-ethereum/p2p/nat"
@@ -135,7 +136,7 @@ func (srv *Server) SuggestPeer(n *discover.Node) {
135136
func (srv *Server) Broadcast(protocol string, code uint64, data ...interface{}) {
136137
var payload []byte
137138
if data != nil {
138-
payload = encodePayload(data...)
139+
payload = ethutil.Encode(data)
139140
}
140141
srv.lock.RLock()
141142
defer srv.lock.RUnlock()

‎p2p/server_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestServerBroadcast(t *testing.T) {
150150

151151
// broadcast one message
152152
srv.Broadcast("discard", 0, "foo")
153-
golden := unhex("66e94e166f0a2c3b884cfa59ca34")
153+
golden := unhex("66e94d166f0a2c3b884cfa59ca34")
154154

155155
// check that the message has been written everywhere
156156
for i, conn := range conns {

0 commit comments

Comments
 (0)