From 0f4e7c9b0d570ff7f79b0765a0bd3737ce635e9a Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 24 Aug 2020 10:32:12 +0200 Subject: [PATCH] eth: utilize sync bloom for getNodeData (#21445) * eth/downloader, eth/handler: utilize sync bloom for getNodeData * trie: handle if bloom is nil * trie, downloader: check bloom nilness externally --- eth/downloader/downloader.go | 9 +++++++++ eth/handler.go | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 59b5abaa6..4c5b270b7 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -283,6 +283,15 @@ func (d *Downloader) Synchronising() bool { return atomic.LoadInt32(&d.synchronising) > 0 } +// SyncBloomContains tests if the syncbloom filter contains the given hash: +// - false: the bloom definitely does not contain hash +// - true: the bloom maybe contains hash +// +// While the bloom is being initialized (or is closed), all queries will return true. +func (d *Downloader) SyncBloomContains(hash []byte) bool { + return d.stateBloom == nil || d.stateBloom.Contains(hash) +} + // RegisterPeer injects a new download peer into the set of block source to be // used for fetching hashes and blocks from. func (d *Downloader) RegisterPeer(id string, version int, peer Peer) error { diff --git a/eth/handler.go b/eth/handler.go index 3a051abf5..7482a2f96 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -610,6 +610,10 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { // Retrieve the requested state entry, stopping if enough was found // todo now the code and trienode is mixed in the protocol level, // separate these two types. + if !pm.downloader.SyncBloomContains(hash[:]) { + // Only lookup the trie node if there's chance that we actually have it + continue + } entry, err := pm.blockchain.TrieNode(hash) if len(entry) == 0 || err != nil { // Read the contract code with prefix only to save unnecessary lookups.