-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.go
71 lines (60 loc) · 1.71 KB
/
bootstrap.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package abft
import (
"errors"
"fmt"
"github.com/Deepchain-foundation/lachesis-base/abft/election"
"github.com/Deepchain-foundation/lachesis-base/inter/idx"
"github.com/Deepchain-foundation/lachesis-base/inter/pos"
)
const (
FirstFrame = idx.Frame(1)
FirstEpoch = idx.Epoch(1)
)
// LastDecidedState is for persistent storing.
type LastDecidedState struct {
// fields can change only after a frame is decided
LastDecidedFrame idx.Frame
}
type EpochState struct {
// stored values
// these values change only after a change of epoch
Epoch idx.Epoch
Validators *pos.Validators
}
func (es EpochState) String() string {
return fmt.Sprintf("%d/%s", es.Epoch, es.Validators.String())
}
// Bootstrap restores abft's state from store.
func (p *Orderer) Bootstrap(callback OrdererCallbacks) error {
if p.election != nil {
return errors.New("already bootstrapped")
}
// block handler must be set before p.handleElection
p.callback = callback
// restore current epoch DB
err := p.loadEpochDB()
if err != nil {
return err
}
if p.callback.EpochDBLoaded != nil {
p.callback.EpochDBLoaded(p.store.GetEpoch())
}
p.election = election.New(p.store.GetValidators(), p.store.GetLastDecidedFrame()+1, p.dagIndex.ForklessCause, p.store.GetFrameRoots)
// events reprocessing
_, err = p.bootstrapElection()
return err
}
// Reset switches epoch state to a new empty epoch.
func (p *Orderer) Reset(epoch idx.Epoch, validators *pos.Validators) error {
p.store.applyGenesis(epoch, validators)
// reset internal epoch DB
err := p.resetEpochStore(epoch)
if err != nil {
return err
}
p.election.Reset(validators, FirstFrame)
return nil
}
func (p *Orderer) loadEpochDB() error {
return p.store.openEpochDB(p.store.GetEpoch())
}