-
Notifications
You must be signed in to change notification settings - Fork 175
/
snapshot.go
102 lines (82 loc) · 2.33 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
package badger
import (
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/cluster"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage/badger/operation"
"github.com/onflow/flow-go/storage/badger/procedure"
)
// Snapshot represents a snapshot of chain state anchored at a particular
// reference block.
type Snapshot struct {
err error
state *State
blockID flow.Identifier
}
func (s *Snapshot) Collection() (*flow.Collection, error) {
if s.err != nil {
return nil, s.err
}
var collection flow.Collection
err := s.state.db.View(func(tx *badger.Txn) error {
// get the header for this snapshot
var header flow.Header
err := s.head(&header)(tx)
if err != nil {
return fmt.Errorf("failed to get snapshot header: %w", err)
}
// get the payload
var payload cluster.Payload
err = procedure.RetrieveClusterPayload(header.ID(), &payload)(tx)
if err != nil {
return fmt.Errorf("failed to get snapshot payload: %w", err)
}
// set the collection
collection = payload.Collection
return nil
})
return &collection, err
}
func (s *Snapshot) Head() (*flow.Header, error) {
if s.err != nil {
return nil, s.err
}
var head flow.Header
err := s.state.db.View(func(tx *badger.Txn) error {
return s.head(&head)(tx)
})
return &head, err
}
func (s *Snapshot) Pending() ([]flow.Identifier, error) {
if s.err != nil {
return nil, s.err
}
return s.pending(s.blockID)
}
// head finds the header referenced by the snapshot.
func (s *Snapshot) head(head *flow.Header) func(*badger.Txn) error {
return func(tx *badger.Txn) error {
// get the snapshot header
err := operation.RetrieveHeader(s.blockID, head)(tx)
if err != nil {
return fmt.Errorf("could not retrieve header for block (%s): %w", s.blockID, err)
}
return nil
}
}
func (s *Snapshot) pending(blockID flow.Identifier) ([]flow.Identifier, error) {
var pendingIDs []flow.Identifier
err := s.state.db.View(procedure.LookupBlockChildren(blockID, &pendingIDs))
if err != nil {
return nil, fmt.Errorf("could not get pending children: %w", err)
}
for _, pendingID := range pendingIDs {
additionalIDs, err := s.pending(pendingID)
if err != nil {
return nil, fmt.Errorf("could not get pending grandchildren: %w", err)
}
pendingIDs = append(pendingIDs, additionalIDs...)
}
return pendingIDs, nil
}