Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Nov 21, 2024
1 parent eb42c4b commit 1078a64
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
35 changes: 25 additions & 10 deletions state/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,19 +266,35 @@ func (sf *factory) newWorkingSet(ctx context.Context, height uint64) (*workingSe
if err != nil {
return nil, err
}
var (
rootKey = ArchiveTrieRootKey
createTrie = true
store, err := newFactoryWorkingSetStore(sf.protocolView, flusher)
if err != nil {
return nil, err
}
return sf.createSfWorkingSet(ctx, height, store)
}

func (sf *factory) newWorkingSetAtHeight(ctx context.Context, height uint64) (*workingSet, error) {
span := tracer.SpanFromContext(ctx)
span.AddEvent("factory.newWorkingSet")
defer span.End()

g := genesis.MustExtractGenesisContext(ctx)
flusher, err := db.NewKVStoreFlusher(
sf.dao,
batch.NewCachedBatch(),
sf.flusherOptions(!g.IsEaster(height))...,
)
if height < sf.currentChainHeight {
// archive mode
rootKey = fmt.Sprintf("%s-%d", ArchiveTrieRootKey, height)
createTrie = false
if err != nil {
return nil, err
}
store, err := newFactoryWorkingSetStore(sf.protocolView, flusher, rootKey, createTrie)
store, err := newFactoryWorkingSetStoreAtHeight(sf.protocolView, flusher, height)
if err != nil {
return nil, err
}
return sf.createSfWorkingSet(ctx, height, store)
}

func (sf *factory) createSfWorkingSet(ctx context.Context, height uint64, store workingSetStore) (*workingSet, error) {
if err := store.Start(ctx); err != nil {
return nil, err
}
Expand All @@ -293,7 +309,6 @@ func (sf *factory) newWorkingSet(ctx context.Context, height uint64) (*workingSe
}
}
}

return newWorkingSet(height, store), nil
}

Expand Down Expand Up @@ -403,7 +418,7 @@ func (sf *factory) WorkingSetAtHeight(ctx context.Context, height uint64) (proto
if height > sf.currentChainHeight {
return nil, errors.Errorf("query height %d is higher than tip height %d", height, sf.currentChainHeight)
}
return sf.newWorkingSet(ctx, height)
return sf.newWorkingSetAtHeight(ctx, height)
}

// PutBlock persists all changes in RunActions() into the DB
Expand Down
2 changes: 1 addition & 1 deletion state/factory/workingset.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (ws *workingSet) Commit(ctx context.Context) error {
return err
}
ws.Reset()
return ws.store.Stop(ctx)
return nil
}

// State pulls a state from DB
Expand Down
4 changes: 3 additions & 1 deletion state/factory/workingset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func newFactoryWorkingSet(t testing.TB) *workingSet {
genesis.Default,
)
r.NoError(sf.Start(ctx))
// defer r.NoError(sf.Stop(ctx))
defer func() {
r.NoError(sf.Stop(ctx))
}()

ws, err := sf.(workingSetCreator).newWorkingSet(ctx, 1)
r.NoError(err)
Expand Down
19 changes: 17 additions & 2 deletions state/factory/workingsetstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,23 @@ func newStateDBWorkingSetStore(view protocol.View, flusher db.KVStoreFlusher, re
}
}

func newFactoryWorkingSetStore(view protocol.View, flusher db.KVStoreFlusher, trieRootKey string, createTrie bool) (workingSetStore, error) {
tlt, err := newTwoLayerTrie(ArchiveTrieNamespace, flusher.KVStoreWithBuffer(), trieRootKey, createTrie)
func newFactoryWorkingSetStore(view protocol.View, flusher db.KVStoreFlusher) (workingSetStore, error) {
tlt, err := newTwoLayerTrie(ArchiveTrieNamespace, flusher.KVStoreWithBuffer(), ArchiveTrieRootKey, true)
if err != nil {
return nil, err
}

return &factoryWorkingSetStore{
flusher: flusher,
view: view,
tlt: tlt,
trieRoots: make(map[int][]byte),
}, nil
}

func newFactoryWorkingSetStoreAtHeight(view protocol.View, flusher db.KVStoreFlusher, height uint64) (workingSetStore, error) {
rootKey := fmt.Sprintf("%s-%d", ArchiveTrieRootKey, height)
tlt, err := newTwoLayerTrie(ArchiveTrieNamespace, flusher.KVStoreWithBuffer(), rootKey, false)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1078a64

Please sign in to comment.