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

feat(snap): add extra checks; update snap discovery #2860

Open
wants to merge 8 commits into
base: snap-v4-final
Choose a base branch
from

Conversation

Vovchyk
Copy link
Contributor

@Vovchyk Vovchyk commented Nov 22, 2024

Description

Motivation and Context

How Has This Been Tested?

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • Tests for the changes have been added (for bug fixes / features)
  • Requires Activation Code (Hard Fork)
  • Other information:

Copy link

github-actions bot commented Nov 22, 2024

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

Copy link
Contributor

@fmacleal fmacleal left a comment

Choose a reason for hiding this comment

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

praise:

Very good job! I can see that this was a very difficult feature change.
I have left some small comments and also some questions, I would like to have a call to discuss better the changes and understand all the additions.

generateChunkRequestTasks(state);
startRequestingChunks(state);

if (blocksVerified(state)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

question:

What exactly means the method blocksVerified? Does it mean that all blocks were verified?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, correct - that the block headers were verified in reverse order till the latest connected block

state.setRemoteRootHash(lastBlock.getStateRoot());
state.setRemoteTrieSize(responseMessage.getTrieSize());

for (int i = 0; i < blocksFromResponse.size(); i++) {
state.addBlock(new ImmutablePair<>(blocksFromResponse.get(i), difficultiesFromResponse.get(i)));
if (!validateAndSaveBlocks(state, sender, blocksFromResponse, difficultiesFromResponse)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

Would be interesting to have a log here to identify that it left the loop with invalid blocks? Or the ones inside the method are already enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's being logged inside that method

requestNextBlockHeadersChunk(state, sender);
}

private boolean validateBlockHeaders(SnapSyncState state, Peer sender, List<BlockHeader> blockHeaders) {
Copy link
Contributor

Choose a reason for hiding this comment

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

issue:

the sender parameter is never used

Comment on lines 470 to 484
if (blocksVerified(state)) {
logger.info("CLIENT - Finished Snap blocks request sending.");

generateChunkRequestTasks(state);
startRequestingChunks(state);
} else if (nextChunk > lastRequiredBlock) {
requestBlocksChunk(sender, nextChunk);
} else {
logger.info("CLIENT - Finished Snap blocks request sending.");

generateChunkRequestTasks(state);
startRequestingChunks(state);

requestNextBlockHeadersChunk(state, sender);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

I see that the difference between the first if and the last else it's because we are requiring headers chunk if block is not verified. Maybe it could be simplified and avoid a bit of DRY:

Suggested change
if (blocksVerified(state)) {
logger.info("CLIENT - Finished Snap blocks request sending.");
generateChunkRequestTasks(state);
startRequestingChunks(state);
} else if (nextChunk > lastRequiredBlock) {
requestBlocksChunk(sender, nextChunk);
} else {
logger.info("CLIENT - Finished Snap blocks request sending.");
generateChunkRequestTasks(state);
startRequestingChunks(state);
requestNextBlockHeadersChunk(state, sender);
}
if (nextChunk > lastRequiredBlock) {
requestBlocksChunk(sender, nextChunk);
} else {
logger.info("CLIENT - Finished Snap blocks request sending.");
generateChunkRequestTasks(state);
startRequestingChunks(state);
if(!blocksVerified(state)) {
requestNextBlockHeadersChunk(state, sender);
}
}

@@ -289,8 +289,8 @@ public void startBlockForwardSyncing(Peer peer) {
}

@Override
public void startSnapSync() {
logger.info("Start Snap syncing");
public void startSnapSync(Peer peer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

question:

Does it mean that we will accept the sync only with a single peer for now right? 🤔
I don't recall if it was possible to receive from multiple peers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, we start with one peer - we send SnapStatusRequestMessage to get initial params for syncing, like state root hash, etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

The previous suggestions to use the any(Class<T>) also applies here.

Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

The previous suggestions to use the any(Class<T>) also applies here.

@@ -99,7 +99,7 @@ public BlockValidatorBuilder addBlockUnclesValidationRule(BlockStore blockStore)
Mockito.when(validationRule.isValid(Mockito.any())).thenReturn(true);

BlockHeaderParentDependantValidationRule parentValidationRule = Mockito.mock(BlockHeaderParentDependantValidationRule.class);
Mockito.when(parentValidationRule.isValid(Mockito.any(), Mockito.any())).thenReturn(true);
Mockito.when(parentValidationRule.isValid(Mockito.any(), (Block) Mockito.any())).thenReturn(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

I would use static import here to import Mockito.

@@ -288,7 +288,7 @@ void invalidPOWUncles() {
store.saveBlock(uncle1a, TEST_DIFFICULTY, false);

BlockHeaderParentDependantValidationRule parentValidationRule = mock(BlockHeaderParentDependantValidationRule.class);
when(parentValidationRule.isValid(Mockito.any(), Mockito.any())).thenReturn(true);
when(parentValidationRule.isValid(Mockito.any(), (Block) Mockito.any())).thenReturn(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion:

I would use static import here to import Mockito.

Copy link
Contributor

Choose a reason for hiding this comment

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

praise:

It looks like the test stayed way simpler. Congrats on the refactor.

@Vovchyk Vovchyk force-pushed the vovchyk/snap/extra-checks branch from 3e9075c to 58edd8e Compare December 5, 2024 15:07
@Vovchyk Vovchyk marked this pull request as ready for review December 5, 2024 16:08
Copy link

sonarqubecloud bot commented Dec 5, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
50.9% Coverage on New Code (required ≥ 55%)

See analysis details on SonarQube Cloud

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.

2 participants