Skip to content

Commit

Permalink
core: properly deliver headers close to head
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Jun 24, 2021
1 parent 2f0fe7a commit 0bc022b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
10 changes: 10 additions & 0 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,16 @@ func (hc *HeaderChain) GetHeaderByNumber(number uint64) *types.Header {
// GetHeadersFrom returns a contiguous segment of headers, in rlp-form, going
// backwards from the given number.
func (hc *HeaderChain) GetHeadersFrom(number, count uint64) []rlp.RawValue {
// If the request is for future headers, we still return the portion of
// headers that we are able to serve
if current := hc.CurrentHeader().Number.Uint64(); current < number {
if count >= number-current {
count -= number - current
number = current
} else {
return nil
}
}
return rawdb.ReadHeadersRLP(hc.chainDb, number, count)
}

Expand Down
2 changes: 1 addition & 1 deletion eth/handler_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package eth

import (
"fmt"
"github.com/ethereum/go-ethereum/rlp"
"math/big"
"math/rand"
"sync/atomic"
Expand All @@ -38,6 +37,7 @@ import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
)

Expand Down
3 changes: 0 additions & 3 deletions eth/protocols/eth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ const (
// softResponseLimit is the target maximum size of replies to data retrievals.
softResponseLimit = 2 * 1024 * 1024

// estHeaderSize is the approximate size of an RLP encoded block header.
estHeaderSize = 500

// maxHeadersServe is the maximum number of block headers to serve. This number
// is there to limit the number of disk lookups.
maxHeadersServe = 1024
Expand Down
15 changes: 10 additions & 5 deletions eth/protocols/eth/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,15 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
{
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: 0}, Amount: 1},
[]common.Hash{backend.chain.GetBlockByNumber(0).Hash()},
}, {
},
{
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: backend.chain.CurrentBlock().NumberU64()}, Amount: 1},
[]common.Hash{backend.chain.CurrentBlock().Hash()},
},
{ // If the peer requests a bit into the future, we deliver what we have
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: backend.chain.CurrentBlock().NumberU64()}, Amount: 10},
[]common.Hash{backend.chain.CurrentBlock().Hash()},
},
// Ensure protocol limits are honored
{
&GetBlockHeadersPacket{Origin: HashOrNumber{Number: backend.chain.CurrentBlock().NumberU64() - 1}, Amount: limit + 10, Reverse: true},
Expand Down Expand Up @@ -259,7 +264,7 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
if protocol <= ETH65 {
p2p.Send(peer.app, GetBlockHeadersMsg, tt.query)
if err := p2p.ExpectMsg(peer.app, BlockHeadersMsg, headers); err != nil {
t.Errorf("test %d: headers mismatch: %v", i, err)
t.Fatalf("test %d: headers mismatch: %v", i, err)
}
} else {
p2p.Send(peer.app, GetBlockHeadersMsg, GetBlockHeadersPacket66{
Expand All @@ -270,7 +275,7 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
RequestId: 123,
BlockHeadersPacket: headers,
}); err != nil {
t.Errorf("test %d: headers mismatch: %v", i, err)
t.Fatalf("test %d: headers mismatch: %v", i, err)
}
}
// If the test used number origins, repeat with hashes as the too
Expand All @@ -281,7 +286,7 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
if protocol <= ETH65 {
p2p.Send(peer.app, GetBlockHeadersMsg, tt.query)
if err := p2p.ExpectMsg(peer.app, BlockHeadersMsg, headers); err != nil {
t.Errorf("test %d by hash: headers mismatch: %v", i, err)
t.Fatalf("test %d by hash: headers mismatch: %v", i, err)
}
} else {
p2p.Send(peer.app, GetBlockHeadersMsg, GetBlockHeadersPacket66{
Expand All @@ -292,7 +297,7 @@ func testGetBlockHeaders(t *testing.T, protocol uint) {
RequestId: 456,
BlockHeadersPacket: headers,
}); err != nil {
t.Errorf("test %d by hash: headers mismatch: %v", i, err)
t.Fatalf("test %d by hash: headers mismatch: %v", i, err)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion eth/protocols/eth/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func answerContiguousBlockHeaderQuery(backend Backend, query *GetBlockHeadersPac
if query.Origin.Hash == (common.Hash{}) {
// Number mode, just return the canon chain segment. The backend
// delivers in [N, N-1, N-2..] descending order, so we need to
// accomodate for that.
// accommodate for that.
from := query.Origin.Number
if !query.Reverse {
from = from + count - 1
Expand Down

0 comments on commit 0bc022b

Please sign in to comment.