-
Notifications
You must be signed in to change notification settings - Fork 177
/
snapshot.go
118 lines (96 loc) · 3.01 KB
/
snapshot.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package inmem
import (
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
)
// Snapshot is a memory-backed implementation of protocol.Snapshot. The snapshot
// data is stored in the embedded encodable snapshot model, which defines the
// canonical structure of an encoded snapshot for the purposes of serialization.
type Snapshot struct {
enc EncodableSnapshot
}
var _ protocol.Snapshot = (*Snapshot)(nil)
func (s Snapshot) Head() (*flow.Header, error) {
return s.enc.Head, nil
}
func (s Snapshot) QuorumCertificate() (*flow.QuorumCertificate, error) {
return s.enc.QuorumCertificate, nil
}
func (s Snapshot) Identities(selector flow.IdentityFilter) (flow.IdentityList, error) {
return s.enc.Identities.Filter(selector), nil
}
func (s Snapshot) Identity(nodeID flow.Identifier) (*flow.Identity, error) {
identity, ok := s.enc.Identities.ByNodeID(nodeID)
if !ok {
return nil, protocol.IdentityNotFoundError{NodeID: nodeID}
}
return identity, nil
}
func (s Snapshot) Commit() (flow.StateCommitment, error) {
return s.enc.LatestSeal.FinalState, nil
}
func (s Snapshot) SealedResult() (*flow.ExecutionResult, *flow.Seal, error) {
return s.enc.LatestResult, s.enc.LatestSeal, nil
}
func (s Snapshot) SealingSegment() (*flow.SealingSegment, error) {
return s.enc.SealingSegment, nil
}
func (s Snapshot) Descendants() ([]flow.Identifier, error) {
// canonical snapshots don't have any descendants
return nil, nil
}
func (s Snapshot) Phase() (flow.EpochPhase, error) {
return s.enc.Phase, nil
}
func (s Snapshot) RandomSource() ([]byte, error) {
return model.BeaconSignature(s.enc.QuorumCertificate)
}
func (s Snapshot) Epochs() protocol.EpochQuery {
return Epochs{s.enc.Epochs}
}
func (s Snapshot) Params() protocol.GlobalParams {
return Params{s.enc.Params}
}
func (s Snapshot) Encodable() EncodableSnapshot {
return s.enc
}
func (s Snapshot) VersionBeacon() (*flow.SealedVersionBeacon, error) {
return s.enc.SealedVersionBeacon, nil
}
func SnapshotFromEncodable(enc EncodableSnapshot) *Snapshot {
return &Snapshot{
enc: enc,
}
}
// StrippedInmemSnapshot removes all the networking address in the snapshot
func StrippedInmemSnapshot(snapshot EncodableSnapshot) EncodableSnapshot {
removeAddress := func(ids flow.IdentityList) {
for _, identity := range ids {
identity.Address = ""
}
}
removeAddressFromEpoch := func(epoch *EncodableEpoch) {
if epoch == nil {
return
}
removeAddress(epoch.InitialIdentities)
for _, cluster := range epoch.Clustering {
removeAddress(cluster)
}
for _, c := range epoch.Clusters {
removeAddress(c.Members)
}
}
removeAddress(snapshot.Identities)
removeAddressFromEpoch(snapshot.Epochs.Previous)
removeAddressFromEpoch(&snapshot.Epochs.Current)
removeAddressFromEpoch(snapshot.Epochs.Next)
for _, event := range snapshot.LatestResult.ServiceEvents {
switch event.Type {
case flow.ServiceEventSetup:
removeAddress(event.Event.(*flow.EpochSetup).Participants)
}
}
return snapshot
}