aboutsummaryrefslogtreecommitdiffstats
path: root/eth/handler.go
diff options
context:
space:
mode:
authorFelföldi Zsolt <zsfelfoldi@gmail.com>2018-06-12 21:52:54 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-06-12 21:52:54 +0800
commit049f5b357200406c01f7bf2d2e2936d1d28a319b (patch)
treeef73043730c3d77359b9163ab6cbb2f053e5cf86 /eth/handler.go
parent0255951587ef0eada5d162f3404bc481f70a2ce2 (diff)
downloaddexon-049f5b357200406c01f7bf2d2e2936d1d28a319b.tar
dexon-049f5b357200406c01f7bf2d2e2936d1d28a319b.tar.gz
dexon-049f5b357200406c01f7bf2d2e2936d1d28a319b.tar.bz2
dexon-049f5b357200406c01f7bf2d2e2936d1d28a319b.tar.lz
dexon-049f5b357200406c01f7bf2d2e2936d1d28a319b.tar.xz
dexon-049f5b357200406c01f7bf2d2e2936d1d28a319b.tar.zst
dexon-049f5b357200406c01f7bf2d2e2936d1d28a319b.zip
core, eth, les: more efficient hash-based header chain retrieval (#16946)
Diffstat (limited to 'eth/handler.go')
-rw-r--r--eth/handler.go37
1 files changed, 23 insertions, 14 deletions
diff --git a/eth/handler.go b/eth/handler.go
index 918d71088..a46e7f13c 100644
--- a/eth/handler.go
+++ b/eth/handler.go
@@ -340,6 +340,8 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
return errResp(ErrDecode, "%v: %v", msg, err)
}
hashMode := query.Origin.Hash != (common.Hash{})
+ first := true
+ maxNonCanonical := uint64(100)
// Gather headers until the fetch or network limits is reached
var (
@@ -351,31 +353,36 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
// Retrieve the next header satisfying the query
var origin *types.Header
if hashMode {
- origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash)
+ if first {
+ first = false
+ origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash)
+ if origin != nil {
+ query.Origin.Number = origin.Number.Uint64()
+ }
+ } else {
+ origin = pm.blockchain.GetHeader(query.Origin.Hash, query.Origin.Number)
+ }
} else {
origin = pm.blockchain.GetHeaderByNumber(query.Origin.Number)
}
if origin == nil {
break
}
- number := origin.Number.Uint64()
headers = append(headers, origin)
bytes += estHeaderRlpSize
// Advance to the next header of the query
switch {
- case query.Origin.Hash != (common.Hash{}) && query.Reverse:
+ case hashMode && query.Reverse:
// Hash based traversal towards the genesis block
- for i := 0; i < int(query.Skip)+1; i++ {
- if header := pm.blockchain.GetHeader(query.Origin.Hash, number); header != nil {
- query.Origin.Hash = header.ParentHash
- number--
- } else {
- unknown = true
- break
- }
+ ancestor := query.Skip + 1
+ if ancestor == 0 {
+ unknown = true
+ } else {
+ query.Origin.Hash, query.Origin.Number = pm.blockchain.GetAncestor(query.Origin.Hash, query.Origin.Number, ancestor, &maxNonCanonical)
+ unknown = (query.Origin.Hash == common.Hash{})
}
- case query.Origin.Hash != (common.Hash{}) && !query.Reverse:
+ case hashMode && !query.Reverse:
// Hash based traversal towards the leaf block
var (
current = origin.Number.Uint64()
@@ -387,8 +394,10 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
unknown = true
} else {
if header := pm.blockchain.GetHeaderByNumber(next); header != nil {
- if pm.blockchain.GetBlockHashesFromHash(header.Hash(), query.Skip+1)[query.Skip] == query.Origin.Hash {
- query.Origin.Hash = header.Hash()
+ nextHash := header.Hash()
+ expOldHash, _ := pm.blockchain.GetAncestor(nextHash, next, query.Skip+1, &maxNonCanonical)
+ if expOldHash == query.Origin.Hash {
+ query.Origin.Hash, query.Origin.Number = nextHash, next
} else {
unknown = true
}