-
Notifications
You must be signed in to change notification settings - Fork 175
/
transaction_results.go
87 lines (70 loc) · 3.11 KB
/
transaction_results.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
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package operation
import (
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
)
func InsertTransactionResult(blockID flow.Identifier, transactionResult *flow.TransactionResult) func(*badger.Txn) error {
return insert(makePrefix(codeTransactionResult, blockID, transactionResult.TransactionID), transactionResult)
}
func BatchInsertTransactionResult(blockID flow.Identifier, transactionResult *flow.TransactionResult) func(batch *badger.WriteBatch) error {
return batchWrite(makePrefix(codeTransactionResult, blockID, transactionResult.TransactionID), transactionResult)
}
func BatchIndexTransactionResult(blockID flow.Identifier, txIndex uint32, transactionResult *flow.TransactionResult) func(batch *badger.WriteBatch) error {
return batchWrite(makePrefix(codeTransactionResultIndex, blockID, txIndex), transactionResult)
}
func RetrieveTransactionResult(blockID flow.Identifier, transactionID flow.Identifier, transactionResult *flow.TransactionResult) func(*badger.Txn) error {
return retrieve(makePrefix(codeTransactionResult, blockID, transactionID), transactionResult)
}
func RetrieveTransactionResultByIndex(blockID flow.Identifier, txIndex uint32, transactionResult *flow.TransactionResult) func(*badger.Txn) error {
return retrieve(makePrefix(codeTransactionResultIndex, blockID, txIndex), transactionResult)
}
func LookupTransactionResultsByBlockID(blockID flow.Identifier, txResults *[]flow.TransactionResult) func(*badger.Txn) error {
txErrIterFunc := func() (checkFunc, createFunc, handleFunc) {
check := func(_ []byte) bool {
return true
}
var val flow.TransactionResult
create := func() interface{} {
return &val
}
handle := func() error {
*txResults = append(*txResults, val)
return nil
}
return check, create, handle
}
return traverse(makePrefix(codeTransactionResult, blockID), txErrIterFunc)
}
// LookupTransactionResultsByBlockIDUsingIndex retrieves all tx results for a block, but using
// tx_index index. This correctly handles cases of duplicate transactions within block, and should
// eventually replace uses of LookupTransactionResultsByBlockID
func LookupTransactionResultsByBlockIDUsingIndex(blockID flow.Identifier, txResults *[]flow.TransactionResult) func(*badger.Txn) error {
txErrIterFunc := func() (checkFunc, createFunc, handleFunc) {
check := func(_ []byte) bool {
return true
}
var val flow.TransactionResult
create := func() interface{} {
return &val
}
handle := func() error {
*txResults = append(*txResults, val)
return nil
}
return check, create, handle
}
return traverse(makePrefix(codeTransactionResultIndex, blockID), txErrIterFunc)
}
// RemoveTransactionResultsByBlockID removes the transaction results for the given blockID
func RemoveTransactionResultsByBlockID(blockID flow.Identifier) func(*badger.Txn) error {
return func(txn *badger.Txn) error {
prefix := makePrefix(codeTransactionResult, blockID)
err := removeByPrefix(prefix)(txn)
if err != nil {
return fmt.Errorf("could not remove transaction results for block %v: %w", blockID, err)
}
return nil
}
}