Skip to content

Commit

Permalink
Merge pull request #780 from multiversx/merge_master_into_feat_refact…
Browse files Browse the repository at this point in the history
…or_eeh_2023.09.18

Merge master into feat refactor eeh 2023.09.18
  • Loading branch information
sstanculeanu authored Sep 18, 2023
2 parents 529cc54 + afab2d8 commit 207dbac
Show file tree
Hide file tree
Showing 102 changed files with 1,515 additions and 556 deletions.
1 change: 1 addition & 0 deletions executor/vmHooks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions executor/wrapper/wrapperVMHooks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ require (
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1
github.com/gogo/protobuf v1.3.2
github.com/mitchellh/mapstructure v1.5.0
github.com/multiversx/mx-chain-core-go v1.2.15-0.20230828081708-1d1190bec635
github.com/multiversx/mx-chain-core-go v1.2.17-0.20230918144211-8728c8a6e2d5
github.com/multiversx/mx-chain-crypto-go v1.2.8
github.com/multiversx/mx-chain-logger-go v1.0.13
github.com/multiversx/mx-chain-scenario-go v1.2.1
github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230901130135-4d211b1a41d2
github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230918144414-73020ec780b7
github.com/multiversx/mx-components-big-int v1.0.0
github.com/pelletier/go-toml v1.9.3
github.com/stretchr/testify v1.8.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiversx/mx-chain-core-go v1.2.15-0.20230828081708-1d1190bec635 h1:BtuOBkYy3heZYLaqRys94rPwEiQq/HKHYRL8RXAq46A=
github.com/multiversx/mx-chain-core-go v1.2.15-0.20230828081708-1d1190bec635/go.mod h1:BILOGHUOIG5dNNX8cgkzCNfDaVtoYrJRYcPnpxRMH84=
github.com/multiversx/mx-chain-core-go v1.2.17-0.20230918144211-8728c8a6e2d5 h1:j20zxnqhffqW35Cek5yvRYYANE+ws4XMB2G4t4o4aOs=
github.com/multiversx/mx-chain-core-go v1.2.17-0.20230918144211-8728c8a6e2d5/go.mod h1:BILOGHUOIG5dNNX8cgkzCNfDaVtoYrJRYcPnpxRMH84=
github.com/multiversx/mx-chain-crypto-go v1.2.8 h1:wOgVlUaO5X4L8iEbFjcQcL8SZvv6WZ7LqH73BiRPhxU=
github.com/multiversx/mx-chain-crypto-go v1.2.8/go.mod h1:fkaWKp1rbQN9wPKya5jeoRyC+c/SyN/NfggreyeBw+8=
github.com/multiversx/mx-chain-logger-go v1.0.13 h1:eru/TETo0MkO4ZTnXsQDKf4PBRpAXmqjT02klNT/JnY=
github.com/multiversx/mx-chain-logger-go v1.0.13/go.mod h1:MZJhTAtZTJxT+yK2EHc4ZW3YOHUc1UdjCD0iahRNBZk=
github.com/multiversx/mx-chain-scenario-go v1.2.1 h1:9eC6VcOEAKRRKZ7EbSWPLzCdNIMWwuNBtAZlgR4cSMA=
github.com/multiversx/mx-chain-scenario-go v1.2.1/go.mod h1:EuZY7DpNFHVNSxJR8dKE1z2I8gBYfEFFPSwNUOXptqE=
github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230901130135-4d211b1a41d2 h1:5ZyyNUvyV4QLIjz/tHgqSDC99FmF3H2q2p3NajOTkVc=
github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230901130135-4d211b1a41d2/go.mod h1:uDaefuPXa9p0sb+wrUk16J0z+U324Hh894Y5oVXeVdI=
github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230918144414-73020ec780b7 h1:MBABPvX640D6NkmuIaEhM8v/WLdSDRRIfEjilQX2NO0=
github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230918144414-73020ec780b7/go.mod h1:VtmaxMU7FtRdTLLcLd0D01vcTm0drVYvaJ0U88ikr38=
github.com/multiversx/mx-components-big-int v1.0.0 h1:Wkr8lSzK2nDqixOrrBa47VNuqdhV1m/aJhaP1EMaiS8=
github.com/multiversx/mx-components-big-int v1.0.0/go.mod h1:maIEMgHlNE2u78JaDD0oLzri+ShgU4okHfzP3LWGdQM=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down
2 changes: 1 addition & 1 deletion integrationTests/json/scenariosAdderLog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ GetPointsUsed: 135352
GetPointsUsed: 135352
Reset: true
SetPointsUsed: 0
SetGasLimit: 18446744073708343115
SetGasLimit: 9223372036853567307
SetBreakpointValue: 0
HasFunction(getSum): true
CallFunction(getSum):
Expand Down
3 changes: 3 additions & 0 deletions integrationTests/json/scenariosContracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package vmjsonintegrationtest

import (
"testing"

worldhook "github.com/multiversx/mx-chain-vm-go/mock/world"
)

func TestRustAdder(t *testing.T) {
Expand Down Expand Up @@ -69,6 +71,7 @@ func TestDnsContract(t *testing.T) {

ScenariosTest(t).
Folder("dns").
WithEnableEpochsHandler(worldhook.EnableEpochsHandlerStubNoFlags()).
Run().
CheckNoError()
}
Expand Down
3 changes: 3 additions & 0 deletions integrationTests/json/scenariosExecutorLogs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vmjsonintegrationtest
import (
"testing"

worldhook "github.com/multiversx/mx-chain-vm-go/mock/world"
"github.com/multiversx/mx-chain-vm-go/testcommon/testexecutor"
"github.com/multiversx/mx-chain-vm-go/wasmer"
"github.com/multiversx/mx-chain-vm-go/wasmer2"
Expand Down Expand Up @@ -223,6 +224,7 @@ func TestDnsContractLog(t *testing.T) {

expected := ScenariosTest(t).
Folder("dns").
WithEnableEpochsHandler(worldhook.EnableEpochsHandlerStubNoFlags()).
WithExecutorFactory(wasmer.ExecutorFactory()).
WithExecutorLogs().
Run().
Expand All @@ -231,6 +233,7 @@ func TestDnsContractLog(t *testing.T) {

ScenariosTest(t).
Folder("dns").
WithEnableEpochsHandler(worldhook.EnableEpochsHandlerStubNoFlags()).
WithExecutorFactory(wasmer2.ExecutorFactory()).
WithExecutorLogs().
Run().
Expand Down
36 changes: 23 additions & 13 deletions integrationTests/json/scenariosTestCommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/multiversx/mx-chain-core-go/core/check"
logger "github.com/multiversx/mx-chain-logger-go"
mc "github.com/multiversx/mx-chain-scenario-go/controller"
vmi "github.com/multiversx/mx-chain-vm-common-go"
"github.com/multiversx/mx-chain-vm-go/executor"
executorwrapper "github.com/multiversx/mx-chain-vm-go/executor/wrapper"
worldhook "github.com/multiversx/mx-chain-vm-go/mock/world"
am "github.com/multiversx/mx-chain-vm-go/scenarioexec"
"github.com/multiversx/mx-chain-vm-go/testcommon/testexecutor"
"github.com/stretchr/testify/require"
Expand All @@ -33,23 +35,25 @@ func getTestRoot() string {

// ScenariosTestBuilder defines the Scenarios builder component
type ScenariosTestBuilder struct {
t *testing.T
folder string
singleFile string
exclusions []string
executorLogger executorwrapper.ExecutorLogger
executorFactory executor.ExecutorAbstractFactory
currentError error
t *testing.T
folder string
singleFile string
exclusions []string
executorLogger executorwrapper.ExecutorLogger
executorFactory executor.ExecutorAbstractFactory
enableEpochsHandler vmi.EnableEpochsHandler
currentError error
}

// ScenariosTest will create a new ScenariosTestBuilder instance
func ScenariosTest(t *testing.T) *ScenariosTestBuilder {
return &ScenariosTestBuilder{
t: t,
folder: "",
singleFile: "",
executorLogger: nil,
executorFactory: nil,
t: t,
folder: "",
singleFile: "",
executorLogger: nil,
executorFactory: nil,
enableEpochsHandler: worldhook.EnableEpochsHandlerStubAllFlags(),
}
}

Expand Down Expand Up @@ -89,6 +93,12 @@ func (mtb *ScenariosTestBuilder) WithExecutorFactory(executorFactory executor.Ex
return mtb
}

// WithEnableEpochsHandler overrides the epoch flags
func (mtb *ScenariosTestBuilder) WithEnableEpochsHandler(enableEpochsHandler vmi.EnableEpochsHandler) *ScenariosTestBuilder {
mtb.enableEpochsHandler = enableEpochsHandler
return mtb
}

// Run will start the testing process
func (mtb *ScenariosTestBuilder) Run() *ScenariosTestBuilder {
executor, err := am.NewVMTestExecutor()
Expand All @@ -98,7 +108,7 @@ func (mtb *ScenariosTestBuilder) Run() *ScenariosTestBuilder {
if check.IfNil(mtb.executorFactory) {
mtb.executorFactory = testexecutor.NewDefaultTestExecutorFactory(mtb.t)
}

executor.World.EnableEpochsHandler = mtb.enableEpochsHandler
executor.OverrideVMExecutor = mtb.executorFactory
if mtb.executorLogger != nil {
executor.OverrideVMExecutor = executorwrapper.NewWrappedExecutorFactory(
Expand Down
1 change: 1 addition & 0 deletions mock/context/executorMockFunc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion mock/context/outputContextMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func (o *OutputContextMock) DeleteFirstReturnData() {
// WriteLog mocked method
func (o *OutputContextMock) WriteLog(_ []byte, _ [][]byte, _ [][]byte) {}

// WriteLogWithIdentifier mocked method
func (o *OutputContextMock) WriteLogWithIdentifier(_ []byte, _ [][]byte, _ [][]byte, _ []byte) {
}

// TransferValueOnly mocked method
func (o *OutputContextMock) TransferValueOnly(_ []byte, _ []byte, _ *big.Int, _ bool) error {
return o.TransferResult
Expand All @@ -177,7 +181,7 @@ func (o *OutputContextMock) Transfer(_ []byte, _ []byte, _ uint64, _ uint64, _ *
}

// TransferESDT mocked method
func (o *OutputContextMock) TransferESDT(transfersArgs *vmhost.ESDTTransfersArgs, _ *vmcommon.ContractCallInput) (uint64, error) {
func (o *OutputContextMock) TransferESDT(_ *vmhost.ESDTTransfersArgs, _ *vmcommon.ContractCallInput) (uint64, error) {
return 0, nil
}

Expand Down
8 changes: 8 additions & 0 deletions mock/context/outputContextStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type OutputContextStub struct {
GetOutputAccountCalled func(address []byte) (*vmcommon.OutputAccount, bool)
DeleteOutputAccountCalled func(address []byte)
WriteLogCalled func(address []byte, topics [][]byte, data [][]byte)
WriteLogWithIdentifierCalled func(address []byte, topics [][]byte, data [][]byte, identifier []byte)
TransferCalled func(destination []byte, sender []byte, gasLimit uint64, gasLocked uint64, value *big.Int, asyncData []byte, input []byte) error
TransferESDTCalled func(transfersArgs *vmhost.ESDTTransfersArgs, input *vmcommon.ContractCallInput) (uint64, error)
GetRefundCalled func() uint64
Expand Down Expand Up @@ -144,6 +145,13 @@ func (o *OutputContextStub) WriteLog(address []byte, topics [][]byte, data [][]b
}
}

// WriteLogWithIdentifier mocked method
func (o *OutputContextStub) WriteLogWithIdentifier(address []byte, topics [][]byte, data [][]byte, identifier []byte) {
if o.WriteLogWithIdentifierCalled != nil {
o.WriteLogWithIdentifierCalled(address, topics, data, identifier)
}
}

// TransferValueOnly mocked method
func (o *OutputContextStub) TransferValueOnly(destination []byte, sender []byte, value *big.Int, checkPayable bool) error {
if o.TransferValueOnlyCalled != nil {
Expand Down
134 changes: 134 additions & 0 deletions mock/contracts/backTransferSC.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package contracts

import (
"bytes"
"fmt"
"math/big"

"github.com/multiversx/mx-chain-core-go/data/vm"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/multiversx/mx-chain-vm-common-go/txDataBuilder"
mock "github.com/multiversx/mx-chain-vm-go/mock/context"
test "github.com/multiversx/mx-chain-vm-go/testcommon"
"github.com/multiversx/mx-chain-vm-go/vmhost"
"github.com/multiversx/mx-chain-vm-go/vmhost/vmhooks"
)

// BackTransfer_ParentCallsChild is an exposed mock contract method
func BackTransfer_ParentCallsChild(instanceMock *mock.InstanceMock, config interface{}) {
instanceMock.AddMockMethod("callChild", func() *mock.InstanceMock {
host := instanceMock.Host
instance := mock.GetMockInstance(host)

storedResult := []byte("ok")

testConfig := config.(*test.TestConfig)
input := test.DefaultTestContractCallInput()
input.GasProvided = testConfig.GasProvidedToChild
input.CallerAddr = testConfig.ParentAddress
input.RecipientAddr = testConfig.ChildAddress
input.Function = "childFunction"
returnValue := ExecuteOnDestContextInMockContracts(host, input)
if returnValue != 0 {
host.Runtime().FailExecution(fmt.Errorf("return value %d", returnValue))
}
managedTypes := host.ManagedTypes()

arguments := host.Runtime().Arguments()
if len(arguments) > 0 {
checkBackTransfers := arguments[0]
if checkBackTransfers[0] == 1 {
esdtTransfers, egld := managedTypes.GetBackTransfers()
if len(esdtTransfers) != 1 {
host.Runtime().FailExecution(fmt.Errorf("found esdt transfers %d", len(esdtTransfers)))
storedResult = []byte("err")
}
if !bytes.Equal(test.ESDTTestTokenName, esdtTransfers[0].ESDTTokenName) {
host.Runtime().FailExecution(fmt.Errorf("invalid token name %s", string(esdtTransfers[0].ESDTTokenName)))
storedResult = []byte("err")
}
if big.NewInt(0).SetUint64(testConfig.ESDTTokensToTransfer).Cmp(esdtTransfers[0].ESDTValue) != 0 {
host.Runtime().FailExecution(fmt.Errorf("invalid token value %d", esdtTransfers[0].ESDTValue.Uint64()))
storedResult = []byte("err")
}
if egld.Cmp(big.NewInt(testConfig.TransferFromChildToParent)) != 0 {
host.Runtime().FailExecution(fmt.Errorf("invalid egld value %d", egld))
storedResult = []byte("err")
}
}
}

_, err := host.Storage().SetStorage(test.ParentKeyA, storedResult)
if err != nil {
host.Runtime().FailExecution(err)
}

return instance
})
}

// BackTransfer_ChildMakesAsync is an exposed mock contract method
func BackTransfer_ChildMakesAsync(instanceMock *mock.InstanceMock, config interface{}) {
instanceMock.AddMockMethod("childFunction", func() *mock.InstanceMock {
host := instanceMock.Host
instance := mock.GetMockInstance(host)
testConfig := config.(*test.TestConfig)

callData := txDataBuilder.NewBuilder()
callData.Func("wasteGas")
callData.Int64(0)

err := host.Async().RegisterAsyncCall("testGroup", &vmhost.AsyncCall{
Status: vmhost.AsyncCallPending,
Destination: testConfig.NephewAddress,
Data: callData.ToBytes(),
ValueBytes: big.NewInt(0).Bytes(),
SuccessCallback: testConfig.SuccessCallback,
ErrorCallback: testConfig.ErrorCallback,
GasLimit: uint64(300),
GasLocked: testConfig.GasToLock,
CallbackClosure: nil,
})
if err != nil {
host.Runtime().FailExecution(err)
}
return instance
})
}

// BackTransfer_ChildCallback is an exposed mock contract method
func BackTransfer_ChildCallback(instanceMock *mock.InstanceMock, config interface{}) {
instanceMock.AddMockMethod("myCallback", func() *mock.InstanceMock {
host := instanceMock.Host
instance := mock.GetMockInstance(host)
testConfig := config.(*test.TestConfig)

valueBytes := big.NewInt(testConfig.TransferFromChildToParent).Bytes()
err := host.Output().Transfer(
testConfig.ParentAddress,
testConfig.ChildAddress, 0, 0, big.NewInt(0).SetBytes(valueBytes), nil, []byte{}, vm.DirectCall)
if err != nil {
host.Runtime().FailExecution(err)
}

transfer := &vmcommon.ESDTTransfer{
ESDTValue: big.NewInt(int64(testConfig.ESDTTokensToTransfer)),
ESDTTokenName: test.ESDTTestTokenName,
ESDTTokenType: 0,
ESDTTokenNonce: 0,
}

ret := vmhooks.TransferESDTNFTExecuteWithTypedArgs(
host,
testConfig.ParentAddress,
[]*vmcommon.ESDTTransfer{transfer},
int64(testConfig.GasProvidedToChild),
nil,
nil)
if ret != 0 {
host.Runtime().FailExecution(fmt.Errorf("Transfer ESDT failed"))
}

return instance
})
}
Loading

0 comments on commit 207dbac

Please sign in to comment.