Skip to content

Commit

Permalink
eth/protocols/eth: poc implement getblockheaders on eth/66
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Jan 26, 2021
1 parent 69516c6 commit d6b1989
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
18 changes: 17 additions & 1 deletion eth/protocols/eth/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ func handleGetBlockHeaders(backend Backend, msg Decoder, peer *Peer) error {
if err := msg.Decode(&query); err != nil {
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
}
response := replyToGetBlockHeaders(backend, query, peer)
return peer.SendBlockHeaders(response)
}

// handleGetBlockHeaders66 is the ETH-66 version of handleGetBlockHeaders
func handleGetBlockHeaders66(backend Backend, msg Decoder, peer *Peer) error {
// Decode the complex header query
var query GetBlockHeadersPacket66
if err := msg.Decode(&query); err != nil {
return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
}
response := replyToGetBlockHeaders(backend, query.GetBlockHeadersPacket, peer)
return peer.ReplyBlockHeaders(query.RequestId, response)
}

func replyToGetBlockHeaders(backend Backend, query GetBlockHeadersPacket, peer *Peer) BlockHeadersPacket {
hashMode := query.Origin.Hash != (common.Hash{})
first := true
maxNonCanonical := uint64(100)
Expand Down Expand Up @@ -116,7 +132,7 @@ func handleGetBlockHeaders(backend Backend, msg Decoder, peer *Peer) error {
query.Origin.Number += query.Skip + 1
}
}
return peer.SendBlockHeaders(headers)
return headers
}

func handleGetBlockBodies(backend Backend, msg Decoder, peer *Peer) error {
Expand Down
4 changes: 4 additions & 0 deletions eth/protocols/eth/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ func (p *Peer) SendBlockHeaders(headers []*types.Header) error {
return p2p.Send(p.rw, BlockHeadersMsg, BlockHeadersPacket(headers))
}

func (p *Peer) ReplyBlockHeaders(id uint64, headers BlockHeadersPacket) error {
return p2p.Send(p.rw, BlockHeadersMsg, BlockHeadersPacket66{id, headers})
}

// SendBlockBodies sends a batch of block contents to the remote peer.
func (p *Peer) SendBlockBodies(bodies []*BlockBody) error {
return p2p.Send(p.rw, BlockBodiesMsg, BlockBodiesPacket(bodies))
Expand Down
2 changes: 1 addition & 1 deletion p2p/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (msg Msg) Discard() error {
return err
}

func (msg Msg) Time() time.Time{
func (msg Msg) Time() time.Time {
return msg.ReceivedAt
}

Expand Down

0 comments on commit d6b1989

Please sign in to comment.