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(fork-network): fix handling of shard IDs #12469

Merged
merged 2 commits into from
Nov 18, 2024

Conversation

marcelo-gonzalez
Copy link
Contributor

When the init command is run, the fork-network command writes state roots to the DB by key "FORK_TOOL_SHARD_ID:{shard_id}", and later reads them back by iterating over that prefix. But there is inconsistent treatment of these ShardIds sometimes as ShardIds and sometimes as ShardIndexes. Firstly, the name of the key itself indicates we should expect ShardIds to be there. But when we write them, we collect the state roots in a Vec in order of ShardLayout::shard_ids(), and then iter().enumerate() over that to write the (shard_id, state_root), which is really a shard index. Then when we read them back, we index into that array with shard_uid.shard_id as usize, which doesn't work when the ShardIds are shuffled.

When running this on a localnet where shard IDs are shuffled, this gives us this error:

thread '<unnamed>' panicked at tools/fork-network/src/cli.rs:732:22:
called `Result::unwrap()` on an `Err` value: StorageInconsistentState("Failed to find root node EEMN57dXxneVQHDZ5aZreA8SHF6Xtee95CKpGfeMEmGN in memtrie")

So fix it by actually writing a real ShardId to the FORK_TOOL_SHARD_ID:... keys, and actually parsing the ShardId from that key when iterating (instead of an enumerate() to get the shard ID). And just be more careful about the ShardId <-> ShardIndex mapping throughout.

When the `init` command is run, the fork-network command writes
state roots to the DB by key "FORK_TOOL_SHARD_ID:{shard_id}", and
later reads them back by iterating over that prefix. But there is
inconsistent treatment of these ShardIds sometimes as ShardIds and sometimes
as ShardIndexes. Firstly, the name of the key itself indicates we should expect
ShardIds to be there. But when we write them, we collect the state roots in a Vec
in order of ShardLayout::shard_ids(), and then iter().enumerate() over that to write
the (shard_id, state_root), which is really a shard index. Then when we read them
back, we index into that array with `shard_uid.shard_id as usize`, which doesn't work
when the ShardIds are shuffled.

When running this on a localnet where shard IDs are shuffled, this gives us this error:

```
thread '<unnamed>' panicked at tools/fork-network/src/cli.rs:732:22:
called `Result::unwrap()` on an `Err` value: StorageInconsistentState("Failed to find root node EEMN57dXxneVQHDZ5aZreA8SHF6Xtee95CKpGfeMEmGN in memtrie")
```

So fix it by actually writing a real ShardId to the `FORK_TOOL_SHARD_ID:...` keys, and actually
parsing the ShardId from that key when iterating (instead of an enumerate() to get the shard ID). And
just be more careful about the ShardId <-> ShardIndex mapping throughout.
Copy link
Contributor

@wacban wacban left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 0% with 46 lines in your changes missing coverage. Please review.

Project coverage is 71.44%. Comparing base (8e0b26f) to head (280ea9f).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
tools/fork-network/src/cli.rs 0.00% 46 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12469      +/-   ##
==========================================
- Coverage   71.46%   71.44%   -0.02%     
==========================================
  Files         838      838              
  Lines      169363   169386      +23     
  Branches   169363   169386      +23     
==========================================
- Hits       121029   121015      -14     
- Misses      42986    43022      +36     
- Partials     5348     5349       +1     
Flag Coverage Δ
backward-compatibility 0.16% <0.00%> (-0.01%) ⬇️
db-migration 0.16% <0.00%> (-0.01%) ⬇️
genesis-check 1.29% <0.00%> (-0.01%) ⬇️
integration-tests 39.33% <0.00%> (+0.02%) ⬆️
linux 70.75% <0.00%> (-0.04%) ⬇️
linux-nightly 71.00% <0.00%> (-0.02%) ⬇️
macos 51.02% <0.00%> (-0.02%) ⬇️
pytests 1.60% <0.00%> (-0.01%) ⬇️
sanity-checks 1.40% <0.00%> (-0.01%) ⬇️
unittests 63.97% <0.00%> (-0.03%) ⬇️
upgradability 0.21% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Merged via the queue into near:master with commit 6dab9ee Nov 18, 2024
29 checks passed
@marcelo-gonzalez marcelo-gonzalez deleted the fork-network-shards branch November 18, 2024 01:59
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