-
Notifications
You must be signed in to change notification settings - Fork 5
feat: triedb.Database.Update options via statedb.Commit
#190
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
Conversation
|
Using the same option type for both package stateconf
type SnapshotUpdateOption = options.Option[snapshotUpdateConfig] // unchanged
type snapshotUpdateConfig {
payload any // unchanged; I don't love this being `any`, but that's a different issue
}
type TrieDBUpdateOption = options.Option[triedbUpdateConfig]
type triedbUpdateConfig {
parentBlock *common.Hash // pointer to differentiate from no Option setting the value
}
func WithParentBlockHash(h common.Hash) TrieDBUpdateOption {
return options.Func[triedbUpdateConfig](func(c *triedbUpdateConfig) {
c.parentBlock = &h
})
}
type StateDBCommitOption = options.Option[stateDBCommitConfig]
type stateDBCommitConfig struct {
snaphotOpts []SnapshotUpdateOption
triedbOpts []TrieDBUpdateOption
}
func WithSnapshotUpdateOpts(opts ... SnapshotUpdateOption) StateDBCommitOption {
return options.Func[stateDBCommitConfig](func(c *stateDBCommitConfig) {
// I don't like append() because there's no way to remove options, but that's a weakly held opinion
c.snapshotOpts = opts
})
}
func ExtractSnapshotUpdateOpts(opts ...StateDBCommitOption) []SnapshotUpdateOption {
return options.As(opts...).snapshotOpts
}
// Similarly for WithTrieDBUpdateOpts() and ExtractTrieDBUpdateOpts().And then used as: package state
func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, opts ...stateconf.StateDBCommitOption) (common.Hash, error) {
// ...
s.db.TrieDB().Update(/* ... */, stateconf.ExtractTrieDBUpdateOpts(opts...)...)
// ...
s.snaps.Update(/* ... */, stateconf.ExtractSnapshotUpdateOpts(opts...)...)
// ...
}RationaleAt risk of being too philosophical about engineering, one of my favourite documents is Bumper-Sticker API Design. This sticker is one I regularly refer to:
The 3-type design makes it impossible to do wrong things. PSMy code is quick and rough. Please include comments on exported types, also calling out behaviour that may be ambiguous (e.g. overwrite instead of append). |
statedb.Committriedb.Database.Update options via statedb.Commit
Why this should be merged
To allow more thorough handling of duplicate state roots (or any other info other users would like), additional information can be provided to a call of
statedb.Commit. This change allows arbitrary types to be sent totriedbas well as theSnapshotTree. However, this is a breaking change for those using the functionality already, since the snapshot commit option is wrapped with another call.How this works
See the edited libevm test for usage.
How this was tested
Edited test case to include
TrieDBUpdateOptionand ensures the payload is sent.