-
Notifications
You must be signed in to change notification settings - Fork 178
/
receipts.go
220 lines (187 loc) · 7.16 KB
/
receipts.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
package badger
import (
"errors"
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/badger/operation"
)
// ExecutionReceipts implements storage for execution receipts.
type ExecutionReceipts struct {
db *badger.DB
results *ExecutionResults
cache *Cache
}
func NewExecutionReceipts(collector module.CacheMetrics, db *badger.DB, results *ExecutionResults) *ExecutionReceipts {
store := func(key interface{}, val interface{}) func(tx *badger.Txn) error {
return func(tx *badger.Txn) error {
receipt := val.(*flow.ExecutionReceipt)
// store the receipt-specific metadata
err := operation.SkipDuplicates(operation.InsertExecutionReceiptMeta(receipt.ID(), receipt.Meta()))(tx)
if err != nil {
return fmt.Errorf("could not store receipt metadata: %w", err)
}
err = results.store(&receipt.ExecutionResult)(tx)
if err != nil {
return fmt.Errorf("could not store result: %w", err)
}
return nil
}
}
retrieve := func(key interface{}) func(tx *badger.Txn) (interface{}, error) {
receiptID := key.(flow.Identifier)
return func(tx *badger.Txn) (interface{}, error) {
var meta flow.ExecutionReceiptMeta
err := operation.RetrieveExecutionReceiptMeta(receiptID, &meta)(tx)
if err != nil {
return nil, fmt.Errorf("could not retrieve receipt meta: %w", err)
}
result, err := results.byID(meta.ResultID)(tx)
if err != nil {
return nil, fmt.Errorf("could not retrieve result: %w", err)
}
return flow.ExecutionReceiptFromMeta(meta, *result), nil
}
}
return &ExecutionReceipts{
db: db,
results: results,
cache: newCache(collector,
withLimit(flow.DefaultTransactionExpiry+100),
withStore(store),
withRetrieve(retrieve),
withResource(metrics.ResourceReceipt)),
}
}
func (r *ExecutionReceipts) store(receipt *flow.ExecutionReceipt) func(*badger.Txn) error {
return r.cache.Put(receipt.ID(), receipt)
}
func (r *ExecutionReceipts) byID(receiptID flow.Identifier) func(*badger.Txn) (*flow.ExecutionReceipt, error) {
return func(tx *badger.Txn) (*flow.ExecutionReceipt, error) {
val, err := r.cache.Get(receiptID)(tx)
if err != nil {
return nil, err
}
return val.(*flow.ExecutionReceipt), nil
}
}
func (r *ExecutionReceipts) byBlockID(blockID flow.Identifier) func(*badger.Txn) (*flow.ExecutionReceipt, error) {
return func(tx *badger.Txn) (*flow.ExecutionReceipt, error) {
var receiptID flow.Identifier
err := operation.LookupExecutionReceipt(blockID, &receiptID)(tx)
if err != nil {
return nil, fmt.Errorf("could not lookup receipt ID: %w", err)
}
return r.byID(receiptID)(tx)
}
}
func (r *ExecutionReceipts) Store(receipt *flow.ExecutionReceipt) error {
return operation.RetryOnConflict(r.db.Update, r.store(receipt))
}
func (r *ExecutionReceipts) ByID(receiptID flow.Identifier) (*flow.ExecutionReceipt, error) {
tx := r.db.NewTransaction(false)
defer tx.Discard()
return r.byID(receiptID)(tx)
}
func (r *ExecutionReceipts) Index(blockID, receiptID flow.Identifier) error {
return operation.RetryOnConflict(r.db.Update, func(tx *badger.Txn) error {
err := operation.IndexExecutionReceipt(blockID, receiptID)(tx)
if err == nil {
return nil
}
if !errors.Is(err, storage.ErrAlreadyExists) {
return err
}
// when trying to index a receipt for a block, and there is already a receipt indexed for this block,
// double check if the indexed receipt has the same result.
// if the result is the same, we could skip indexing the receipt
// if the result is different, then return error
var storedReceiptID flow.Identifier
err = operation.LookupExecutionReceipt(blockID, &storedReceiptID)(tx)
if err != nil {
return fmt.Errorf("there is a receipt stored already, but cannot retrieve the ID of it: %w", err)
}
storedReceipt, err := r.byID(storedReceiptID)(tx)
if err != nil {
return fmt.Errorf("there is a receipt stored already, but cannot retrieve of it: %w", err)
}
storingReceipt, err := r.byID(receiptID)(tx)
if err != nil {
return fmt.Errorf("attempting to index a receipt, but the receipt is not stored yet: %w", err)
}
storedResultID := storedReceipt.ExecutionResult.ID()
storingResultID := storingReceipt.ExecutionResult.ID()
if storedResultID != storingResultID {
return fmt.Errorf(
"storing receipt that is different from the already stored one for block: %v, storing receipt: %v, stored receipt: %v. storing result: %v, stored result: %v, %w",
blockID, receiptID, storedReceiptID, storingResultID, storedResultID, storage.ErrDataMismatch)
}
return nil
})
}
func (r *ExecutionReceipts) IndexByBlockIDAndExecutionID(blockID, executorID, receiptID flow.Identifier) error {
return operation.RetryOnConflict(r.db.Update, func(tx *badger.Txn) error {
err := operation.IndexExecutionReceiptByBlockIDExecutionID(blockID, executorID, receiptID)(tx)
if err == nil {
return nil
}
if !errors.Is(err, storage.ErrAlreadyExists) {
return err
}
// when trying to index a receipt for a block, and there is already a receipt indexed for this block,
// double check if the indexed receipt has the same result.
// if the result is the same, we could skip indexing the receipt
// if the result is different, then return error
var storedReceiptID flow.Identifier
err = operation.LookupExecutionReceiptByBlockIDExecutionID(blockID, executorID, &storedReceiptID)(tx)
if err != nil {
return fmt.Errorf("there is a receipt stored already, but cannot retrieve the ID of it: %w", err)
}
storedReceipt, err := r.byID(storedReceiptID)(tx)
if err != nil {
return fmt.Errorf("there is a receipt stored already, but cannot retrieve of it: %w", err)
}
storingReceipt, err := r.byID(receiptID)(tx)
if err != nil {
return fmt.Errorf("attempting to index a receipt, but the receipt is not stored yet: %w", err)
}
storedResultID := storedReceipt.ExecutionResult.ID()
storingResultID := storingReceipt.ExecutionResult.ID()
if storedResultID != storingResultID {
return fmt.Errorf(
"storing receipt that is different from the already stored one for block: %v, execution ID: %v, storing receipt: %v, stored receipt: %v. storing result: %v, stored result: %v, %w",
blockID, executorID, receiptID, storedReceiptID, storingResultID, storedResultID, storage.ErrDataMismatch)
}
return nil
})
}
func (r *ExecutionReceipts) ByBlockID(blockID flow.Identifier) (*flow.ExecutionReceipt, error) {
tx := r.db.NewTransaction(false)
defer tx.Discard()
return r.byBlockID(blockID)(tx)
}
func (r *ExecutionReceipts) ByBlockIDAllExecutionReceipts(blockID flow.Identifier) ([]flow.ExecutionReceipt, error) {
var receipts []flow.ExecutionReceipt
err := r.db.View(func(btx *badger.Txn) error {
var receiptIDs []flow.Identifier
err := operation.LookupExecutionReceiptByBlockIDAllExecutionIDs(blockID, &receiptIDs)(btx)
if err != nil {
return err
}
for _, id := range receiptIDs {
receipt, err := r.byID(id)(btx)
if err != nil {
return err
}
receipts = append(receipts, *receipt)
}
return nil
})
if err != nil {
return nil, err
}
return receipts, nil
}