From d6b198929c5243d7e7627653708a1e16239b4623 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 26 Jan 2021 13:45:59 +0100 Subject: [PATCH] eth/protocols/eth: poc implement getblockheaders on eth/66 --- eth/protocols/eth/handlers.go | 18 +++++++++++++++++- eth/protocols/eth/peer.go | 4 ++++ p2p/message.go | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/eth/protocols/eth/handlers.go b/eth/protocols/eth/handlers.go index 91b4263ed6bf..a52837d3d68b 100644 --- a/eth/protocols/eth/handlers.go +++ b/eth/protocols/eth/handlers.go @@ -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) @@ -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 { diff --git a/eth/protocols/eth/peer.go b/eth/protocols/eth/peer.go index 735ef78ce71b..08fd2c1acaca 100644 --- a/eth/protocols/eth/peer.go +++ b/eth/protocols/eth/peer.go @@ -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)) diff --git a/p2p/message.go b/p2p/message.go index bd9761e6b4e9..bd048138c3ab 100644 --- a/p2p/message.go +++ b/p2p/message.go @@ -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 }