Skip to content

Scoped WorldState#9028

Merged
asdacap merged 8 commits into
masterfrom
feature/scoped-storage
Aug 14, 2025
Merged

Scoped WorldState#9028
asdacap merged 8 commits into
masterfrom
feature/scoped-storage

Conversation

@asdacap

@asdacap asdacap commented Jul 23, 2025

Copy link
Copy Markdown
Contributor
  • Require Remove use of blockchain processor in RPC #9021
  • Replace SetBaseBlock with BeginScope(BlockHeader baseBlock) that returns a disposable.
  • Any IWorldState can only be called in between starting and closing of the scope.
  • Most change is in test.
  • Notable non-test code is in WorldState and BranchProcessor.
  • The rest was isolated in previous PRs.

Types of changes

What types of changes does your code introduce?

  • New feature (a non-breaking change that adds functionality)
  • Refactoring

Testing

Requires testing

  • Yes
  • No

If yes, did you write tests?

  • Yes
  • No

Notes on testing

  • Mainnet can sync normally
  • Archive sync from genesis

@asdacap asdacap changed the title Scoped storage Scoped WorldState Jul 24, 2025
Comment thread src/Nethermind/Nethermind.Consensus/Processing/BranchProcessor.cs
Comment thread src/Nethermind/Nethermind.Consensus/Processing/BranchProcessor.cs
@asdacap asdacap marked this pull request as ready for review July 24, 2025 05:12
@benaadams

Copy link
Copy Markdown
Member
  • Any IWorldState can only be called in between starting and closing of the scope.

Would be better then if IWorldState was the scope; so is a compilation constraint rather than a mode switch of worldstate?

e.g. rather than

using (stateProvider.BeginScope(null))
{
    stateProvider.CreateAccount(TestItem.AddressA, UInt256.One);
    stateProvider.CreateAccount(TestItem.AddressB, UInt256.One);
    stateProvider.Commit(London.Instance);

you would do

using (var state = stateProvider.BeginScope(null))
{
    state.CreateAccount(TestItem.AddressA, UInt256.One);
    state.CreateAccount(TestItem.AddressB, UInt256.One);
    state.Commit(London.Instance);

@asdacap

asdacap commented Jul 24, 2025

Copy link
Copy Markdown
Contributor Author

I tried that about 1 year ago, @tanishqjasoria tried that about half year ago. Result it A LOT of refactor. And we still dont have proper worldstate scope.

Comment thread src/Nethermind/Nethermind.State/WorldState.cs Outdated

@LukaszRozmej LukaszRozmej left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would be better if IWorldState was the scope and it would have been created from something like IWoldStateSource/Manager


InitializeTestState(test, stateProvider, specProvider);
// Genesis processing
using (stateProvider.BeginScope(null))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you make some constant called Genesis for the parameter?

worldState.SetBaseBlock(header);
return new ReadOnlyTxProcessingScope(transactionProcessor, worldState);
IDisposable closer = worldState.BeginScope(header);
return new ReadOnlyTxProcessingScope(transactionProcessor, closer, worldState);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

closer is terrible name, maybe just call it currentScope?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

@LukaszRozmej

Copy link
Copy Markdown
Member

I tried that about 1 year ago, @tanishqjasoria tried that about half year ago. Result it A LOT of refactor. And we still dont have proper worldstate scope.

image

@benaadams benaadams requested a review from Copilot July 26, 2025 13:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces scoped WorldState by replacing SetBaseBlock with a disposable BeginScope pattern. The change ensures that any IWorldState operations must occur within a properly opened and closed scope, enhancing safety and preventing misuse of state operations outside of valid contexts.

  • Replace SetBaseBlock method with BeginScope(BlockHeader baseBlock) that returns a disposable
  • Add scope validation to prevent WorldState operations outside of valid scopes
  • Update all test code to use the new scoped pattern with proper dispose handling

Reviewed Changes

Copilot reviewed 59 out of 59 changed files in this pull request and generated 1 comment.

File Description
src/Nethermind/Nethermind.State/WorldState.cs Core implementation of scoped WorldState with BeginScope method and scope validation guards
src/Nethermind/Nethermind.Evm/State/IWorldState.cs Interface changes removing SetBaseBlock and adding BeginScope and IsInScope
src/Nethermind/Nethermind.Consensus/Processing/BranchProcessor.cs Updated to use scoped WorldState pattern in block processing
Multiple test files Updated test implementations to use BeginScope with proper dispose patterns
Comments suppressed due to low confidence (2)

src/Nethermind/Nethermind.Consensus/Processing/BranchProcessor.cs:69

  • [nitpick] The variable name stateProvider is inconsistent with the field name _stateProvider used elsewhere in the class. Consider using the field directly or renaming for consistency.
        if (stateProvider.IsInScope)

src/Nethermind/Nethermind.Consensus/Processing/BranchProcessor.cs:85

  • The variable name stateProvider should be _stateProvider to match the class field being used throughout the method.
            worldStateCloser = stateProvider.BeginScope(baseBlock);

Comment thread src/Nethermind/Nethermind.State/WorldState.cs Outdated
@asdacap asdacap force-pushed the cleanup/remove-use-of-branchprocessor-in-rpc branch from 8694204 to 91e14bd Compare August 1, 2025 12:07
@asdacap asdacap force-pushed the feature/scoped-storage branch from e4f31cb to f2d5d45 Compare August 1, 2025 12:10
@asdacap asdacap force-pushed the feature/scoped-storage branch from f2d5d45 to 5749f95 Compare August 4, 2025 05:05
@asdacap asdacap mentioned this pull request Aug 5, 2025
7 tasks
@asdacap asdacap force-pushed the feature/scoped-storage branch from 5749f95 to 3ee99cd Compare August 5, 2025 09:14
@asdacap asdacap mentioned this pull request Aug 7, 2025
5 tasks
Base automatically changed from cleanup/remove-use-of-branchprocessor-in-rpc to master August 11, 2025 06:34
@tanishqjasoria tanishqjasoria self-requested a review August 12, 2025 09:43
Comment on lines -117 to -119
// Init state if we need system calls before actual processing starts
worldState.SetBaseBlock(blockTree.Head?.Header);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I dont know too.

worldState.SetBaseBlock(header);
return new ReadOnlyTxProcessingScope(transactionProcessor, worldState);
IDisposable closer = worldState.BeginScope(header);
return new ReadOnlyTxProcessingScope(transactionProcessor, closer, worldState);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

Comment thread src/Nethermind/Nethermind.Consensus/Processing/BranchProcessor.cs
@asdacap asdacap merged commit cf526ee into master Aug 14, 2025
78 checks passed
@asdacap asdacap deleted the feature/scoped-storage branch August 14, 2025 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants