-
Notifications
You must be signed in to change notification settings - Fork 178
/
api.go
128 lines (107 loc) · 3.06 KB
/
api.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
119
120
121
122
123
124
125
126
127
128
package protocol
import (
"context"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/common/rpc"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
type NetworkAPI interface {
GetNetworkParameters(ctx context.Context) access.NetworkParameters
GetLatestProtocolStateSnapshot(ctx context.Context) ([]byte, error)
}
type API interface {
NetworkAPI
GetLatestBlockHeader(ctx context.Context, isSealed bool) (*flow.Header, error)
GetBlockHeaderByID(ctx context.Context, id flow.Identifier) (*flow.Header, error)
GetBlockHeaderByHeight(ctx context.Context, height uint64) (*flow.Header, error)
GetLatestBlock(ctx context.Context, isSealed bool) (*flow.Block, error)
GetBlockByID(ctx context.Context, id flow.Identifier) (*flow.Block, error)
GetBlockByHeight(ctx context.Context, height uint64) (*flow.Block, error)
}
type backend struct {
NetworkAPI
blocks storage.Blocks
headers storage.Headers
state protocol.State
}
func New(
state protocol.State,
blocks storage.Blocks,
headers storage.Headers,
network NetworkAPI,
) API {
return &backend{
NetworkAPI: network,
headers: headers,
blocks: blocks,
state: state,
}
}
func (b *backend) GetLatestBlock(_ context.Context, isSealed bool) (*flow.Block, error) {
header, err := b.getLatestHeader(isSealed)
if err != nil {
err = rpc.ConvertStorageError(err)
return nil, err
}
block, err := b.blocks.ByID(header.ID())
if err != nil {
err = rpc.ConvertStorageError(err)
return nil, err
}
return block, nil
}
func (b *backend) GetBlockByID(_ context.Context, id flow.Identifier) (*flow.Block, error) {
block, err := b.blocks.ByID(id)
if err != nil {
err = rpc.ConvertStorageError(err)
return nil, err
}
return block, nil
}
func (b *backend) GetBlockByHeight(_ context.Context, height uint64) (*flow.Block, error) {
block, err := b.blocks.ByHeight(height)
if err != nil {
err = rpc.ConvertStorageError(err)
return nil, err
}
return block, nil
}
func (b *backend) GetLatestBlockHeader(_ context.Context, isSealed bool) (*flow.Header, error) {
header, err := b.getLatestHeader(isSealed)
if err != nil {
err = rpc.ConvertStorageError(err)
return nil, err
}
return header, nil
}
func (b *backend) GetBlockHeaderByID(_ context.Context, id flow.Identifier) (*flow.Header, error) {
header, err := b.headers.ByBlockID(id)
if err != nil {
err = rpc.ConvertStorageError(err)
return nil, err
}
return header, nil
}
func (b *backend) GetBlockHeaderByHeight(_ context.Context, height uint64) (*flow.Header, error) {
header, err := b.headers.ByHeight(height)
if err != nil {
err = rpc.ConvertStorageError(err)
return nil, err
}
return header, nil
}
func (b *backend) getLatestHeader(isSealed bool) (*flow.Header, error) {
var header *flow.Header
var err error
if isSealed {
// get the latest seal header from storage
header, err = b.state.Sealed().Head()
return header, err
} else {
// get the finalized header from state
header, err = b.state.Final().Head()
return header, err
}
}