Skip to content

Commit e80d556

Browse files
authored
Merge pull request #921 from multiversx/merge-vm-andromeda
Merge vm andromeda
2 parents 5afcffc + 7d894cc commit e80d556

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

mock/contracts/transferAndExecuteSC.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package contracts
22

33
import (
4+
"encoding/hex"
45
"fmt"
56
"math/big"
67

@@ -64,6 +65,27 @@ func TransferEGLDToParent(instanceMock *mock.InstanceMock, config interface{}) {
6465
})
6566
}
6667

68+
func TransferAndExecuteWithBuiltIn(instanceMock *mock.InstanceMock, config interface{}) {
69+
instanceMock.AddMockMethod("transferAndExecuteWithBuiltIn", func() *mock.InstanceMock {
70+
testConfig := config.(*test.TestConfig)
71+
host := instanceMock.Host
72+
instance := mock.GetMockInstance(host)
73+
74+
_ = host.Metering().UseGasBounded(testConfig.GasUsedByChild)
75+
76+
transferString := "ESDTTransfer@" + hex.EncodeToString([]byte("XYY-ABCDEF")) + "@" + hex.EncodeToString(big.NewInt(100000).Bytes())
77+
vmhooks.TransferValueExecuteWithTypedArgs(host,
78+
test.UserAddress,
79+
big.NewInt(0),
80+
1,
81+
[]byte(transferString),
82+
[][]byte{},
83+
)
84+
85+
return instance
86+
})
87+
}
88+
6789
// GetChildAddressForTransfer -
6890
func GetChildAddressForTransfer(transfer int) []byte {
6991
return testcommon.MakeTestSCAddress(fmt.Sprintf("childSC-%d", transfer))

vmhost/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ const (
1111

1212
// UseGasBoundedShouldFailExecutionFlag defines the flag that activates failing of execution if gas bounded check fails
1313
UseGasBoundedShouldFailExecutionFlag core.EnableEpochFlag = "UseGasBoundedShouldFailExecutionFlag"
14+
15+
// CheckBuiltInCallOnTransferValueAndFailExecutionFlag defines the flag that activates failing of execution if gas bounded check fails
16+
CheckBuiltInCallOnTransferValueAndFailExecutionFlag core.EnableEpochFlag = "CheckBuiltInCallOnTransferValueAndFailExecutionFlag"
1417
)

vmhost/hostCore/host.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var allFlags = []core.EnableEpochFlag{
4242
vmhost.CryptoOpcodesV2Flag,
4343
vmhost.MultiESDTNFTTransferAndExecuteByUserFlag,
4444
vmhost.UseGasBoundedShouldFailExecutionFlag,
45+
vmhost.CheckBuiltInCallOnTransferValueAndFailExecutionFlag,
4546
}
4647

4748
// vmHost implements HostContext interface.

vmhost/hosttest/execution_gas_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,36 @@ func TestGasUsed_SingleContract_TransferFromChild(t *testing.T) {
174174
assert.Nil(t, err)
175175
}
176176

177+
func TestGasUsed_SingleContract_TransferFromChildWithBuiltIn(t *testing.T) {
178+
testConfig := makeTestConfig()
179+
180+
_, err := test.BuildMockInstanceCallTest(t).
181+
WithContracts(
182+
test.CreateMockContract(test.ParentAddress).
183+
WithBalance(testConfig.ParentBalance).
184+
WithCodeMetadata([]byte{0, vmcommon.MetadataPayable}).
185+
WithConfig(testConfig).
186+
WithMethods(contracts.ExecOnDestCtxSingleCallParentMock),
187+
test.CreateMockContract(test.ChildAddress).
188+
WithBalance(testConfig.ChildBalance).
189+
WithConfig(testConfig).
190+
WithMethods(contracts.TransferAndExecuteWithBuiltIn)).
191+
WithInput(test.CreateTestContractCallInputBuilder().
192+
WithRecipientAddr(test.ParentAddress).
193+
WithGasProvided(testConfig.GasProvided).
194+
WithFunction("execOnDestCtxSingleCall").
195+
WithArguments(test.ChildAddress, []byte("transferAndExecuteWithBuiltIn")).
196+
Build()).
197+
WithSetup(func(host vmhost.VMHost, world *worldmock.MockWorld) {
198+
createMockBuiltinFunctions(t, host, world)
199+
setZeroCodeCosts(host)
200+
}).
201+
AndAssertResults(func(world *worldmock.MockWorld, verify *test.VMOutputVerifier) {
202+
verify.ReturnCode(vmcommon.ExecutionFailed)
203+
})
204+
assert.Nil(t, err)
205+
}
206+
177207
func TestGasUsed_ExecuteOnDestChain(t *testing.T) {
178208
alphaAddress := test.MakeTestSCAddress("alpha")
179209
betaAddress := test.MakeTestSCAddress("beta")

vmhost/vmhooks/baseOps.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,18 @@ func TransferValueExecuteWithTypedArgs(
841841
}
842842
}
843843

844+
data := ""
845+
if contractCallInput != nil {
846+
data = makeCrossShardCallFromInput(contractCallInput.Function, contractCallInput.Arguments)
847+
}
848+
849+
lastRound := host.Blockchain().LastRound()
850+
if host.IsBuiltinFunctionCall([]byte(data)) &&
851+
lastRound >= uint64(host.EnableEpochsHandler().GetActivationEpoch(vmhost.CheckBuiltInCallOnTransferValueAndFailExecutionFlag)) {
852+
WithFaultAndHost(host, vmhost.ErrTransferValueOnESDTCall, runtime.BaseOpsErrorShouldFailExecution())
853+
return 1
854+
}
855+
844856
if host.AreInSameShard(sender, dest) && contractCallInput != nil && host.Blockchain().IsSmartContract(dest) {
845857
logEEI.Trace("eGLD pre-transfer execution begin")
846858
vmOutput, err := executeOnDestContextFromAPI(host, contractCallInput)
@@ -854,11 +866,6 @@ func TransferValueExecuteWithTypedArgs(
854866
return 0
855867
}
856868

857-
data := ""
858-
if contractCallInput != nil {
859-
data = makeCrossShardCallFromInput(contractCallInput.Function, contractCallInput.Arguments)
860-
}
861-
862869
err = metering.UseGasBounded(uint64(gasLimit))
863870
if WithFaultAndHost(host, err, runtime.UseGasBoundedShouldFailExecution()) {
864871
return 1

0 commit comments

Comments
 (0)