-
Notifications
You must be signed in to change notification settings - Fork 179
/
state.go
40 lines (33 loc) · 1.19 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
package protocol
import (
"fmt"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
// FindLatest retrieves the latest finalized header and all of its pending
// children. These pending children have been verified by the compliance layer
// but are NOT guaranteed to have been verified by HotStuff. They MUST be
// validated by HotStuff during the recovery process.
func FindLatest(state protocol.State, headers storage.Headers) (*flow.Header, []*flow.Header, error) {
// find finalized block
finalized, err := state.Final().Head()
if err != nil {
return nil, nil, fmt.Errorf("could not find finalized block")
}
// find all pending blockIDs
pendingIDs, err := state.Final().Descendants()
if err != nil {
return nil, nil, fmt.Errorf("could not find pending block")
}
// find all pending header by ID
pending := make([]*flow.Header, 0, len(pendingIDs))
for _, pendingID := range pendingIDs {
pendingHeader, err := headers.ByBlockID(pendingID)
if err != nil {
return nil, nil, fmt.Errorf("could not find pending block by ID: %w", err)
}
pending = append(pending, pendingHeader)
}
return finalized, pending, nil
}