-
Notifications
You must be signed in to change notification settings - Fork 110
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
Conversation
ce7b981
to
bfa2fb5
Compare
bfa2fb5
to
2fc8827
Compare
71f5f40
to
97fe789
Compare
015c45d
to
294d3ed
Compare
229542a
to
d1ee86d
Compare
56ab6ce
to
e2429de
Compare
0c6a076
to
4cbd713
Compare
e2429de
to
8c5bac2
Compare
e5c675e
to
c69f6f0
Compare
8c5bac2
to
eab9165
Compare
bba6fbb
to
ed7a7e5
Compare
eab9165
to
2de6f19
Compare
ed7a7e5
to
4bf0404
Compare
4710df0
to
1babd11
Compare
1babd11
to
d2c3dae
Compare
d2c3dae
to
47ed6d1
Compare
validity, err = rt.ValidateTransaction(externalExt) | ||
if err != nil { | ||
logger.Debugf("failed to validate transaction: %s", err) | ||
return nil, err | ||
} | ||
rt.SetContextStorage(nil) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Line 59 in 4ea0a70
Version Version |
} | ||
return rt.Metadata() | ||
|
||
return runtime.Metadata() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
Snapshot()
was part ofTrieState()
previously- We don't want to modify the underlying data of the original
ts
from the storage state. We are overriding thets
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #2912 (comment)
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
e50c403
to
b438802
Compare
b438802
to
3555018
Compare
Changes
TrieState
methodTrieState
methodSnapshot
returning a snapshotted trie stateGetRuntimeMetadata
andGetRuntimeVersion
do not need the trieTests
go test -tags integration github.com/ChainSafe/gossamer
Issues
Primary Reviewer
@timwu20