Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get logs patch 2 #1381

Merged
merged 5 commits into from
Sep 21, 2020
Merged

Conversation

matkt
Copy link
Contributor

@matkt matkt commented Sep 14, 2020

Signed-off-by: Karim TAAM karim.t2am@gmail.com

PR description

LogBloom could be invalid in the cache after a reorg. With this PR, there is an update of the cache every time a reorg takes place

Test performed

  • Sync on goerli (reorg etc.)

Changelog

@matkt matkt force-pushed the feature/fix-get-logs-patch-2 branch from be61973 to 04739bb Compare September 16, 2020 10:06
Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
@matkt matkt force-pushed the feature/fix-get-logs-patch-2 branch from 71bb9b6 to 50a21b3 Compare September 16, 2020 16:48
Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
Comment on lines +159 to +172
final Optional<Long> ancestorBlockNumber =
commonAncestorBlockHeader.map(ProcessableBlockHeader::getNumber);
if (ancestorBlockNumber.isPresent()) {
// walk through the blocks from the common ancestor to the received block in order to
// reload the cache in case of reorg
for (long number = ancestorBlockNumber.get() + 1;
number < blockHeader.getNumber();
number++) {
Optional<BlockHeader> ancestorBlockHeader = blockchain.getBlockHeader(number);
if (ancestorBlockHeader.isPresent()) {
cacheSingleBlock(ancestorBlockHeader.get(), cacheFile);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Feels a bit weird to use .map and then .isPresent here. Would be worth considering using .ifPresent:

Suggested change
final Optional<Long> ancestorBlockNumber =
commonAncestorBlockHeader.map(ProcessableBlockHeader::getNumber);
if (ancestorBlockNumber.isPresent()) {
// walk through the blocks from the common ancestor to the received block in order to
// reload the cache in case of reorg
for (long number = ancestorBlockNumber.get() + 1;
number < blockHeader.getNumber();
number++) {
Optional<BlockHeader> ancestorBlockHeader = blockchain.getBlockHeader(number);
if (ancestorBlockHeader.isPresent()) {
cacheSingleBlock(ancestorBlockHeader.get(), cacheFile);
}
}
}
commonAncestorBlockHeader
.map(ProcessableBlockHeader::getNumber)
.ifPresent(ancestorBlockNumber -> {
// walk through the blocks from the common ancestor to the received block in order to
// reload the cache in case of reorg
for (long number = ancestorBlockNumber.get() + 1;
number < blockHeader.getNumber();
number++) {
blockchain.getBlockHeader(number).ifPresent(
header -> cacheSingleBlock(ancestorBlockHeader.get(), cacheFile));
}
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this isn't removing logs from the cache correctly when a reorg makes the chain shorter. It needs to delete all cache entries between the new chain head number and the old one if the new head number is less.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if this index is only used as the initial filter, you wouldn't need to remove the later blocks when a reorg makes the chain shorter - you'd potentially check the blocks in this range when you didn't need to but it would just be treated as a false positive and sorted out once you're pulling logs out of the actual blocks.

In that case I think this should work. Will deploy it and test it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in fact in case of a shorter reorg the cache will be truncated the first time cacheSingleBlock is used. this is why I did not add anything else here.

Copy link
Contributor Author

@matkt matkt Sep 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always use ifpresent but in this case cacheSingleBlock can throw an exception and there is a catch here. if I use ifPresent I should put 2 catch with the same implementation. this is the main reason for using isPresent here

https://github.com/matkt/besu/blob/931f3c2a3c56fa489b62a0a872aa54edc412bce4/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/cache/TransactionLogBloomCacher.java#L174

Copy link
Contributor

@ajsutton ajsutton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I'd strongly recommend updating the cache version again though - those who skip release to release still only reindex once, but anyone who updated to the snapshot reindexes again to fix the log cache. So helps some people with no down side.

@matkt
Copy link
Contributor Author

matkt commented Sep 18, 2020

LGTM. I'd strongly recommend updating the cache version again though - those who skip release to release still only reindex once, but anyone who updated to the snapshot reindexes again to fix the log cache. So helps some people with no down side.

ok you convinced me I will update the version

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
@matkt matkt marked this pull request as ready for review September 21, 2020 09:44
Copy link
Contributor

@AbdelStark AbdelStark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@matkt matkt merged commit 9f893f7 into hyperledger:master Sep 21, 2020
@matkt matkt deleted the feature/fix-get-logs-patch-2 branch September 29, 2020 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants