-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathprovider.go
More file actions
245 lines (200 loc) · 6.77 KB
/
provider.go
File metadata and controls
245 lines (200 loc) · 6.77 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
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(*Provider)
func WithBlobSizeLimit(size int) ProviderOption {
return func(p *Provider) {
p.maxBlobSize = size
}
}
// Provider is used to provide execution data blobs over the network via a blob service.
type Provider struct {
logger zerolog.Logger
metrics module.ExecutionDataProviderMetrics
maxBlobSize int
serializer execution_data.Serializer
blobService network.BlobService
storage tracker.Storage
}
func NewProvider(
logger zerolog.Logger,
metrics module.ExecutionDataProviderMetrics,
serializer execution_data.Serializer,
blobService network.BlobService,
storage tracker.Storage,
opts ...ProviderOption,
) *Provider {
p := &Provider{
logger: logger.With().Str("component", "execution_data_provider").Logger(),
metrics: metrics,
maxBlobSize: execution_data.DefaultMaxBlobSize,
serializer: serializer,
blobService: blobService,
storage: storage,
}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *Provider) 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 *Provider) Provide(ctx context.Context, blockHeight uint64, executionData *execution_data.BlockExecutionData) (flow.Identifier, error) {
rootID, errCh, err := p.provide(ctx, blockHeight, executionData)
storeErr, ok := <-errCh
if err != nil {
return flow.ZeroID, err
}
if ok {
return flow.ZeroID, storeErr
} else {
return rootID, nil
}
}
func (p *Provider) provide(ctx context.Context, blockHeight uint64, executionData *execution_data.BlockExecutionData) (flow.Identifier, <-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, gCtx := errgroup.WithContext(ctx)
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.addChunkExecutionData(gCtx, 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, errCh, err
}
edRoot := &execution_data.BlockExecutionDataRoot{
BlockID: executionData.BlockID,
ChunkExecutionDataIDs: chunkDataIDs,
}
rootID, err := p.addExecutionDataRoot(ctx, edRoot, blobCh)
if err != nil {
return flow.ZeroID, 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, errCh, nil
}
func (p *Provider) addExecutionDataRoot(
ctx context.Context,
edRoot *execution_data.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())
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 *Provider) addChunkExecutionData(
ctx context.Context,
ced *execution_data.ChunkExecutionData,
blobCh chan<- blobs.Blob,
) (cid.Cid, error) {
cids, err := p.addBlobs(ctx, 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(ctx, 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 *Provider) addBlobs(ctx context.Context, 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
}