forked from Fantom-foundation/lachesis-base
/
frame_decide.go
55 lines (49 loc) · 1.49 KB
/
frame_decide.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 abft
import (
"github.com/frenchie-foundation/lachesis-base/hash"
"github.com/frenchie-foundation/lachesis-base/inter/idx"
"github.com/frenchie-foundation/lachesis-base/inter/pos"
)
// onFrameDecided moves LastDecidedFrameN to frame.
// It includes: moving current decided frame, txs ordering and execution, epoch sealing.
func (p *Orderer) onFrameDecided(frame idx.Frame, atropos hash.Event) (bool, error) {
// new checkpoint
var newValidators *pos.Validators
if p.callback.ApplyAtropos != nil {
newValidators = p.callback.ApplyAtropos(frame, atropos)
}
lastDecidedState := *p.store.GetLastDecidedState()
if newValidators != nil {
lastDecidedState.LastDecidedFrame = FirstFrame - 1
err := p.sealEpoch(newValidators)
if err != nil {
return true, err
}
p.election.Reset(newValidators, FirstFrame)
} else {
lastDecidedState.LastDecidedFrame = frame
p.election.Reset(p.store.GetValidators(), frame+1)
}
p.store.SetLastDecidedState(&lastDecidedState)
return newValidators != nil, nil
}
func (p *Orderer) sealEpoch(newValidators *pos.Validators) error {
// new PrevEpoch state
epochState := *p.store.GetEpochState()
epochState.Epoch++
epochState.Validators = newValidators
p.store.SetEpochState(&epochState)
// reset internal epoch DB
err := p.store.dropEpochDB()
if err != nil {
return err
}
err = p.store.openEpochDB(epochState.Epoch)
if err != nil {
return err
}
if p.callback.EpochDBLoaded != nil {
p.callback.EpochDBLoaded(epochState.Epoch)
}
return nil
}