Skip to content

Commit

Permalink
[protocol] add PreCommit() interface
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderZhi authored and dustinxie committed Oct 12, 2022
1 parent b8c38e6 commit 0c5eccc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions action/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ type PreStatesCreator interface {
CreatePreStates(context.Context, StateManager) error
}

// PreCommitter performs pre-commit action of the protocol
type PreCommitter interface {
PreCommit(context.Context, StateManager) error
}

// Committer performs commit action of the protocol
type Committer interface {
Commit(context.Context, StateManager) error
Expand Down
14 changes: 14 additions & 0 deletions state/factory/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ func generateWorkingSetCacheKey(blkHeader block.Header, producerAddr string) has
return hash.Hash256b(sum)
}

func protocolPreCommit(ctx context.Context, sr protocol.StateManager) error {
if reg, ok := protocol.GetRegistry(ctx); ok {
for _, p := range reg.All() {
post, ok := p.(protocol.PreCommitter)
if ok && sr.ProtocolDirty(p.Name()) {
if err := post.PreCommit(ctx, sr); err != nil {
return err
}
}
}
}
return nil
}

func protocolCommit(ctx context.Context, sr protocol.StateManager) error {
if reg, ok := protocol.GetRegistry(ctx); ok {
for _, p := range reg.All() {
Expand Down
4 changes: 4 additions & 0 deletions state/factory/workingset.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,14 @@ func (ws *workingSet) ResetSnapshots() {

// Commit persists all changes in RunActions() into the DB
func (ws *workingSet) Commit(ctx context.Context) error {
if err := protocolPreCommit(ctx, ws); err != nil {
return err
}
if err := ws.store.Commit(); err != nil {
return err
}
if err := protocolCommit(ctx, ws); err != nil {
// TODO (zhi): wrap the error and eventually panic it in caller side
return err
}
ws.Reset()
Expand Down

0 comments on commit 0c5eccc

Please sign in to comment.