diff --git a/persistence/docs/CHANGELOG.md b/persistence/docs/CHANGELOG.md index 116ec780c..9ce5f0102 100644 --- a/persistence/docs/CHANGELOG.md +++ b/persistence/docs/CHANGELOG.md @@ -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 diff --git a/persistence/module.go b/persistence/module.go index 2fb370617..cd943394f 100644 --- a/persistence/module.go +++ b/persistence/module.go @@ -17,8 +17,7 @@ import ( ) var ( - _ modules.PersistenceModule = &persistenceModule{} - + _ modules.PersistenceModule = &persistenceModule{} _ modules.PersistenceRWContext = &PostgresContext{} ) @@ -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 } diff --git a/persistence/trees/module.go b/persistence/trees/module.go index 7942ad60a..9c0d5eff0 100644 --- a/persistence/trees/module.go +++ b/persistence/trees/module.go @@ -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 } } diff --git a/persistence/trees/trees.go b/persistence/trees/trees.go index 62824bc89..de51f0184 100644 --- a/persistence/trees/trees.go +++ b/persistence/trees/trees.go @@ -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 @@ -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) } @@ -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 { @@ -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) } } @@ -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 } ////////////////////////