-
Notifications
You must be signed in to change notification settings - Fork 178
/
backend_block_headers.go
102 lines (89 loc) · 3.5 KB
/
backend_block_headers.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 backend
import (
"context"
"github.com/onflow/flow-go/engine/common/rpc"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/irrecoverable"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
type backendBlockHeaders struct {
headers storage.Headers
state protocol.State
}
func (b *backendBlockHeaders) GetLatestBlockHeader(ctx context.Context, isSealed bool) (*flow.Header, flow.BlockStatus, error) {
var header *flow.Header
var err error
if isSealed {
// get the latest seal header from storage
header, err = b.state.Sealed().Head()
if err != nil {
err = irrecoverable.NewExceptionf("failed to lookup sealed header: %w", err)
}
} else {
// get the finalized header from state
header, err = b.state.Final().Head()
if err != nil {
err = irrecoverable.NewExceptionf("failed to lookup final header: %w", err)
}
}
if err != nil {
// node should always have the latest block
// In the RPC engine, if we encounter an error from the protocol state indicating state corruption,
// we should halt processing requests, but do throw an exception which might cause a crash:
// - It is unsafe to process requests if we have an internally bad state.
// - We would like to avoid throwing an exception as a result of an Access API request by policy
// because this can cause DOS potential
// - Since the protocol state is widely shared, we assume that in practice another component will
// observe the protocol state error and throw an exception.
irrecoverable.Throw(ctx, err)
return nil, flow.BlockStatusUnknown, err
}
stat, err := b.getBlockStatus(ctx, header)
if err != nil {
return nil, stat, err
}
return header, stat, nil
}
func (b *backendBlockHeaders) GetBlockHeaderByID(ctx context.Context, id flow.Identifier) (*flow.Header, flow.BlockStatus, error) {
header, err := b.headers.ByBlockID(id)
if err != nil {
return nil, flow.BlockStatusUnknown, rpc.ConvertStorageError(err)
}
stat, err := b.getBlockStatus(ctx, header)
if err != nil {
return nil, stat, err
}
return header, stat, nil
}
func (b *backendBlockHeaders) GetBlockHeaderByHeight(ctx context.Context, height uint64) (*flow.Header, flow.BlockStatus, error) {
header, err := b.headers.ByHeight(height)
if err != nil {
return nil, flow.BlockStatusUnknown, rpc.ConvertStorageError(err)
}
stat, err := b.getBlockStatus(ctx, header)
if err != nil {
return nil, stat, err
}
return header, stat, nil
}
// No errors are expected during normal operations.
func (b *backendBlockHeaders) getBlockStatus(ctx context.Context, header *flow.Header) (flow.BlockStatus, error) {
sealed, err := b.state.Sealed().Head()
if err != nil {
// In the RPC engine, if we encounter an error from the protocol state indicating state corruption,
// we should halt processing requests, but do throw an exception which might cause a crash:
// - It is unsafe to process requests if we have an internally bad State.
// - We would like to avoid throwing an exception as a result of an Access API request by policy
// because this can cause DOS potential
// - Since the protocol state is widely shared, we assume that in practice another component will
// observe the protocol state error and throw an exception.
err := irrecoverable.NewExceptionf("failed to lookup sealed header: %w", err)
irrecoverable.Throw(ctx, err)
return flow.BlockStatusUnknown, err
}
if header.Height > sealed.Height {
return flow.BlockStatusFinalized, nil
}
return flow.BlockStatusSealed, nil
}