This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
/
batchfossilizer.go
299 lines (251 loc) · 8.07 KB
/
batchfossilizer.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2016-2018 Stratumn SAS. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package batchfossilizer implements a fossilizer that fossilizes batches of
// data using a merkle tree.
// The evidence will contain the merkle root, the merkle path, and a timestamp.
package batchfossilizer
import (
"context"
"encoding/hex"
"fmt"
"sync"
"time"
"github.com/stratumn/go-chainscript"
"github.com/stratumn/go-core/batchfossilizer/evidences"
"github.com/stratumn/go-core/fossilizer"
"github.com/stratumn/go-core/monitoring"
"github.com/stratumn/merkle"
)
const (
// Name is the name set in the fossilizer's information.
Name = "batchfossilizer"
// Description is the description set in the fossilizer's information.
Description = "Stratumn Batch Fossilizer"
)
// Info is the info returned by GetInfo.
type Info struct {
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
Commit string `json:"commit"`
}
// PendingProof is used to store an incomplete proof (waiting for the proof of
// the wrapped fossilizer).
type PendingProof struct {
fossil *fossilizer.Fossil
proof *evidences.BatchProof
}
// Fossilizer is the type that
// implements github.com/stratumn/go-core/fossilizer.Adapter.
type Fossilizer struct {
config *Config
ticker *time.Ticker
foss fossilizer.Adapter
queue fossilizer.FossilsQueue
eventChansLock sync.RWMutex
eventChans []chan *fossilizer.Event
pendingProofsLock sync.Mutex
pendingProofs map[string][]*PendingProof
}
// New creates an instance of a batch Fossilizer by wrapping an existing
// fossilizer.
// You should cancel the input context to properly free internal go routines
// when you don't need the fossilizer.
func New(ctx context.Context, config *Config, f fossilizer.Adapter, q fossilizer.FossilsQueue) fossilizer.Adapter {
t := time.NewTicker(config.GetInterval())
a := &Fossilizer{
config: config,
ticker: t,
foss: f,
queue: q,
pendingProofs: make(map[string][]*PendingProof),
}
// The wrapped fossilizer will be the one doing the actual fossilization
// and producing a blockchain proof.
// So we need to listen to its events and re-trigger our own events on top.
fChan := make(chan *fossilizer.Event)
f.AddFossilizerEventChan(fChan)
go a.eventLoop(ctx, fChan)
go a.fossilizeLoop(ctx)
return a
}
// GetInfo returns the fossilizer info.
func (a *Fossilizer) GetInfo(ctx context.Context) (interface{}, error) {
return &Info{
Name: Name,
Description: Description,
Version: a.config.Version,
Commit: a.config.Commit,
}, nil
}
// AddFossilizerEventChan adds a new listener.
func (a *Fossilizer) AddFossilizerEventChan(fossilizerEventChan chan *fossilizer.Event) {
a.eventChansLock.Lock()
defer a.eventChansLock.Unlock()
a.eventChans = append(a.eventChans, fossilizerEventChan)
}
// Fossilize adds to the fossilizing queue.
func (a *Fossilizer) Fossilize(ctx context.Context, data []byte, meta []byte) error {
return a.queue.Push(ctx, &fossilizer.Fossil{
Data: data,
Meta: meta,
})
}
func (a *Fossilizer) fossilizeLoop(ctx context.Context) {
for {
select {
case <-a.ticker.C:
a.fossilizeBatch(context.Background())
case <-ctx.Done():
a.ticker.Stop()
return
}
}
}
func (a *Fossilizer) fossilizeBatch(ctx context.Context) {
span, ctx := monitoring.StartSpanProcessing(ctx, "batchfossilizer/fossilizeBatch")
defer span.End()
batchSize := a.config.GetMaxLeaves()
for i := 0; i < a.config.GetMaxSimBatches(); i++ {
batchCount.Inc()
fossils, err := a.queue.Pop(ctx, batchSize)
if err != nil {
monitoring.SetSpanStatus(span, err)
return
}
if len(fossils) == 0 {
return
}
leaves := make([][]byte, len(fossils))
for i := 0; i < len(fossils); i++ {
leaves[i] = fossils[i].Data
}
tree, err := merkle.NewStaticTree(leaves)
if err != nil {
monitoring.SetSpanStatus(span, err)
return
}
root := tree.Root()
a.addPendingProofs(fossils, root, tree)
err = a.foss.Fossilize(ctx, root, nil)
if err != nil {
monitoring.SetSpanStatus(span, err)
monitoring.TxLogEntry(ctx).
WithField("fossils_count", len(fossils)).
WithError(err).
Warn("Batch fossilization failed. Pushing pending fossils back to the queue.")
a.removePendingProofs(root)
// Some fossilizers rely on external APIs that can transiently fail
// (btcfossilizer for example).
// We want to keep the failed fossils in the queue and retry at the
// next batch.
for _, fossil := range fossils {
err := a.queue.Push(ctx, fossil)
if err != nil {
monitoring.TxLogEntry(ctx).
WithField("fossil", hex.EncodeToString(fossil.Data)).
WithError(err).
Error("Could not enqueue fossil. Fossilization failed, please investigate.")
}
}
return
}
// If the queue is empty, early return instead of polling the queue.
if len(fossils) < batchSize {
return
}
}
}
// addPendingProofs pre-fills proofs for all the fossils that are pending.
// Once the wrapped fossilizer confirms fossilization of the merkle tree, we
// can create fossilization events for all those pending proofs.
func (a *Fossilizer) addPendingProofs(fossils []*fossilizer.Fossil, root []byte, tree *merkle.StaticTree) {
a.pendingProofsLock.Lock()
defer a.pendingProofsLock.Unlock()
pendingProofs := make([]*PendingProof, len(fossils))
for i := 0; i < len(fossils); i++ {
pendingProofs[i] = &PendingProof{
fossil: fossils[i],
proof: &evidences.BatchProof{
Root: root,
Path: tree.Path(i),
},
}
}
key := hex.EncodeToString(root)
a.pendingProofs[key] = pendingProofs
}
// removePendingProofs removes an obsolete proof (for example if child
// fossilizer failed) to prevent memory leaks.
func (a *Fossilizer) removePendingProofs(root []byte) {
a.pendingProofsLock.Lock()
defer a.pendingProofsLock.Unlock()
key := hex.EncodeToString(root)
delete(a.pendingProofs, key)
}
func (a *Fossilizer) eventLoop(ctx context.Context, fChan chan *fossilizer.Event) {
for {
select {
case e := <-fChan:
a.eventBatch(context.Background(), e)
case <-ctx.Done():
close(fChan)
return
}
}
}
// eventBatch transforms a single fossilization event of a merkle root into
// individual fossilization events for each fossil included in the merkle tree.
// It then sends these events to all registered listeners.
func (a *Fossilizer) eventBatch(ctx context.Context, e *fossilizer.Event) {
span, _ := monitoring.StartSpanProcessing(ctx, "batchfossilizer/eventBatch")
defer span.End()
if e.EventType != fossilizer.DidFossilize {
return
}
r := e.Data.(*fossilizer.Result)
key := hex.EncodeToString(r.Data)
a.pendingProofsLock.Lock()
defer a.pendingProofsLock.Unlock()
pendingProofs, ok := a.pendingProofs[key]
if !ok {
monitoring.SetSpanStatus(span, fmt.Errorf("pending proofs not found for root %s", key))
return
}
a.eventChansLock.RLock()
defer a.eventChansLock.RUnlock()
for _, p := range pendingProofs {
// Wrap the underlying (blockchain) proof.
p.proof.Timestamp = time.Now().Unix()
p.proof.Proof = r.Evidence.Proof
ev, err := p.proof.Evidence(r.Evidence.Provider)
if err != nil {
monitoring.TxLogEntry(ctx).WithError(err).Warnf("could not create evidence")
continue
}
for _, l := range a.eventChans {
fossilizedLinksCount.Inc()
go func(l chan *fossilizer.Event, fossil fossilizer.Fossil, evidence chainscript.Evidence) {
l <- &fossilizer.Event{
EventType: fossilizer.DidFossilize,
Data: &fossilizer.Result{
Fossil: fossil,
Evidence: evidence,
},
}
}(l, *p.fossil, *ev)
}
}
delete(a.pendingProofs, key)
}