Skip to content

Commit 0c86257

Browse files
committed
[WIP] implement ClearAll for treeStore
1 parent dbc29bb commit 0c86257

1 file changed

Lines changed: 20 additions & 27 deletions

File tree

persistence/trees/trees.go

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,32 @@ const (
8686
// of the underlying trees by utilizing the lazy loading
8787
// functionality provided by the underlying smt library.
8888
type treeStore struct {
89-
merkleTrees map[merkleTree]*smt.SMT
90-
nodeStores map[merkleTree]kvstore.KVStore
89+
treeStoreDir string
90+
merkleTrees map[merkleTree]*smt.SMT
91+
nodeStores map[merkleTree]kvstore.KVStore
9192
}
9293

9394
// Update takes a transaction and a height and updates
9495
// all of the trees in the treeStore for that height.
9596
func (t *treeStore) Update(pgtx pgx.Tx, txi indexer.TxIndexer, height uint64) (string, error) {
96-
// NB: Consider not wrapping this
9797
return t.updateMerkleTrees(pgtx, txi, height)
9898
}
9999

100100
// NewStateTrees takes a directory location and a TxIndexer and returns a TreeStore
101101
// implementation that guarantees
102-
func NewStateTrees(treesStoreDir string, txi indexer.TxIndexer) (*treeStore, error) {
103-
if treesStoreDir == "" {
102+
func NewStateTrees(dir string, txi indexer.TxIndexer) (*treeStore, error) {
103+
if dir == "" {
104104
return newMemStateTrees()
105105
}
106106

107107
stateTrees := &treeStore{
108-
merkleTrees: make(map[merkleTree]*smt.SMT, int(numMerkleTrees)),
109-
nodeStores: make(map[merkleTree]kvstore.KVStore, int(numMerkleTrees)),
108+
treeStoreDir: dir,
109+
merkleTrees: make(map[merkleTree]*smt.SMT, int(numMerkleTrees)),
110+
nodeStores: make(map[merkleTree]kvstore.KVStore, int(numMerkleTrees)),
110111
}
111112

112113
for tree := merkleTree(0); tree < numMerkleTrees; tree++ {
113-
nodeStore, err := kvstore.NewKVStore(fmt.Sprintf("%s/%s_nodes", treesStoreDir, merkleTreeToString[tree]))
114+
nodeStore, err := kvstore.NewKVStore(fmt.Sprintf("%s/%s_nodes", dir, merkleTreeToString[tree]))
114115
if err != nil {
115116
return nil, err
116117
}
@@ -136,7 +137,6 @@ func newMemStateTrees() (*treeStore, error) {
136137
// updateMerkleTrees updates all of the merkle trees that TreeStore manages.
137138
// * it returns an hash of the output or an error.
138139
func (t *treeStore) updateMerkleTrees(pgtx pgx.Tx, txi indexer.TxIndexer, height uint64) (string, error) {
139-
// TODO: loop through the trees, update each & commit; Trigger rollback if any fail to update.
140140
for treeType := merkleTree(0); treeType < numMerkleTrees; treeType++ {
141141
switch treeType {
142142
// Actor Merkle Trees
@@ -369,7 +369,7 @@ func (t *treeStore) getActorsUpdated(
369369

370370
actors := make([]*coreTypes.Actor, len(addrs))
371371
for i, addr := range addrs {
372-
// TODO same goes here for int64 height cast here
372+
// TECHDEBT #XXX: Avoid this cast to int64
373373
actor, err := t.getActor(pgtx, actorSchema, addr, int64(height))
374374
if err != nil {
375375
return nil, err
@@ -466,7 +466,7 @@ func (t *treeStore) getParams(pgtx pgx.Tx, height uint64) ([]*coreTypes.Param, e
466466
}
467467
defer rows.Close()
468468

469-
var paramSlice []*coreTypes.Param // Store returned rows
469+
var paramSlice []*coreTypes.Param
470470
for rows.Next() {
471471
param := new(coreTypes.Param)
472472
if err := rows.Scan(&param.Name, &param.Value); err != nil {
@@ -475,6 +475,7 @@ func (t *treeStore) getParams(pgtx pgx.Tx, height uint64) ([]*coreTypes.Param, e
475475
param.Height = int64(height)
476476
paramSlice = append(paramSlice, param)
477477
}
478+
478479
return paramSlice, nil
479480
}
480481

@@ -539,20 +540,12 @@ func (t *treeStore) getChainsForActor(
539540
// be handled here in the TreeStore instead of the level above.
540541
func (t *treeStore) ClearAll() error {
541542
for treeType := merkleTree(0); treeType < numMerkleTrees; treeType++ {
542-
// nodeStore := t.nodeStores[treeType]
543-
// tree := t.merkleTrees[treeType]
544-
// TODO: implement the below
545-
// if err := valueStore.ClearAll(); err != nil {
546-
// return err
547-
// }
548-
// if err := tree.ClearAll(); err != nil {
549-
// return err
550-
// }
551-
552-
// p.stateTrees.merkleTrees[treeType] = smt.NewSparseMerkleTree(valueStore, nodeStore, sha256.New())
553-
// TODO: like the line above, create a new set of state tree and assign it to the merkleTrees property
554-
// // This is Needed in order to make sure the root is re-set correctly after clearing
555-
// t.merkleTrees[treeType] = smt.NewSparseMerkleTree()
556-
}
557-
return fmt.Errorf("not impl")
543+
nodeStore := t.nodeStores[treeType]
544+
if err := nodeStore.ClearAll(); err != nil {
545+
return fmt.Errorf("failed to clear %s node store: %w", merkleTreeToString[treeType], err)
546+
}
547+
t.merkleTrees[treeType] = smt.NewSparseMerkleTree(nodeStore, sha256.New())
548+
}
549+
550+
return nil
558551
}

0 commit comments

Comments
 (0)