p2p: remove Msg.Value and MsgLoop

This commit is contained in:
Felix Lange 2014-11-25 16:01:39 +01:00
parent f816fdcb69
commit 9b85002b70
3 changed files with 19 additions and 49 deletions

@ -41,13 +41,6 @@ func encodePayload(params ...interface{}) []byte {
return buf.Bytes()
}
// Value returns the decoded RLP payload items in a message.
func (msg Msg) Value() (*ethutil.Value, error) {
var v []interface{}
err := msg.Decode(&v)
return ethutil.NewValue(v), err
}
// Decode parse the RLP content of a message into
// the given value, which must be a pointer.
//
@ -84,31 +77,6 @@ type MsgReadWriter interface {
MsgWriter
}
// MsgLoop reads messages off the given reader and
// calls the handler function for each decoded message until
// it returns an error or the peer connection is closed.
//
// If a message is larger than the given maximum size,
// MsgLoop returns an appropriate error.
func MsgLoop(r MsgReader, maxsize uint32, f func(code uint64, data *ethutil.Value) error) error {
for {
msg, err := r.ReadMsg()
if err != nil {
return err
}
if msg.Size > maxsize {
return newPeerError(errInvalidMsg, "size %d exceeds maximum size of %d", msg.Size, maxsize)
}
value, err := msg.Value()
if err != nil {
return err
}
if err := f(msg.Code, value); err != nil {
return err
}
}
}
var magicToken = []byte{34, 64, 8, 145}
func writeMsg(w io.Writer, msg Msg) error {

@ -29,8 +29,7 @@ func TestEncodeDecodeMsg(t *testing.T) {
if err := writeMsg(buf, msg); err != nil {
t.Fatalf("encodeMsg error: %v", err)
}
t.Logf("encoded: %x", buf.Bytes())
// t.Logf("encoded: %x", buf.Bytes())
decmsg, err := readMsg(buf)
if err != nil {
@ -42,18 +41,19 @@ func TestEncodeDecodeMsg(t *testing.T) {
if decmsg.Size != 5 {
t.Errorf("incorrect size %d, want %d", decmsg.Size, 5)
}
data, err := decmsg.Value()
if err != nil {
t.Fatalf("first payload item decode error: %v", err)
var data struct {
I int
S string
}
if v := data.Len(); v != 2 {
t.Errorf("incorrect data.Len(): got %v, expected %d", v, 1)
if err := decmsg.Decode(&data); err != nil {
t.Fatalf("Decode error: %v", err)
}
if v := data.Get(0).Uint(); v != 1 {
t.Errorf("incorrect data[0]: got %v, expected %d", v, 1)
if data.I != 1 {
t.Errorf("incorrect data.I: got %v, expected %d", data.I, 1)
}
if v := data.Get(1).Str(); v != "000" {
t.Errorf("incorrect data[1]: got %q, expected %q", v, "000")
if data.S != "000" {
t.Errorf("incorrect data.S: got %q, expected %q", data.S, "000")
}
}

@ -2,8 +2,10 @@ package p2p
import (
"bufio"
"bytes"
"encoding/hex"
"io/ioutil"
"net"
"reflect"
"testing"
"time"
)
@ -53,13 +55,13 @@ func TestPeerProtoReadMsg(t *testing.T) {
if msg.Code != 2 {
t.Errorf("incorrect msg code %d relayed to protocol", msg.Code)
}
data, err := msg.Value()
data, err := ioutil.ReadAll(msg.Payload)
if err != nil {
t.Errorf("data decoding error: %v", err)
t.Errorf("payload read error: %v", err)
}
expdata := []interface{}{[]byte{0x01}, []byte{0x30, 0x30, 0x30}}
if !reflect.DeepEqual(data.Slice(), expdata) {
t.Errorf("incorrect msg data %#v", data.Slice())
expdata, _ := hex.DecodeString("0183303030")
if !bytes.Equal(expdata, data) {
t.Errorf("incorrect msg data %x", data)
}
close(done)
return nil