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

refactor: handle state patching in a more principled way #6967

Merged
merged 4 commits into from
Jun 9, 2022

Conversation

matklad
Copy link
Contributor

@matklad matklad commented Jun 3, 2022

The main idea here is to a avoid cfg-guarding all places related to state patching, and instead guard only patch-creation API. Stated positively, presense of StatePatch object is a proof that you are in a sandbox.

@matklad matklad changed the title M/state patch refactor: handle state patching in a more principled way Jun 3, 2022
@matklad
Copy link
Contributor Author

matklad commented Jun 3, 2022

follow up to #6962

chain/chain/src/chain.rs Outdated Show resolved Hide resolved
self.runtime_adapter.get_epoch_protocol_version(block.header().epoch_id())?;
/// Sandbox node specific operations
impl Chain {
pub fn patch_state(&mut self, patch: StatePatch) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we still add #[cfg(feature = "sandbox")] 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 we shouldn't. The current design guarantees that you only can have an StatePatch if cfg(feature = "sandbox") is true. That is, the only thing which is gated by the feature are StatePatch constructors.

I am bit torn on whether this is a good approach. I like it because it is more convenient (conditional compilation is localized) and because it is more robust (you don't need to make sure that every use-site is feature gated, there's type-system level guarantee that, to do anything with StatePatch, you have to be sandbox).

I don't like it because it is somewhat non-obvious, and any future reader would think "wait, shouldn't this be gated by cfg", and would have to spend some cycles looking at the definition of StatePatch and figuring the pattern of gating constructors out.

On balance, I think this falls under acceptable and encourages uses of the type system, and is not an abuse just to add some fanciness. But happy to reconsider if folks feel otherwise!

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a comment in the code documenting that? I think that should be good!

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for more documentation here..

and I wonder if we maybe should call it 'SandboxStatePatch'?

(as I'm worried that future reader would think that state_patch is a 'normal' part of the system used in production)

@matklad matklad force-pushed the m/state-patch branch 3 times, most recently from 996418b to b9b7eb7 Compare June 8, 2022 14:38
@matklad matklad marked this pull request as ready for review June 8, 2022 14:39
@matklad matklad requested a review from a team as a code owner June 8, 2022 14:39
@matklad matklad requested review from nagisa and mm-near and removed request for nagisa June 8, 2022 14:39
@matklad matklad force-pushed the m/state-patch branch 2 times, most recently from 9c81fe1 to 61bf628 Compare June 8, 2022 15:30
) -> Result<ApplyResult, RuntimeError> {
let _span = tracing::debug_span!(
target: "runtime",
"apply",
num_transactions = transactions.len())
.entered();

if states_to_patch.is_some() && !cfg!(feature = "sandbox") {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd leave this -- as the 'final' line of defense.

}

impl StatePatch {
// NB: it's crucial that all creation APIs are guarded with `#[cfg(feature = "sandbox")]`.
Copy link
Contributor

Choose a reason for hiding this comment

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

'records' is a public field, so people could create StatePatch as:

StatePatch {records: XX}

right?

self.runtime_adapter.get_epoch_protocol_version(block.header().epoch_id())?;
/// Sandbox node specific operations
impl Chain {
pub fn patch_state(&mut self, patch: StatePatch) {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for more documentation here..

and I wonder if we maybe should call it 'SandboxStatePatch'?

(as I'm worried that future reader would think that state_patch is a 'normal' part of the system used in production)

@matklad matklad requested a review from mm-near June 9, 2022 13:09
@near-bulldozer near-bulldozer bot merged commit 600ef89 into near:master Jun 9, 2022
nikurt pushed a commit that referenced this pull request Jun 13, 2022
The main idea here is to a avoid cfg-guarding all places related to state patching, and instead guard only patch-creation API. Stated positively, presense of `StatePatch` object is a proof that you are in a sandbox.
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.

None yet

3 participants