-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathprovider.go
More file actions
283 lines (229 loc) · 8.02 KB
/
provider.go
File metadata and controls
283 lines (229 loc) · 8.02 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package provider
import (
"bytes"
"context"
"errors"
"fmt"
"time"
"github.com/ipfs/go-cid"
"github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/blobs"
"github.com/onflow/flow-go/module/executiondatasync/execution_data"
"github.com/onflow/flow-go/module/executiondatasync/tracker"
"github.com/onflow/flow-go/network"
)
type ProviderOption func(*ExecutionDataProvider)
func WithBlobSizeLimit(size int) ProviderOption {
return func(p *ExecutionDataProvider) {
p.maxBlobSize = size
}
}
// Provider is used to provide execution data blobs over the network via a blob service.
type Provider interface {
Provide(ctx context.Context, blockHeight uint64, executionData *execution_data.BlockExecutionData) (flow.Identifier, *flow.BlockExecutionDataRoot, error)
}
type ExecutionDataProvider struct {
logger zerolog.Logger
metrics module.ExecutionDataProviderMetrics
maxBlobSize int
blobService network.BlobService
storage tracker.Storage
cidsProvider *ExecutionDataCIDProvider
}
var _ Provider = (*ExecutionDataProvider)(nil)
func NewProvider(
logger zerolog.Logger,
metrics module.ExecutionDataProviderMetrics,
serializer execution_data.Serializer,
blobService network.BlobService,
storage tracker.Storage,
opts ...ProviderOption,
) *ExecutionDataProvider {
if storage == nil {
storage = &tracker.NoopStorage{}
}
p := &ExecutionDataProvider{
logger: logger.With().Str("component", "execution_data_provider").Logger(),
metrics: metrics,
maxBlobSize: execution_data.DefaultMaxBlobSize,
cidsProvider: NewExecutionDataCIDProvider(serializer),
blobService: blobService,
storage: storage,
}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *ExecutionDataProvider) storeBlobs(parent context.Context, blockHeight uint64, blobCh <-chan blobs.Blob) <-chan error {
ch := make(chan error, 1)
go func() {
defer close(ch)
start := time.Now()
var blobs []blobs.Blob
var cids []cid.Cid
var totalSize uint64
for blob := range blobCh {
blobs = append(blobs, blob)
cids = append(cids, blob.Cid())
totalSize += uint64(len(blob.RawData()))
}
if p.logger.Debug().Enabled() {
cidArr := zerolog.Arr()
for _, cid := range cids {
cidArr = cidArr.Str(cid.String())
}
p.logger.Debug().Array("cids", cidArr).Uint64("height", blockHeight).Msg("storing blobs")
}
err := p.storage.Update(func(trackBlobs tracker.TrackBlobsFn) error {
ctx, cancel := context.WithCancel(parent)
defer cancel()
// track new blobs so that they can be pruned later
if err := trackBlobs(blockHeight, cids...); err != nil {
return fmt.Errorf("failed to track blobs: %w", err)
}
if err := p.blobService.AddBlobs(ctx, blobs); err != nil {
return fmt.Errorf("failed to add blobs: %w", err)
}
return nil
})
duration := time.Since(start)
if err != nil {
ch <- err
p.metrics.AddBlobsFailed()
} else {
p.metrics.AddBlobsSucceeded(duration, totalSize)
}
}()
return ch
}
// Provide adds the block execution data for a newly executed (generally not sealed or finalized) block to the blob store for distribution using Bitswap.
// It computes and returns the root CID of the execution data blob tree.
// This function returns once the root CID has been computed, and all blobs are successfully stored
// in the Bitswap Blobstore.
func (p *ExecutionDataProvider) Provide(ctx context.Context, blockHeight uint64, executionData *execution_data.BlockExecutionData) (flow.Identifier, *flow.BlockExecutionDataRoot, error) {
rootID, rootData, errCh, err := p.provide(ctx, blockHeight, executionData)
storeErr, ok := <-errCh
if err != nil {
return flow.ZeroID, nil, err
}
if ok {
return flow.ZeroID, nil, storeErr
}
if err = p.storage.SetFulfilledHeight(blockHeight); err != nil {
return flow.ZeroID, nil, err
}
return rootID, rootData, nil
}
func (p *ExecutionDataProvider) provide(ctx context.Context, blockHeight uint64, executionData *execution_data.BlockExecutionData) (flow.Identifier, *flow.BlockExecutionDataRoot, <-chan error, error) {
logger := p.logger.With().Uint64("height", blockHeight).Str("block_id", executionData.BlockID.String()).Logger()
logger.Debug().Msg("providing execution data")
start := time.Now()
blobCh := make(chan blobs.Blob)
defer close(blobCh)
errCh := p.storeBlobs(ctx, blockHeight, blobCh)
g := new(errgroup.Group)
chunkDataIDs := make([]cid.Cid, len(executionData.ChunkExecutionDatas))
for i, chunkExecutionData := range executionData.ChunkExecutionDatas {
i := i
chunkExecutionData := chunkExecutionData
g.Go(func() error {
logger.Debug().Int("chunk_index", i).Msg("adding chunk execution data")
cedID, err := p.cidsProvider.addChunkExecutionData(chunkExecutionData, blobCh)
if err != nil {
return fmt.Errorf("failed to add chunk execution data at index %d: %w", i, err)
}
logger.Debug().Int("chunk_index", i).Str("chunk_execution_data_id", cedID.String()).Msg("chunk execution data added")
chunkDataIDs[i] = cedID
return nil
})
}
if err := g.Wait(); err != nil {
return flow.ZeroID, nil, errCh, err
}
edRoot := &flow.BlockExecutionDataRoot{
BlockID: executionData.BlockID,
ChunkExecutionDataIDs: chunkDataIDs,
}
rootID, err := p.cidsProvider.addExecutionDataRoot(edRoot, blobCh)
if err != nil {
return flow.ZeroID, nil, errCh, fmt.Errorf("failed to add execution data root: %w", err)
}
logger.Debug().Str("root_id", rootID.String()).Msg("root ID computed")
duration := time.Since(start)
p.metrics.RootIDComputed(duration, len(executionData.ChunkExecutionDatas))
return rootID, edRoot, errCh, nil
}
func NewExecutionDataCIDProvider(serializer execution_data.Serializer) *ExecutionDataCIDProvider {
return &ExecutionDataCIDProvider{
serializer: serializer,
maxBlobSize: execution_data.DefaultMaxBlobSize,
}
}
type ExecutionDataCIDProvider struct {
serializer execution_data.Serializer
maxBlobSize int
}
func (p *ExecutionDataCIDProvider) CalculateExecutionDataRootID(
edRoot flow.BlockExecutionDataRoot,
) (flow.Identifier, error) {
return p.addExecutionDataRoot(&edRoot, nil)
}
func (p *ExecutionDataCIDProvider) CalculateChunkExecutionDataID(
ced execution_data.ChunkExecutionData,
) (cid.Cid, error) {
return p.addChunkExecutionData(&ced, nil)
}
func (p *ExecutionDataCIDProvider) addExecutionDataRoot(
edRoot *flow.BlockExecutionDataRoot,
blobCh chan<- blobs.Blob,
) (flow.Identifier, error) {
buf := new(bytes.Buffer)
if err := p.serializer.Serialize(buf, edRoot); err != nil {
return flow.ZeroID, fmt.Errorf("failed to serialize execution data root: %w", err)
}
if buf.Len() > p.maxBlobSize {
return flow.ZeroID, errors.New("execution data root blob exceeds maximum allowed size")
}
rootBlob := blobs.NewBlob(buf.Bytes())
if blobCh != nil {
blobCh <- rootBlob
}
rootID, err := flow.CidToId(rootBlob.Cid())
if err != nil {
return flow.ZeroID, fmt.Errorf("failed to convert root blob cid to id: %w", err)
}
return rootID, nil
}
func (p *ExecutionDataCIDProvider) addChunkExecutionData(
ced *execution_data.ChunkExecutionData,
blobCh chan<- blobs.Blob,
) (cid.Cid, error) {
cids, err := p.addBlobs(ced, blobCh)
if err != nil {
return cid.Undef, fmt.Errorf("failed to add chunk execution data blobs: %w", err)
}
for {
if len(cids) == 1 {
return cids[0], nil
}
if cids, err = p.addBlobs(cids, blobCh); err != nil {
return cid.Undef, fmt.Errorf("failed to add cid blobs: %w", err)
}
}
}
// addBlobs serializes the given object, splits the serialized data into blobs, and sends them to the given channel.
func (p *ExecutionDataCIDProvider) addBlobs(v interface{}, blobCh chan<- blobs.Blob) ([]cid.Cid, error) {
bcw := blobs.NewBlobChannelWriter(blobCh, p.maxBlobSize)
defer bcw.Close()
if err := p.serializer.Serialize(bcw, v); err != nil {
return nil, fmt.Errorf("failed to serialize object: %w", err)
}
if err := bcw.Flush(); err != nil {
return nil, fmt.Errorf("failed to flush blob channel writer: %w", err)
}
return bcw.CidsSent(), nil
}