11package eth
22
33import (
4- "bytes"
54 "fmt"
65 "math/big"
76
7+ "github.com/ethereum/go-ethereum/common"
88 "github.com/ethereum/go-ethereum/core/types"
99 "github.com/ethereum/go-ethereum/errs"
10- "github.com/ethereum/go-ethereum/common"
1110 "github.com/ethereum/go-ethereum/logger"
1211 "github.com/ethereum/go-ethereum/p2p"
1312 "github.com/ethereum/go-ethereum/rlp"
@@ -76,15 +75,15 @@ type txPool interface {
7675}
7776
7877type chainManager interface {
79- GetBlockHashesFromHash (hash [] byte , amount uint64 ) (hashes [][] byte )
80- GetBlock (hash [] byte ) (block * types.Block )
81- Status () (td * big.Int , currentBlock [] byte , genesisBlock [] byte )
78+ GetBlockHashesFromHash (hash common. Hash , amount uint64 ) (hashes []common. Hash )
79+ GetBlock (hash common. Hash ) (block * types.Block )
80+ Status () (td * big.Int , currentBlock common. Hash , genesisBlock common. Hash )
8281}
8382
8483type blockPool interface {
85- AddBlockHashes (next func () ([] byte , bool ), peerId string )
84+ AddBlockHashes (next func () (common. Hash , bool ), peerId string )
8685 AddBlock (block * types.Block , peerId string )
87- AddPeer (td * big.Int , currentBlock [] byte , peerId string , requestHashes func ([] byte ) error , requestBlocks func ([][] byte ) error , peerError func (* errs.Error )) (best bool )
86+ AddPeer (td * big.Int , currentBlock common. Hash , peerId string , requestHashes func (common. Hash ) error , requestBlocks func ([]common. Hash ) error , peerError func (* errs.Error )) (best bool )
8887 RemovePeer (peerId string )
8988}
9089
@@ -95,7 +94,7 @@ type newBlockMsgData struct {
9594}
9695
9796type getBlockHashesMsgData struct {
98- Hash [] byte
97+ Hash common. Hash
9998 Amount uint64
10099}
101100
@@ -167,7 +166,7 @@ func (self *ethProtocol) handle() error {
167166 }
168167 for _ , tx := range txs {
169168 jsonlogger .LogJson (& logger.EthTxReceived {
170- TxHash : common . Bytes2Hex ( tx .Hash ()),
169+ TxHash : tx .Hash (). Hex ( ),
171170 RemoteId : self .peer .ID ().String (),
172171 })
173172 }
@@ -183,7 +182,7 @@ func (self *ethProtocol) handle() error {
183182 request .Amount = maxHashes
184183 }
185184 hashes := self .chainManager .GetBlockHashesFromHash (request .Hash , request .Amount )
186- return p2p .EncodeMsg (self .rw , BlockHashesMsg , common . ByteSliceToInterface (hashes )... )
185+ return p2p .EncodeMsg (self .rw , BlockHashesMsg , rlp . Flat (hashes ))
187186
188187 case BlockHashesMsg :
189188 msgStream := rlp .NewStream (msg .Payload )
@@ -192,14 +191,16 @@ func (self *ethProtocol) handle() error {
192191 }
193192
194193 var i int
195- iter := func () (hash []byte , ok bool ) {
196- hash , err := msgStream .Bytes ()
194+ iter := func () (hash common.Hash , ok bool ) {
195+ var h common.Hash
196+ err := msgStream .Decode (& h )
197197 if err == rlp .EOL {
198- return nil , false
198+ return common. Hash {} , false
199199 } else if err != nil {
200200 self .protoError (ErrDecode , "msg %v: after %v hashes : %v" , msg , i , err )
201- return nil , false
201+ return common. Hash {} , false
202202 }
203+
203204 i ++
204205 return hash , true
205206 }
@@ -215,14 +216,14 @@ func (self *ethProtocol) handle() error {
215216 var i int
216217 for {
217218 i ++
218- var hash []byte
219- if err := msgStream .Decode (& hash ); err != nil {
220- if err == rlp .EOL {
221- break
222- } else {
223- return self .protoError (ErrDecode , "msg %v: %v" , msg , err )
224- }
219+ var hash common.Hash
220+ err := msgStream .Decode (& hash )
221+ if err == rlp .EOL {
222+ break
223+ } else {
224+ return self .protoError (ErrDecode , "msg %v: %v" , msg , err )
225225 }
226+
226227 block := self .chainManager .GetBlock (hash )
227228 if block != nil {
228229 blocks = append (blocks , block )
@@ -259,10 +260,10 @@ func (self *ethProtocol) handle() error {
259260 _ , chainHead , _ := self .chainManager .Status ()
260261
261262 jsonlogger .LogJson (& logger.EthChainReceivedNewBlock {
262- BlockHash : common . Bytes2Hex ( hash ),
263+ BlockHash : hash . Hex ( ),
263264 BlockNumber : request .Block .Number (), // this surely must be zero
264- ChainHeadHash : common . Bytes2Hex ( chainHead ),
265- BlockPrevHash : common . Bytes2Hex ( request .Block .ParentHash ()),
265+ ChainHeadHash : chainHead . Hex ( ),
266+ BlockPrevHash : request .Block .ParentHash (). Hex ( ),
266267 RemoteId : self .peer .ID ().String (),
267268 })
268269 // to simplify backend interface adding a new block
@@ -282,8 +283,8 @@ type statusMsgData struct {
282283 ProtocolVersion uint32
283284 NetworkId uint32
284285 TD * big.Int
285- CurrentBlock [] byte
286- GenesisBlock [] byte
286+ CurrentBlock common. Hash
287+ GenesisBlock common. Hash
287288}
288289
289290func (self * ethProtocol ) statusMsg () p2p.Msg {
@@ -325,7 +326,7 @@ func (self *ethProtocol) handleStatus() error {
325326
326327 _ , _ , genesisBlock := self .chainManager .Status ()
327328
328- if ! bytes . Equal ( status .GenesisBlock , genesisBlock ) {
329+ if status .GenesisBlock != genesisBlock {
329330 return self .protoError (ErrGenesisBlockMismatch , "%x (!= %x)" , status .GenesisBlock , genesisBlock )
330331 }
331332
@@ -344,14 +345,14 @@ func (self *ethProtocol) handleStatus() error {
344345 return nil
345346}
346347
347- func (self * ethProtocol ) requestBlockHashes (from [] byte ) error {
348+ func (self * ethProtocol ) requestBlockHashes (from common. Hash ) error {
348349 self .peer .Debugf ("fetching hashes (%d) %x...\n " , maxHashes , from [0 :4 ])
349350 return p2p .EncodeMsg (self .rw , GetBlockHashesMsg , interface {}(from ), uint64 (maxHashes ))
350351}
351352
352- func (self * ethProtocol ) requestBlocks (hashes [][] byte ) error {
353+ func (self * ethProtocol ) requestBlocks (hashes []common. Hash ) error {
353354 self .peer .Debugf ("fetching %v blocks" , len (hashes ))
354- return p2p .EncodeMsg (self .rw , GetBlocksMsg , common . ByteSliceToInterface (hashes )... )
355+ return p2p .EncodeMsg (self .rw , GetBlocksMsg , rlp . Flat (hashes ))
355356}
356357
357358func (self * ethProtocol ) protoError (code int , format string , params ... interface {}) (err * errs.Error ) {
0 commit comments