Skip to content

Commit

Permalink
[factory] WorkingSetAtHeight() to take pre-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Dec 30, 2024
1 parent 36a8913 commit 9e082fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
21 changes: 17 additions & 4 deletions state/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type (
NewBlockBuilder(context.Context, actpool.ActPool, func(action.Envelope) (*action.SealedEnvelope, error)) (*block.Builder, error)
PutBlock(context.Context, *block.Block) error
WorkingSet(context.Context) (protocol.StateManager, error)
WorkingSetAtHeight(context.Context, uint64) (protocol.StateManager, error)
WorkingSetAtHeight(context.Context, uint64, ...*action.SealedEnvelope) (protocol.StateManager, error)
}

// factory implements StateFactory interface, tracks changes to account/contract and batch-commits to DB
Expand Down Expand Up @@ -408,16 +408,29 @@ func (sf *factory) WorkingSet(ctx context.Context) (protocol.StateManager, error
return sf.newWorkingSet(ctx, sf.currentChainHeight+1)
}

func (sf *factory) WorkingSetAtHeight(ctx context.Context, height uint64) (protocol.StateManager, error) {
func (sf *factory) WorkingSetAtHeight(ctx context.Context, height uint64, preacts ...*action.SealedEnvelope) (protocol.StateManager, error) {
if !sf.saveHistory {
return nil, ErrNoArchiveData
}
sf.mutex.Lock()
defer sf.mutex.Unlock()
if height > sf.currentChainHeight {
sf.mutex.Unlock()
return nil, errors.Errorf("query height %d is higher than tip height %d", height, sf.currentChainHeight)
}
return sf.newWorkingSetAtHeight(ctx, height)
ws, err := sf.newWorkingSetAtHeight(ctx, height)
sf.mutex.Unlock()
if err != nil {
return nil, errors.Wrap(err, "failed to obtain working set from state factory")
}
if len(preacts) == 0 {
return ws, nil
}
// prepare workingset at height, and run acts
ws.height++
if err := ws.Process(ctx, preacts); err != nil {
return nil, err
}
return ws, nil
}

// PutBlock persists all changes in RunActions() into the DB
Expand Down
17 changes: 14 additions & 3 deletions state/factory/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,20 @@ func (sdb *stateDB) WorkingSet(ctx context.Context) (protocol.StateManager, erro
return sdb.newWorkingSet(ctx, height+1)
}

func (sdb *stateDB) WorkingSetAtHeight(ctx context.Context, height uint64) (protocol.StateManager, error) {
// TODO: implement archive mode
return sdb.newWorkingSet(ctx, height)
func (sdb *stateDB) WorkingSetAtHeight(ctx context.Context, height uint64, preacts ...*action.SealedEnvelope) (protocol.StateManager, error) {
ws, err := sdb.newWorkingSet(ctx, height)
if err != nil {
return nil, errors.Wrap(err, "failed to obtain working set from state db")
}
if len(preacts) == 0 {
return ws, nil
}
// prepare workingset at height, and run acts
ws.height++
if err := ws.Process(ctx, preacts); err != nil {
return nil, err
}
return ws, nil
}

// PutBlock persists all changes in RunActions() into the DB
Expand Down
13 changes: 9 additions & 4 deletions test/mock/mock_factory/mock_factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9e082fd

Please sign in to comment.