Skip to content

Commit

Permalink
Merge pull request #1 from fjl/fix-encode-msg
Browse files Browse the repository at this point in the history
Fix EncodeMsg
  • Loading branch information
zelig committed Jan 3, 2015
2 parents ba0ede1 + 2ef85b4 commit 1531915
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
22 changes: 10 additions & 12 deletions eth/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eth
import (
"bytes"
"fmt"
"io"
"math/big"

"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -139,19 +140,18 @@ func (self *ethProtocol) handle() error {
self.txPool.AddTransactions(txs)

case GetBlockHashesMsg:
var request [1]getBlockHashesMsgData
var request getBlockHashesMsgData
if err := msg.Decode(&request); err != nil {
return self.protoError(ErrDecode, "->msg %v: %v", msg, err)
}
hashes := self.chainManager.GetBlockHashesFromHash(request[0].Hash, request[0].Amount)
hashes := self.chainManager.GetBlockHashesFromHash(request.Hash, request.Amount)
protologger.Debugf("hashes length %v", len(hashes))
return self.rw.EncodeMsg(BlockHashesMsg, ethutil.ByteSliceToInterface(hashes)...)

case BlockHashesMsg:
// TODO: redo using lazy decode , this way very inefficient on known chains
protologger.Debugf("payload size %v", msg.Size)
msgStream := rlp.NewStream(msg.Payload)
msgStream.List()
var err error
var i int

Expand All @@ -161,7 +161,7 @@ func (self *ethProtocol) handle() error {
i++
ok = true
} else {
if err != rlp.EOL {
if err != io.EOF {
self.protoError(ErrDecode, "msg %v: after %v hashes : %v", msg, i, err)
}
}
Expand All @@ -172,14 +172,13 @@ func (self *ethProtocol) handle() error {

case GetBlocksMsg:
msgStream := rlp.NewStream(msg.Payload)
msgStream.List()
var blocks []interface{}
var i int
for {
i++
var hash []byte
if err := msgStream.Decode(&hash); err != nil {
if err == rlp.EOL {
if err == io.EOF {
break
} else {
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
Expand All @@ -197,11 +196,10 @@ func (self *ethProtocol) handle() error {

case BlocksMsg:
msgStream := rlp.NewStream(msg.Payload)
msgStream.List()
for {
var block types.Block
if err := msgStream.Decode(&block); err != nil {
if err == rlp.EOL {
if err == io.EOF {
break
} else {
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
Expand All @@ -211,15 +209,15 @@ func (self *ethProtocol) handle() error {
}

case NewBlockMsg:
var request [1]newBlockMsgData
var request newBlockMsgData
if err := msg.Decode(&request); err != nil {
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
}
hash := request[0].Block.Hash()
hash := request.Block.Hash()
// to simplify backend interface adding a new block
// uses AddPeer followed by AddHashes, AddBlock only if peer is the best peer
// (or selected as new best peer)
if self.blockPool.AddPeer(request[0].TD, hash, self.id, self.requestBlockHashes, self.requestBlocks, self.protoErrorDisconnect) {
if self.blockPool.AddPeer(request.TD, hash, self.id, self.requestBlockHashes, self.requestBlocks, self.protoErrorDisconnect) {
called := true
iter := func() (hash []byte, ok bool) {
if called {
Expand All @@ -230,7 +228,7 @@ func (self *ethProtocol) handle() error {
}
}
self.blockPool.AddBlockHashes(iter, self.id)
self.blockPool.AddBlock(request[0].Block, self.id)
self.blockPool.AddBlock(request.Block, self.id)
}

default:
Expand Down
2 changes: 1 addition & 1 deletion eth/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (self *testMsgReadWriter) WriteMsg(msg p2p.Msg) error {
}

func (self *testMsgReadWriter) EncodeMsg(code uint64, data ...interface{}) error {
return self.WriteMsg(p2p.NewMsg(code, data))
return self.WriteMsg(p2p.NewMsg(code, data...))
}

func (self *testMsgReadWriter) ReadMsg() (p2p.Msg, error) {
Expand Down
6 changes: 2 additions & 4 deletions p2p/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (d peerAddr) String() string {
}

func (d *peerAddr) RlpData() interface{} {
return []interface{}{d.IP, d.Port, d.Pubkey}
return []interface{}{string(d.IP), d.Port, d.Pubkey}
}

// Peer represents a remote peer.
Expand Down Expand Up @@ -426,7 +426,7 @@ func (rw *proto) WriteMsg(msg Msg) error {
}

func (rw *proto) EncodeMsg(code uint64, data ...interface{}) error {
return rw.WriteMsg(NewMsg(code, data))
return rw.WriteMsg(NewMsg(code, data...))
}

func (rw *proto) ReadMsg() (Msg, error) {
Expand Down Expand Up @@ -463,7 +463,6 @@ func (r *eofSignal) Read(buf []byte) (int, error) {

func (peer *Peer) PeerList() []interface{} {
peers := peer.otherPeers()
fmt.Printf("address length: %v\n", len(peers))
ds := make([]interface{}, 0, len(peers))
for _, p := range peers {
p.infolock.Lock()
Expand All @@ -482,6 +481,5 @@ func (peer *Peer) PeerList() []interface{} {
// if ourAddr != nil && !ourAddr.IP.IsLoopback() && !ourAddr.IP.IsUnspecified() {
ds = append(ds, ourAddr)
}
fmt.Printf("address length: %v\n", len(ds))
return ds
}
9 changes: 8 additions & 1 deletion p2p/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestPeerProtoEncodeMsg(t *testing.T) {
if err := rw.EncodeMsg(2); err == nil {
t.Error("expected error for out-of-range msg code, got nil")
}
if err := rw.EncodeMsg(1); err != nil {
if err := rw.EncodeMsg(1, "foo", "bar"); err != nil {
t.Errorf("write error: %v", err)
}
return nil
Expand All @@ -148,6 +148,13 @@ func TestPeerProtoEncodeMsg(t *testing.T) {
if msg.Code != 17 {
t.Errorf("incorrect message code: got %d, expected %d", msg.Code, 17)
}
var data []string
if err := msg.Decode(&data); err != nil {
t.Errorf("payload decode error: %v", err)
}
if !reflect.DeepEqual(data, []string{"foo", "bar"}) {
t.Errorf("payload RLP mismatch, got %#v, want %#v", data, []string{"foo", "bar"})
}
}

func TestPeerWrite(t *testing.T) {
Expand Down
5 changes: 1 addition & 4 deletions p2p/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package p2p

import (
"bytes"
"fmt"
"time"
)

Expand Down Expand Up @@ -171,8 +170,6 @@ func (bp *baseProtocol) handle(rw MsgReadWriter) error {

case getPeersMsg:
peers := bp.peer.PeerList()
fmt.Printf("get Peers Msg: peers length:%v\n", len(peers))

// this is dangerous. the spec says that we should _delay_
// sending the response if no new information is available.
// this means that would need to send a response later when
Expand All @@ -186,7 +183,7 @@ func (bp *baseProtocol) handle(rw MsgReadWriter) error {
case peersMsg:
var peers []*peerAddr
if err := msg.Decode(&peers); err != nil {
return newPeerError(errInvalidMsg, "msg %v : %v", msg, err)
return err
}
for _, addr := range peers {
bp.peer.Debugf("received peer suggestion: %v", addr)
Expand Down

0 comments on commit 1531915

Please sign in to comment.