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: block apply in the past #11767

Merged
merged 6 commits into from
Jul 12, 2024
Merged

Conversation

Trisfald
Copy link
Contributor

This PR fixes commands such as neard view-state apply and neard view-state apply-range when used on a block height that doesn't have flat storage built for it.

It works by re enabling the feature of accessing trie nodes without paying gas costs (removed in #10490).

Probably it also fixes #8741.

@Trisfald Trisfald requested a review from a team as a code owner July 11, 2024 09:02
@Trisfald Trisfald requested review from akhi3030 and wacban July 11, 2024 09:02
Copy link

codecov bot commented Jul 11, 2024

Codecov Report

Attention: Patch coverage is 48.51485% with 52 lines in your changes missing coverage. Please review.

Project coverage is 71.80%. Comparing base (5928beb) to head (5b8d3ca).
Report is 8 commits behind head on master.

Files Patch % Lines
tools/state-viewer/src/commands.rs 39.39% 19 Missing and 1 partial ⚠️
tools/state-viewer/src/cli.rs 23.52% 13 Missing ⚠️
chain/chain/src/types.rs 0.00% 8 Missing ⚠️
tools/state-viewer/src/apply_chunk.rs 85.71% 0 Missing and 4 partials ⚠️
chain/chain/src/runtime/mod.rs 0.00% 3 Missing ⚠️
core/store/src/trie/mod.rs 0.00% 3 Missing ⚠️
tools/state-viewer/src/apply_chain_range.rs 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11767      +/-   ##
==========================================
- Coverage   71.82%   71.80%   -0.02%     
==========================================
  Files         792      792              
  Lines      162765   162806      +41     
  Branches   162765   162806      +41     
==========================================
+ Hits       116899   116901       +2     
- Misses      40822    40853      +31     
- Partials     5044     5052       +8     
Flag Coverage Δ
backward-compatibility 0.23% <0.00%> (-0.01%) ⬇️
db-migration 0.23% <0.00%> (-0.01%) ⬇️
genesis-check 1.35% <0.00%> (-0.01%) ⬇️
integration-tests 37.96% <0.00%> (+0.09%) ⬆️
linux 71.31% <48.51%> (-0.04%) ⬇️
linux-nightly 71.40% <48.51%> (-0.01%) ⬇️
macos 54.52% <48.51%> (-0.07%) ⬇️
pytests 1.58% <0.00%> (-0.01%) ⬇️
sanity-checks 1.38% <0.00%> (-0.01%) ⬇️
unittests 66.24% <48.51%> (-0.02%) ⬇️
upgradability 0.27% <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.

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.

Looks great I just have some nice and minor comments!

chain/chain/src/runtime/mod.rs Show resolved Hide resolved
tools/state-viewer/src/apply_chain_range.rs Outdated Show resolved Hide resolved
Comment on lines 237 to 241
let mut storage =
RuntimeStorageConfig::new(*chunk_inner.prev_state_root(), use_flat_storage);
if use_trie_for_free {
storage.source = StorageDataSource::DbTrieOnly;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like use flat storage and source should be exclusive options. Can you structure it that way in code?

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 have started in such a way, but then I realized you could potentially use both together.. for example use flat storage and still access trie for free.
No strong opinion if you prefer I'll make the options in CLI exclusive

Copy link
Contributor

Choose a reason for hiding this comment

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

When would it be possible to use both?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think when flat storage is enabled we don't read trie at all (except for the non-inlined values).

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'm referring to this struct

pub struct RuntimeStorageConfig {
    pub state_root: StateRoot,
    pub use_flat_storage: bool,
    pub source: StorageDataSource,
    pub state_patch: SandboxStatePatch,
}

API-wise it gives the impression use-flat-storage is orthogonal from the StorageDataSource. You can use flat storage or not over a DB that charges gas costs or not.
Yes not charging gas costs is similar to using flat-storage in some contexts (replaying block)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah well then this struct is wrong too :) Given the source is already there the 'use_flat_storage' should be derivable from it. I would just remove it. If you don't want to go so far in this PR perhaps you can just add a new ctor? I just dislike this pattern of calling ctor and then immediately overwriting one of the fields, it seems very volatile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, I'll add another constructor!

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 have made some sweeping changes to make stuff more explicit. No more flags to enable flat storage or free tries, instead now there's an enum StorageSource with all different options

Comment on lines -228 to -230
/// TODO(#8741): doesn't work. Remove dependency on flat storage
/// by simulating correct costs. Consider reintroducing DbTrieOnly
/// read mode removed at #10490.
Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

Comment on lines 271 to 276
#[clap(long)]
use_flat_storage: bool,
/// Use the data stored in trie, but without paying extra gas costs.
/// This could be used to simulate flat storage when the latter is not present.
#[clap(long)]
use_trie_for_free: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is just fine as is but I wonder if we could auto detect the right configuration. It should be possible to check
a) if flat storage should be used, based on the protocol version of the block
b) if flat storage is present at the given height

Then you can setup the db source based on that. This is totally optional as in practice we will almost always use (false, true).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting idea, maybe for improvement as a default it could use autodetect, I'll file an issue. I feel there are interesting scenarios (to check some stuff) where someone would prefer to fine tune the flags, so it makes sense to leave such option available

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah in the follow up you could have something like enum StorageSource { AutoDetect, Trie, TrieFree, FS }

pub(crate) fn resulting_chunk_extra(
result: &ApplyChunkResult,
gas_limit: Gas,
protocol_version: ProtocolVersion,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

tools/state-viewer/src/commands.rs Show resolved Hide resolved
@wacban wacban requested a review from pugachAG July 11, 2024 10:04
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!

@@ -180,27 +181,42 @@ impl StateViewerSubCommand {
}
}

#[derive(clap::ValueEnum, Debug, Clone, Copy)]
#[clap(rename_all = "kebab_case")]
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL & LOL about kebab_case

chain/chain/src/types.rs Outdated Show resolved Hide resolved
@Trisfald Trisfald added this pull request to the merge queue Jul 12, 2024
Merged via the queue into near:master with commit 837a29f Jul 12, 2024
28 of 30 checks passed
@Trisfald Trisfald deleted the fix-block-apply-in-the-past branch July 12, 2024 13:02
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.

Support block replayability with enabled flat storage
2 participants