-
Notifications
You must be signed in to change notification settings - Fork 212
/
state.go
80 lines (68 loc) · 2.18 KB
/
state.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
72
73
74
75
76
77
78
79
80
package sim
import (
"errors"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/blocks"
"github.com/spacemeshos/go-spacemesh/sql/certificates"
"github.com/spacemeshos/go-spacemesh/sql/layers"
)
func newState(logger log.Log, conf config) State {
return State{
logger: logger,
DB: newCacheDB(logger, conf),
Beacons: newBeaconStore(),
}
}
// State of the node.
type State struct {
logger log.Log
DB *datastore.CachedDB
Beacons *beaconStore
}
// OnBeacon callback to store generated beacon.
func (s *State) OnBeacon(eid types.EpochID, beacon types.Beacon) {
s.Beacons.StoreBeacon(eid, beacon)
}
// OnActivationTx callback to store activation transaction.
func (s *State) OnActivationTx(atx *types.VerifiedActivationTx) {
if err := atxs.Add(s.DB, atx); err != nil {
s.logger.With().Panic("failed to add atx", log.Err(err))
}
}
// OnBallot callback to store ballot.
func (s *State) OnBallot(ballot *types.Ballot) {
exist, _ := ballots.Get(s.DB, ballot.ID())
if exist != nil {
return
}
if err := ballots.Add(s.DB, ballot); err != nil {
s.logger.With().Panic("failed to save ballot", log.Err(err))
}
}
// OnBlock callback to store block.
func (s *State) OnBlock(block *types.Block) {
exist, _ := blocks.Get(s.DB, block.ID())
if exist != nil {
return
}
if err := blocks.Add(s.DB, block); err != nil && !errors.Is(err, sql.ErrObjectExists) {
s.logger.With().Panic("failed to save block", log.Err(err))
}
}
// OnHareOutput callback to store hare output.
func (s *State) OnHareOutput(lid types.LayerID, bid types.BlockID) {
if err := certificates.SetHareOutput(s.DB, lid, bid); err != nil {
s.logger.With().Panic("failed to save hare output", log.Err(err))
}
}
// OnCoinflip callback to store coinflip.
func (s *State) OnCoinflip(lid types.LayerID, coinflip bool) {
if err := layers.SetWeakCoin(s.DB, lid, coinflip); err != nil {
s.logger.With().Panic("failed to save coin flip", log.Err(err))
}
}