Skip to content

Commit

Permalink
events: notify listeners before commit state tx
Browse files Browse the repository at this point in the history
  • Loading branch information
p4u committed Sep 1, 2023
1 parent bb970dd commit 960d9f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
8 changes: 7 additions & 1 deletion vochain/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,13 @@ func (idx *Indexer) Commit(height uint32) error {
// TODO: can we get previousVote from sqlite via blockTx?
var previousVote *models.StateDBVote
if v.Overwrites > 0 {
previousVote, _ = idx.App.State.Vote(v.ProcessID, v.Nullifier, true)
previousVote, err = idx.App.State.Vote(v.ProcessID, v.Nullifier, true)
if err != nil {
log.Warnw("cannot get previous vote",
"nullifier", hex.EncodeToString(v.Nullifier),
"processID", hex.EncodeToString(v.ProcessID),
"error", err.Error())
}
}
if previousVote != nil {
log.Debugw("vote overwrite, previous vote",
Expand Down
29 changes: 17 additions & 12 deletions vochain/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,23 @@ func (v *State) RevealProcessKeys(tx *models.AdminTx) error {
func (v *State) Save() ([]byte, error) {
height := v.CurrentHeight()
var pidsStartNextBlock [][]byte

// Notify listeners about processes that start in the next block.
if len(pidsStartNextBlock) > 0 {
for _, l := range v.eventListeners {
l.OnProcessesStart(pidsStartNextBlock)
}
}
// Notify listeners about the commit state
for _, l := range v.eventListeners {
if err := l.Commit(height); err != nil {
log.Warnf("event callback error on commit: %v", err)
}
}

// Commit the statedb tx
// Note that we need to commit the tx after calling listeners, because
// the listeners may need to get the previous (not committed) state.
v.tx.Lock()
err := func() error {
var err error
Expand All @@ -401,18 +418,6 @@ func (v *State) Save() ([]byte, error) {
if err != nil {
return nil, err
}
// Notify listeners about processes that start in the next block.
if len(pidsStartNextBlock) > 0 {
for _, l := range v.eventListeners {
l.OnProcessesStart(pidsStartNextBlock)
}
}
// Notify listeners about the commit state
for _, l := range v.eventListeners {
if err := l.Commit(height); err != nil {
log.Warnf("event callback error on commit: %v", err)
}
}

// Update the main state tree
mainTreeView, err := v.store.TreeView(nil)
Expand Down
1 change: 1 addition & 0 deletions vochain/state/votes.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func (s *State) Vote(processID, nullifier []byte, committed bool) (*models.State
} else if err != nil {
return nil, err
}

sdbVoteBytes, err := votesTree.Get(vid)
if errors.Is(err, arbo.ErrKeyNotFound) {
return nil, ErrVoteNotFound
Expand Down

0 comments on commit 960d9f3

Please sign in to comment.