Skip to content

Commit

Permalink
stops exporting treeStore
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Jun 27, 2023
1 parent bd40ea9 commit 3f68ff0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
28 changes: 14 additions & 14 deletions persistence/trees/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/pokt-network/smt"
)

var _ modules.Module = &TreeStore{}
var _ modules.Module = &treeStore{}

func (*TreeStore) Create(bus modules.Bus, options ...modules.ModuleOption) (modules.Module, error) {
m := &TreeStore{}
func (*treeStore) Create(bus modules.Bus, options ...modules.ModuleOption) (modules.Module, error) {
m := &treeStore{}

bus.RegisterModule(m)

Expand All @@ -32,13 +32,13 @@ func (*TreeStore) Create(bus modules.Bus, options ...modules.ModuleOption) (modu
}

func Create(bus modules.Bus, options ...modules.ModuleOption) (modules.Module, error) {
return new(TreeStore).Create(bus, options...)
return new(treeStore).Create(bus, options...)
}

// WithLogger assigns a logger for the tree store
func WithLogger(logger *modules.Logger) modules.ModuleOption {
return func(m modules.InitializableModule) {
if mod, ok := m.(*TreeStore); ok {
if mod, ok := m.(*treeStore); ok {
mod.logger = logger
}
}
Expand All @@ -48,7 +48,7 @@ func WithLogger(logger *modules.Logger) modules.ModuleOption {
// saves its data.
func WithTreeStoreDirectory(path string) modules.ModuleOption {
return func(m modules.InitializableModule) {
mod, ok := m.(*TreeStore)
mod, ok := m.(*treeStore)
if ok {
mod.treeStoreDir = path
}
Expand All @@ -58,20 +58,20 @@ func WithTreeStoreDirectory(path string) modules.ModuleOption {
// WithTxIndexer assigns a TxIndexer for use during operation.
func WithTxIndexer(txi indexer.TxIndexer) modules.ModuleOption {
return func(m modules.InitializableModule) {
mod, ok := m.(*TreeStore)
mod, ok := m.(*treeStore)
if ok {
mod.txi = txi
}
}
}

func (t *TreeStore) GetModuleName() string { return modules.TreeStoreModuleName }
func (t *TreeStore) Start() error { return nil }
func (t *TreeStore) Stop() error { return nil }
func (t *TreeStore) GetBus() modules.Bus { return t.bus }
func (t *TreeStore) SetBus(bus modules.Bus) { t.bus = bus }
func (t *treeStore) GetModuleName() string { return modules.TreeStoreModuleName }
func (t *treeStore) Start() error { return nil }
func (t *treeStore) Stop() error { return nil }
func (t *treeStore) GetBus() modules.Bus { return t.bus }
func (t *treeStore) SetBus(bus modules.Bus) { t.bus = bus }

func (t *TreeStore) setupTrees() error {
func (t *treeStore) setupTrees() error {
if t.treeStoreDir == ":memory:" {
return t.setupInMemory()
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (t *TreeStore) setupTrees() error {
return nil
}

func (t *TreeStore) setupInMemory() error {
func (t *treeStore) setupInMemory() error {
nodeStore := kvstore.NewMemKVStore()
t.rootTree = &stateTree{
name: RootTreeName,
Expand Down
49 changes: 26 additions & 23 deletions persistence/trees/trees.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ type stateTree struct {
nodeStore kvstore.KVStore
}

var _ modules.TreeStoreModule = &TreeStore{}
var _ modules.TreeStoreModule = &treeStore{}

// TreeStore stores a set of merkle trees that
// it manages. It fulfills the modules.TreeStore interface.
// treeStore stores a set of merkle trees that
// it manages. It fulfills the modules.treeStore interface.
// * It is responsible for atomic commit or rollback behavior
// of the underlying trees by utilizing the lazy loading
// functionality provided by the underlying smt library.
type TreeStore struct {
type treeStore struct {
base_modules.IntegratableModule

logger *modules.Logger
Expand All @@ -94,7 +94,7 @@ type TreeStore struct {
// GetTree returns the name, root hash, and nodeStore for the matching tree tree
// stored in the TreeStore. This enables the caller to import the smt and not
// change the one stored
func (t *TreeStore) GetTree(name string) ([]byte, kvstore.KVStore) {
func (t *treeStore) GetTree(name string) ([]byte, kvstore.KVStore) {
if name == RootTreeName {
return t.rootTree.tree.Root(), t.rootTree.nodeStore
}
Expand All @@ -112,10 +112,10 @@ type Worldstate struct {
}

// Update takes a pgx transaction and a height and updates all of the trees in the TreeStore for that height.
func (t *TreeStore) Update(pgtx pgx.Tx, height uint64) (string, error) {
func (t *treeStore) Update(pgtx pgx.Tx, height uint64) (string, error) {
t.logger.Info().Msgf("🌴 updating state trees at height %d", height)

previous, err := t.save()
previous, err := t.Save()
if err != nil {
return "", fmt.Errorf("failed to create valid rollback point: %w", err)
}
Expand All @@ -124,11 +124,10 @@ func (t *TreeStore) Update(pgtx pgx.Tx, height uint64) (string, error) {

hash, err := t.updateMerkleTrees(pgtx, t.txi, height)
if err != nil {
// TODO t.load(previous) and take
t.Rollback()
return "", fmt.Errorf("err not handled")
}
if err := t.Commit(); err != nil {
// TODO t.load(previous) state
return "", fmt.Errorf("failed to commit tree state: %w", err)
}

Expand All @@ -138,7 +137,7 @@ func (t *TreeStore) Update(pgtx pgx.Tx, height uint64) (string, error) {
// DebugClearAll is used by the debug cli to completely reset all merkle trees.
// This should only be called by the debug CLI.
// TECHDEBT: Move this into a separate file with a debug build flag to avoid accidental usage in prod
func (t *TreeStore) DebugClearAll() error {
func (t *treeStore) DebugClearAll() error {
if err := t.rootTree.nodeStore.ClearAll(); err != nil {
return fmt.Errorf("failed to clear root node store: %w", err)
}
Expand All @@ -155,7 +154,7 @@ func (t *TreeStore) DebugClearAll() error {

// updateMerkleTrees updates all of the merkle trees in order defined by `numMerkleTrees`
// * it returns the new state hash capturing the state of all the trees or an error if one occurred
func (t *TreeStore) updateMerkleTrees(pgtx pgx.Tx, txi indexer.TxIndexer, height uint64) (string, error) {
func (t *treeStore) updateMerkleTrees(pgtx pgx.Tx, txi indexer.TxIndexer, height uint64) (string, error) {
for treeName := range t.merkleTrees {
switch treeName {
// Actor Merkle Trees
Expand Down Expand Up @@ -230,8 +229,8 @@ func (t *TreeStore) updateMerkleTrees(pgtx pgx.Tx, txi indexer.TxIndexer, height
return t.getStateHash(), nil
}

func (t *TreeStore) Prepare(_ modules.Tx) error {
w, err := t.save()
func (t *treeStore) Prepare(_ modules.Tx) error {
w, err := t.Save()
if err != nil {
return fmt.Errorf("failed to save worldstate: %w", err)
}
Expand All @@ -240,7 +239,7 @@ func (t *TreeStore) Prepare(_ modules.Tx) error {
return nil
}

func (t *TreeStore) save() (*Worldstate, error) {
func (t *treeStore) Save() (*Worldstate, error) {
w := &Worldstate{
TreeStoreDir: t.treeStoreDir,
MerkleTrees: map[string]*stateTree{},
Expand All @@ -254,7 +253,11 @@ func (t *TreeStore) save() (*Worldstate, error) {
return w, nil
}

func (t *TreeStore) Commit() error {
func (t *treeStore) Load(w *Worldstate) error {
return fmt.Errorf("not impl")
}

func (t *treeStore) Commit() error {
for treeName, stateTree := range t.merkleTrees {
if err := stateTree.tree.Commit(); err != nil {
return fmt.Errorf("failed to commit %s: %w", treeName, err)
Expand All @@ -263,13 +266,13 @@ func (t *TreeStore) Commit() error {
return nil
}

func (t *TreeStore) Rollback() {
func (t *treeStore) Rollback() {
t.merkleTrees = t.prev.MerkleTrees
t.rootTree = t.prev.RootTree
t.prev = nil
}

func (t *TreeStore) getStateHash() string {
func (t *treeStore) getStateHash() string {
for _, stateTree := range t.merkleTrees {
if err := t.rootTree.tree.Update([]byte(stateTree.name), stateTree.tree.Root()); err != nil {
log.Fatalf("failed to update root tree with %s tree's hash: %v", stateTree.name, err)
Expand All @@ -287,7 +290,7 @@ func (t *TreeStore) getStateHash() string {
////////////////////////

// NB: I think this needs to be done manually for all 4 types.
func (t *TreeStore) updateActorsTree(actorType coreTypes.ActorType, actors []*coreTypes.Actor) error {
func (t *treeStore) updateActorsTree(actorType coreTypes.ActorType, actors []*coreTypes.Actor) error {
for _, actor := range actors {
bzAddr, err := hex.DecodeString(actor.GetAddress())
if err != nil {
Expand Down Expand Up @@ -315,7 +318,7 @@ func (t *TreeStore) updateActorsTree(actorType coreTypes.ActorType, actors []*co
// Account Tree Helpers //
//////////////////////////

func (t *TreeStore) updateAccountTrees(accounts []*coreTypes.Account) error {
func (t *treeStore) updateAccountTrees(accounts []*coreTypes.Account) error {
for _, account := range accounts {
bzAddr, err := hex.DecodeString(account.GetAddress())
if err != nil {
Expand All @@ -335,7 +338,7 @@ func (t *TreeStore) updateAccountTrees(accounts []*coreTypes.Account) error {
return nil
}

func (t *TreeStore) updatePoolTrees(pools []*coreTypes.Account) error {
func (t *treeStore) updatePoolTrees(pools []*coreTypes.Account) error {
for _, pool := range pools {
bzAddr, err := hex.DecodeString(pool.GetAddress())
if err != nil {
Expand All @@ -359,7 +362,7 @@ func (t *TreeStore) updatePoolTrees(pools []*coreTypes.Account) error {
// Data Tree Helpers //
///////////////////////

func (t *TreeStore) updateTransactionsTree(indexedTxs []*coreTypes.IndexedTransaction) error {
func (t *treeStore) updateTransactionsTree(indexedTxs []*coreTypes.IndexedTransaction) error {
for _, idxTx := range indexedTxs {
txBz := idxTx.GetTx()
txHash := crypto.SHA3Hash(txBz)
Expand All @@ -370,7 +373,7 @@ func (t *TreeStore) updateTransactionsTree(indexedTxs []*coreTypes.IndexedTransa
return nil
}

func (t *TreeStore) updateParamsTree(params []*coreTypes.Param) error {
func (t *treeStore) updateParamsTree(params []*coreTypes.Param) error {
for _, param := range params {
paramBz, err := codec.GetCodec().Marshal(param)
paramKey := crypto.SHA3Hash([]byte(param.Name))
Expand All @@ -385,7 +388,7 @@ func (t *TreeStore) updateParamsTree(params []*coreTypes.Param) error {
return nil
}

func (t *TreeStore) updateFlagsTree(flags []*coreTypes.Flag) error {
func (t *treeStore) updateFlagsTree(flags []*coreTypes.Flag) error {
for _, flag := range flags {
flagBz, err := codec.GetCodec().Marshal(flag)
flagKey := crypto.SHA3Hash([]byte(flag.Name))
Expand Down
13 changes: 9 additions & 4 deletions persistence/trees/trees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ func TestTreeStore_Update(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, hash)
})
}

func TestPersistenceContext_Apply(t *testing.T) {
pool, resource, dbUrl := test_artifacts.SetupPostgresDocker()
t.Cleanup(func() {
if err := pool.Purge(resource); err != nil {
log.Fatalf("could not purge resource: %s", err)
}
})

t.Run("should apply commit to all stores and return nil", func(t *testing.T) {
pmod := newTestPersistenceModule(t, dbUrl)
Expand All @@ -85,10 +94,6 @@ func TestTreeStore_Update(t *testing.T) {
})
assert.NoError(t, err)
})

t.Run("should rollback if any store fails to update", func(t *testing.T) {
t.Skip()
})
}

// createMockBus returns a mock bus with stubbed out functions for bus registration
Expand Down

0 comments on commit 3f68ff0

Please sign in to comment.