Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
cb1f17a
[chore] submodule pattern updates to TreeStore
dylanlott Jul 11, 2023
34d4e82
updates TreeStore to use Submodule type
dylanlott Jul 12, 2023
8fdebb8
[fixup] fixes mocks for treestore module
dylanlott Jul 12, 2023
aba1bb6
[fixup] embeds TreeStoreFactory on to TreeStoreModule
dylanlott Jul 12, 2023
342c611
[fixup] TreeStoreOption and TreeStoreModule updates
dylanlott Jul 12, 2023
a3b6d34
[docs] updates the Modules readme to reflect some best practices
dylanlott Jul 12, 2023
3beb528
[fixup] removes bus and txindexer from TreeStore struct and only fetc…
dylanlott Jul 12, 2023
b98ddec
[fixup] get height provider from bus in tree tests
dylanlott Jul 17, 2023
c004456
[fixup] only use bus to fetch TreeStore
dylanlott Jul 17, 2023
614597b
[fixup] renames TreeStoreModuleName
dylanlott Jul 17, 2023
a6f8b27
[fixup] renames tree store factory function
dylanlott Jul 17, 2023
4b3ce44
[docs] adds techdebt comment about privatization to TreeStore struct
dylanlott Jul 18, 2023
7ce02c0
[fixup] privatize treeStoreDir field on TreeStore struct
dylanlott Jul 18, 2023
ad0cdcb
[docs] Apply documentation suggestions from code review
dylanlott Jul 19, 2023
4a9d866
[Temp] s/TreeStore/treeStore (#920)
Olshansk Jul 19, 2023
61e2f61
[fixup] adds error logs to treeStore#Commit error paths
dylanlott Jul 20, 2023
6aaff89
[fixup] formatting long mock definitions
dylanlott Jul 20, 2023
c9dfe00
[fixup] change assertion on empty byte slice
dylanlott Jul 20, 2023
33a42da
[docs] adds documentation on order of module registration
dylanlott Jul 20, 2023
9b0ab2c
[fixup] register treestore submodule after processing options
dylanlott Jul 20, 2023
c8acc11
Merge branch 'main' into persistence/tree-store-submodule
dylanlott Jul 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ibc/store/provable_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func newTreeStoreMock(t *testing.T,

ctrl := gomock.NewController(t)
treeStoreMock := mockModules.NewMockTreeStoreModule(ctrl)
treeStoreMock.EXPECT().GetModuleName().Return(modules.TreeStoreModuleName).AnyTimes()
treeStoreMock.EXPECT().GetModuleName().Return(modules.TreeStoreSubmoduleName).AnyTimes()
treeStoreMock.EXPECT().SetBus(gomock.Any()).Return().AnyTimes()
treeStoreMock.EXPECT().GetBus().Return(bus).AnyTimes()

Expand Down
19 changes: 6 additions & 13 deletions persistence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ type persistenceModule struct {
// IMPORTANT: It doubles as the data store for the transaction tree in state tree set.
txIndexer indexer.TxIndexer

// stateTrees manages all of the merkle trees maintained by the
// persistence module that roll up into the state commitment.
stateTrees modules.TreeStoreModule

// Only one write context is allowed at a time
writeContext *PostgresContext

Expand Down Expand Up @@ -103,21 +99,22 @@ func (*persistenceModule) Create(bus modules.Bus, options ...modules.ModuleOptio
return nil, err
}

treeModule, err := trees.Create(
_, err = trees.Create(
bus,
trees.WithTreeStoreDirectory(persistenceCfg.TreesStoreDir),
trees.WithLogger(m.logger))
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create TreeStoreModule: %w", err)
}

m.config = persistenceCfg
m.genesisState = genesisState
m.networkId = runtimeMgr.GetConfig().NetworkId

// TECHDEBT: fetch blockstore from bus once it's a proper submodule
m.blockStore = blockStore
// TECHDEBT: fetch txIndexer from bus
m.txIndexer = txIndexer
m.stateTrees = treeModule

// TECHDEBT: reconsider if this is the best place to call `populateGenesisState`. Note that
// this forces the genesis state to be reloaded on every node startup until state
Expand Down Expand Up @@ -180,7 +177,7 @@ func (m *persistenceModule) NewRWContext(height int64) (modules.PersistenceRWCon
stateHash: "",
blockStore: m.blockStore,
txIndexer: m.txIndexer,
stateTrees: m.stateTrees,
stateTrees: m.GetBus().GetTreeStore(),
networkId: m.networkId,
}

Expand Down Expand Up @@ -212,7 +209,7 @@ func (m *persistenceModule) NewReadContext(height int64) (modules.PersistenceRea
stateHash: "",
blockStore: m.blockStore,
txIndexer: m.txIndexer,
stateTrees: m.stateTrees,
stateTrees: m.GetBus().GetTreeStore(),
networkId: m.networkId,
}, nil
}
Expand All @@ -238,10 +235,6 @@ func (m *persistenceModule) GetTxIndexer() indexer.TxIndexer {
return m.txIndexer
}

func (m *persistenceModule) GetTreeStore() modules.TreeStoreModule {
return m.stateTrees
}

func (m *persistenceModule) GetNetworkID() string {
return m.networkId
}
Expand Down
11 changes: 0 additions & 11 deletions persistence/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/jackc/pgx/v5"
"github.com/pokt-network/pocket/persistence/indexer"
ptypes "github.com/pokt-network/pocket/persistence/types"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
)
Expand Down Expand Up @@ -91,16 +90,6 @@ func GetAccountsUpdated(
return accounts, nil
}

// GetTransactions takes a transaction indexer and returns the transactions for the current height
func GetTransactions(txi indexer.TxIndexer, height uint64) ([]*coreTypes.IndexedTransaction, error) {
// TECHDEBT(#813): Avoid this cast to int64
indexedTxs, err := txi.GetByHeight(int64(height), false)
if err != nil {
return nil, fmt.Errorf("failed to get transactions by height: %w", err)
}
return indexedTxs, nil
}

// GetPools returns the pools updated at the given height
func GetPools(pgtx pgx.Tx, height uint64) ([]*coreTypes.Account, error) {
pools, err := GetAccountsUpdated(pgtx, ptypes.Pool, height)
Expand Down
7 changes: 6 additions & 1 deletion persistence/trees/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/pokt-network/smt"
)

var _ modules.TreeStoreModule = &treeStore{}

func (*treeStore) Create(bus modules.Bus, options ...modules.TreeStoreOption) (modules.TreeStoreModule, error) {
m := &treeStore{}

Expand Down Expand Up @@ -41,12 +43,15 @@ func WithLogger(logger *modules.Logger) modules.TreeStoreOption {
// saves its data.
func WithTreeStoreDirectory(path string) modules.TreeStoreOption {
return func(m modules.TreeStoreModule) {
if mod, ok := m.(*treeStore); ok {
mod, ok := m.(*treeStore)
if ok {
mod.treeStoreDir = path
}
}
}

func (t *treeStore) GetModuleName() string { return modules.TreeStoreSubmoduleName }

func (t *treeStore) setupTrees() error {
if t.treeStoreDir == ":memory:" {
return t.setupInMemory()
Expand Down
176 changes: 176 additions & 0 deletions persistence/trees/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package trees_test

import (
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/pokt-network/pocket/internal/testutil"
"github.com/pokt-network/pocket/p2p/providers/peerstore_provider"
"github.com/pokt-network/pocket/persistence/trees"
"github.com/pokt-network/pocket/runtime"
"github.com/pokt-network/pocket/runtime/genesis"
"github.com/pokt-network/pocket/runtime/test_artifacts"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
"github.com/pokt-network/pocket/shared/modules"
mockModules "github.com/pokt-network/pocket/shared/modules/mocks"
)

const (
serviceURLFormat = "node%d.consensus:42069"
)

func TestTreeStore_Create(t *testing.T) {
ctrl := gomock.NewController(t)
mockRuntimeMgr := mockModules.NewMockRuntimeMgr(ctrl)
mockBus := createMockBus(t, mockRuntimeMgr)
genesisStateMock := createMockGenesisState(nil)
persistenceMock := preparePersistenceMock(t, mockBus, genesisStateMock)

mockBus.EXPECT().
GetPersistenceModule().
Return(persistenceMock).
AnyTimes()
persistenceMock.EXPECT().
GetBus().
AnyTimes().
Return(mockBus)
persistenceMock.EXPECT().
NewRWContext(int64(0)).
AnyTimes()
persistenceMock.EXPECT().
GetTxIndexer().
AnyTimes()

treemod, err := trees.Create(mockBus, trees.WithTreeStoreDirectory(":memory:"))
assert.NoError(t, err)

got := treemod.GetBus()
assert.Equal(t, got, mockBus)

// root hash should be empty for empty tree
root, ns := treemod.GetTree(trees.TransactionsTreeName)
require.Equal(t, root, make([]byte, 32))

// nodestore should have no values in it
keys, vals, err := ns.GetAll(nil, false)
require.NoError(t, err)
require.Empty(t, keys, vals)
}

func TestTreeStore_DebugClearAll(t *testing.T) {
// TODO: Write test case for the DebugClearAll method
t.Skip("TODO: Write test case for DebugClearAll method")
}

// createMockGenesisState configures and returns a mocked GenesisState
func createMockGenesisState(valKeys []cryptoPocket.PrivateKey) *genesis.GenesisState {
genesisState := new(genesis.GenesisState)
validators := make([]*coreTypes.Actor, len(valKeys))
for i, valKey := range valKeys {
addr := valKey.Address().String()
mockActor := &coreTypes.Actor{
ActorType: coreTypes.ActorType_ACTOR_TYPE_VAL,
Address: addr,
PublicKey: valKey.PublicKey().String(),
ServiceUrl: validatorId(i + 1),
StakedAmount: test_artifacts.DefaultStakeAmountString,
PausedHeight: int64(0),
UnstakingHeight: int64(0),
Output: addr,
}
validators[i] = mockActor
}
genesisState.Validators = validators

return genesisState
}

// Persistence mock - only needed for validatorMap access
func preparePersistenceMock(t *testing.T, busMock *mockModules.MockBus, genesisState *genesis.GenesisState) *mockModules.MockPersistenceModule {
ctrl := gomock.NewController(t)

persistenceModuleMock := mockModules.NewMockPersistenceModule(ctrl)
readCtxMock := mockModules.NewMockPersistenceReadContext(ctrl)

readCtxMock.EXPECT().
GetAllValidators(gomock.Any()).
Return(genesisState.GetValidators(), nil).AnyTimes()
readCtxMock.EXPECT().
GetAllStakedActors(gomock.Any()).
DoAndReturn(func(height int64) ([]*coreTypes.Actor, error) {
return testutil.Concatenate[*coreTypes.Actor](
genesisState.GetValidators(),
genesisState.GetServicers(),
genesisState.GetFishermen(),
genesisState.GetApplications(),
), nil
}).
AnyTimes()
persistenceModuleMock.EXPECT().
NewReadContext(gomock.Any()).
Return(readCtxMock, nil).
AnyTimes()
readCtxMock.EXPECT().
Release().
AnyTimes()
persistenceModuleMock.EXPECT().
GetBus().
Return(busMock).
AnyTimes()
persistenceModuleMock.EXPECT().
SetBus(busMock).
AnyTimes()
persistenceModuleMock.EXPECT().
GetModuleName().
Return(modules.PersistenceModuleName).
AnyTimes()
busMock.
RegisterModule(persistenceModuleMock)

return persistenceModuleMock
}

func validatorId(i int) string {
return fmt.Sprintf(serviceURLFormat, i)
}

// createMockBus returns a mock bus with stubbed out functions for bus registration
func createMockBus(t *testing.T, runtimeMgr modules.RuntimeMgr) *mockModules.MockBus {
t.Helper()
ctrl := gomock.NewController(t)
mockBus := mockModules.NewMockBus(ctrl)
mockModulesRegistry := mockModules.NewMockModulesRegistry(ctrl)

mockBus.EXPECT().
GetRuntimeMgr().
Return(runtimeMgr).
AnyTimes()
mockBus.EXPECT().
RegisterModule(gomock.Any()).
DoAndReturn(func(m modules.Submodule) {
m.SetBus(mockBus)
}).
AnyTimes()
mockModulesRegistry.EXPECT().
GetModule(peerstore_provider.PeerstoreProviderSubmoduleName).
Return(nil, runtime.ErrModuleNotRegistered(peerstore_provider.PeerstoreProviderSubmoduleName)).
AnyTimes()
mockModulesRegistry.EXPECT().
GetModule(modules.CurrentHeightProviderSubmoduleName).
Return(nil, runtime.ErrModuleNotRegistered(modules.CurrentHeightProviderSubmoduleName)).
AnyTimes()
mockBus.EXPECT().
GetModulesRegistry().
Return(mockModulesRegistry).
AnyTimes()
mockBus.EXPECT().
PublishEventToBus(gomock.Any()).
AnyTimes()

return mockBus
}
Loading