-
Notifications
You must be signed in to change notification settings - Fork 199
/
txSimulator.go
221 lines (191 loc) · 6.56 KB
/
txSimulator.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
package txsimulator
import (
"encoding/hex"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data/block"
"github.com/ElrondNetwork/elrond-go-core/data/receipt"
"github.com/ElrondNetwork/elrond-go-core/data/smartContractResult"
"github.com/ElrondNetwork/elrond-go-core/data/transaction"
"github.com/ElrondNetwork/elrond-go-core/hashing"
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go/process"
txSimData "github.com/ElrondNetwork/elrond-go/process/txsimulator/data"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/storage"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
// ArgsTxSimulator holds the arguments required for creating a new transaction simulator
type ArgsTxSimulator struct {
TransactionProcessor TransactionProcessor
IntermediateProcContainer process.IntermediateProcessorContainer
AddressPubKeyConverter core.PubkeyConverter
ShardCoordinator sharding.Coordinator
VMOutputCacher storage.Cacher
Hasher hashing.Hasher
Marshalizer marshal.Marshalizer
}
type transactionSimulator struct {
txProcessor TransactionProcessor
intermProcContainer process.IntermediateProcessorContainer
addressPubKeyConverter core.PubkeyConverter
shardCoordinator sharding.Coordinator
vmOutputCacher storage.Cacher
hasher hashing.Hasher
marshalizer marshal.Marshalizer
}
// NewTransactionSimulator returns a new instance of a transactionSimulator
func NewTransactionSimulator(args ArgsTxSimulator) (*transactionSimulator, error) {
if check.IfNil(args.TransactionProcessor) {
return nil, ErrNilTxSimulatorProcessor
}
if check.IfNil(args.IntermediateProcContainer) {
return nil, ErrNilIntermediateProcessorContainer
}
if check.IfNil(args.AddressPubKeyConverter) {
return nil, ErrNilPubkeyConverter
}
if check.IfNil(args.ShardCoordinator) {
return nil, ErrNilShardCoordinator
}
if check.IfNil(args.VMOutputCacher) {
return nil, ErrNilCacher
}
if check.IfNil(args.Marshalizer) {
return nil, ErrNilMarshalizer
}
if check.IfNil(args.Hasher) {
return nil, ErrNilHasher
}
return &transactionSimulator{
txProcessor: args.TransactionProcessor,
intermProcContainer: args.IntermediateProcContainer,
addressPubKeyConverter: args.AddressPubKeyConverter,
shardCoordinator: args.ShardCoordinator,
vmOutputCacher: args.VMOutputCacher,
marshalizer: args.Marshalizer,
hasher: args.Hasher,
}, nil
}
// ProcessTx will process the transaction in a special environment, where state-writing is not allowed
func (ts *transactionSimulator) ProcessTx(tx *transaction.Transaction) (*txSimData.SimulationResults, error) {
txStatus := transaction.TxStatusPending
failReason := ""
retCode, err := ts.txProcessor.ProcessTransaction(tx)
if err != nil {
failReason = err.Error()
txStatus = transaction.TxStatusFail
} else {
if retCode == vmcommon.Ok {
txStatus = transaction.TxStatusSuccess
}
}
results := &txSimData.SimulationResults{
Status: txStatus,
FailReason: failReason,
}
err = ts.addIntermediateTxsToResult(results)
if err != nil {
return nil, err
}
vmOutput, ok := ts.getVMOutputOfTx(tx)
if ok {
results.VMOutput = vmOutput
}
return results, nil
}
func (ts *transactionSimulator) getVMOutputOfTx(tx *transaction.Transaction) (*vmcommon.VMOutput, bool) {
txHash, err := core.CalculateHash(ts.marshalizer, ts.hasher, tx)
if err != nil {
return nil, false
}
defer ts.vmOutputCacher.Remove(txHash)
vmOutputI, ok := ts.vmOutputCacher.Get(txHash)
if !ok {
return nil, false
}
vmOutput, ok := vmOutputI.(*vmcommon.VMOutput)
if !ok {
return nil, false
}
return vmOutput, true
}
func (ts *transactionSimulator) addIntermediateTxsToResult(result *txSimData.SimulationResults) error {
defer func() {
processorsKeys := ts.intermProcContainer.Keys()
for _, procKey := range processorsKeys {
processor, errGetProc := ts.intermProcContainer.Get(procKey)
if errGetProc != nil || processor == nil {
continue
}
processor.CreateBlockStarted()
}
}()
scrForwarder, err := ts.intermProcContainer.Get(block.SmartContractResultBlock)
if err != nil {
return err
}
scResults := make(map[string]*transaction.ApiSmartContractResult)
for hash, value := range scrForwarder.GetAllCurrentFinishedTxs() {
scr, ok := value.(*smartContractResult.SmartContractResult)
if !ok {
continue
}
scResults[hex.EncodeToString([]byte(hash))] = ts.adaptSmartContractResult(scr)
}
result.ScResults = scResults
if ts.shardCoordinator.SelfId() == core.MetachainShardId {
return nil
}
receiptsForwarder, err := ts.intermProcContainer.Get(block.ReceiptBlock)
if err != nil {
return err
}
receipts := make(map[string]*transaction.ApiReceipt)
for hash, value := range receiptsForwarder.GetAllCurrentFinishedTxs() {
rcpt, ok := value.(*receipt.Receipt)
if !ok {
continue
}
receipts[hex.EncodeToString([]byte(hash))] = ts.adaptReceipt(rcpt)
}
result.Receipts = receipts
return nil
}
func (ts *transactionSimulator) adaptSmartContractResult(scr *smartContractResult.SmartContractResult) *transaction.ApiSmartContractResult {
resScr := &transaction.ApiSmartContractResult{
Nonce: scr.Nonce,
Value: scr.Value,
RcvAddr: ts.addressPubKeyConverter.Encode(scr.RcvAddr),
SndAddr: ts.addressPubKeyConverter.Encode(scr.SndAddr),
RelayedValue: scr.RelayedValue,
Code: string(scr.Code),
Data: string(scr.Data),
PrevTxHash: hex.EncodeToString(scr.PrevTxHash),
OriginalTxHash: hex.EncodeToString(scr.OriginalTxHash),
GasLimit: scr.GasLimit,
GasPrice: scr.GasPrice,
CallType: scr.CallType,
CodeMetadata: string(scr.CodeMetadata),
ReturnMessage: string(scr.ReturnMessage),
}
if scr.OriginalSender != nil {
resScr.OriginalSender = ts.addressPubKeyConverter.Encode(scr.OriginalSender)
}
if scr.RelayerAddr != nil {
resScr.RelayerAddr = ts.addressPubKeyConverter.Encode(scr.RelayerAddr)
}
return resScr
}
func (ts *transactionSimulator) adaptReceipt(rcpt *receipt.Receipt) *transaction.ApiReceipt {
return &transaction.ApiReceipt{
Value: rcpt.Value,
SndAddr: ts.addressPubKeyConverter.Encode(rcpt.SndAddr),
Data: string(rcpt.Data),
TxHash: hex.EncodeToString(rcpt.TxHash),
}
}
// IsInterfaceNil returns true if there is no value under the interface
func (ts *transactionSimulator) IsInterfaceNil() bool {
return ts == nil
}