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

Add MMR-sync to consensus chain snap sync. #3206

Merged
merged 5 commits into from
Nov 5, 2024

Conversation

shamil-gadelshin
Copy link
Member

This PR adds MMR-sync as part of the consensus snap sync. It will simplify future domain snap sync because the consensus chain network will contain proper local MMR data storage.

Code contributor checklist:

@shamil-gadelshin shamil-gadelshin marked this pull request as draft November 4, 2024 15:58
@shamil-gadelshin shamil-gadelshin marked this pull request as ready for review November 4, 2024 16:06
@nazar-pc
Copy link
Member

nazar-pc commented Nov 4, 2024

Cherry-picked #3208 for easier testing

Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Just some questions to make sure I understand what the code is doing.


starting_position += 1;

if u64::from(*position) >= target_position {
Copy link
Contributor

Choose a reason for hiding this comment

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

It took me a while to understand the logic here. I don’t think there’s an actual bug, but maybe the loop conditions could be rewritten to be clearer?

  • we check position != starting_position and only get to this code if they’re equal, then add one to starting_position, then compare position >= target_position, which guarantees target_position <= starting_position below (because that was true when they were equal, before we added one). Is there a simpler way to express this condition that makes this relationship clearer? Maybe a boolean reached_target flag or something?
  • this condition is position >= target_position, but the one below is target_position <= starting_position. A consistent order would make it easier to see they’re the same condition.

Totally up to you, but it might also be clearer to split one or more of the inner loops into their own function.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure I understand your comments. There are several nested loops, and they have different conditions. I added a couple of code comments to clarify the conditions you mentioned.

crates/subspace-service/src/mmr/sync.rs Show resolved Hide resolved
crates/subspace-service/src/mmr/sync.rs Show resolved Hide resolved
crates/subspace-service/src/mmr/request_handler.rs Outdated Show resolved Hide resolved
@nazar-pc
Copy link
Member

nazar-pc commented Nov 4, 2024

There is definitely a bug here, it results in an attempt to query block number that was not yet imported during verification:

2024-11-04T19:15:14.333156Z INFO Consensus: substrate: :gear: Syncing 0.0 bps, target=#97810 (11 peers), best: #11369 (0xe2cd…9f26), finalized #11113 (0x29fe…21d0), ⬇ 4.7kiB/s ⬆ 0.2kiB/s
...
2024-11-04T19:15:24.092197Z ERROR Consensus: subspace_service::mmr::sync: MMR data verification: error during hash acquisition leaves_number=97810 block_number=97810

Copy link
Member Author

@shamil-gadelshin shamil-gadelshin left a comment

Choose a reason for hiding this comment

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

The last commit fixes the bug. The bug was a branch from the early PoC that wasn't removed and related to the default target_block when we didn't provide the correct value. Domain sync always provided the correct target block and the incorrect code branch was never detected during the test.

crates/subspace-service/src/mmr/sync.rs Show resolved Hide resolved
crates/subspace-service/src/mmr/sync.rs Show resolved Hide resolved

starting_position += 1;

if u64::from(*position) >= target_position {
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure I understand your comments. There are several nested loops, and they have different conditions. I added a couple of code comments to clarify the conditions you mentioned.

Copy link
Member

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

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

Couple of non-critical issues that could be handled with TODOs for now so we can merge this, but should be fixed shortly afterwards.

Tested, it works now.

crates/subspace-service/src/mmr/sync.rs Show resolved Hide resolved
crates/subspace-service/src/mmr/sync.rs Show resolved Hide resolved
Comment on lines +425 to +435
if let Some(offchain_storage) = offchain_storage {
mmr_sync(
fork_id.map(|v| v.into()),
client.clone(),
network_request,
sync_service.clone(),
offchain_storage,
mmr_target_block,
)
.await?;
}
Copy link
Member

Choose a reason for hiding this comment

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

This is not the right level to check this at. It should be checked at higher level outside of Snap sync or else it is possible for Snap sync to start, being interrupted in the middle and MMR sync to never actually happen.

Copy link
Member Author

Choose a reason for hiding this comment

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

If I understood your comment correctly, "your_claim == true" even I move this check elsewhere because we don't have transactional operations in this context.

@nazar-pc nazar-pc force-pushed the mainnet-release-test-2-mmr-sync branch from 9059d07 to f15e6d5 Compare November 5, 2024 17:57
@nazar-pc nazar-pc changed the base branch from mainnet-release-test-2 to main November 5, 2024 17:57
Copy link
Member

@nazar-pc nazar-pc left a comment

Choose a reason for hiding this comment

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

Rebased on main without any other changes and changed merge target to main

@nazar-pc nazar-pc merged commit 4452df8 into main Nov 5, 2024
3 of 10 checks passed
@nazar-pc nazar-pc deleted the mainnet-release-test-2-mmr-sync branch November 5, 2024 18:03
@nazar-pc
Copy link
Member

nazar-pc commented Nov 5, 2024

GitHub didn't want to trigger CI, so I just merged it directly

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