Skip to content

Commit

Permalink
[Persistence] Adds a logger to TreeStore module
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Jun 27, 2023
1 parent 9276154 commit 320ef16
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
6 changes: 5 additions & 1 deletion persistence/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Refactors the persistence treeStore to be an IntegratableModule

## [0.0.0.58] - 2023-06-13
## [0.0.0.58] - 2023-06-07

- Adds a logger to TreeStore

## [0.0.0.57] - 2023-06-07

- Refactors the stateTrees implementation off of the PersistenceContext and into its in module
- Implements the new Update and Commit pattern with the SMT trees in the trees module
Expand Down
8 changes: 5 additions & 3 deletions persistence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
)

var (
_ modules.PersistenceModule = &persistenceModule{}

_ modules.PersistenceModule = &persistenceModule{}
_ modules.PersistenceRWContext = &PostgresContext{}
)

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

treeModule, err := trees.Create(bus, trees.WithTreeStoreDirectory(persistenceCfg.TreesStoreDir))
treeModule, err := trees.Create(
bus,
trees.WithTreeStoreDirectory(persistenceCfg.TreesStoreDir),
trees.WithLogger(m.logger))
if err != nil {
return nil, err
}
Expand Down
12 changes: 10 additions & 2 deletions persistence/trees/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ func Create(bus modules.Bus, options ...modules.TreeStoreOption) (modules.TreeSt
return new(treeStore).Create(bus, options...)
}

// WithLogger assigns a logger for the tree store
func WithLogger(logger *modules.Logger) modules.TreeStoreOption {
return func(m modules.TreeStoreModule) {
if mod, ok := m.(*treeStore); ok {
mod.logger = logger
}
}
}

// WithTreeStoreDirectory assigns the path where the tree store
// saves its data.
func WithTreeStoreDirectory(path string) modules.TreeStoreOption {
return func(m modules.TreeStoreModule) {
mod, ok := m.(*treeStore)
if ok {
if mod, ok := m.(*treeStore); ok {
mod.treeStoreDir = path
}
}
Expand Down
14 changes: 10 additions & 4 deletions persistence/trees/trees.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var _ modules.TreeStoreModule = &treeStore{}
type treeStore struct {
base_modules.IntegratableModule

logger *modules.Logger
treeStoreDir string
rootTree *stateTree
merkleTrees map[string]*stateTree
Expand All @@ -103,6 +104,7 @@ func (t *treeStore) GetTree(name string) ([]byte, kvstore.KVStore) {
// all of the trees in the treeStore for that height.
func (t *treeStore) Update(pgtx pgx.Tx, height uint64) (string, error) {
txi := t.GetBus().GetPersistenceModule().GetTxIndexer()
t.logger.Info().Msgf("🌴 updating state trees at height %d", height)
return t.updateMerkleTrees(pgtx, txi, height)
}

Expand Down Expand Up @@ -138,7 +140,7 @@ func (t *treeStore) updateMerkleTrees(pgtx pgx.Tx, txi indexer.TxIndexer, height

actors, err := sql.GetActors(pgtx, actorType, height)
if err != nil {
return "", fmt.Errorf("failed to get actors at height: %w", err)
return "", fmt.Errorf("failed to get actors at height %d: %w", height, err)
}

if err := t.updateActorsTree(actorType, actors); err != nil {
Expand Down Expand Up @@ -188,9 +190,9 @@ func (t *treeStore) updateMerkleTrees(pgtx pgx.Tx, txi indexer.TxIndexer, height
if err := t.updateFlagsTree(flags); err != nil {
return "", fmt.Errorf("failed to update flags tree - %w", err)
}
// Default
// Default should not happen; panic and log the treeType - this is a strong code smell.
default:
log.Fatalf("not handled in state commitment update. Merkle tree: %s", treeName)
t.logger.Panic().Msgf("unhandled merkle tree type: %s", treeName)
}
}

Expand All @@ -215,7 +217,11 @@ func (t *treeStore) getStateHash() string {
log.Fatalf("failed to update root tree with %s tree's hash: %v", stateTree.name, err)
}
}
return hex.EncodeToString(t.rootTree.tree.Root())
// Convert the array to a slice and return it
// REF: https://stackoverflow.com/questions/28886616/convert-array-to-slice-in-go
hexHash := hex.EncodeToString(t.rootTree.tree.Root())
t.logger.Info().Msgf("#️⃣ calculated state hash: %s", hexHash)
return hexHash
}

////////////////////////
Expand Down

0 comments on commit 320ef16

Please sign in to comment.