mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 12:46:44 +00:00
protocol and rlp
- getBlockHashes lazy encoder NewListStream -> NewStream - need stream.List() - add logging to protocol - fix newBlockMsgData flat rlp
This commit is contained in:
parent
340eac708c
commit
e2b8e05a71
1 changed files with 19 additions and 8 deletions
|
|
@ -8,10 +8,13 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/ethutil"
|
"github.com/ethereum/go-ethereum/ethutil"
|
||||||
|
"github.com/ethereum/go-ethereum/logger"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var protologger = logger.NewLogger("ETH")
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ProtocolVersion = 51
|
ProtocolVersion = 51
|
||||||
NetworkId = 0
|
NetworkId = 0
|
||||||
|
|
@ -141,23 +144,31 @@ func (self *ethProtocol) handle() error {
|
||||||
return self.protoError(ErrDecode, "->msg %v: %v", msg, err)
|
return self.protoError(ErrDecode, "->msg %v: %v", msg, err)
|
||||||
}
|
}
|
||||||
hashes := self.chainManager.GetBlockHashesFromHash(request[0].Hash, request[0].Amount)
|
hashes := self.chainManager.GetBlockHashesFromHash(request[0].Hash, request[0].Amount)
|
||||||
|
protologger.Debugf("hashes length %v", len(hashes))
|
||||||
return self.rw.EncodeMsg(BlockHashesMsg, ethutil.ByteSliceToInterface(hashes)...)
|
return self.rw.EncodeMsg(BlockHashesMsg, ethutil.ByteSliceToInterface(hashes)...)
|
||||||
|
|
||||||
case BlockHashesMsg:
|
case BlockHashesMsg:
|
||||||
// TODO: redo using lazy decode , this way very inefficient on known chains
|
// TODO: redo using lazy decode , this way very inefficient on known chains
|
||||||
msgStream := rlp.NewListStream(msg.Payload, uint64(msg.Size))
|
protologger.Debugf("payload size %v", msg.Size)
|
||||||
|
msgStream := rlp.NewStream(msg.Payload)
|
||||||
|
msgStream.List()
|
||||||
var err error
|
var err error
|
||||||
|
var i int
|
||||||
|
|
||||||
iter := func() (hash []byte, ok bool) {
|
iter := func() (hash []byte, ok bool) {
|
||||||
hash, err = msgStream.Bytes()
|
hash, err = msgStream.Bytes()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
i++
|
||||||
ok = true
|
ok = true
|
||||||
|
} else {
|
||||||
|
if err != rlp.EOL {
|
||||||
|
self.protoError(ErrDecode, "msg %v: after %v hashes : %v", msg, i, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.blockPool.AddBlockHashes(iter, self.id)
|
self.blockPool.AddBlockHashes(iter, self.id)
|
||||||
if err != nil && err != rlp.EOL {
|
|
||||||
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
case GetBlocksMsg:
|
case GetBlocksMsg:
|
||||||
var blockHashes [][]byte
|
var blockHashes [][]byte
|
||||||
|
|
@ -192,15 +203,15 @@ func (self *ethProtocol) handle() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
case NewBlockMsg:
|
case NewBlockMsg:
|
||||||
var request newBlockMsgData
|
var request [1]newBlockMsgData
|
||||||
if err := msg.Decode(&request); err != nil {
|
if err := msg.Decode(&request); err != nil {
|
||||||
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
|
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
|
||||||
}
|
}
|
||||||
hash := request.Block.Hash()
|
hash := request[0].Block.Hash()
|
||||||
// to simplify backend interface adding a new block
|
// to simplify backend interface adding a new block
|
||||||
// uses AddPeer followed by AddHashes, AddBlock only if peer is the best peer
|
// uses AddPeer followed by AddHashes, AddBlock only if peer is the best peer
|
||||||
// (or selected as new best peer)
|
// (or selected as new best peer)
|
||||||
if self.blockPool.AddPeer(request.TD, hash, self.id, self.requestBlockHashes, self.requestBlocks, self.protoErrorDisconnect) {
|
if self.blockPool.AddPeer(request[0].TD, hash, self.id, self.requestBlockHashes, self.requestBlocks, self.protoErrorDisconnect) {
|
||||||
called := true
|
called := true
|
||||||
iter := func() (hash []byte, ok bool) {
|
iter := func() (hash []byte, ok bool) {
|
||||||
if called {
|
if called {
|
||||||
|
|
@ -211,7 +222,7 @@ func (self *ethProtocol) handle() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.blockPool.AddBlockHashes(iter, self.id)
|
self.blockPool.AddBlockHashes(iter, self.id)
|
||||||
self.blockPool.AddBlock(request.Block, self.id)
|
self.blockPool.AddBlock(request[0].Block, self.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue