-
Notifications
You must be signed in to change notification settings - Fork 175
/
block_info.go
156 lines (133 loc) · 3.42 KB
/
block_info.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package environment
import (
"fmt"
"github.com/onflow/cadence/runtime"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/storage"
"github.com/onflow/flow-go/fvm/tracing"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
storageErr "github.com/onflow/flow-go/storage"
)
type BlockInfo interface {
// GetCurrentBlockHeight returns the current block height.
GetCurrentBlockHeight() (uint64, error)
// GetBlockAtHeight returns the block at the given height.
GetBlockAtHeight(
height uint64,
) (
runtime.Block,
bool,
error,
)
}
type ParseRestrictedBlockInfo struct {
txnState storage.TransactionPreparer
impl BlockInfo
}
func NewParseRestrictedBlockInfo(
txnState storage.TransactionPreparer,
impl BlockInfo,
) BlockInfo {
return ParseRestrictedBlockInfo{
txnState: txnState,
impl: impl,
}
}
func (info ParseRestrictedBlockInfo) GetCurrentBlockHeight() (uint64, error) {
return parseRestrict1Ret(
info.txnState,
trace.FVMEnvGetCurrentBlockHeight,
info.impl.GetCurrentBlockHeight)
}
func (info ParseRestrictedBlockInfo) GetBlockAtHeight(
height uint64,
) (
runtime.Block,
bool,
error,
) {
return parseRestrict1Arg2Ret(
info.txnState,
trace.FVMEnvGetBlockAtHeight,
info.impl.GetBlockAtHeight,
height)
}
type BlockInfoParams struct {
Blocks Blocks
BlockHeader *flow.Header
}
func DefaultBlockInfoParams() BlockInfoParams {
return BlockInfoParams{
Blocks: nil,
BlockHeader: nil,
}
}
type blockInfo struct {
tracer tracing.TracerSpan
meter Meter
blockHeader *flow.Header
blocks Blocks
}
func NewBlockInfo(
tracer tracing.TracerSpan,
meter Meter,
blockHeader *flow.Header,
blocks Blocks,
) BlockInfo {
return &blockInfo{
tracer: tracer,
meter: meter,
blockHeader: blockHeader,
blocks: blocks,
}
}
// GetCurrentBlockHeight returns the current block height.
func (info *blockInfo) GetCurrentBlockHeight() (uint64, error) {
defer info.tracer.StartExtensiveTracingChildSpan(
trace.FVMEnvGetCurrentBlockHeight).End()
err := info.meter.MeterComputation(
ComputationKindGetCurrentBlockHeight,
1)
if err != nil {
return 0, fmt.Errorf("get current block height failed: %w", err)
}
if info.blockHeader == nil {
return 0, errors.NewOperationNotSupportedError("GetCurrentBlockHeight")
}
return info.blockHeader.Height, nil
}
// GetBlockAtHeight returns the block at the given height.
func (info *blockInfo) GetBlockAtHeight(
height uint64,
) (
runtime.Block,
bool,
error,
) {
defer info.tracer.StartChildSpan(trace.FVMEnvGetBlockAtHeight).End()
err := info.meter.MeterComputation(
ComputationKindGetBlockAtHeight,
1)
if err != nil {
return runtime.Block{}, false, fmt.Errorf(
"get block at height failed: %w", err)
}
if info.blocks == nil {
return runtime.Block{}, false, errors.NewOperationNotSupportedError(
"GetBlockAtHeight")
}
if info.blockHeader != nil && height == info.blockHeader.Height {
return runtimeBlockFromHeader(info.blockHeader), true, nil
}
header, err := info.blocks.ByHeightFrom(height, info.blockHeader)
// TODO (ramtin): remove dependency on storage and move this if condition
// to blockfinder
if errors.Is(err, storageErr.ErrNotFound) {
return runtime.Block{}, false, nil
} else if err != nil {
return runtime.Block{}, false, fmt.Errorf(
"get block at height failed for height %v: %w", height, err)
}
return runtimeBlockFromHeader(header), true, nil
}