Skip to content

Commit 33f2198

Browse files
committed
resolving todo's
1 parent 8ea3608 commit 33f2198

File tree

7 files changed

+24
-159
lines changed

7 files changed

+24
-159
lines changed

scenario/gasSchedules/gasSchedules.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package gasschedules
22

3-
// TODO: go:embed can be used after we upgrade to go 1.16
43
// import _ "embed"
54

65
// //go:embed gasScheduleV1.toml

test/contracts/erc20/erc20.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ void computeAllowanceKey(byte *destination, byte *from, byte* to) {
4444
// Note: in smart contract addresses, the first 10 bytes are all 0
4545
// therefore we read from byte 10 onwards to provide more significant bytes
4646
// and to minimize the chance for collisions
47-
// TODO: switching to a hash instead of a concatenation of addresses might make it safer
4847
for (int i = 0; i < 15; i++) {
4948
destination[1+i] = from[10+i];
5049
}

vmhost/contexts/async.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (context *asyncContext) PushState() {
183183
callbackData: context.callbackData,
184184
gasAccumulated: context.gasAccumulated,
185185
returnData: context.returnData,
186-
asyncCallGroups: context.asyncCallGroups, // TODO matei-p use cloneCallGroups()?
186+
asyncCallGroups: context.cloneCallGroups(),
187187

188188
callType: context.callType,
189189
callbackAsyncInitiatorCallID: context.callbackAsyncInitiatorCallID,
@@ -864,7 +864,7 @@ func (context *asyncContext) callCallback(callID []byte, vmOutput *vmcommon.VMOu
864864
}
865865

866866
context.host.Metering().DisableRestoreGas()
867-
isComplete, callbackVMOutput := loadedContext.ExecuteSyncCallbackAndFinishOutput(asyncCall, vmOutput, nil, gasAccumulated, err)
867+
isComplete, callbackVMOutput := loadedContext.ExecuteLocalCallbackAndFinishOutput(asyncCall, vmOutput, nil, gasAccumulated, err)
868868
context.host.Metering().EnableRestoreGas()
869869
return isComplete, callbackVMOutput, nil
870870
}

vmhost/contexts/asyncLocal.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func (context *asyncContext) executeAsyncLocalCalls() error {
3535
return nil
3636
}
3737

38-
// TODO split this method into smaller ones
3938
func (context *asyncContext) executeAsyncLocalCall(asyncCall *vmhost.AsyncCall) error {
4039
destinationCallInput, err := context.createContractCallInput(asyncCall)
4140
if err != nil {
@@ -79,56 +78,54 @@ func (context *asyncContext) executeAsyncLocalCall(asyncCall *vmhost.AsyncCall)
7978
asyncCall.UpdateStatus(vmOutput.ReturnCode)
8079

8180
if isComplete {
81+
callbackGasRemaining := uint64(0)
8282
if asyncCall.HasCallback() {
8383
// Restore gas locked while still on the caller instance; otherwise, the
8484
// locked gas will appear to have been used twice by the caller instance.
85-
isCallbackComplete, callbackVMOutput := context.ExecuteSyncCallbackAndFinishOutput(asyncCall, vmOutput, destinationCallInput, 0, err)
85+
isCallbackComplete, callbackVMOutput := context.ExecuteLocalCallbackAndFinishOutput(asyncCall, vmOutput, destinationCallInput, 0, err)
8686
if callbackVMOutput == nil {
8787
return vmhost.ErrAsyncNoOutputFromCallback
8888
}
8989

9090
context.host.CompleteLogEntriesWithCallType(callbackVMOutput, vmhost.AsyncCallbackString)
9191

9292
if isCallbackComplete {
93-
callbackGasRemaining := callbackVMOutput.GasRemaining
93+
callbackGasRemaining = callbackVMOutput.GasRemaining
9494
callbackVMOutput.GasRemaining = 0
95-
return context.completeChild(asyncCall.CallID, callbackGasRemaining)
9695
}
97-
} else {
98-
return context.completeChild(asyncCall.CallID, 0)
9996
}
97+
98+
return context.completeChild(asyncCall.CallID, callbackGasRemaining)
10099
}
101100

102101
return nil
103102
}
104103

105-
// ExecuteSyncCallbackAndFinishOutput executes the callback and finishes the output
106-
// TODO rename to executeLocalCallbackAndFinishOutput
107-
func (context *asyncContext) ExecuteSyncCallbackAndFinishOutput(
104+
// ExecuteLocalCallbackAndFinishOutput executes the callback and finishes the output
105+
func (context *asyncContext) ExecuteLocalCallbackAndFinishOutput(
108106
asyncCall *vmhost.AsyncCall,
109107
vmOutput *vmcommon.VMOutput,
110108
_ *vmcommon.ContractCallInput,
111109
gasAccumulated uint64,
112110
err error) (bool, *vmcommon.VMOutput) {
113-
callbackVMOutput, isComplete, _ := context.executeSyncCallback(asyncCall, vmOutput, gasAccumulated, err)
111+
callbackVMOutput, isComplete, _ := context.executeLocalCallback(asyncCall, vmOutput, gasAccumulated, err)
114112
context.finishAsyncLocalCallbackExecution()
115113
return isComplete, callbackVMOutput
116114
}
117115

118-
// TODO rename to executeLocalCallback
119-
func (context *asyncContext) executeSyncCallback(
116+
func (context *asyncContext) executeLocalCallback(
120117
asyncCall *vmhost.AsyncCall,
121118
destinationVMOutput *vmcommon.VMOutput,
122119
gasAccumulated uint64,
123120
destinationErr error,
124121
) (*vmcommon.VMOutput, bool, error) {
125122
callbackInput, err := context.createCallbackInput(asyncCall, destinationVMOutput, gasAccumulated, destinationErr)
126123
if err != nil {
127-
logAsync.Trace("executeSyncCallback", "error", err)
124+
logAsync.Trace("executeLocalCallback", "error", err)
128125
return nil, true, err
129126
}
130127

131-
logAsync.Trace("executeSyncCallback",
128+
logAsync.Trace("executeLocalCallback",
132129
"caller", callbackInput.CallerAddr,
133130
"dest", callbackInput.RecipientAddr,
134131
"func", callbackInput.Function,
@@ -183,7 +180,7 @@ func (context *asyncContext) executeSyncHalfOfBuiltinFunction(asyncCall *vmhost.
183180
if vmOutput.ReturnCode != vmcommon.Ok {
184181
asyncCall.Reject()
185182
if asyncCall.HasCallback() {
186-
_, _, _ = context.executeSyncCallback(asyncCall, vmOutput, 0, err)
183+
_, _, _ = context.executeLocalCallback(asyncCall, vmOutput, 0, err)
187184
context.finishAsyncLocalCallbackExecution()
188185
}
189186
}
@@ -240,7 +237,6 @@ func (context *asyncContext) createContractCallInput(asyncCall *vmhost.AsyncCall
240237
return contractCallInput, nil
241238
}
242239

243-
// TODO function too large; refactor needed
244240
func (context *asyncContext) createCallbackInput(
245241
asyncCall *vmhost.AsyncCall,
246242
vmOutput *vmcommon.VMOutput,
@@ -255,32 +251,29 @@ func (context *asyncContext) createCallbackInput(
255251
}
256252

257253
arguments := context.getArgumentsForCallback(vmOutput, destinationErr)
258-
259254
returnWithError := false
260255
if destinationErr != nil || vmOutput.ReturnCode != vmcommon.Ok {
261256
returnWithError = true
262257
}
263258

264259
callbackFunction := asyncCall.GetCallbackName()
265-
266260
dataLength := computeDataLengthFromArguments(callbackFunction, arguments)
267261
gasLimit, err := context.computeGasLimitForCallback(asyncCall, vmOutput, dataLength)
268262
if err != nil {
269263
return nil, err
270264
}
271265

272266
originalCaller := runtime.GetOriginalCallerAddress()
273-
274267
caller := context.address
275-
lastTransferInfo := context.extractLastTransferWithoutData(caller, vmOutput)
268+
lastTransferData := context.extractLastTransferWithoutData(caller, vmOutput)
276269

277270
// Return to the sender SC, calling its specified callback method.
278271
contractCallInput := &vmcommon.ContractCallInput{
279272
VMInput: vmcommon.VMInput{
280273
OriginalCallerAddr: originalCaller,
281274
CallerAddr: actualCallbackInitiator,
282275
Arguments: arguments,
283-
CallValue: lastTransferInfo.callValue,
276+
CallValue: lastTransferData.callValue,
284277
CallType: vm.AsynchronousCallBack,
285278
GasPrice: runtime.GetVMInput().GasPrice,
286279
GasProvided: gasLimit,
@@ -289,7 +282,7 @@ func (context *asyncContext) createCallbackInput(
289282
OriginalTxHash: runtime.GetOriginalTxHash(),
290283
PrevTxHash: runtime.GetPrevTxHash(),
291284
ReturnCallAfterError: returnWithError,
292-
ESDTTransfers: lastTransferInfo.lastESDTTransfers,
285+
ESDTTransfers: lastTransferData.lastESDTTransfers,
293286
},
294287
RecipientAddr: caller,
295288
Function: callbackFunction,

vmhost/contexts/asyncParams.go

Lines changed: 6 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,23 @@ import (
88
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
99
"github.com/multiversx/mx-chain-vm-common-go/txDataBuilder"
1010
"github.com/multiversx/mx-chain-vm-go/crypto"
11-
"github.com/multiversx/mx-chain-vm-go/vmhost"
1211
)
1312

14-
/*
15-
Called to process OutputTransfers created by a
16-
direct call (on dest) builtin function call by the VM
17-
*/
13+
// AddAsyncArgumentsToOutputTransfers
14+
// Called to process OutputTransfers created by a
15+
// direct call (on dest) builtin function call by the VM
1816
func AddAsyncArgumentsToOutputTransfers(
19-
output vmhost.OutputContext,
20-
address []byte,
2117
asyncParams *vmcommon.AsyncArguments,
2218
callType vm.CallType,
23-
vmOutput *vmcommon.VMOutput) error {
19+
vmOutput *vmcommon.VMOutput,
20+
) error {
2421
if asyncParams == nil {
2522
return nil
2623
}
24+
2725
for _, outAcc := range vmOutput.OutputAccounts {
28-
// if !bytes.Equal(address, outAcc.Address) {
29-
// continue
30-
// }
3126

3227
for t, outTransfer := range outAcc.OutputTransfers {
33-
// if !bytes.Equal(address, outTransfer.SenderAddress) {
34-
// continue
35-
// }
3628
if outTransfer.CallType != callType {
3729
continue
3830
}
@@ -84,122 +76,6 @@ func createDataFromAsyncParams(
8476
return callData.ToBytes(), nil
8577
}
8678

87-
/*
88-
Called when a SCR for a callback is created outside the VM
89-
(by createAsyncCallBackSCRFromVMOutput())
90-
This is the case
91-
A) after an async call executed following a builtin function call,
92-
B) other cases where processing the output trasnfers of a VMOutput did
93-
not produce a SCR of type AsynchronousCallBack
94-
TODO(check): function not used?
95-
*/
96-
func AppendAsyncArgumentsToCallbackCallData(
97-
hasher crypto.Hasher,
98-
data []byte,
99-
asyncArguments *vmcommon.AsyncArguments,
100-
parseArgumentsFunc func(data string) ([][]byte, error)) ([]byte, error) {
101-
102-
return appendAsyncParamsToCallData(
103-
CreateCallbackAsyncParams(hasher, asyncArguments),
104-
data,
105-
false,
106-
parseArgumentsFunc)
107-
}
108-
109-
/*
110-
Called when a SCR is created from VMOutput in order to recompose
111-
async data and call data into a transfer data ready for the SCR
112-
(by preprocessOutTransferToSCR())
113-
TODO(check): function not used?
114-
*/
115-
func AppendTransferAsyncDataToCallData(
116-
callData []byte,
117-
asyncData []byte,
118-
parseArgumentsFunc func(data string) ([][]byte, error)) ([]byte, error) {
119-
120-
var asyncParams [][]byte
121-
if asyncData != nil {
122-
asyncParams, _ = parseArgumentsFunc(string(asyncData))
123-
// string start with a @ so first parsed argument will be empty always
124-
asyncParams = asyncParams[1:]
125-
} else {
126-
return callData, nil
127-
}
128-
129-
return appendAsyncParamsToCallData(
130-
asyncParams,
131-
callData,
132-
true,
133-
parseArgumentsFunc)
134-
}
135-
136-
func appendAsyncParamsToCallData(
137-
asyncParams [][]byte,
138-
data []byte,
139-
hasFunction bool,
140-
parseArgumentsFunc func(data string) ([][]byte, error)) ([]byte, error) {
141-
142-
if data == nil {
143-
return nil, nil
144-
}
145-
146-
args, err := parseArgumentsFunc(string(data))
147-
if err != nil {
148-
return nil, err
149-
}
150-
151-
var functionName string
152-
if hasFunction {
153-
functionName = string(args[0])
154-
}
155-
156-
// check if there is only one argument and that is 0
157-
if len(args) != 0 {
158-
args = args[1:]
159-
}
160-
161-
callData := txDataBuilder.NewBuilder()
162-
163-
if functionName != "" {
164-
callData.Func(functionName)
165-
}
166-
167-
if len(args) != 0 {
168-
for _, arg := range args {
169-
callData.Bytes(arg)
170-
}
171-
} else {
172-
if !hasFunction {
173-
callData.Bytes([]byte{})
174-
}
175-
}
176-
177-
for _, asyncParam := range asyncParams {
178-
callData.Bytes(asyncParam)
179-
}
180-
181-
return callData.ToBytes(), nil
182-
}
183-
184-
/*
185-
Used by when a callback SCR is created
186-
1) after a failure of an async call
187-
Async data is extracted (by extractAsyncCallParamsFromTxData()) and then
188-
reappended to the new SCR's callback data (by reapendAsyncParamsToTxData())
189-
2) from the last transfer (see useLastTransferAsAsyncCallBackWhenNeeded())
190-
*/
191-
func CreateCallbackAsyncParams(hasher crypto.Hasher, asyncParams *vmcommon.AsyncArguments) [][]byte {
192-
if asyncParams == nil {
193-
return nil
194-
}
195-
newAsyncParams := make([][]byte, 4)
196-
newAsyncParams[0] = GenerateNewCallID(hasher, asyncParams.CallID, []byte{0})
197-
newAsyncParams[1] = asyncParams.CallID
198-
newAsyncParams[2] = asyncParams.CallerCallID
199-
newAsyncParams[3] = []byte{0}
200-
return newAsyncParams
201-
}
202-
20379
// GenerateNewCallID will generate a new call ID as byte slice
20480
func GenerateNewCallID(hasher crypto.Hasher, parentCallID []byte, suffix []byte) []byte {
20581
newCallID := append(parentCallID, suffix...)

vmhost/hostCore/execution.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,6 @@ func (host *vmHost) handleBuiltinFunctionCall(input *vmcommon.ContractCallInput)
429429
}
430430

431431
err = contexts.AddAsyncArgumentsToOutputTransfers(
432-
host.Output(),
433-
input.RecipientAddr,
434432
input.AsyncArguments,
435433
vm.AsynchronousCall,
436434
builtinOutput)

vmhost/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ type AsyncContext interface {
399399

400400
GetAsyncCallByCallID(callID []byte) AsyncCallLocation
401401
LoadParentContextFromStackOrStorage() (AsyncContext, error)
402-
ExecuteSyncCallbackAndFinishOutput(
402+
ExecuteLocalCallbackAndFinishOutput(
403403
asyncCall *AsyncCall,
404404
vmOutput *vmcommon.VMOutput,
405405
destinationCallInput *vmcommon.ContractCallInput,

0 commit comments

Comments
 (0)