/
state_add_commit.go
55 lines (46 loc) · 1.55 KB
/
state_add_commit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package consensus
import (
"context"
"fmt"
cstypes "github.com/hellarcore/tenderhellar/internal/consensus/types"
tmtime "github.com/hellarcore/tenderhellar/libs/time"
"github.com/hellarcore/tenderhellar/types"
)
type AddCommitEvent struct {
Commit *types.Commit
}
// GetType returns AddCommitType event-type
func (e *AddCommitEvent) GetType() EventType {
return AddCommitType
}
type AddCommitAction struct {
eventPublisher *EventPublisher
statsQueue *chanQueue[msgInfo]
proposalUpdater *proposalUpdater
}
func (c *AddCommitAction) Execute(ctx context.Context, stateEvent StateEvent) error {
event := stateEvent.Data.(*AddCommitEvent)
commit := event.Commit
stateData := stateEvent.StateData
// The commit is all good, let's apply it to the state
err := c.proposalUpdater.updateStateData(stateData, commit.BlockID)
if err != nil {
return err
}
stateData.updateRoundStep(stateData.Round, cstypes.RoundStepApplyCommit)
stateData.CommitRound = commit.Round
stateData.CommitTime = tmtime.Now()
c.eventPublisher.PublishNewRoundStepEvent(stateData.RoundState)
// The commit is all good, let's apply it to the state
_ = stateEvent.Ctrl.Dispatch(ctx, &ApplyCommitEvent{Commit: commit}, stateData)
// This will relay the commit to peers
err = c.eventPublisher.PublishCommitEvent(commit)
if err != nil {
return fmt.Errorf("error adding commit: %w", err)
}
if stateData.bypassCommitTimeout() {
_ = stateEvent.Ctrl.Dispatch(ctx, &EnterNewRoundEvent{Height: stateData.Height}, stateData)
}
_ = c.statsQueue.send(ctx, msgInfoFromCtx(ctx))
return nil
}