-
Notifications
You must be signed in to change notification settings - Fork 126
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
feat(internal/client/db) Introduce api.Backend
interface and db.Backend
implementation
#4405
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
timwu20
force-pushed
the
tim/client-db
branch
3 times, most recently
from
December 12, 2024 21:05
2a05b0a
to
a063b68
Compare
timwu20
requested review from
dimartiro,
EclesioMeloJunior,
jimjbrettj and
P1sar
as code owners
December 13, 2024 17:33
timwu20
force-pushed
the
tim/client-db
branch
from
December 13, 2024 19:33
b98394b
to
35dc78c
Compare
timwu20
force-pushed
the
refactor/client-db
branch
from
December 13, 2024 19:34
4f44860
to
d53e91c
Compare
haikoschol
reviewed
Dec 20, 2024
Co-authored-by: Haiko Schol <[email protected]>
Co-authored-by: Haiko Schol <[email protected]>
haikoschol
approved these changes
Jan 7, 2025
dimartiro
requested changes
Jan 9, 2025
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.
overall looks good but I have some suggestions that want to discuss.
Apart from that I'd suggest to follow the godoc conventions when documenting exported struct and functions, I noticed you are following it but not always
timwu20
force-pushed
the
tim/client-db
branch
from
January 10, 2025 20:20
b5d5c2f
to
83edb1e
Compare
dimartiro
approved these changes
Jan 10, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
api.Backend
This is the client backend interface where trie state can be retrieved for a given block hash. State pruning achieved using
StateDB
, and historical block pruning is performed byblockchainDB
.api.AuxStore
This is an interface that provides access to an auxiliary database. This database is not fork-aware. This interface is embedded into
api.Backend
Backend.BeginOperation
Begins a new block insertion transaction. What is returned is something that implements
api.BlockImportOperation
.Backend.BeginStateOperation
Notes the
BlockImportOperation
to contain a state transition.Backend.CommitOperation
Commits a
BlockImportOperation
into the backend.Backend.FinalizeBlock
Finalizes a block for a given hash. Takes an optional justification to be stored.
Backend.PinBlock
andBackend.UnpinBlock
This will pin a block into memory to avoid it from being pruned during state pruning or block pruning.
UnpinBlock
will allow the block to be pruned from memory.Backend.StateAt
Retrieve the state at a given block. Returns a
statemachine.Backend
implementation which will allow you to query state, but not mutate the state.Backend.GetImportLock
Returns a pointer to the shared
sync.RWMutex
for the backend. NOTE: the backend doesn't actually acquire the lock by itself, this is supposed to be used by the callers ofapi.Backend
to handle concurrent block imports.api.BlockImportOperation
An implementation of this interface is returned you by calling
api.Backend.BeginOperation
. This keeps track of the inserted block data and state mutations that will be commited into the backend usingapi.Backend.CommitOperation
.BlockImportOperation.State
Returns the pending state for this block. Can be
nil
if no state is associated.BlockImportOperation.SetBlockData
Sets block data to the operation. The header, body, indexed body, and justifications can be set. The state of the block can be set as
Normal
,Best
andFinal
.BlockImportOperation.SetGenesisState
Accepts a
storage.Storage
which holds all the top trie and child trie changes. Accepts acommit
boolean which will determine whether or not the changes will be commited to the underlying database.BlockImportOperation.UpdateStorage
Updates the top level trie as well as child tries.
BlockImportOperation.InsertAux
Insert/update auxiliary data.
BlockImportOperation.MarkFinalized
Marks the incoming block as finalized. Accepts an optional justification.
BlockImportOperation.MarkHead
Marks the incoming block as the new head. Will override the backend best block rule.
db.Backend
This is the default implementation of
api.Backend
. CallingBackend.BeginOperation
will return adb.BlockImportOperation
which implementsapi.BlockImportOperation
.db.NewBackend
This is the constructor for
db.Backend
. It accepts two parameters:dbConfig
which is an instance ofDatabaseSettings
canonicalizationDelay
which represents the delay in number of blocks that canonicalization/finalization will enact on the backend.DatabaseSettings
This type represents the configurable settings for
db.Backend
. The maximum size of the trie cache in bytes can be set.statedb.PruningMode
can be set to alter how the trie is pruned.BlocksPruning
can be set toKeepAll
,KeepFinalized
orKeepSome
which will constrain the amount of blocks to a certain number of finalized blocks.db.Backend
dependenciesThis is a quick overview on the introduced dependencies for
db.Backend
.offchain.LocalStorage
Used for offchain storage. Will store in the underlying DB.
dbGenesisStorage
A private type to represent the genesis state when we don't want to commit the state to the underlying database.
db.BlockImportOperation
This is the implementation of
api.BLockImportOperation
which is utilized bydb.Backend
. The introduced dependencies are outlined below.db.refTrackingState
This is used within
db.BlockImportOperation
as a reference to the trie backend as well as the underlying database andstatedb.StateDB
. It stores an optional parent hash.db.pendingBlock
A private type to represent a pending block.
Other Notable Additions
blockchain.TreeTroute
A type with an associated
NewTreeRoute
constructor that represents a block tree route.kvdb.KeyValueDB
An interface for a database that deal with "column families" which are distinct tables within a database. This is essentially the interface that a key value database must conform to.
memorykvdb.MemoryKVDB
An implementation of
kvdb.KeyValueDB
completely in memory. Used only in tests.database.DBAdapter
A helper type that wraps a
kvdb.KeyValueDB
that implementsdatabase.Database
interface. Supports the reference counting required fordatabase.Database
by adding counter records to the underlying database.saturating
packageThis package introduces
Sub
andInto
which is used for saturating operations for unsigned integers. This is to ensure we don't overflow minimum and maximum values when casting between unsigned integer types.Tests
go test -tags integration github.com/ChainSafe/gossamer
Issues
closes #3900