-
Notifications
You must be signed in to change notification settings - Fork 202
/
eventsHashesByTxHashIndex.go
133 lines (110 loc) · 3.85 KB
/
eventsHashesByTxHashIndex.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
//go:generate protoc -I=proto -I=$GOPATH/src -I=$GOPATH/src/github.com/ElrondNetwork/protobuf/protobuf --gogoslick_out=. resultsHashesByTxHash.proto
package dblookupext
import (
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go-core/data/receipt"
"github.com/ElrondNetwork/elrond-go-core/data/smartContractResult"
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go/storage"
)
type eventsHashesByTxHash struct {
marshalizer marshal.Marshalizer
storer storage.Storer
}
func newEventsHashesByTxHash(storer storage.Storer, marshalizer marshal.Marshalizer) *eventsHashesByTxHash {
return &eventsHashesByTxHash{
marshalizer: marshalizer,
storer: storer,
}
}
func (eht *eventsHashesByTxHash) saveResultsHashes(epoch uint32, scResults, receipts map[string]data.TransactionHandler) error {
resultsHashes := eht.groupTransactionOutcomesByTransactionHash(epoch, scResults, receipts)
results := eht.mergeRecordsFromStorageIfExists(resultsHashes)
for txHash, resultHashes := range results {
resultHashesBytes, err := eht.marshalizer.Marshal(resultHashes)
if err != nil {
continue
}
err = eht.storer.Put([]byte(txHash), resultHashesBytes)
if err != nil {
log.Warn("saveResultsHashes() cannot save resultHashesByte",
"error", err.Error())
continue
}
}
return nil
}
func (eht *eventsHashesByTxHash) groupTransactionOutcomesByTransactionHash(
epoch uint32,
scResults,
receipts map[string]data.TransactionHandler,
) map[string]*ResultsHashesByTxHash {
resultsHashesMap := eht.groupSmartContractResults(epoch, scResults)
for receiptHash, receiptHandler := range receipts {
rec, ok := receiptHandler.(*receipt.Receipt)
if !ok {
log.Error("groupTransactionOutcomesByTransactionHash() cannot cast TransactionHandler to Receipt")
continue
}
originalTxHash := string(rec.TxHash)
resultsHashesMap[originalTxHash] = &ResultsHashesByTxHash{
ReceiptsHash: []byte(receiptHash),
}
}
return resultsHashesMap
}
func (eht *eventsHashesByTxHash) groupSmartContractResults(
epoch uint32,
scrResults map[string]data.TransactionHandler,
) map[string]*ResultsHashesByTxHash {
resultsHashesMap := make(map[string]*ResultsHashesByTxHash)
for scrHash, scrHandler := range scrResults {
scrResult, ok := scrHandler.(*smartContractResult.SmartContractResult)
if !ok {
log.Error("groupSmartContractResults() cannot cast TransactionHandler to SmartContractResult")
continue
}
originalTxHash := string(scrResult.OriginalTxHash)
if _, hashExists := resultsHashesMap[originalTxHash]; !hashExists {
resultsHashesMap[originalTxHash] = &ResultsHashesByTxHash{
ScResultsHashesAndEpoch: []*ScResultsHashesAndEpoch{
{
Epoch: epoch,
},
},
}
}
scResultsHashesAndEpoch := resultsHashesMap[originalTxHash].ScResultsHashesAndEpoch[0]
scResultsHashesAndEpoch.ScResultsHashes = append(scResultsHashesAndEpoch.ScResultsHashes, []byte(scrHash))
}
return resultsHashesMap
}
func (eht *eventsHashesByTxHash) mergeRecordsFromStorageIfExists(
records map[string]*ResultsHashesByTxHash,
) map[string]*ResultsHashesByTxHash {
for originalTxHash, results := range records {
rawBytes, err := eht.storer.Get([]byte(originalTxHash))
if err != nil {
continue
}
record := &ResultsHashesByTxHash{}
err = eht.marshalizer.Unmarshal(record, rawBytes)
if err != nil {
continue
}
results.ScResultsHashesAndEpoch = append(record.ScResultsHashesAndEpoch, results.ScResultsHashesAndEpoch...)
}
return records
}
func (eht *eventsHashesByTxHash) getEventsHashesByTxHash(txHash []byte, epoch uint32) (*ResultsHashesByTxHash, error) {
rawBytes, err := eht.storer.GetFromEpoch(txHash, epoch)
if err != nil {
return nil, err
}
record := &ResultsHashesByTxHash{}
err = eht.marshalizer.Unmarshal(record, rawBytes)
if err != nil {
return nil, err
}
return record, nil
}