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

forknet: support arbitrary shard layouts #12938

Merged
merged 9 commits into from
Feb 19, 2025

Conversation

marcelo-gonzalez
Copy link
Contributor

We'd like to use forknet with more shards than we have currently on mainnet, or just any arbitrary shard layout. But it won't work to just change the shard layout in the genesis file, since we need to rewrite everything in the tries. So this PR implements support for an optional custom shard layout when generating the state.

To do this, we use the existing StorageMutator that keeps a list of updates per shard, and we change it to be aware of which shards belong to the source shard layout and which belong to the target. We still have one thread per source shard, but now it writes state to shards that possibly don't exist in the shard layout.

This also requires writing a new bandwidth scheduler state in each target shard that doesn't exist in the source shard layout

@marcelo-gonzalez
Copy link
Contributor Author

I know it is kind of difficult for reviewers to test this sorry... I have been testing w some ad hoc scripts, but next week in a separate PR maybe I can clean some of them up so testing is easier

Copy link

codecov bot commented Feb 15, 2025

Codecov Report

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

Project coverage is 70.26%. Comparing base (9a0a06c) to head (989f7a5).
Report is 11 commits behind head on master.

Files with missing lines Patch % Lines
tools/fork-network/src/storage_mutator.rs 0.00% 288 Missing ⚠️
tools/fork-network/src/cli.rs 0.00% 190 Missing ⚠️
tools/fork-network/src/delayed_receipts.rs 0.00% 47 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12938      +/-   ##
==========================================
- Coverage   70.37%   70.26%   -0.11%     
==========================================
  Files         854      855       +1     
  Lines      175421   176151     +730     
  Branches   175421   176151     +730     
==========================================
+ Hits       123444   123780     +336     
- Misses      46848    47223     +375     
- Partials     5129     5148      +19     
Flag Coverage Δ
backward-compatibility 0.35% <0.00%> (-0.01%) ⬇️
db-migration 0.35% <0.00%> (-0.01%) ⬇️
genesis-check 1.41% <0.00%> (-0.01%) ⬇️
linux 69.90% <0.00%> (-0.30%) ⬇️
linux-nightly 69.90% <0.00%> (-0.11%) ⬇️
pytests 1.72% <0.00%> (-0.01%) ⬇️
sanity-checks 1.53% <0.00%> (-0.01%) ⬇️
unittests 70.10% <0.00%> (-0.11%) ⬇️
upgradability 0.35% <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.

}
None => shard_layout.clone(),
};

// Flat state can be at different heights for different shards.
// That is fine, we'll simply lookup state root for each .
let fork_heads = get_fork_heads(&all_shard_uids, store.clone())?;
Copy link
Contributor

Choose a reason for hiding this comment

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

I had to look into the function get_fork_heads, is a misleading name 🤣

epoch_length,
num_seats,
block_height,
flat_head.height + 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you clarify why we need to add +1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I honestly don't know.. I bet it doesn't really matter too much. But here I was just making sure to keep the old behavior because in the current code, we do let block_height = block_height + 1; before writing it to FORK_TOOL_BLOCK_HEIGHT

};

let mut link_allowances = Vec::new();
// TODO: maybe do something other than this
Copy link
Contributor

Choose a reason for hiding this comment

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

A few options:

  • compute average allowance from all links
  • use max allowance every time

Some(root) => commit_to_existing_state(shard_tries, shard_uid, root, updates)?,
None => {
let state_root = commit_to_new_state(shard_tries, shard_uid, updates)?;
// TODO: load memtrie
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the purpose of loading memtrie here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it will all just be a lot faster if there ends up being lots of updates to that shard. thats why right now this fork-network command loads memtries for all shards before it does all the rewriting so itll be faster (although it uses an insane amount of memory)

struct InitCmd {
/// If given, the shard layout in this file will be used to generate the forked genesis state
#[arg(long)]
pub shard_layout_file: Option<PathBuf>,
Copy link
Contributor

@Trisfald Trisfald Feb 19, 2025

Choose a reason for hiding this comment

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

In practice, this shard layout file, is it supposed to be a json file with the same content of the field shard_layout in genesis?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, just copy pasting it to a new file should work

Copy link
Contributor

@Trisfald Trisfald left a comment

Choose a reason for hiding this comment

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

🚀
thanks!

@marcelo-gonzalez marcelo-gonzalez added this pull request to the merge queue Feb 19, 2025
Merged via the queue into near:master with commit ac5aa6a Feb 19, 2025
29 checks passed
@marcelo-gonzalez marcelo-gonzalez deleted the forknet-shard-layout branch February 19, 2025 17:04
github-merge-queue bot pushed a commit that referenced this pull request Feb 21, 2025
#12938 changed the format of the
data we write to the `FORK_TOOL` Misc column keys in the `init` command,
but didn't keep backward compatibility. But this should have been done
because we need to run `fork-network` again when starting a forknet so
we can run the `set-validators` and `finalize` commands. So this PR
reads the values the old way if there's nothing in the
`FORK_TOOL_FLAT_HEAD` key
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