-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsmartContractResultsProcessor.go
More file actions
213 lines (186 loc) · 7.05 KB
/
smartContractResultsProcessor.go
File metadata and controls
213 lines (186 loc) · 7.05 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
package transactions
import (
"encoding/hex"
"strconv"
"github.com/multiversx/mx-chain-core-go/core"
coreData "github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/data/outport"
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
indexerData "github.com/multiversx/mx-chain-es-indexer-go/data"
"github.com/multiversx/mx-chain-es-indexer-go/process/dataindexer"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/converters"
)
type smartContractResultsProcessor struct {
pubKeyConverter core.PubkeyConverter
hasher hashing.Hasher
marshalizer marshal.Marshalizer
dataFieldParser DataFieldParser
balanceConverter dataindexer.BalanceConverter
relayedV1V2DisableEpoch uint32
}
func newSmartContractResultsProcessor(
pubKeyConverter core.PubkeyConverter,
marshalzier marshal.Marshalizer,
hasher hashing.Hasher,
dataFieldParser DataFieldParser,
balanceConverter dataindexer.BalanceConverter,
relayedV1V2DisableEpoch uint32,
) *smartContractResultsProcessor {
return &smartContractResultsProcessor{
pubKeyConverter: pubKeyConverter,
marshalizer: marshalzier,
hasher: hasher,
dataFieldParser: dataFieldParser,
balanceConverter: balanceConverter,
relayedV1V2DisableEpoch: relayedV1V2DisableEpoch,
}
}
func (proc *smartContractResultsProcessor) processSCRs(
miniBlocks []*block.MiniBlock,
header coreData.HeaderHandler,
scrs map[string]*outport.SCRInfo,
numOfShards uint32,
timestampMs uint64,
) []*indexerData.ScResult {
allSCRs := make([]*indexerData.ScResult, 0, len(scrs))
// a copy of the SCRS map is needed because proc.processSCRsFromMiniblock would remove items from the original map
workingSCRSMap := copySCRSMap(scrs)
for _, mb := range miniBlocks {
if mb.Type != block.SmartContractResultBlock {
continue
}
indexerSCRs := proc.processSCRsFromMiniblock(header, mb, workingSCRSMap, numOfShards, timestampMs)
allSCRs = append(allSCRs, indexerSCRs...)
}
selfShardID := header.GetShardID()
for scrHashHex, noMBScrInfo := range workingSCRSMap {
indexerScr := proc.prepareSmartContractResult(scrHashHex, nil, noMBScrInfo, header, selfShardID, selfShardID, numOfShards, timestampMs)
allSCRs = append(allSCRs, indexerScr)
}
return allSCRs
}
func (proc *smartContractResultsProcessor) processSCRsFromMiniblock(
header coreData.HeaderHandler,
mb *block.MiniBlock,
scrs map[string]*outport.SCRInfo,
numOfShards uint32,
timestampMs uint64,
) []*indexerData.ScResult {
mbHash, err := core.CalculateHash(proc.marshalizer, proc.hasher, mb)
if err != nil {
log.Warn("smartContractResultsProcessor.processSCRsFromMiniblock cannot calculate miniblock hash")
return []*indexerData.ScResult{}
}
indexerSCRs := make([]*indexerData.ScResult, 0, len(mb.TxHashes))
for _, scrHash := range mb.TxHashes {
scrHashHex := hex.EncodeToString(scrHash)
scrInfo, ok := scrs[scrHashHex]
if !ok {
log.Warn("smartContractResultsProcessor.processSCRsFromMiniblock scr not found in map",
"scr hash", scrHashHex,
)
continue
}
indexerSCR := proc.prepareSmartContractResult(hex.EncodeToString(scrHash), mbHash, scrInfo, header, mb.SenderShardID, mb.ReceiverShardID, numOfShards, timestampMs)
indexerSCRs = append(indexerSCRs, indexerSCR)
delete(scrs, scrHashHex)
}
return indexerSCRs
}
func (proc *smartContractResultsProcessor) prepareSmartContractResult(
scrHashHex string,
mbHash []byte,
scrInfo *outport.SCRInfo,
header coreData.HeaderHandler,
senderShard uint32,
receiverShard uint32,
numOfShards uint32,
timestampMs uint64,
) *indexerData.ScResult {
scr := scrInfo.SmartContractResult
hexEncodedMBHash := ""
if len(mbHash) > 0 {
hexEncodedMBHash = hex.EncodeToString(mbHash)
}
relayerAddr := ""
if len(scr.RelayerAddr) > 0 {
relayerAddr = proc.pubKeyConverter.SilentEncode(scr.RelayerAddr, log)
}
relayedValue := ""
if scr.RelayedValue != nil {
relayedValue = scr.RelayedValue.String()
}
originalSenderAddr := ""
if scr.OriginalSender != nil {
originalSenderAddr = proc.pubKeyConverter.SilentEncode(scr.OriginalSender, log)
}
res := proc.dataFieldParser.Parse(scr.Data, scr.SndAddr, scr.RcvAddr, numOfShards, header.GetEpoch())
senderAddr := proc.pubKeyConverter.SilentEncode(scr.SndAddr, log)
receiverAddr := proc.pubKeyConverter.SilentEncode(scr.RcvAddr, log)
receiversAddr, _ := proc.pubKeyConverter.EncodeSlice(res.Receivers)
valueNum, err := proc.balanceConverter.ConvertBigValueToFloat(scr.Value)
if err != nil {
log.Warn("smartContractResultsProcessor.prepareSmartContractResult cannot compute scr value as num",
"value", scr.Value, "hash", scrHashHex, "error", err)
}
esdtValuesNum, err := proc.balanceConverter.ComputeSliceOfStringsAsFloat(res.ESDTValues)
if err != nil {
log.Warn("smartContractResultsProcessor.prepareSmartContractResult cannot compute scr esdt values as num",
"esdt values", res.ESDTValues, "hash", scrHashHex, "error", err)
}
var esdtValues []string
if areESDTValuesOK(res.ESDTValues) {
esdtValues = res.ESDTValues
}
isRelayed := res.IsRelayed && header.GetEpoch() < proc.relayedV1V2DisableEpoch
feeInfo := getFeeInfo(scrInfo)
return &indexerData.ScResult{
Hash: scrHashHex,
MBHash: hexEncodedMBHash,
Nonce: scr.Nonce,
GasLimit: scr.GasLimit,
GasPrice: scr.GasPrice,
Value: scr.Value.String(),
ValueNum: valueNum,
Sender: senderAddr,
Receiver: receiverAddr,
RelayerAddr: relayerAddr,
RelayedValue: relayedValue,
Code: string(scr.Code),
Data: scr.Data,
PrevTxHash: hex.EncodeToString(scr.PrevTxHash),
OriginalTxHash: hex.EncodeToString(scr.OriginalTxHash),
CallType: strconv.Itoa(int(scr.CallType)),
CodeMetadata: scr.CodeMetadata,
ReturnMessage: string(scr.ReturnMessage),
Timestamp: header.GetTimeStamp(),
SenderAddressBytes: scr.SndAddr,
SenderShard: senderShard,
ReceiverShard: receiverShard,
Operation: res.Operation,
Function: converters.TruncateFieldIfExceedsMaxLength(res.Function),
ESDTValues: esdtValues,
ESDTValuesNum: esdtValuesNum,
Tokens: converters.TruncateSliceElementsIfExceedsMaxLength(res.Tokens),
Receivers: receiversAddr,
ReceiversShardIDs: res.ReceiversShardID,
IsRelayed: isRelayed,
OriginalSender: originalSenderAddr,
InitialTxFee: feeInfo.Fee.String(),
InitialTxGasUsed: feeInfo.GasUsed,
GasRefunded: feeInfo.GasRefunded,
ExecutionOrder: int(scrInfo.ExecutionOrder),
UUID: converters.GenerateBase64UUID(),
Epoch: header.GetEpoch(),
TimestampMs: timestampMs,
}
}
func copySCRSMap(initial map[string]*outport.SCRInfo) map[string]*outport.SCRInfo {
newMap := make(map[string]*outport.SCRInfo)
for key, value := range initial {
newMap[key] = value
}
return newMap
}