Skip to content

Commit c6624f7

Browse files
committed
TreeStore module implementation patch
1 parent b168429 commit c6624f7

7 files changed

Lines changed: 27 additions & 14 deletions

File tree

persistence/module.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package persistence
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/jackc/pgx/v5"
@@ -107,14 +108,18 @@ func (*persistenceModule) Create(bus modules.Bus, options ...modules.ModuleOptio
107108
if err != nil {
108109
return nil, err
109110
}
111+
treeStoreModule, ok := treeModule.(modules.TreeStoreModule)
112+
if !ok {
113+
return nil, errors.New("error casting TreeStoreModule")
114+
}
110115

111116
m.config = persistenceCfg
112117
m.genesisState = genesisState
113118
m.networkId = runtimeMgr.GetConfig().NetworkId
114119

115120
m.blockStore = blockStore
116121
m.txIndexer = txIndexer
117-
m.stateTrees = treeModule
122+
m.stateTrees = treeStoreModule
118123

119124
// TECHDEBT: reconsider if this is the best place to call `populateGenesisState`. Note that
120125
// this forces the genesis state to be reloaded on every node startup until state
@@ -235,10 +240,6 @@ func (m *persistenceModule) GetTxIndexer() indexer.TxIndexer {
235240
return m.txIndexer
236241
}
237242

238-
func (m *persistenceModule) GetTreeStore() modules.TreeStoreModule {
239-
return m.stateTrees
240-
}
241-
242243
func (m *persistenceModule) GetNetworkID() string {
243244
return m.networkId
244245
}

persistence/trees/module.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ import (
88
"github.com/pokt-network/smt"
99
)
1010

11-
func (*treeStore) Create(bus modules.Bus, options ...modules.TreeStoreOption) (modules.TreeStoreModule, error) {
11+
var _ modules.Module = &treeStore{}
12+
13+
func (*treeStore) Create(bus modules.Bus, options ...modules.ModuleOption) (modules.Module, error) {
1214
m := &treeStore{}
1315

1416
for _, option := range options {
1517
option(m)
1618
}
1719

18-
m.SetBus(bus)
20+
bus.RegisterModule(m)
1921

2022
if err := m.setupTrees(); err != nil {
2123
return nil, err
@@ -24,14 +26,20 @@ func (*treeStore) Create(bus modules.Bus, options ...modules.TreeStoreOption) (m
2426
return m, nil
2527
}
2628

27-
func Create(bus modules.Bus, options ...modules.TreeStoreOption) (modules.TreeStoreModule, error) {
29+
func Create(bus modules.Bus, options ...modules.ModuleOption) (modules.Module, error) {
2830
return new(treeStore).Create(bus, options...)
2931
}
3032

33+
func (t *treeStore) GetModuleName() string { return modules.TreeStoreModuleName }
34+
func (t *treeStore) Start() error { return nil }
35+
func (t *treeStore) Stop() error { return nil }
36+
func (t *treeStore) GetBus() modules.Bus { return t.bus }
37+
func (t *treeStore) SetBus(bus modules.Bus) { t.bus = bus }
38+
3139
// WithTreeStoreDirectory assigns the path where the tree store
3240
// saves its data.
33-
func WithTreeStoreDirectory(path string) modules.TreeStoreOption {
34-
return func(m modules.TreeStoreModule) {
41+
func WithTreeStoreDirectory(path string) modules.ModuleOption {
42+
return func(m modules.InitializableModule) {
3543
mod, ok := m.(*treeStore)
3644
if ok {
3745
mod.treeStoreDir = path

persistence/trees/trees.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ var _ modules.TreeStoreModule = &treeStore{}
8181
type treeStore struct {
8282
base_modules.IntegratableModule
8383

84+
bus modules.Bus
85+
8486
treeStoreDir string
8587
rootTree *stateTree
8688
merkleTrees map[string]*stateTree

runtime/bus.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (m *bus) GetPersistenceModule() modules.PersistenceModule {
7171
return getModuleFromRegistry[modules.PersistenceModule](m, modules.PersistenceModuleName)
7272
}
7373

74+
func (m *bus) GetTreeStoreModule() modules.TreeStoreModule {
75+
return getModuleFromRegistry[modules.TreeStoreModule](m, modules.TreeStoreModuleName)
76+
}
77+
7478
func (m *bus) GetP2PModule() modules.P2PModule {
7579
return getModuleFromRegistry[modules.P2PModule](m, modules.P2PModuleName)
7680
}

shared/modules/bus_module.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Bus interface {
2525

2626
// Pocket modules
2727
GetPersistenceModule() PersistenceModule
28+
GetTreeStoreModule() TreeStoreModule
2829
GetP2PModule() P2PModule
2930
GetUtilityModule() UtilityModule
3031
GetConsensusModule() ConsensusModule

shared/modules/persistence_module.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ type PersistenceModule interface {
3030
GetTxIndexer() indexer.TxIndexer
3131
TransactionExists(transactionHash string) (bool, error)
3232

33-
// TreeStore operations
34-
GetTreeStore() TreeStoreModule
35-
3633
// Debugging / development only
3734
HandleDebugMessage(*messaging.DebugMessage) error
3835
}

shared/modules/treestore_module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type TreeStoreFactory = FactoryWithOptions[TreeStoreModule, TreeStoreOption]
1616
// TreeStoreModules defines the interface for atomic updates and rollbacks to the internal
1717
// merkle trees that compose the state hash of pocket.
1818
type TreeStoreModule interface {
19-
IntegratableModule
19+
Module
2020

2121
// Update returns the new state hash for a given height.
2222
// * Height is passed through to the Update function and is used to query the TxIndexer for transactions

0 commit comments

Comments
 (0)