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

chore(state-transition): cleaned up state in state-transition UTs #2518

Merged
merged 2 commits into from
Feb 17, 2025

Conversation

abi87
Copy link
Collaborator

@abi87 abi87 commented Feb 17, 2025

Made state handling closer to what we have in production.
I need this to keep building unit tests in both state-transition (see ongoing #2516) or other packages (validators APIs, not yet pushed)

@abi87 abi87 requested a review from a team as a code owner February 17, 2025 22:22
@abi87 abi87 self-assigned this Feb 17, 2025
@@ -58,23 +58,24 @@ type (
TestStateProcessorT = core.StateProcessor
)

type testKVStoreService struct {
ctx sdk.Context
Copy link
Collaborator Author

@abi87 abi87 Feb 17, 2025

Choose a reason for hiding this comment

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

this was a first attempt at creating state but it is wrong.
We should use the context provided by the transactions and extract the state frorm there, not use and reuse a single context.
It worked so far because we never handled rejected blocks or multiple blocks for the same height, which we need in upcoming tests.

Copy link

codecov bot commented Feb 17, 2025

Codecov Report

Attention: Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.

Project coverage is 32.30%. Comparing base (ac596fd) to head (371cda6).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
testing/state-transition/state-transition.go 84.61% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2518   +/-   ##
=======================================
  Coverage   32.30%   32.30%           
=======================================
  Files         350      350           
  Lines       15673    15674    +1     
  Branches       20       20           
=======================================
+ Hits         5063     5064    +1     
  Misses      10247    10247           
  Partials      363      363           
Files with missing lines Coverage Δ
testing/state-transition/state-transition.go 84.84% <84.61%> (+0.23%) ⬆️

@@ -135,7 +138,7 @@ func SetupTestState(t *testing.T, cs chain.Spec) (
)

ctx := transition.NewTransitionCtx(
context.Background(),
ms,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is important because the state passed to state_processor is extract from an sdk.Context. If we use any ordinary context.Context tests would panic.

beaconState := statedb.NewBeaconStateFromDB(kvStore, cs)

ms := sdk.NewContext(cms.CacheMultiStore(), true, log.NewNopLogger())
beaconState := statedb.NewBeaconStateFromDB(kvStore.WithContext(ms), cs)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this beaconState is basically what storage.StateFromContext does.
I should consider refactoring it out to make it clear

return storage.NewKVStore(store)
}

//nolint:gochecknoglobals // unexported and use only in tests
var testStoreKey = storetypes.NewKVStoreKey("state-transition-tests")

func initTestStores() (*beacondb.KVStore, *depositstore.KVStore, error) {
func BuildTestStores() (
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

pre-emptively exporting BuildTestStores which I am going to use in other packages

return storage.NewKVStore(store)
}

//nolint:gochecknoglobals // unexported and use only in tests
//nolint:gochecknoglobals // unexported and used only in tests
var testStoreKey = storetypes.NewKVStoreKey("state-transition-tests")
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the purpose of this special key? iirc the key by default in prod is beacond

Copy link
Collaborator Author

@abi87 abi87 Feb 17, 2025

Choose a reason for hiding this comment

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

@rezbera I could have reused the prod one, but when I first built these tests I wanted to make sure I fully controlled them and I could use a different one.
One interesting point here is that Comet requires this very key to work: if you use this key in MountStoreWithDB and copy it in OpenKVStore, it won't be able to access the state with the copy.
This is something I would not have found out if I had not tried to use a test key

Copy link
Contributor

@rezbera rezbera left a comment

Choose a reason for hiding this comment

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

doesn't hit the critical path so nothing contentious here. bit tricky to see the end-goal though

@abi87
Copy link
Collaborator Author

abi87 commented Feb 17, 2025

doesn't hit the critical path so nothing contentious here. bit tricky to see the end-goal though

I agree. I made a single PR out of this just to make tests follow prod in the way state is handled (although not showing it by other tests, one has to take my word or dig into prod).

I hope to come up soon with tests where multiple, independent states must be created on top of a finalized state, in which case I really need this PR and the caches it introduces, to make the tests independent.

@abi87 abi87 merged commit 5e3edfa into main Feb 17, 2025
19 checks passed
@abi87 abi87 deleted the fix-state-in-state-transitions-UTs branch February 17, 2025 22:56
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.

2 participants