mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
protocol rlp
- getBlocksMsg list of hashes parsed with rlp.NewStream - blocksMsg fix lazy rlp - newBlockMsg fix flat rlp decoding
This commit is contained in:
parent
9ab3530df3
commit
72432fb164
1 changed files with 20 additions and 13 deletions
|
|
@ -3,7 +3,6 @@ package eth
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
|
@ -171,27 +170,35 @@ func (self *ethProtocol) handle() error {
|
||||||
self.blockPool.AddBlockHashes(iter, self.id)
|
self.blockPool.AddBlockHashes(iter, self.id)
|
||||||
|
|
||||||
case GetBlocksMsg:
|
case GetBlocksMsg:
|
||||||
var blockHashes [][]byte
|
msgStream := rlp.NewStream(msg.Payload)
|
||||||
if err := msg.Decode(&blockHashes); err != nil {
|
msgStream.List()
|
||||||
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
|
|
||||||
}
|
|
||||||
max := int(math.Min(float64(len(blockHashes)), blockHashesBatchSize))
|
|
||||||
var blocks []interface{}
|
var blocks []interface{}
|
||||||
for i, hash := range blockHashes {
|
var i int
|
||||||
if i >= max {
|
for {
|
||||||
break
|
i++
|
||||||
|
var hash []byte
|
||||||
|
if err := msgStream.Decode(&hash); err != nil {
|
||||||
|
if err == rlp.EOL {
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
block := self.chainManager.GetBlock(hash)
|
block := self.chainManager.GetBlock(hash)
|
||||||
if block != nil {
|
if block != nil {
|
||||||
blocks = append(blocks, block.RlpData())
|
blocks = append(blocks, block.RlpData())
|
||||||
}
|
}
|
||||||
|
if i == blockHashesBatchSize {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return self.rw.EncodeMsg(BlocksMsg, blocks...)
|
return self.rw.EncodeMsg(BlocksMsg, blocks...)
|
||||||
|
|
||||||
case BlocksMsg:
|
case BlocksMsg:
|
||||||
msgStream := rlp.NewListStream(msg.Payload, uint64(msg.Size))
|
msgStream := rlp.NewStream(msg.Payload)
|
||||||
|
msgStream.List()
|
||||||
for {
|
for {
|
||||||
var block *types.Block
|
var block [1]*types.Block
|
||||||
if err := msgStream.Decode(&block); err != nil {
|
if err := msgStream.Decode(&block); err != nil {
|
||||||
if err == rlp.EOL {
|
if err == rlp.EOL {
|
||||||
break
|
break
|
||||||
|
|
@ -199,7 +206,7 @@ func (self *ethProtocol) handle() error {
|
||||||
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
|
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.blockPool.AddBlock(block, self.id)
|
self.blockPool.AddBlock(block[0], self.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
case NewBlockMsg:
|
case NewBlockMsg:
|
||||||
|
|
@ -304,7 +311,7 @@ func (self *ethProtocol) requestBlockHashes(from []byte) error {
|
||||||
|
|
||||||
func (self *ethProtocol) requestBlocks(hashes [][]byte) error {
|
func (self *ethProtocol) requestBlocks(hashes [][]byte) error {
|
||||||
self.peer.Debugf("fetching %v blocks", len(hashes))
|
self.peer.Debugf("fetching %v blocks", len(hashes))
|
||||||
return self.rw.EncodeMsg(GetBlocksMsg, ethutil.ByteSliceToInterface(hashes))
|
return self.rw.EncodeMsg(GetBlocksMsg, ethutil.ByteSliceToInterface(hashes)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *ethProtocol) protoError(code int, format string, params ...interface{}) (err *protocolError) {
|
func (self *ethProtocol) protoError(code int, format string, params ...interface{}) (err *protocolError) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue