From cb4497d9a2c562e60ba46bf0c384ad8d6200f7fe Mon Sep 17 00:00:00 2001 From: shakezula Date: Thu, 27 Jul 2023 13:01:30 -0600 Subject: [PATCH] [fixup] reduce export footprint of worldState --- persistence/trees/atomic_test.go | 8 +++--- persistence/trees/trees.go | 42 ++++++++++++++++---------------- persistence/trees/trees_test.go | 10 ++++---- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/persistence/trees/atomic_test.go b/persistence/trees/atomic_test.go index 388ad57db..06fdbaf8c 100644 --- a/persistence/trees/atomic_test.go +++ b/persistence/trees/atomic_test.go @@ -60,10 +60,10 @@ func TestTreeStore_AtomicUpdatesWithSuccessfulRollback(t *testing.T) { // set a new savepoint require.NoError(t, ts.Savepoint()) - require.NotEmpty(t, ts.PrevState.MerkleTrees) - require.NotEmpty(t, ts.PrevState.RootTree) + require.NotEmpty(t, ts.prevState.merkleTrees) + require.NotEmpty(t, ts.prevState.rootTree) // assert that savepoint creation doesn't mutate state hash - require.Equal(t, hash1, hex.EncodeToString(ts.PrevState.RootTree.tree.Root())) + require.Equal(t, hash1, hex.EncodeToString(ts.prevState.rootTree.tree.Root())) // verify that creating a savepoint does not change state hash hash2 := ts.getStateHash() @@ -73,7 +73,7 @@ func TestTreeStore_AtomicUpdatesWithSuccessfulRollback(t *testing.T) { // validate that state tree was updated and a previous savepoint is created for _, treeName := range stateTreeNames { require.NotEmpty(t, ts.merkleTrees[treeName]) - require.NotEmpty(t, ts.PrevState.MerkleTrees[treeName]) + require.NotEmpty(t, ts.prevState.merkleTrees[treeName]) } // insert additional test data into all of the trees diff --git a/persistence/trees/trees.go b/persistence/trees/trees.go index ce378e979..8a56328de 100644 --- a/persistence/trees/trees.go +++ b/persistence/trees/trees.go @@ -100,17 +100,17 @@ type treeStore struct { rootTree *stateTree merkleTrees map[string]*stateTree - // PrevState holds a previous view of the Worldstate. + // prevState holds a previous view of the worldState. // The tree store rolls back to this view if errors are encountered during block application. - PrevState *worldstate + prevState *worldState } -// worldstate holds a (de)serializable view of the entire tree state. +// worldState holds a (de)serializable view of the entire tree state. // TECHDEBT(#566) - Hook this up to node CLI subcommands -type worldstate struct { - TreeStoreDir string - RootTree *stateTree - MerkleTrees map[string]*stateTree +type worldState struct { + treeStoreDir string + rootTree *stateTree + merkleTrees map[string]*stateTree } // GetTree returns the root hash and nodeStore for the matching tree stored in the TreeStore. @@ -303,47 +303,47 @@ func (t *treeStore) getStateHash() string { // AtomicStore Implementation // //////////////////////////////// -// Savepoint generates a new savepoint for the tree store and saves it internally. +// Savepoint generates a new savepoint (i.e. a worldState) for the tree store and saves it internally. func (t *treeStore) Savepoint() error { w, err := t.save() if err != nil { return err } - t.PrevState = w + t.prevState = w return nil } -// Rollback rolls back to the last saved world state maintained by the treeStore. +// Rollback rolls back to the last saved worldState maintained by the treeStore. // Rollback intentionally can't return an error because at this point we're out of tricks // to recover from problems. func (t *treeStore) Rollback() error { - if t.PrevState != nil { - t.merkleTrees = t.PrevState.MerkleTrees - t.rootTree = t.PrevState.RootTree + if t.prevState != nil { + t.merkleTrees = t.prevState.merkleTrees + t.rootTree = t.prevState.rootTree return nil } t.logger.Err(ErrFailedRollback) return ErrFailedRollback } -// save commits any pending changes to the trees and creates a copy of the current state of the -// tree store then saves that copy as a rollback point for later use if errors are encountered. +// save commits any pending changes to the trees and creates a copy of the current worldState, +// then saves that copy as a rollback point for later use if errors are encountered. // OPTIMIZE: Consider saving only the root hash of each tree and the tree directory here and then // load the trees up in Rollback instead of setting them up here. -func (t *treeStore) save() (*worldstate, error) { +func (t *treeStore) save() (*worldState, error) { if err := t.Commit(); err != nil { return nil, err } - w := &worldstate{ - TreeStoreDir: t.treeStoreDir, - MerkleTrees: map[string]*stateTree{}, + w := &worldState{ + treeStoreDir: t.treeStoreDir, + merkleTrees: map[string]*stateTree{}, } for treeName := range t.merkleTrees { root, nodeStore := t.GetTree(treeName) tree := smt.ImportSparseMerkleTree(nodeStore, smtTreeHasher, root) - w.MerkleTrees[treeName] = &stateTree{ + w.merkleTrees[treeName] = &stateTree{ name: treeName, tree: tree, nodeStore: nodeStore, @@ -352,7 +352,7 @@ func (t *treeStore) save() (*worldstate, error) { root, nodeStore := t.GetTree(RootTreeName) tree := smt.ImportSparseMerkleTree(nodeStore, smtTreeHasher, root) - w.RootTree = &stateTree{ + w.rootTree = &stateTree{ name: RootTreeName, tree: tree, nodeStore: nodeStore, diff --git a/persistence/trees/trees_test.go b/persistence/trees/trees_test.go index 27ecc69d7..a8428b872 100644 --- a/persistence/trees/trees_test.go +++ b/persistence/trees/trees_test.go @@ -12,7 +12,7 @@ import ( "github.com/pokt-network/pocket/runtime/configs" "github.com/pokt-network/pocket/runtime/test_artifacts" "github.com/pokt-network/pocket/runtime/test_artifacts/keygen" - coreTypes "github.com/pokt-network/pocket/shared/core/types" + core_types "github.com/pokt-network/pocket/shared/core/types" "github.com/pokt-network/pocket/shared/crypto" "github.com/pokt-network/pocket/shared/messaging" "github.com/pokt-network/pocket/shared/modules" @@ -25,7 +25,7 @@ var ( defaultChains = []string{"0001"} defaultStakeBig = big.NewInt(1000000000000000) defaultStake = utils.BigIntToString(defaultStakeBig) - defaultStakeStatus = int32(coreTypes.StakeStatus_Staked) + defaultStakeStatus = int32(core_types.StakeStatus_Staked) defaultPauseHeight = int64(-1) // pauseHeight=-1 implies not paused defaultUnstakingHeight = int64(-1) // unstakingHeight=-1 implies not unstaking @@ -121,7 +121,7 @@ func newTestDefaultConfig(t *testing.T, databaseURL string) *configs.Config { } return cfg } -func createAndInsertDefaultTestApp(t *testing.T, db *persistence.PostgresContext) (*coreTypes.Actor, error) { +func createAndInsertDefaultTestApp(t *testing.T, db *persistence.PostgresContext) (*core_types.Actor, error) { t.Helper() app := newTestApp(t) @@ -146,14 +146,14 @@ func createAndInsertDefaultTestApp(t *testing.T, db *persistence.PostgresContext } // TECHDEBT(#796): Test helpers should be consolidated in a single place -func newTestApp(t *testing.T) *coreTypes.Actor { +func newTestApp(t *testing.T) *core_types.Actor { operatorKey, err := crypto.GeneratePublicKey() require.NoError(t, err) outputAddr, err := crypto.GenerateAddress() require.NoError(t, err) - return &coreTypes.Actor{ + return &core_types.Actor{ Address: hex.EncodeToString(operatorKey.Address()), PublicKey: hex.EncodeToString(operatorKey.Bytes()), Chains: defaultChains,