Skip to content

Commit

Permalink
[fixup] reduce export footprint of worldState
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Jul 27, 2023
1 parent 0f87f0b commit cb4497d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
8 changes: 4 additions & 4 deletions persistence/trees/atomic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
42 changes: 21 additions & 21 deletions persistence/trees/trees.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions persistence/trees/trees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand Down

0 comments on commit cb4497d

Please sign in to comment.