Skip to content

Commit 208fb81

Browse files
committedFeb 25, 2025
refactor access node's transactions and collections storage module

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed
 

‎cmd/access/node_builder/access_node_builder.go

+29-7
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ type FlowAccessNodeBuilder struct {
346346
ExecutionDataTracker tracker.Storage
347347
VersionControl *version.VersionControl
348348
StopControl *stop.StopControl
349+
Transactions storage.Transactions
350+
Collections storage.Collections
349351

350352
// The sync engine participants provider is the libp2p peer store for the access node
351353
// which is not available until after the network has started.
@@ -574,6 +576,14 @@ func (builder *FlowAccessNodeBuilder) BuildExecutionSyncComponents() *FlowAccess
574576
AdminCommand("read-execution-data", func(config *cmd.NodeConfig) commands.AdminCommand {
575577
return stateSyncCommands.NewReadExecutionDataCommand(builder.ExecutionDataStore)
576578
}).
579+
Module("transactions and collections storage", func(node *cmd.NodeConfig) error {
580+
// TODO: replace with node.ProtocolDB
581+
db := badgerimpl.ToDB(node.DB)
582+
transactions := store.NewTransactions(node.Metrics.Cache, db)
583+
builder.Collections = store.NewCollections(db, transactions)
584+
builder.Transactions = transactions
585+
return nil
586+
}).
577587
Module("execution data datastore and blobstore", func(node *cmd.NodeConfig) error {
578588
datastoreDir := filepath.Join(builder.executionDataDir, "blobstore")
579589
err := os.MkdirAll(datastoreDir, 0700)
@@ -966,8 +976,8 @@ func (builder *FlowAccessNodeBuilder) BuildExecutionSyncComponents() *FlowAccess
966976
builder.Storage.RegisterIndex,
967977
builder.Storage.Headers,
968978
builder.Storage.Events,
969-
builder.Storage.Collections,
970-
builder.Storage.Transactions,
979+
notNil(builder.Collections),
980+
notNil(builder.Transactions),
971981
builder.Storage.LightTransactionResults,
972982
builder.RootChainID.Chain(),
973983
indexerDerivedChainData,
@@ -1613,7 +1623,7 @@ func (builder *FlowAccessNodeBuilder) Initialize() error {
16131623
builder.EnqueueNetworkInit()
16141624

16151625
builder.AdminCommand("get-transactions", func(conf *cmd.NodeConfig) commands.AdminCommand {
1616-
return storageCommands.NewGetTransactionsCommand(conf.State, conf.Storage.Payloads, conf.Storage.Collections)
1626+
return storageCommands.NewGetTransactionsCommand(conf.State, conf.Storage.Payloads, notNil(builder.Collections))
16171627
})
16181628

16191629
// if this is an access node that supports public followers, enqueue the public network
@@ -2034,8 +2044,8 @@ func (builder *FlowAccessNodeBuilder) Build() (cmd.Node, error) {
20342044
HistoricalAccessNodes: builder.HistoricalAccessRPCs,
20352045
Blocks: node.Storage.Blocks,
20362046
Headers: node.Storage.Headers,
2037-
Collections: node.Storage.Collections,
2038-
Transactions: node.Storage.Transactions,
2047+
Collections: notNil(builder.Collections),
2048+
Transactions: notNil(builder.Transactions),
20392049
ExecutionReceipts: node.Storage.Receipts,
20402050
ExecutionResults: node.Storage.Results,
20412051
TxResultErrorMessages: node.Storage.TransactionResultErrorMessages,
@@ -2137,8 +2147,8 @@ func (builder *FlowAccessNodeBuilder) Build() (cmd.Node, error) {
21372147
builder.RequestEng,
21382148
node.Storage.Blocks,
21392149
node.Storage.Headers,
2140-
node.Storage.Collections,
2141-
node.Storage.Transactions,
2150+
notNil(builder.Collections),
2151+
notNil(builder.Transactions),
21422152
node.Storage.Results,
21432153
node.Storage.Receipts,
21442154
builder.collectionExecutedMetric,
@@ -2384,3 +2394,15 @@ func (builder *FlowAccessNodeBuilder) initPublicLibp2pNode(networkKey crypto.Pri
23842394

23852395
return libp2pNode, nil
23862396
}
2397+
2398+
// notNil ensures that the input is not nil and returns it
2399+
// the usage is to ensure the dependencies are initialized before initializing a module.
2400+
// for instance, the IngestionEngine depends on storage.Collections, which is initialized in a
2401+
// different function, so we need to ensure that the storage.Collections is initialized before
2402+
// creating the IngestionEngine.
2403+
func notNil[T any](dep T) T {
2404+
if any(dep) == nil {
2405+
panic("dependency is nil")
2406+
}
2407+
return dep
2408+
}

0 commit comments

Comments
 (0)
Please sign in to comment.