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): review usages of TrieState #2912

Closed
wants to merge 15 commits into from

Conversation

qdm12
Copy link
Contributor

@qdm12 qdm12 commented Nov 1, 2022

Changes

  • Split out snapshotting feature out of the TrieState method
  • Add TrieState method Snapshot returning a snapshotted trie state
  • GetRuntimeMetadata and GetRuntimeVersion do not need the trie
  • Temporary modifiable trie snapshot for each pending transaction (instead of sharing the same trie)
  • Move snapshotting operations closer to where they are needed
  • Rework some related error wrappings and tests
  • Remove impossible nil trie error check

Tests

go test -tags integration github.com/ChainSafe/gossamer

Issues

Primary Reviewer

@timwu20

@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch 4 times, most recently from ce7b981 to bfa2fb5 Compare November 2, 2022 15:22
@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch from bfa2fb5 to 2fc8827 Compare November 3, 2022 13:39
@qdm12 qdm12 changed the base branch from development to qdm12/dot/sync/processblockdata November 3, 2022 13:39
@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch 2 times, most recently from 71f5f40 to 97fe789 Compare November 3, 2022 14:28
@qdm12 qdm12 force-pushed the qdm12/dot/sync/processblockdata branch from 015c45d to 294d3ed Compare November 4, 2022 13:15
@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch 3 times, most recently from 229542a to d1ee86d Compare November 7, 2022 15:19
@qdm12 qdm12 force-pushed the qdm12/dot/sync/processblockdata branch 3 times, most recently from 56ab6ce to e2429de Compare November 9, 2022 13:46
@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch 2 times, most recently from 0c6a076 to 4cbd713 Compare November 9, 2022 16:22
@qdm12 qdm12 force-pushed the qdm12/dot/sync/processblockdata branch from e2429de to 8c5bac2 Compare November 9, 2022 16:41
@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch 6 times, most recently from e5c675e to c69f6f0 Compare November 15, 2022 11:07
@qdm12 qdm12 force-pushed the qdm12/dot/sync/processblockdata branch from 8c5bac2 to eab9165 Compare November 15, 2022 11:20
@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch 2 times, most recently from bba6fbb to ed7a7e5 Compare November 15, 2022 13:08
@qdm12 qdm12 force-pushed the qdm12/dot/sync/processblockdata branch from eab9165 to 2de6f19 Compare November 15, 2022 15:08
@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch from ed7a7e5 to 4bf0404 Compare November 15, 2022 15:25
dot/core/messages_test.go Outdated Show resolved Hide resolved
dot/core/service.go Outdated Show resolved Hide resolved
validity, err = rt.ValidateTransaction(externalExt)
if err != nil {
logger.Debugf("failed to validate transaction: %s", err)
return nil, err
}
rt.SetContextStorage(nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we have to do this now?

Copy link
Contributor Author

@qdm12 qdm12 Jan 24, 2023

Choose a reason for hiding this comment

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

Clearing the storage trie state for that runtime, so that it can be garbage collected. Otherwise the runtime may hold it or some time/forever (especially since we are keeping older runtimes in memory right now). In most cases, these are temporary states that we don't want to keep anyway (such as here).
The point is to reduce/avoid memory leaks, so this aims at setting the state and then clearing it from the runtime structure; all calls get a new trie state anyway

txnValidity, err := rt.ValidateTransaction(externalExt)
rt.SetContextStorage(nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we set it as nil? Shouldn't we restore back to original ts?

}
return rt.Version(), nil

return runtime.Version(), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be nice to just cache this, so we don't need to execute the Version function every time. Can also help when we don't store every running runtime instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's already cached, see

Version Version

}
return rt.Metadata()

return runtime.Metadata()
Copy link
Contributor

Choose a reason for hiding this comment

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

same with this.. should we consider caching this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, created #3065

@@ -60,6 +61,7 @@ func TestStorageState_RegisterStorageObserver_Multi(t *testing.T) {
ss := newTestStorageState(t)
ts, err := ss.TrieState(nil)
require.NoError(t, err)
ts = ts.Snapshot()
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need to snapshot if we are overwriting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because

  1. Snapshot() was part of TrieState() previously
  2. We don't want to modify the underlying data of the original ts from the storage state. We are overriding the ts variable, but that's just a pointer variable, and Snapshot gives it another pointer.

@@ -240,6 +240,8 @@ func generateBlockWithRandomTrie(t *testing.T, serv *Service,
trieState, err := serv.Storage.TrieState(nil)
require.NoError(t, err)

trieState = trieState.Snapshot()
Copy link
Contributor

Choose a reason for hiding this comment

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

again why do we need to snapshot 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.

return fmt.Errorf("failed to execute block %d: %w", block.Header.Number, err)
}

if err = s.blockImportHandler.HandleBlockImport(block, ts, announceImportedBlock); err != nil {
return err
rt.SetContextStorage(nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

could we not just defer this line before line 267

Copy link
Contributor Author

@qdm12 qdm12 Jan 24, 2023

Choose a reason for hiding this comment

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

We could, I did that more-explicit-less-deferred to highlight the storage is no longer needed right after rt.ExecuteBlock(block), but I can change it to a defer if you feel it's better.

@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch 2 times, most recently from e50c403 to b438802 Compare January 25, 2023 15:15
@qdm12 qdm12 force-pushed the qdm12/state/triestate-review branch from b438802 to 3555018 Compare January 26, 2023 08:54
@qdm12 qdm12 requested a review from timwu20 January 26, 2023 08:57
@dimartiro dimartiro mentioned this pull request May 30, 2023
27 tasks
@timwu20 timwu20 closed this Aug 14, 2023
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.

4 participants