diff --git a/go.mod b/go.mod index 905dbbaa9..97166a2d4 100644 --- a/go.mod +++ b/go.mod @@ -36,6 +36,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.8.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect golang.org/x/sys v0.2.0 // indirect google.golang.org/protobuf v1.28.0 // indirect diff --git a/go.sum b/go.sum index 7825dc23d..afd8927f6 100644 --- a/go.sum +++ b/go.sum @@ -118,6 +118,7 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/mock/mockery/AsnycContext.go b/mock/mockery/AsnycContext.go new file mode 100644 index 000000000..0b9999674 --- /dev/null +++ b/mock/mockery/AsnycContext.go @@ -0,0 +1,692 @@ +package mockery + +import ( + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + mock "github.com/stretchr/testify/mock" + + vmhost "github.com/multiversx/mx-chain-vm-go/vmhost" +) + +// MockAsyncContext is an autogenerated mock type for the AsyncContext type +type MockAsyncContext struct { + mock.Mock +} + +// ClearStateStack provides a mock function with no fields +func (_m *MockAsyncContext) ClearStateStack() { + _m.Called() +} + +// Clone provides a mock function with no fields +func (_m *MockAsyncContext) Clone() vmhost.AsyncContext { + ret := _m.Called() + + if len(ret) == 0 { + return _m + } + + var r0 vmhost.AsyncContext + if rf, ok := ret.Get(0).(func() vmhost.AsyncContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.AsyncContext) + } + } + + return r0 +} + +// CompleteChildConditional provides a mock function with given fields: isChildComplete, callID, gasToAccumulate +func (_m *MockAsyncContext) CompleteChildConditional(isChildComplete bool, callID []byte, gasToAccumulate uint64) error { + ret := _m.Called(isChildComplete, callID, gasToAccumulate) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(bool, []byte, uint64) error); ok { + r0 = rf(isChildComplete, callID, gasToAccumulate) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// DeleteFromCallID provides a mock function with given fields: address +func (_m *MockAsyncContext) DeleteFromCallID(address []byte) error { + ret := _m.Called(address) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func([]byte) error); ok { + r0 = rf(address) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Execute provides a mock function with no fields +func (_m *MockAsyncContext) Execute() error { + ret := _m.Called() + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// ExecuteSyncCallbackAndFinishOutput provides a mock function with given fields: asyncCall, vmOutput, destinationCallInput, gasAccumulated, err +func (_m *MockAsyncContext) ExecuteSyncCallbackAndFinishOutput(asyncCall *vmhost.AsyncCall, vmOutput *vmcommon.VMOutput, destinationCallInput *vmcommon.ContractCallInput, gasAccumulated uint64, err error) (bool, *vmcommon.VMOutput) { + ret := _m.Called(asyncCall, vmOutput, destinationCallInput, gasAccumulated, err) + + if len(ret) == 0 { + return true, &vmcommon.VMOutput{} + } + + var r0 bool + var r1 *vmcommon.VMOutput + if rf, ok := ret.Get(0).(func(*vmhost.AsyncCall, *vmcommon.VMOutput, *vmcommon.ContractCallInput, uint64, error) (bool, *vmcommon.VMOutput)); ok { + return rf(asyncCall, vmOutput, destinationCallInput, gasAccumulated, err) + } + if rf, ok := ret.Get(0).(func(*vmhost.AsyncCall, *vmcommon.VMOutput, *vmcommon.ContractCallInput, uint64, error) bool); ok { + r0 = rf(asyncCall, vmOutput, destinationCallInput, gasAccumulated, err) + } else { + r0 = ret.Get(0).(bool) + } + + if rf, ok := ret.Get(1).(func(*vmhost.AsyncCall, *vmcommon.VMOutput, *vmcommon.ContractCallInput, uint64, error) *vmcommon.VMOutput); ok { + r1 = rf(asyncCall, vmOutput, destinationCallInput, gasAccumulated, err) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*vmcommon.VMOutput) + } + } + + return r0, r1 +} + +// GetAsyncCallByCallID provides a mock function with given fields: callID +func (_m *MockAsyncContext) GetAsyncCallByCallID(callID []byte) vmhost.AsyncCallLocation { + ret := _m.Called(callID) + + if len(ret) == 0 { + return nil + } + + var r0 vmhost.AsyncCallLocation + if rf, ok := ret.Get(0).(func([]byte) vmhost.AsyncCallLocation); ok { + r0 = rf(callID) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.AsyncCallLocation) + } + } + + return r0 +} + +// GetCallGroup provides a mock function with given fields: groupID +func (_m *MockAsyncContext) GetCallGroup(groupID string) (*vmhost.AsyncCallGroup, bool) { + ret := _m.Called(groupID) + + if len(ret) == 0 { + return &vmhost.AsyncCallGroup{}, true + } + + var r0 *vmhost.AsyncCallGroup + var r1 bool + if rf, ok := ret.Get(0).(func(string) (*vmhost.AsyncCallGroup, bool)); ok { + return rf(groupID) + } + if rf, ok := ret.Get(0).(func(string) *vmhost.AsyncCallGroup); ok { + r0 = rf(groupID) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmhost.AsyncCallGroup) + } + } + + if rf, ok := ret.Get(1).(func(string) bool); ok { + r1 = rf(groupID) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// GetCallID provides a mock function with no fields +func (_m *MockAsyncContext) GetCallID() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("callID") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetCallbackAsyncInitiatorCallID provides a mock function with no fields +func (_m *MockAsyncContext) GetCallbackAsyncInitiatorCallID() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("callID") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetCallbackClosure provides a mock function with no fields +func (_m *MockAsyncContext) GetCallbackClosure() ([]byte, error) { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("callID"), nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func() ([]byte, error)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetCallerAddress provides a mock function with no fields +func (_m *MockAsyncContext) GetCallerAddress() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("caller") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetCallerCallID provides a mock function with no fields +func (_m *MockAsyncContext) GetCallerCallID() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("callID") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetGasAccumulated provides a mock function with no fields +func (_m *MockAsyncContext) GetGasAccumulated() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetParentAddress provides a mock function with no fields +func (_m *MockAsyncContext) GetParentAddress() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("parent") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetReturnData provides a mock function with no fields +func (_m *MockAsyncContext) GetReturnData() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{} + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// HasCallback provides a mock function with no fields +func (_m *MockAsyncContext) HasCallback() bool { + ret := _m.Called() + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// HasLegacyGroup provides a mock function with no fields +func (_m *MockAsyncContext) HasLegacyGroup() bool { + ret := _m.Called() + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// HasPendingCallGroups provides a mock function with no fields +func (_m *MockAsyncContext) HasPendingCallGroups() bool { + ret := _m.Called() + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// InitState provides a mock function with no fields +func (_m *MockAsyncContext) InitState() { + _m.Called() +} + +// InitStateFromInput provides a mock function with given fields: input +func (_m *MockAsyncContext) InitStateFromInput(input *vmcommon.ContractCallInput) error { + ret := _m.Called(input) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) error); ok { + r0 = rf(input) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// IsComplete provides a mock function with no fields +func (_m *MockAsyncContext) IsComplete() bool { + ret := _m.Called() + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsCrossShard provides a mock function with no fields +func (_m *MockAsyncContext) IsCrossShard() bool { + ret := _m.Called() + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// LoadParentContext provides a mock function with no fields +func (_m *MockAsyncContext) LoadParentContext() error { + ret := _m.Called() + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// LoadParentContextFromStackOrStorage provides a mock function with no fields +func (_m *MockAsyncContext) LoadParentContextFromStackOrStorage() (vmhost.AsyncContext, error) { + ret := _m.Called() + + if len(ret) == 0 { + return _m, nil + } + + var r0 vmhost.AsyncContext + var r1 error + if rf, ok := ret.Get(0).(func() (vmhost.AsyncContext, error)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() vmhost.AsyncContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.AsyncContext) + } + } + + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// NotifyChildIsComplete provides a mock function with given fields: callID, gasToAccumulate +func (_m *MockAsyncContext) NotifyChildIsComplete(callID []byte, gasToAccumulate uint64) error { + ret := _m.Called(callID, gasToAccumulate) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func([]byte, uint64) error); ok { + r0 = rf(callID, gasToAccumulate) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// PopDiscard provides a mock function with no fields +func (_m *MockAsyncContext) PopDiscard() { + _m.Called() +} + +// PopSetActiveState provides a mock function with no fields +func (_m *MockAsyncContext) PopSetActiveState() { + _m.Called() +} + +// PushState provides a mock function with no fields +func (_m *MockAsyncContext) PushState() { + _m.Called() +} + +// RegisterAsyncCall provides a mock function with given fields: groupID, call +func (_m *MockAsyncContext) RegisterAsyncCall(groupID string, call *vmhost.AsyncCall) error { + ret := _m.Called(groupID, call) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(string, *vmhost.AsyncCall) error); ok { + r0 = rf(groupID, call) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// RegisterLegacyAsyncCall provides a mock function with given fields: address, data, value +func (_m *MockAsyncContext) RegisterLegacyAsyncCall(address []byte, data []byte, value []byte) error { + ret := _m.Called(address, data, value) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func([]byte, []byte, []byte) error); ok { + r0 = rf(address, data, value) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Save provides a mock function with no fields +func (_m *MockAsyncContext) Save() error { + ret := _m.Called() + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// SetAsyncArgumentsForCall provides a mock function with given fields: input +func (_m *MockAsyncContext) SetAsyncArgumentsForCall(input *vmcommon.ContractCallInput) { + _m.Called(input) +} + +// SetAsyncArgumentsForCallback provides a mock function with given fields: input, asyncCall, gasAccumulated +func (_m *MockAsyncContext) SetAsyncArgumentsForCallback(input *vmcommon.ContractCallInput, asyncCall *vmhost.AsyncCall, gasAccumulated uint64) { + _m.Called(input, asyncCall, gasAccumulated) +} + +// SetCallID provides a mock function with given fields: callID +func (_m *MockAsyncContext) SetCallID(callID []byte) { + _m.Called(callID) +} + +// SetCallIDForCallInGroup provides a mock function with given fields: groupIndex, callIndex, callID +func (_m *MockAsyncContext) SetCallIDForCallInGroup(groupIndex int, callIndex int, callID []byte) { + _m.Called(groupIndex, callIndex, callID) +} + +// SetCallbackParentCall provides a mock function with given fields: asyncCall +func (_m *MockAsyncContext) SetCallbackParentCall(asyncCall *vmhost.AsyncCall) { + _m.Called(asyncCall) +} + +// SetContextCallback provides a mock function with given fields: callbackName, data, gas +func (_m *MockAsyncContext) SetContextCallback(callbackName string, data []byte, gas uint64) error { + ret := _m.Called(callbackName, data, gas) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(string, []byte, uint64) error); ok { + r0 = rf(callbackName, data, gas) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// SetResults provides a mock function with given fields: vmOutput +func (_m *MockAsyncContext) SetResults(vmOutput *vmcommon.VMOutput) { + _m.Called(vmOutput) +} + +// SetReturnData provides a mock function with given fields: data +func (_m *MockAsyncContext) SetReturnData(data []byte) { + _m.Called(data) +} + +// UpdateCurrentAsyncCallStatus provides a mock function with given fields: address, callID, vmInput +func (_m *MockAsyncContext) UpdateCurrentAsyncCallStatus(address []byte, callID []byte, vmInput *vmcommon.VMInput) (*vmhost.AsyncCall, bool, error) { + ret := _m.Called(address, callID, vmInput) + + if len(ret) == 0 { + return &vmhost.AsyncCall{}, false, nil + } + + var r0 *vmhost.AsyncCall + var r1 bool + var r2 error + if rf, ok := ret.Get(0).(func([]byte, []byte, *vmcommon.VMInput) (*vmhost.AsyncCall, bool, error)); ok { + return rf(address, callID, vmInput) + } + if rf, ok := ret.Get(0).(func([]byte, []byte, *vmcommon.VMInput) *vmhost.AsyncCall); ok { + r0 = rf(address, callID, vmInput) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmhost.AsyncCall) + } + } + + if rf, ok := ret.Get(1).(func([]byte, []byte, *vmcommon.VMInput) bool); ok { + r1 = rf(address, callID, vmInput) + } else { + r1 = ret.Get(1).(bool) + } + + if rf, ok := ret.Get(2).(func([]byte, []byte, *vmcommon.VMInput) error); ok { + r2 = rf(address, callID, vmInput) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// NewMockAsyncContext creates a new instance of MockAsyncContext. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockAsyncContext(t interface { + mock.TestingT + Cleanup(func()) +}) *MockAsyncContext { + mock := &MockAsyncContext{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mock/mockery/BlockchainContext.go b/mock/mockery/BlockchainContext.go new file mode 100644 index 000000000..f8af64b7b --- /dev/null +++ b/mock/mockery/BlockchainContext.go @@ -0,0 +1,898 @@ +package mockery + +import ( + big "math/big" + + esdt "github.com/multiversx/mx-chain-core-go/data/esdt" + mock "github.com/stretchr/testify/mock" + + vmcommon "github.com/multiversx/mx-chain-vm-common-go" +) + +// MockBlockchainContext is an autogenerated mock type for the BlockchainContext type +type MockBlockchainContext struct { + mock.Mock +} + +// AccountExists provides a mock function with given fields: addr +func (_m *MockBlockchainContext) AccountExists(addr []byte) bool { + ret := _m.Called(addr) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func([]byte) bool); ok { + r0 = rf(addr) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// BlockHash provides a mock function with given fields: number +func (_m *MockBlockchainContext) BlockHash(number uint64) []byte { + ret := _m.Called(number) + + if len(ret) == 0 { + return []byte("hash") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func(uint64) []byte); ok { + r0 = rf(number) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// ClearCompiledCodes provides a mock function with no fields +func (_m *MockBlockchainContext) ClearCompiledCodes() { + _m.Called() +} + +// ClearStateStack provides a mock function with no fields +func (_m *MockBlockchainContext) ClearStateStack() { + _m.Called() +} + +// CurrentEpoch provides a mock function with no fields +func (_m *MockBlockchainContext) CurrentEpoch() uint32 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint32 + if rf, ok := ret.Get(0).(func() uint32); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint32) + } + + return r0 +} + +// CurrentNonce provides a mock function with no fields +func (_m *MockBlockchainContext) CurrentNonce() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// CurrentRandomSeed provides a mock function with no fields +func (_m *MockBlockchainContext) CurrentRandomSeed() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("hash") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// CurrentRound provides a mock function with no fields +func (_m *MockBlockchainContext) CurrentRound() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// CurrentTimeStamp provides a mock function with no fields +func (_m *MockBlockchainContext) CurrentTimeStamp() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// CurrentTimeStampMs provides a mock function with no fields +func (_m *MockBlockchainContext) CurrentTimeStampMs() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1000 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// EpochStartBlockNonce provides a mock function with no fields +func (_m *MockBlockchainContext) EpochStartBlockNonce() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// EpochStartBlockRound provides a mock function with no fields +func (_m *MockBlockchainContext) EpochStartBlockRound() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// EpochStartBlockTimeStampMs provides a mock function with no fields +func (_m *MockBlockchainContext) EpochStartBlockTimeStampMs() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// ExecuteSmartContractCallOnOtherVM provides a mock function with given fields: input +func (_m *MockBlockchainContext) ExecuteSmartContractCallOnOtherVM(input *vmcommon.ContractCallInput) (*vmcommon.VMOutput, error) { + ret := _m.Called(input) + + if len(ret) == 0 { + return &vmcommon.VMOutput{}, nil + } + + var r0 *vmcommon.VMOutput + var r1 error + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) (*vmcommon.VMOutput, error)); ok { + return rf(input) + } + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) *vmcommon.VMOutput); ok { + r0 = rf(input) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.VMOutput) + } + } + + if rf, ok := ret.Get(1).(func(*vmcommon.ContractCallInput) error); ok { + r1 = rf(input) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetBalance provides a mock function with given fields: addr +func (_m *MockBlockchainContext) GetBalance(addr []byte) []byte { + ret := _m.Called(addr) + + if len(ret) == 0 { + return big.NewInt(1).Bytes() + } + + var r0 []byte + if rf, ok := ret.Get(0).(func([]byte) []byte); ok { + r0 = rf(addr) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetBalanceBigInt provides a mock function with given fields: addr +func (_m *MockBlockchainContext) GetBalanceBigInt(addr []byte) *big.Int { + ret := _m.Called(addr) + + if len(ret) == 0 { + return big.NewInt(1) + } + + var r0 *big.Int + if rf, ok := ret.Get(0).(func([]byte) *big.Int); ok { + r0 = rf(addr) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Int) + } + } + + return r0 +} + +// GetCode provides a mock function with given fields: addr +func (_m *MockBlockchainContext) GetCode(addr []byte) ([]byte, error) { + ret := _m.Called(addr) + + if len(ret) == 0 { + return []byte("code"), nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func([]byte) ([]byte, error)); ok { + return rf(addr) + } + if rf, ok := ret.Get(0).(func([]byte) []byte); ok { + r0 = rf(addr) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func([]byte) error); ok { + r1 = rf(addr) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetCodeHash provides a mock function with given fields: addr +func (_m *MockBlockchainContext) GetCodeHash(addr []byte) []byte { + ret := _m.Called(addr) + + if len(ret) == 0 { + return []byte("hash") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func([]byte) []byte); ok { + r0 = rf(addr) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetCodeSize provides a mock function with given fields: addr +func (_m *MockBlockchainContext) GetCodeSize(addr []byte) (int32, error) { + ret := _m.Called(addr) + + if len(ret) == 0 { + return 1, nil + } + + var r0 int32 + var r1 error + if rf, ok := ret.Get(0).(func([]byte) (int32, error)); ok { + return rf(addr) + } + if rf, ok := ret.Get(0).(func([]byte) int32); ok { + r0 = rf(addr) + } else { + r0 = ret.Get(0).(int32) + } + + if rf, ok := ret.Get(1).(func([]byte) error); ok { + r1 = rf(addr) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetCompiledCode provides a mock function with given fields: codeHash +func (_m *MockBlockchainContext) GetCompiledCode(codeHash []byte) (bool, []byte) { + ret := _m.Called(codeHash) + + if len(ret) == 0 { + return true, []byte("hash") + } + + var r0 bool + var r1 []byte + if rf, ok := ret.Get(0).(func([]byte) (bool, []byte)); ok { + return rf(codeHash) + } + if rf, ok := ret.Get(0).(func([]byte) bool); ok { + r0 = rf(codeHash) + } else { + r0 = ret.Get(0).(bool) + } + + if rf, ok := ret.Get(1).(func([]byte) []byte); ok { + r1 = rf(codeHash) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).([]byte) + } + } + + return r0, r1 +} + +// GetESDTToken provides a mock function with given fields: address, tokenID, nonce +func (_m *MockBlockchainContext) GetESDTToken(address []byte, tokenID []byte, nonce uint64) (*esdt.ESDigitalToken, error) { + ret := _m.Called(address, tokenID, nonce) + + if len(ret) == 0 { + return &esdt.ESDigitalToken{Value: big.NewInt(0)}, nil + } + + var r0 *esdt.ESDigitalToken + var r1 error + if rf, ok := ret.Get(0).(func([]byte, []byte, uint64) (*esdt.ESDigitalToken, error)); ok { + return rf(address, tokenID, nonce) + } + if rf, ok := ret.Get(0).(func([]byte, []byte, uint64) *esdt.ESDigitalToken); ok { + r0 = rf(address, tokenID, nonce) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*esdt.ESDigitalToken) + } + } + + if rf, ok := ret.Get(1).(func([]byte, []byte, uint64) error); ok { + r1 = rf(address, tokenID, nonce) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetNonce provides a mock function with given fields: addr +func (_m *MockBlockchainContext) GetNonce(addr []byte) (uint64, error) { + ret := _m.Called(addr) + + if len(ret) == 0 { + return 1, nil + } + + var r0 uint64 + var r1 error + if rf, ok := ret.Get(0).(func([]byte) (uint64, error)); ok { + return rf(addr) + } + if rf, ok := ret.Get(0).(func([]byte) uint64); ok { + r0 = rf(addr) + } else { + r0 = ret.Get(0).(uint64) + } + + if rf, ok := ret.Get(1).(func([]byte) error); ok { + r1 = rf(addr) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetOwnerAddress provides a mock function with no fields +func (_m *MockBlockchainContext) GetOwnerAddress() ([]byte, error) { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("address"), nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func() ([]byte, error)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetShardOfAddress provides a mock function with given fields: addr +func (_m *MockBlockchainContext) GetShardOfAddress(addr []byte) uint32 { + ret := _m.Called(addr) + + if len(ret) == 0 { + return 0 + } + + var r0 uint32 + if rf, ok := ret.Get(0).(func([]byte) uint32); ok { + r0 = rf(addr) + } else { + r0 = ret.Get(0).(uint32) + } + + return r0 +} + +// GetSnapshot provides a mock function with no fields +func (_m *MockBlockchainContext) GetSnapshot() int { + ret := _m.Called() + + if len(ret) == 0 { + return 0 + } + + var r0 int + if rf, ok := ret.Get(0).(func() int); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(int) + } + + return r0 +} + +// GetStateRootHash provides a mock function with no fields +func (_m *MockBlockchainContext) GetStateRootHash() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("root") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetUserAccount provides a mock function with given fields: address +func (_m *MockBlockchainContext) GetUserAccount(address []byte) (vmcommon.UserAccountHandler, error) { + ret := _m.Called(address) + + if len(ret) == 0 { + panic("no return value specified for GetUserAccount") + } + + var r0 vmcommon.UserAccountHandler + var r1 error + if rf, ok := ret.Get(0).(func([]byte) (vmcommon.UserAccountHandler, error)); ok { + return rf(address) + } + if rf, ok := ret.Get(0).(func([]byte) vmcommon.UserAccountHandler); ok { + r0 = rf(address) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmcommon.UserAccountHandler) + } + } + + if rf, ok := ret.Get(1).(func([]byte) error); ok { + r1 = rf(address) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// IncreaseNonce provides a mock function with given fields: addr +func (_m *MockBlockchainContext) IncreaseNonce(addr []byte) { + _m.Called(addr) +} + +// InitState provides a mock function with no fields +func (_m *MockBlockchainContext) InitState() { + _m.Called() +} + +// IsLimitedTransfer provides a mock function with given fields: tokenID +func (_m *MockBlockchainContext) IsLimitedTransfer(tokenID []byte) bool { + ret := _m.Called(tokenID) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func([]byte) bool); ok { + r0 = rf(tokenID) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsPaused provides a mock function with given fields: tokenID +func (_m *MockBlockchainContext) IsPaused(tokenID []byte) bool { + ret := _m.Called(tokenID) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func([]byte) bool); ok { + r0 = rf(tokenID) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsPayable provides a mock function with given fields: sndAddress, rcvAddress +func (_m *MockBlockchainContext) IsPayable(sndAddress []byte, rcvAddress []byte) (bool, error) { + ret := _m.Called(sndAddress, rcvAddress) + + if len(ret) == 0 { + return true, nil + } + + var r0 bool + var r1 error + if rf, ok := ret.Get(0).(func([]byte, []byte) (bool, error)); ok { + return rf(sndAddress, rcvAddress) + } + if rf, ok := ret.Get(0).(func([]byte, []byte) bool); ok { + r0 = rf(sndAddress, rcvAddress) + } else { + r0 = ret.Get(0).(bool) + } + + if rf, ok := ret.Get(1).(func([]byte, []byte) error); ok { + r1 = rf(sndAddress, rcvAddress) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// IsSmartContract provides a mock function with given fields: addr +func (_m *MockBlockchainContext) IsSmartContract(addr []byte) bool { + ret := _m.Called(addr) + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func([]byte) bool); ok { + r0 = rf(addr) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// LastEpoch provides a mock function with no fields +func (_m *MockBlockchainContext) LastEpoch() uint32 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint32 + if rf, ok := ret.Get(0).(func() uint32); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint32) + } + + return r0 +} + +// LastNonce provides a mock function with no fields +func (_m *MockBlockchainContext) LastNonce() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// LastRandomSeed provides a mock function with no fields +func (_m *MockBlockchainContext) LastRandomSeed() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("random") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// LastRound provides a mock function with no fields +func (_m *MockBlockchainContext) LastRound() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// LastTimeStamp provides a mock function with no fields +func (_m *MockBlockchainContext) LastTimeStamp() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// LastTimeStampMs provides a mock function with no fields +func (_m *MockBlockchainContext) LastTimeStampMs() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1000 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// NewAddress provides a mock function with given fields: creatorAddress +func (_m *MockBlockchainContext) NewAddress(creatorAddress []byte) ([]byte, error) { + ret := _m.Called(creatorAddress) + + if len(ret) == 0 { + return []byte("addr"), nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func([]byte) ([]byte, error)); ok { + return rf(creatorAddress) + } + if rf, ok := ret.Get(0).(func([]byte) []byte); ok { + r0 = rf(creatorAddress) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func([]byte) error); ok { + r1 = rf(creatorAddress) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// PopDiscard provides a mock function with no fields +func (_m *MockBlockchainContext) PopDiscard() { + _m.Called() +} + +// PopSetActiveState provides a mock function with no fields +func (_m *MockBlockchainContext) PopSetActiveState() { + _m.Called() +} + +// ProcessBuiltInFunction provides a mock function with given fields: input +func (_m *MockBlockchainContext) ProcessBuiltInFunction(input *vmcommon.ContractCallInput) (*vmcommon.VMOutput, error) { + ret := _m.Called(input) + + if len(ret) == 0 { + return &vmcommon.VMOutput{}, nil + } + + var r0 *vmcommon.VMOutput + var r1 error + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) (*vmcommon.VMOutput, error)); ok { + return rf(input) + } + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) *vmcommon.VMOutput); ok { + r0 = rf(input) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.VMOutput) + } + } + + if rf, ok := ret.Get(1).(func(*vmcommon.ContractCallInput) error); ok { + r1 = rf(input) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// PushState provides a mock function with no fields +func (_m *MockBlockchainContext) PushState() { + _m.Called() +} + +// RevertToSnapshot provides a mock function with given fields: snapshot +func (_m *MockBlockchainContext) RevertToSnapshot(snapshot int) { + _m.Called(snapshot) +} + +// RoundTime provides a mock function with no fields +func (_m *MockBlockchainContext) RoundTime() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 0 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// SaveCompiledCode provides a mock function with given fields: codeHash, code +func (_m *MockBlockchainContext) SaveCompiledCode(codeHash []byte, code []byte) { + _m.Called(codeHash, code) +} + +// NewMockBlockchainContext creates a new instance of MockBlockchainContext. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockBlockchainContext(t interface { + mock.TestingT + Cleanup(func()) +}) *MockBlockchainContext { + mock := &MockBlockchainContext{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mock/mockery/Instance.go b/mock/mockery/Instance.go new file mode 100644 index 000000000..443aa36cd --- /dev/null +++ b/mock/mockery/Instance.go @@ -0,0 +1,433 @@ +package mockery + +import ( + executor "github.com/multiversx/mx-chain-vm-go/executor" + mock "github.com/stretchr/testify/mock" +) + +// MockInstance is an autogenerated mock type for the Instance type +type MockInstance struct { + mock.Mock +} + +// Cache provides a mock function with no fields +func (_m *MockInstance) Cache() ([]byte, error) { + ret := _m.Called() + + if len(ret) == 0 { + return []byte("cache"), nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func() ([]byte, error)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// CallFunction provides a mock function with given fields: functionName +func (_m *MockInstance) CallFunction(functionName string) error { + ret := _m.Called(functionName) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(string) error); ok { + r0 = rf(functionName) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Clean provides a mock function with no fields +func (_m *MockInstance) Clean() bool { + ret := _m.Called() + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// GetBreakpointValue provides a mock function with no fields +func (_m *MockInstance) GetBreakpointValue() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetFunctionNames provides a mock function with no fields +func (_m *MockInstance) GetFunctionNames() []string { + ret := _m.Called() + + if len(ret) == 0 { + return []string{} + } + + var r0 []string + if rf, ok := ret.Get(0).(func() []string); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]string) + } + } + + return r0 +} + +// GetPointsUsed provides a mock function with no fields +func (_m *MockInstance) GetPointsUsed() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetVMHooksPtr provides a mock function with no fields +func (_m *MockInstance) GetVMHooksPtr() uintptr { + ret := _m.Called() + + if len(ret) == 0 { + return uintptr(1) + } + + var r0 uintptr + if rf, ok := ret.Get(0).(func() uintptr); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uintptr) + } + + return r0 +} + +// HasFunction provides a mock function with given fields: functionName +func (_m *MockInstance) HasFunction(functionName string) bool { + ret := _m.Called(functionName) + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func(string) bool); ok { + r0 = rf(functionName) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// HasMemory provides a mock function with no fields +func (_m *MockInstance) HasMemory() bool { + ret := _m.Called() + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// ID provides a mock function with no fields +func (_m *MockInstance) ID() string { + ret := _m.Called() + + if len(ret) == 0 { + return "ID" + } + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// IsAlreadyCleaned provides a mock function with no fields +func (_m *MockInstance) IsAlreadyCleaned() bool { + ret := _m.Called() + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsFunctionImported provides a mock function with given fields: name +func (_m *MockInstance) IsFunctionImported(name string) bool { + ret := _m.Called(name) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func(string) bool); ok { + r0 = rf(name) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsInterfaceNil provides a mock function with no fields +func (_m *MockInstance) IsInterfaceNil() bool { + ret := _m.Called() + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// MemDump provides a mock function with no fields +func (_m *MockInstance) MemDump() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{} + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// MemGrow provides a mock function with given fields: pages +func (_m *MockInstance) MemGrow(pages uint32) error { + ret := _m.Called(pages) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(uint32) error); ok { + r0 = rf(pages) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// MemLength provides a mock function with no fields +func (_m *MockInstance) MemLength() uint32 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint32 + if rf, ok := ret.Get(0).(func() uint32); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint32) + } + + return r0 +} + +// MemLoad provides a mock function with given fields: memPtr, length +func (_m *MockInstance) MemLoad(memPtr executor.MemPtr, length int32) ([]byte, error) { + ret := _m.Called(memPtr, length) + + if len(ret) == 0 { + return []byte{}, nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(executor.MemPtr, int32) ([]byte, error)); ok { + return rf(memPtr, length) + } + if rf, ok := ret.Get(0).(func(executor.MemPtr, int32) []byte); ok { + r0 = rf(memPtr, length) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(executor.MemPtr, int32) error); ok { + r1 = rf(memPtr, length) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// MemStore provides a mock function with given fields: memPtr, data +func (_m *MockInstance) MemStore(memPtr executor.MemPtr, data []byte) error { + ret := _m.Called(memPtr, data) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(executor.MemPtr, []byte) error); ok { + r0 = rf(memPtr, data) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Reset provides a mock function with no fields +func (_m *MockInstance) Reset() bool { + ret := _m.Called() + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// SetBreakpointValue provides a mock function with given fields: value +func (_m *MockInstance) SetBreakpointValue(value uint64) { + _m.Called(value) +} + +// SetGasLimit provides a mock function with given fields: gasLimit +func (_m *MockInstance) SetGasLimit(gasLimit uint64) { + _m.Called(gasLimit) +} + +// SetPointsUsed provides a mock function with given fields: points +func (_m *MockInstance) SetPointsUsed(points uint64) { + _m.Called(points) +} + +// SetVMHooksPtr provides a mock function with given fields: vmHooksPtr +func (_m *MockInstance) SetVMHooksPtr(vmHooksPtr uintptr) { + _m.Called(vmHooksPtr) +} + +// ValidateFunctionArities provides a mock function with no fields +func (_m *MockInstance) ValidateFunctionArities() error { + ret := _m.Called() + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewMockInstance creates a new instance of MockInstance. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockInstance(t interface { + mock.TestingT + Cleanup(func()) +}) *MockInstance { + mock := &MockInstance{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mock/mockery/ManagedTypesContext.go b/mock/mockery/ManagedTypesContext.go new file mode 100644 index 000000000..f6e67661a --- /dev/null +++ b/mock/mockery/ManagedTypesContext.go @@ -0,0 +1,1016 @@ +package mockery + +import ( + elliptic "crypto/elliptic" + big "math/big" + + io "io" + + mock "github.com/stretchr/testify/mock" + + vmcommon "github.com/multiversx/mx-chain-vm-common-go" +) + +// MockManagedTypesContext is an autogenerated mock type for the ManagedTypesContext type +type MockManagedTypesContext struct { + mock.Mock +} + +// AddBackTransfers provides a mock function with given fields: value, transfers, index +func (_m *MockManagedTypesContext) AddBackTransfers(value *big.Int, transfers []*vmcommon.ESDTTransfer, index uint32) { + _m.Called(value, transfers, index) +} + +// AppendBytes provides a mock function with given fields: mBufferHandle, bytes +func (_m *MockManagedTypesContext) AppendBytes(mBufferHandle int32, bytes []byte) bool { + ret := _m.Called(mBufferHandle, bytes) + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func(int32, []byte) bool); ok { + r0 = rf(mBufferHandle, bytes) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// BigFloatExpIsNotValid provides a mock function with given fields: exponent +func (_m *MockManagedTypesContext) BigFloatExpIsNotValid(exponent int) bool { + ret := _m.Called(exponent) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func(int) bool); ok { + r0 = rf(exponent) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// BigFloatPrecIsNotValid provides a mock function with given fields: precision +func (_m *MockManagedTypesContext) BigFloatPrecIsNotValid(precision uint) bool { + ret := _m.Called(precision) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func(uint) bool); ok { + r0 = rf(precision) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// ClearStateStack provides a mock function with no fields +func (_m *MockManagedTypesContext) ClearStateStack() { + _m.Called() +} + +// ConsumeGasForBigFloatCopy provides a mock function with given fields: values +func (_m *MockManagedTypesContext) ConsumeGasForBigFloatCopy(values ...*big.Float) error { + _va := make([]interface{}, len(values)) + for _i := range values { + _va[_i] = values[_i] + } + var _ca []interface{} + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(...*big.Float) error); ok { + r0 = rf(values...) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// ConsumeGasForBigIntCopy provides a mock function with given fields: values +func (_m *MockManagedTypesContext) ConsumeGasForBigIntCopy(values ...*big.Int) error { + _va := make([]interface{}, len(values)) + for _i := range values { + _va[_i] = values[_i] + } + var _ca []interface{} + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(...*big.Int) error); ok { + r0 = rf(values...) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// ConsumeGasForBytes provides a mock function with given fields: bytes +func (_m *MockManagedTypesContext) ConsumeGasForBytes(bytes []byte) error { + ret := _m.Called(bytes) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func([]byte) error); ok { + r0 = rf(bytes) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// ConsumeGasForThisBigIntNumberOfBytes provides a mock function with given fields: byteLen +func (_m *MockManagedTypesContext) ConsumeGasForThisBigIntNumberOfBytes(byteLen *big.Int) error { + ret := _m.Called(byteLen) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(*big.Int) error); ok { + r0 = rf(byteLen) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// ConsumeGasForThisIntNumberOfBytes provides a mock function with given fields: byteLen +func (_m *MockManagedTypesContext) ConsumeGasForThisIntNumberOfBytes(byteLen int) error { + ret := _m.Called(byteLen) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(int) error); ok { + r0 = rf(byteLen) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// DeleteSlice provides a mock function with given fields: mBufferHandle, startPosition, lengthOfSlice +func (_m *MockManagedTypesContext) DeleteSlice(mBufferHandle int32, startPosition int32, lengthOfSlice int32) ([]byte, error) { + ret := _m.Called(mBufferHandle, startPosition, lengthOfSlice) + + if len(ret) == 0 { + return []byte{}, nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(int32, int32, int32) ([]byte, error)); ok { + return rf(mBufferHandle, startPosition, lengthOfSlice) + } + if rf, ok := ret.Get(0).(func(int32, int32, int32) []byte); ok { + r0 = rf(mBufferHandle, startPosition, lengthOfSlice) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(int32, int32, int32) error); ok { + r1 = rf(mBufferHandle, startPosition, lengthOfSlice) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// EncodedBigFloatIsNotValid provides a mock function with given fields: encodedBigFloat +func (_m *MockManagedTypesContext) EncodedBigFloatIsNotValid(encodedBigFloat []byte) bool { + ret := _m.Called(encodedBigFloat) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func([]byte) bool); ok { + r0 = rf(encodedBigFloat) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// Get100xCurveGasCostMultiplier provides a mock function with given fields: ecHandle +func (_m *MockManagedTypesContext) Get100xCurveGasCostMultiplier(ecHandle int32) int32 { + ret := _m.Called(ecHandle) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func(int32) int32); ok { + r0 = rf(ecHandle) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// GetBackTransfers provides a mock function with no fields +func (_m *MockManagedTypesContext) GetBackTransfers() ([]*vmcommon.ESDTTransfer, *big.Int) { + ret := _m.Called() + + if len(ret) == 0 { + return []*vmcommon.ESDTTransfer{}, big.NewInt(0) + } + + var r0 []*vmcommon.ESDTTransfer + var r1 *big.Int + if rf, ok := ret.Get(0).(func() ([]*vmcommon.ESDTTransfer, *big.Int)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() []*vmcommon.ESDTTransfer); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]*vmcommon.ESDTTransfer) + } + } + + if rf, ok := ret.Get(1).(func() *big.Int); ok { + r1 = rf() + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*big.Int) + } + } + + return r0, r1 +} + +// GetBigFloat provides a mock function with given fields: handle +func (_m *MockManagedTypesContext) GetBigFloat(handle int32) (*big.Float, error) { + ret := _m.Called(handle) + + if len(ret) == 0 { + return big.NewFloat(0), nil + } + + var r0 *big.Float + var r1 error + if rf, ok := ret.Get(0).(func(int32) (*big.Float, error)); ok { + return rf(handle) + } + if rf, ok := ret.Get(0).(func(int32) *big.Float); ok { + r0 = rf(handle) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Float) + } + } + + if rf, ok := ret.Get(1).(func(int32) error); ok { + r1 = rf(handle) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetBigFloatOrCreate provides a mock function with given fields: handle +func (_m *MockManagedTypesContext) GetBigFloatOrCreate(handle int32) (*big.Float, error) { + ret := _m.Called(handle) + + if len(ret) == 0 { + return big.NewFloat(0), nil + } + + var r0 *big.Float + var r1 error + if rf, ok := ret.Get(0).(func(int32) (*big.Float, error)); ok { + return rf(handle) + } + if rf, ok := ret.Get(0).(func(int32) *big.Float); ok { + r0 = rf(handle) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Float) + } + } + + if rf, ok := ret.Get(1).(func(int32) error); ok { + r1 = rf(handle) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetBigInt provides a mock function with given fields: id +func (_m *MockManagedTypesContext) GetBigInt(id int32) (*big.Int, error) { + ret := _m.Called(id) + + if len(ret) == 0 { + return big.NewInt(0), nil + } + + var r0 *big.Int + var r1 error + if rf, ok := ret.Get(0).(func(int32) (*big.Int, error)); ok { + return rf(id) + } + if rf, ok := ret.Get(0).(func(int32) *big.Int); ok { + r0 = rf(id) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Int) + } + } + + if rf, ok := ret.Get(1).(func(int32) error); ok { + r1 = rf(id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetBigIntOrCreate provides a mock function with given fields: handle +func (_m *MockManagedTypesContext) GetBigIntOrCreate(handle int32) *big.Int { + ret := _m.Called(handle) + + if len(ret) == 0 { + return big.NewInt(0) + } + + var r0 *big.Int + if rf, ok := ret.Get(0).(func(int32) *big.Int); ok { + r0 = rf(handle) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Int) + } + } + + return r0 +} + +// GetBytes provides a mock function with given fields: mBufferHandle +func (_m *MockManagedTypesContext) GetBytes(mBufferHandle int32) ([]byte, error) { + ret := _m.Called(mBufferHandle) + + if len(ret) == 0 { + return []byte(""), nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(int32) ([]byte, error)); ok { + return rf(mBufferHandle) + } + if rf, ok := ret.Get(0).(func(int32) []byte); ok { + r0 = rf(mBufferHandle) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(int32) error); ok { + r1 = rf(mBufferHandle) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetEllipticCurve provides a mock function with given fields: handle +func (_m *MockManagedTypesContext) GetEllipticCurve(handle int32) (*elliptic.CurveParams, error) { + ret := _m.Called(handle) + + if len(ret) == 0 { + panic("no return value specified for GetEllipticCurve") + } + + var r0 *elliptic.CurveParams + var r1 error + if rf, ok := ret.Get(0).(func(int32) (*elliptic.CurveParams, error)); ok { + return rf(handle) + } + if rf, ok := ret.Get(0).(func(int32) *elliptic.CurveParams); ok { + r0 = rf(handle) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*elliptic.CurveParams) + } + } + + if rf, ok := ret.Get(1).(func(int32) error); ok { + r1 = rf(handle) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetEllipticCurveSizeOfField provides a mock function with given fields: ecHandle +func (_m *MockManagedTypesContext) GetEllipticCurveSizeOfField(ecHandle int32) int32 { + ret := _m.Called(ecHandle) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func(int32) int32); ok { + r0 = rf(ecHandle) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// GetLength provides a mock function with given fields: mBufferHandle +func (_m *MockManagedTypesContext) GetLength(mBufferHandle int32) int32 { + ret := _m.Called(mBufferHandle) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func(int32) int32); ok { + r0 = rf(mBufferHandle) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// GetPrivateKeyByteLengthEC provides a mock function with given fields: ecHandle +func (_m *MockManagedTypesContext) GetPrivateKeyByteLengthEC(ecHandle int32) int32 { + ret := _m.Called(ecHandle) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func(int32) int32); ok { + r0 = rf(ecHandle) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// GetRandReader provides a mock function with no fields +func (_m *MockManagedTypesContext) GetRandReader() io.Reader { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetRandReader") + } + + var r0 io.Reader + if rf, ok := ret.Get(0).(func() io.Reader); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(io.Reader) + } + } + + return r0 +} + +// GetScalarMult100xCurveGasCostMultiplier provides a mock function with given fields: ecHandle +func (_m *MockManagedTypesContext) GetScalarMult100xCurveGasCostMultiplier(ecHandle int32) int32 { + ret := _m.Called(ecHandle) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func(int32) int32); ok { + r0 = rf(ecHandle) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// GetSlice provides a mock function with given fields: mBufferHandle, startPosition, lengthOfSlice +func (_m *MockManagedTypesContext) GetSlice(mBufferHandle int32, startPosition int32, lengthOfSlice int32) ([]byte, error) { + ret := _m.Called(mBufferHandle, startPosition, lengthOfSlice) + + if len(ret) == 0 { + return []byte(""), nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(int32, int32, int32) ([]byte, error)); ok { + return rf(mBufferHandle, startPosition, lengthOfSlice) + } + if rf, ok := ret.Get(0).(func(int32, int32, int32) []byte); ok { + r0 = rf(mBufferHandle, startPosition, lengthOfSlice) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(int32, int32, int32) error); ok { + r1 = rf(mBufferHandle, startPosition, lengthOfSlice) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetTwoBigFloats provides a mock function with given fields: handle1, handle2 +func (_m *MockManagedTypesContext) GetTwoBigFloats(handle1 int32, handle2 int32) (*big.Float, *big.Float, error) { + ret := _m.Called(handle1, handle2) + + if len(ret) == 0 { + return big.NewFloat(0), big.NewFloat(0), nil + } + + var r0 *big.Float + var r1 *big.Float + var r2 error + if rf, ok := ret.Get(0).(func(int32, int32) (*big.Float, *big.Float, error)); ok { + return rf(handle1, handle2) + } + if rf, ok := ret.Get(0).(func(int32, int32) *big.Float); ok { + r0 = rf(handle1, handle2) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Float) + } + } + + if rf, ok := ret.Get(1).(func(int32, int32) *big.Float); ok { + r1 = rf(handle1, handle2) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*big.Float) + } + } + + if rf, ok := ret.Get(2).(func(int32, int32) error); ok { + r2 = rf(handle1, handle2) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// GetTwoBigInt provides a mock function with given fields: handle1, handle2 +func (_m *MockManagedTypesContext) GetTwoBigInt(handle1 int32, handle2 int32) (*big.Int, *big.Int, error) { + ret := _m.Called(handle1, handle2) + + if len(ret) == 0 { + return big.NewInt(0), big.NewInt(0), nil + } + + var r0 *big.Int + var r1 *big.Int + var r2 error + if rf, ok := ret.Get(0).(func(int32, int32) (*big.Int, *big.Int, error)); ok { + return rf(handle1, handle2) + } + if rf, ok := ret.Get(0).(func(int32, int32) *big.Int); ok { + r0 = rf(handle1, handle2) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*big.Int) + } + } + + if rf, ok := ret.Get(1).(func(int32, int32) *big.Int); ok { + r1 = rf(handle1, handle2) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*big.Int) + } + } + + if rf, ok := ret.Get(2).(func(int32, int32) error); ok { + r2 = rf(handle1, handle2) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// GetUCompressed100xCurveGasCostMultiplier provides a mock function with given fields: ecHandle +func (_m *MockManagedTypesContext) GetUCompressed100xCurveGasCostMultiplier(ecHandle int32) int32 { + ret := _m.Called(ecHandle) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func(int32) int32); ok { + r0 = rf(ecHandle) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// InitState provides a mock function with no fields +func (_m *MockManagedTypesContext) InitState() { + _m.Called() +} + +// InsertSlice provides a mock function with given fields: mBufferHandle, startPosition, slice +func (_m *MockManagedTypesContext) InsertSlice(mBufferHandle int32, startPosition int32, slice []byte) ([]byte, error) { + ret := _m.Called(mBufferHandle, startPosition, slice) + + if len(ret) == 0 { + return []byte{}, nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(int32, int32, []byte) ([]byte, error)); ok { + return rf(mBufferHandle, startPosition, slice) + } + if rf, ok := ret.Get(0).(func(int32, int32, []byte) []byte); ok { + r0 = rf(mBufferHandle, startPosition, slice) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(int32, int32, []byte) error); ok { + r1 = rf(mBufferHandle, startPosition, slice) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// ManagedMapContains provides a mock function with given fields: mMapHandle, keyHandle +func (_m *MockManagedTypesContext) ManagedMapContains(mMapHandle int32, keyHandle int32) (bool, error) { + ret := _m.Called(mMapHandle, keyHandle) + + if len(ret) == 0 { + return true, nil + } + + var r0 bool + var r1 error + if rf, ok := ret.Get(0).(func(int32, int32) (bool, error)); ok { + return rf(mMapHandle, keyHandle) + } + if rf, ok := ret.Get(0).(func(int32, int32) bool); ok { + r0 = rf(mMapHandle, keyHandle) + } else { + r0 = ret.Get(0).(bool) + } + + if rf, ok := ret.Get(1).(func(int32, int32) error); ok { + r1 = rf(mMapHandle, keyHandle) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// ManagedMapGet provides a mock function with given fields: mMapHandle, keyHandle, outValueHandle +func (_m *MockManagedTypesContext) ManagedMapGet(mMapHandle int32, keyHandle int32, outValueHandle int32) error { + ret := _m.Called(mMapHandle, keyHandle, outValueHandle) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(int32, int32, int32) error); ok { + r0 = rf(mMapHandle, keyHandle, outValueHandle) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// ManagedMapPut provides a mock function with given fields: mMapHandle, keyHandle, valueHandle +func (_m *MockManagedTypesContext) ManagedMapPut(mMapHandle int32, keyHandle int32, valueHandle int32) error { + ret := _m.Called(mMapHandle, keyHandle, valueHandle) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(int32, int32, int32) error); ok { + r0 = rf(mMapHandle, keyHandle, valueHandle) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// ManagedMapRemove provides a mock function with given fields: mMapHandle, keyHandle, outValueHandle +func (_m *MockManagedTypesContext) ManagedMapRemove(mMapHandle int32, keyHandle int32, outValueHandle int32) error { + ret := _m.Called(mMapHandle, keyHandle, outValueHandle) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(int32, int32, int32) error); ok { + r0 = rf(mMapHandle, keyHandle, outValueHandle) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewBigInt provides a mock function with given fields: value +func (_m *MockManagedTypesContext) NewBigInt(value *big.Int) int32 { + ret := _m.Called(value) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func(*big.Int) int32); ok { + r0 = rf(value) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// NewBigIntFromInt64 provides a mock function with given fields: int64Value +func (_m *MockManagedTypesContext) NewBigIntFromInt64(int64Value int64) int32 { + ret := _m.Called(int64Value) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func(int64) int32); ok { + r0 = rf(int64Value) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// NewManagedBuffer provides a mock function with no fields +func (_m *MockManagedTypesContext) NewManagedBuffer() int32 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func() int32); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// NewManagedBufferFromBytes provides a mock function with given fields: bytes +func (_m *MockManagedTypesContext) NewManagedBufferFromBytes(bytes []byte) int32 { + ret := _m.Called(bytes) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func([]byte) int32); ok { + r0 = rf(bytes) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// NewManagedMap provides a mock function with no fields +func (_m *MockManagedTypesContext) NewManagedMap() int32 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func() int32); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// PopBackTransferIfAsyncCallBack provides a mock function with given fields: vmInput +func (_m *MockManagedTypesContext) PopBackTransferIfAsyncCallBack(vmInput *vmcommon.ContractCallInput) { + _m.Called(vmInput) +} + +// PopDiscard provides a mock function with no fields +func (_m *MockManagedTypesContext) PopDiscard() { + _m.Called() +} + +// PopSetActiveState provides a mock function with no fields +func (_m *MockManagedTypesContext) PopSetActiveState() { + _m.Called() +} + +// PushState provides a mock function with no fields +func (_m *MockManagedTypesContext) PushState() { + _m.Called() +} + +// PutBigFloat provides a mock function with given fields: value +func (_m *MockManagedTypesContext) PutBigFloat(value *big.Float) (int32, error) { + ret := _m.Called(value) + + if len(ret) == 0 { + return 1, nil + } + + var r0 int32 + var r1 error + if rf, ok := ret.Get(0).(func(*big.Float) (int32, error)); ok { + return rf(value) + } + if rf, ok := ret.Get(0).(func(*big.Float) int32); ok { + r0 = rf(value) + } else { + r0 = ret.Get(0).(int32) + } + + if rf, ok := ret.Get(1).(func(*big.Float) error); ok { + r1 = rf(value) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// PutEllipticCurve provides a mock function with given fields: ec +func (_m *MockManagedTypesContext) PutEllipticCurve(ec *elliptic.CurveParams) int32 { + ret := _m.Called(ec) + + if len(ret) == 0 { + return 1 + } + + var r0 int32 + if rf, ok := ret.Get(0).(func(*elliptic.CurveParams) int32); ok { + r0 = rf(ec) + } else { + r0 = ret.Get(0).(int32) + } + + return r0 +} + +// ReadManagedVecOfManagedBuffers provides a mock function with given fields: managedVecHandle +func (_m *MockManagedTypesContext) ReadManagedVecOfManagedBuffers(managedVecHandle int32) ([][]byte, uint64, error) { + ret := _m.Called(managedVecHandle) + + if len(ret) == 0 { + return [][]byte{}, 0, nil + } + + var r0 [][]byte + var r1 uint64 + var r2 error + if rf, ok := ret.Get(0).(func(int32) ([][]byte, uint64, error)); ok { + return rf(managedVecHandle) + } + if rf, ok := ret.Get(0).(func(int32) [][]byte); ok { + r0 = rf(managedVecHandle) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([][]byte) + } + } + + if rf, ok := ret.Get(1).(func(int32) uint64); ok { + r1 = rf(managedVecHandle) + } else { + r1 = ret.Get(1).(uint64) + } + + if rf, ok := ret.Get(2).(func(int32) error); ok { + r2 = rf(managedVecHandle) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// SetBytes provides a mock function with given fields: mBufferHandle, bytes +func (_m *MockManagedTypesContext) SetBytes(mBufferHandle int32, bytes []byte) { + _m.Called(mBufferHandle, bytes) +} + +// WriteManagedVecOfManagedBuffers provides a mock function with given fields: data, destinationHandle +func (_m *MockManagedTypesContext) WriteManagedVecOfManagedBuffers(data [][]byte, destinationHandle int32) error { + ret := _m.Called(data, destinationHandle) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func([][]byte, int32) error); ok { + r0 = rf(data, destinationHandle) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewMockManagedTypesContext creates a new instance of MockManagedTypesContext. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockManagedTypesContext(t interface { + mock.TestingT + Cleanup(func()) +}) *MockManagedTypesContext { + mock := &MockManagedTypesContext{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mock/mockery/MeteringContext.go b/mock/mockery/MeteringContext.go new file mode 100644 index 000000000..8e9cde4fe --- /dev/null +++ b/mock/mockery/MeteringContext.go @@ -0,0 +1,460 @@ +package mockery + +import ( + config "github.com/multiversx/mx-chain-vm-go/config" + mock "github.com/stretchr/testify/mock" + + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + + vmhost "github.com/multiversx/mx-chain-vm-go/vmhost" +) + +// MockMeteringContext is an autogenerated mock type for the MeteringContext type +type MockMeteringContext struct { + mock.Mock +} + +// BlockGasLimit provides a mock function with no fields +func (_m *MockMeteringContext) BlockGasLimit() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// BoundGasLimit provides a mock function with given fields: value +func (_m *MockMeteringContext) BoundGasLimit(value int64) uint64 { + ret := _m.Called(value) + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func(int64) uint64); ok { + r0 = rf(value) + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// ClearStateStack provides a mock function with no fields +func (_m *MockMeteringContext) ClearStateStack() { + _m.Called() +} + +// ComputeExtraGasLockedForAsync provides a mock function with no fields +func (_m *MockMeteringContext) ComputeExtraGasLockedForAsync() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// DeductInitialGasForDirectDeployment provides a mock function with given fields: input +func (_m *MockMeteringContext) DeductInitialGasForDirectDeployment(input vmhost.CodeDeployInput) error { + ret := _m.Called(input) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(vmhost.CodeDeployInput) error); ok { + r0 = rf(input) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// DeductInitialGasForExecution provides a mock function with given fields: contract +func (_m *MockMeteringContext) DeductInitialGasForExecution(contract []byte) error { + ret := _m.Called(contract) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func([]byte) error); ok { + r0 = rf(contract) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// DeductInitialGasForIndirectDeployment provides a mock function with given fields: input +func (_m *MockMeteringContext) DeductInitialGasForIndirectDeployment(input vmhost.CodeDeployInput) error { + ret := _m.Called(input) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(vmhost.CodeDeployInput) error); ok { + r0 = rf(input) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// DisableRestoreGas provides a mock function with no fields +func (_m *MockMeteringContext) DisableRestoreGas() { + _m.Called() +} + +// EnableRestoreGas provides a mock function with no fields +func (_m *MockMeteringContext) EnableRestoreGas() { + _m.Called() +} + +// FreeGas provides a mock function with given fields: gas +func (_m *MockMeteringContext) FreeGas(gas uint64) { + _m.Called(gas) +} + +// GasLeft provides a mock function with no fields +func (_m *MockMeteringContext) GasLeft() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GasSchedule provides a mock function with no fields +func (_m *MockMeteringContext) GasSchedule() *config.GasCost { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GasSchedule") + } + + var r0 *config.GasCost + if rf, ok := ret.Get(0).(func() *config.GasCost); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*config.GasCost) + } + } + + return r0 +} + +// GasSpentByContract provides a mock function with no fields +func (_m *MockMeteringContext) GasSpentByContract() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GasUsedForExecution provides a mock function with no fields +func (_m *MockMeteringContext) GasUsedForExecution() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetGasForExecution provides a mock function with no fields +func (_m *MockMeteringContext) GetGasForExecution() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetGasLocked provides a mock function with no fields +func (_m *MockMeteringContext) GetGasLocked() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetGasProvided provides a mock function with no fields +func (_m *MockMeteringContext) GetGasProvided() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetGasTrace provides a mock function with no fields +func (_m *MockMeteringContext) GetGasTrace() map[string]map[string][]uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return map[string]map[string][]uint64{} + } + + var r0 map[string]map[string][]uint64 + if rf, ok := ret.Get(0).(func() map[string]map[string][]uint64); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(map[string]map[string][]uint64) + } + } + + return r0 +} + +// GetSCPrepareInitialCost provides a mock function with no fields +func (_m *MockMeteringContext) GetSCPrepareInitialCost() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// InitState provides a mock function with no fields +func (_m *MockMeteringContext) InitState() { + _m.Called() +} + +// InitStateFromContractCallInput provides a mock function with given fields: input +func (_m *MockMeteringContext) InitStateFromContractCallInput(input *vmcommon.VMInput) { + _m.Called(input) +} + +// PopDiscard provides a mock function with no fields +func (_m *MockMeteringContext) PopDiscard() { + _m.Called() +} + +// PopMergeActiveState provides a mock function with no fields +func (_m *MockMeteringContext) PopMergeActiveState() { + _m.Called() +} + +// PopSetActiveState provides a mock function with no fields +func (_m *MockMeteringContext) PopSetActiveState() { + _m.Called() +} + +// PushState provides a mock function with no fields +func (_m *MockMeteringContext) PushState() { + _m.Called() +} + +// RestoreGas provides a mock function with given fields: gas +func (_m *MockMeteringContext) RestoreGas(gas uint64) { + _m.Called(gas) +} + +// SetGasSchedule provides a mock function with given fields: gasMap +func (_m *MockMeteringContext) SetGasSchedule(gasMap map[string]map[string]uint64) { + _m.Called(gasMap) +} + +// SetGasTracing provides a mock function with given fields: enableGasTracing +func (_m *MockMeteringContext) SetGasTracing(enableGasTracing bool) { + _m.Called(enableGasTracing) +} + +// StartGasTracing provides a mock function with given fields: functionName +func (_m *MockMeteringContext) StartGasTracing(functionName string) { + _m.Called(functionName) +} + +// TrackGasUsedByOutOfVMFunction provides a mock function with given fields: builtinInput, builtinOutput, postBuiltinInput +func (_m *MockMeteringContext) TrackGasUsedByOutOfVMFunction(builtinInput *vmcommon.ContractCallInput, builtinOutput *vmcommon.VMOutput, postBuiltinInput *vmcommon.ContractCallInput) { + _m.Called(builtinInput, builtinOutput, postBuiltinInput) +} + +// UpdateGasStateOnFailure provides a mock function with given fields: vmOutput +func (_m *MockMeteringContext) UpdateGasStateOnFailure(vmOutput *vmcommon.VMOutput) { + _m.Called(vmOutput) +} + +// UpdateGasStateOnSuccess provides a mock function with given fields: vmOutput +func (_m *MockMeteringContext) UpdateGasStateOnSuccess(vmOutput *vmcommon.VMOutput) error { + ret := _m.Called(vmOutput) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(*vmcommon.VMOutput) error); ok { + r0 = rf(vmOutput) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// UseGasBounded provides a mock function with given fields: gasToUse +func (_m *MockMeteringContext) UseGasBounded(gasToUse uint64) error { + ret := _m.Called(gasToUse) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(uint64) error); ok { + r0 = rf(gasToUse) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// UseGasBoundedAndAddTracedGas provides a mock function with given fields: functionName, gas +func (_m *MockMeteringContext) UseGasBoundedAndAddTracedGas(functionName string, gas uint64) error { + ret := _m.Called(functionName, gas) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(string, uint64) error); ok { + r0 = rf(functionName, gas) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// UseGasForAsyncStep provides a mock function with no fields +func (_m *MockMeteringContext) UseGasForAsyncStep() error { + ret := _m.Called() + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// UseGasForContractInit provides a mock function with given fields: gasToUse +func (_m *MockMeteringContext) UseGasForContractInit(gasToUse uint64) { + _m.Called(gasToUse) +} + +// NewMockMeteringContext creates a new instance of MockMeteringContext. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockMeteringContext(t interface { + mock.TestingT + Cleanup(func()) +}) *MockMeteringContext { + mock := &MockMeteringContext{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mock/mockery/OutputContext.go b/mock/mockery/OutputContext.go new file mode 100644 index 000000000..3326e5735 --- /dev/null +++ b/mock/mockery/OutputContext.go @@ -0,0 +1,428 @@ +package mockery + +import ( + big "math/big" + + vm "github.com/multiversx/mx-chain-core-go/data/vm" + mock "github.com/stretchr/testify/mock" + + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + + vmhost "github.com/multiversx/mx-chain-vm-go/vmhost" +) + +// MockOutputContext is an autogenerated mock type for the OutputContext type +type MockOutputContext struct { + mock.Mock +} + +// AddToActiveState provides a mock function with given fields: rightOutput +func (_m *MockOutputContext) AddToActiveState(rightOutput *vmcommon.VMOutput) { + _m.Called(rightOutput) +} + +// AddTxValueToAccount provides a mock function with given fields: address, value +func (_m *MockOutputContext) AddTxValueToAccount(address []byte, value *big.Int) { + _m.Called(address, value) +} + +// CensorVMOutput provides a mock function with no fields +func (_m *MockOutputContext) CensorVMOutput() { + _m.Called() +} + +// ClearReturnData provides a mock function with no fields +func (_m *MockOutputContext) ClearReturnData() { + _m.Called() +} + +// ClearStateStack provides a mock function with no fields +func (_m *MockOutputContext) ClearStateStack() { + _m.Called() +} + +// CreateVMOutputInCaseOfError provides a mock function with given fields: err +func (_m *MockOutputContext) CreateVMOutputInCaseOfError(err error) *vmcommon.VMOutput { + ret := _m.Called(err) + + if len(ret) == 0 { + return &vmcommon.VMOutput{ReturnCode: vmcommon.UserError} + } + + var r0 *vmcommon.VMOutput + if rf, ok := ret.Get(0).(func(error) *vmcommon.VMOutput); ok { + r0 = rf(err) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.VMOutput) + } + } + + return r0 +} + +// DeleteFirstReturnData provides a mock function with no fields +func (_m *MockOutputContext) DeleteFirstReturnData() { + _m.Called() +} + +// DeleteOutputAccount provides a mock function with given fields: address +func (_m *MockOutputContext) DeleteOutputAccount(address []byte) { + _m.Called(address) +} + +// DeployCode provides a mock function with given fields: input +func (_m *MockOutputContext) DeployCode(input vmhost.CodeDeployInput) { + _m.Called(input) +} + +// Finish provides a mock function with given fields: data +func (_m *MockOutputContext) Finish(data []byte) { + _m.Called(data) +} + +// GetCrtTransferIndex provides a mock function with no fields +func (_m *MockOutputContext) GetCrtTransferIndex() uint32 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint32 + if rf, ok := ret.Get(0).(func() uint32); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint32) + } + + return r0 +} + +// GetOutputAccount provides a mock function with given fields: address +func (_m *MockOutputContext) GetOutputAccount(address []byte) (*vmcommon.OutputAccount, bool) { + ret := _m.Called(address) + + if len(ret) == 0 { + return &vmcommon.OutputAccount{}, true + } + + var r0 *vmcommon.OutputAccount + var r1 bool + if rf, ok := ret.Get(0).(func([]byte) (*vmcommon.OutputAccount, bool)); ok { + return rf(address) + } + if rf, ok := ret.Get(0).(func([]byte) *vmcommon.OutputAccount); ok { + r0 = rf(address) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.OutputAccount) + } + } + + if rf, ok := ret.Get(1).(func([]byte) bool); ok { + r1 = rf(address) + } else { + r1 = ret.Get(1).(bool) + } + + return r0, r1 +} + +// GetOutputAccounts provides a mock function with no fields +func (_m *MockOutputContext) GetOutputAccounts() map[string]*vmcommon.OutputAccount { + ret := _m.Called() + + if len(ret) == 0 { + return map[string]*vmcommon.OutputAccount{} + } + + var r0 map[string]*vmcommon.OutputAccount + if rf, ok := ret.Get(0).(func() map[string]*vmcommon.OutputAccount); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(map[string]*vmcommon.OutputAccount) + } + } + + return r0 +} + +// GetRefund provides a mock function with no fields +func (_m *MockOutputContext) GetRefund() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 0 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetVMOutput provides a mock function with no fields +func (_m *MockOutputContext) GetVMOutput() *vmcommon.VMOutput { + ret := _m.Called() + + if len(ret) == 0 { + return &vmcommon.VMOutput{} + } + + var r0 *vmcommon.VMOutput + if rf, ok := ret.Get(0).(func() *vmcommon.VMOutput); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.VMOutput) + } + } + + return r0 +} + +// InitState provides a mock function with no fields +func (_m *MockOutputContext) InitState() { + _m.Called() +} + +// IsInterfaceNil provides a mock function with no fields +func (_m *MockOutputContext) IsInterfaceNil() bool { + ret := _m.Called() + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// NextOutputTransferIndex provides a mock function with no fields +func (_m *MockOutputContext) NextOutputTransferIndex() uint32 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint32 + if rf, ok := ret.Get(0).(func() uint32); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint32) + } + + return r0 +} + +// PopDiscard provides a mock function with no fields +func (_m *MockOutputContext) PopDiscard() { + _m.Called() +} + +// PopMergeActiveState provides a mock function with no fields +func (_m *MockOutputContext) PopMergeActiveState() { + _m.Called() +} + +// PopSetActiveState provides a mock function with no fields +func (_m *MockOutputContext) PopSetActiveState() { + _m.Called() +} + +// PrependFinish provides a mock function with given fields: data +func (_m *MockOutputContext) PrependFinish(data []byte) { + _m.Called(data) +} + +// PushState provides a mock function with no fields +func (_m *MockOutputContext) PushState() { + _m.Called() +} + +// RemoveNonUpdatedStorage provides a mock function with no fields +func (_m *MockOutputContext) RemoveNonUpdatedStorage() { + _m.Called() +} + +// RemoveReturnData provides a mock function with given fields: index +func (_m *MockOutputContext) RemoveReturnData(index uint32) { + _m.Called(index) +} + +// ReturnCode provides a mock function with no fields +func (_m *MockOutputContext) ReturnCode() vmcommon.ReturnCode { + ret := _m.Called() + + if len(ret) == 0 { + return vmcommon.Ok + } + + var r0 vmcommon.ReturnCode + if rf, ok := ret.Get(0).(func() vmcommon.ReturnCode); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(vmcommon.ReturnCode) + } + + return r0 +} + +// ReturnData provides a mock function with no fields +func (_m *MockOutputContext) ReturnData() [][]byte { + ret := _m.Called() + + if len(ret) == 0 { + return [][]byte{} + } + + var r0 [][]byte + if rf, ok := ret.Get(0).(func() [][]byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([][]byte) + } + } + + return r0 +} + +// ReturnMessage provides a mock function with no fields +func (_m *MockOutputContext) ReturnMessage() string { + ret := _m.Called() + + if len(ret) == 0 { + return "" + } + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// SetCrtTransferIndex provides a mock function with given fields: index +func (_m *MockOutputContext) SetCrtTransferIndex(index uint32) { + _m.Called(index) +} + +// SetRefund provides a mock function with given fields: refund +func (_m *MockOutputContext) SetRefund(refund uint64) { + _m.Called(refund) +} + +// SetReturnCode provides a mock function with given fields: returnCode +func (_m *MockOutputContext) SetReturnCode(returnCode vmcommon.ReturnCode) { + _m.Called(returnCode) +} + +// SetReturnMessage provides a mock function with given fields: message +func (_m *MockOutputContext) SetReturnMessage(message string) { + _m.Called(message) +} + +// Transfer provides a mock function with given fields: destination, sender, gasLimit, gasLocked, value, asyncData, input, callType +func (_m *MockOutputContext) Transfer(destination []byte, sender []byte, gasLimit uint64, gasLocked uint64, value *big.Int, asyncData []byte, input []byte, callType vm.CallType) error { + ret := _m.Called(destination, sender, gasLimit, gasLocked, value, asyncData, input, callType) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func([]byte, []byte, uint64, uint64, *big.Int, []byte, []byte, vm.CallType) error); ok { + r0 = rf(destination, sender, gasLimit, gasLocked, value, asyncData, input, callType) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// TransferESDT provides a mock function with given fields: transfersArgs, callInput +func (_m *MockOutputContext) TransferESDT(transfersArgs *vmhost.ESDTTransfersArgs, callInput *vmcommon.ContractCallInput) (uint64, error) { + ret := _m.Called(transfersArgs, callInput) + + if len(ret) == 0 { + return 0, nil + } + + var r0 uint64 + var r1 error + if rf, ok := ret.Get(0).(func(*vmhost.ESDTTransfersArgs, *vmcommon.ContractCallInput) (uint64, error)); ok { + return rf(transfersArgs, callInput) + } + if rf, ok := ret.Get(0).(func(*vmhost.ESDTTransfersArgs, *vmcommon.ContractCallInput) uint64); ok { + r0 = rf(transfersArgs, callInput) + } else { + r0 = ret.Get(0).(uint64) + } + + if rf, ok := ret.Get(1).(func(*vmhost.ESDTTransfersArgs, *vmcommon.ContractCallInput) error); ok { + r1 = rf(transfersArgs, callInput) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// TransferValueOnly provides a mock function with given fields: destination, sender, value, checkPayable +func (_m *MockOutputContext) TransferValueOnly(destination []byte, sender []byte, value *big.Int, checkPayable bool) error { + ret := _m.Called(destination, sender, value, checkPayable) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func([]byte, []byte, *big.Int, bool) error); ok { + r0 = rf(destination, sender, value, checkPayable) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// WriteLog provides a mock function with given fields: address, topics, data +func (_m *MockOutputContext) WriteLog(address []byte, topics [][]byte, data [][]byte) { + _m.Called(address, topics, data) +} + +// WriteLogWithIdentifier provides a mock function with given fields: address, topics, data, identifier +func (_m *MockOutputContext) WriteLogWithIdentifier(address []byte, topics [][]byte, data [][]byte, identifier []byte) { + _m.Called(address, topics, data, identifier) +} + +// NewMockOutputContext creates a new instance of MockOutputContext. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockOutputContext(t interface { + mock.TestingT + Cleanup(func()) +}) *MockOutputContext { + mock := &MockOutputContext{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mock/mockery/RuntimeContext.go b/mock/mockery/RuntimeContext.go new file mode 100644 index 000000000..fad4517e0 --- /dev/null +++ b/mock/mockery/RuntimeContext.go @@ -0,0 +1,764 @@ +package mockery + +import ( + executor "github.com/multiversx/mx-chain-vm-go/executor" + mock "github.com/stretchr/testify/mock" + + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + + vmhost "github.com/multiversx/mx-chain-vm-go/vmhost" +) + +// MockRuntimeContext is an autogenerated mock type for the RuntimeContext type +type MockRuntimeContext struct { + mock.Mock +} + +// AddError provides a mock function with given fields: err, otherInfo +func (_m *MockRuntimeContext) AddError(err error, otherInfo ...string) { + _va := make([]interface{}, len(otherInfo)) + for _i := range otherInfo { + _va[_i] = otherInfo[_i] + } + var _ca []interface{} + _ca = append(_ca, err) + _ca = append(_ca, _va...) + _m.Called(_ca...) +} + +// Arguments provides a mock function with no fields +func (_m *MockRuntimeContext) Arguments() [][]byte { + ret := _m.Called() + + if len(ret) == 0 { + return [][]byte{} + } + + var r0 [][]byte + if rf, ok := ret.Get(0).(func() [][]byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([][]byte) + } + } + + return r0 +} + +// CallSCFunction provides a mock function with given fields: functionName +func (_m *MockRuntimeContext) CallSCFunction(functionName string) error { + ret := _m.Called(functionName) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(string) error); ok { + r0 = rf(functionName) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// CleanInstance provides a mock function with no fields +func (_m *MockRuntimeContext) CleanInstance() { + _m.Called() +} + +// ClearStateStack provides a mock function with no fields +func (_m *MockRuntimeContext) ClearStateStack() { + _m.Called() +} + +// ClearWarmInstanceCache provides a mock function with no fields +func (_m *MockRuntimeContext) ClearWarmInstanceCache() { + _m.Called() +} + +// CountSameContractInstancesOnStack provides a mock function with given fields: address +func (_m *MockRuntimeContext) CountSameContractInstancesOnStack(address []byte) uint64 { + ret := _m.Called(address) + + if len(ret) == 0 { + return 0 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func([]byte) uint64); ok { + r0 = rf(address) + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// EndExecution provides a mock function with no fields +func (_m *MockRuntimeContext) EndExecution() { + _m.Called() +} + +// ExtractCodeUpgradeFromArgs provides a mock function with no fields +func (_m *MockRuntimeContext) ExtractCodeUpgradeFromArgs() ([]byte, []byte, error) { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{}, []byte{}, nil + } + + var r0 []byte + var r1 []byte + var r2 error + if rf, ok := ret.Get(0).(func() ([]byte, []byte, error)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func() []byte); ok { + r1 = rf() + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).([]byte) + } + } + + if rf, ok := ret.Get(2).(func() error); ok { + r2 = rf() + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// FailExecution provides a mock function with given fields: err +func (_m *MockRuntimeContext) FailExecution(err error) { + _m.Called(err) +} + +// FunctionName provides a mock function with no fields +func (_m *MockRuntimeContext) FunctionName() string { + ret := _m.Called() + + if len(ret) == 0 { + return "function" + } + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// FunctionNameChecked provides a mock function with no fields +func (_m *MockRuntimeContext) FunctionNameChecked() (string, error) { + ret := _m.Called() + + if len(ret) == 0 { + return "function", nil + } + + var r0 string + var r1 error + if rf, ok := ret.Get(0).(func() (string, error)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetAllErrors provides a mock function with no fields +func (_m *MockRuntimeContext) GetAllErrors() error { + ret := _m.Called() + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// GetContextAddress provides a mock function with no fields +func (_m *MockRuntimeContext) GetContextAddress() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{} + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetCurrentTxHash provides a mock function with no fields +func (_m *MockRuntimeContext) GetCurrentTxHash() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{} + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetInstance provides a mock function with no fields +func (_m *MockRuntimeContext) GetInstance() executor.Instance { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetInstance") + } + + var r0 executor.Instance + if rf, ok := ret.Get(0).(func() executor.Instance); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(executor.Instance) + } + } + + return r0 +} + +// GetInstanceStackSize provides a mock function with no fields +func (_m *MockRuntimeContext) GetInstanceStackSize() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetInstanceTracker provides a mock function with no fields +func (_m *MockRuntimeContext) GetInstanceTracker() vmhost.InstanceTracker { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetInstanceTracker") + } + + var r0 vmhost.InstanceTracker + if rf, ok := ret.Get(0).(func() vmhost.InstanceTracker); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.InstanceTracker) + } + } + + return r0 +} + +// GetOriginalCallerAddress provides a mock function with no fields +func (_m *MockRuntimeContext) GetOriginalCallerAddress() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{} + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetOriginalTxHash provides a mock function with no fields +func (_m *MockRuntimeContext) GetOriginalTxHash() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{} + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetPointsUsed provides a mock function with no fields +func (_m *MockRuntimeContext) GetPointsUsed() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetPrevTxHash provides a mock function with no fields +func (_m *MockRuntimeContext) GetPrevTxHash() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{} + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// GetRuntimeBreakpointValue provides a mock function with no fields +func (_m *MockRuntimeContext) GetRuntimeBreakpointValue() vmhost.BreakpointValue { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 vmhost.BreakpointValue + if rf, ok := ret.Get(0).(func() vmhost.BreakpointValue); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(vmhost.BreakpointValue) + } + + return r0 +} + +// GetSCCode provides a mock function with no fields +func (_m *MockRuntimeContext) GetSCCode() ([]byte, error) { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{}, nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func() ([]byte, error)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetSCCodeSize provides a mock function with no fields +func (_m *MockRuntimeContext) GetSCCodeSize() uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return 1 + } + + var r0 uint64 + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + return r0 +} + +// GetVMExecutor provides a mock function with no fields +func (_m *MockRuntimeContext) GetVMExecutor() executor.Executor { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetVMExecutor") + } + + var r0 executor.Executor + if rf, ok := ret.Get(0).(func() executor.Executor); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(executor.Executor) + } + } + + return r0 +} + +// GetVMInput provides a mock function with no fields +func (_m *MockRuntimeContext) GetVMInput() *vmcommon.ContractCallInput { + ret := _m.Called() + + if len(ret) == 0 { + return &vmcommon.ContractCallInput{} + } + + var r0 *vmcommon.ContractCallInput + if rf, ok := ret.Get(0).(func() *vmcommon.ContractCallInput); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.ContractCallInput) + } + } + + return r0 +} + +// GetVMType provides a mock function with no fields +func (_m *MockRuntimeContext) GetVMType() []byte { + ret := _m.Called() + + if len(ret) == 0 { + return []byte{} + } + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// HasFunction provides a mock function with given fields: functionName +func (_m *MockRuntimeContext) HasFunction(functionName string) bool { + ret := _m.Called(functionName) + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func(string) bool); ok { + r0 = rf(functionName) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// InitState provides a mock function with no fields +func (_m *MockRuntimeContext) InitState() { + _m.Called() +} + +// InitStateFromContractCallInput provides a mock function with given fields: input +func (_m *MockRuntimeContext) InitStateFromContractCallInput(input *vmcommon.ContractCallInput) { + _m.Called(input) +} + +// IsFunctionImported provides a mock function with given fields: name +func (_m *MockRuntimeContext) IsFunctionImported(name string) bool { + ret := _m.Called(name) + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func(string) bool); ok { + r0 = rf(name) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsReservedFunctionName provides a mock function with given fields: functionName +func (_m *MockRuntimeContext) IsReservedFunctionName(functionName string) bool { + ret := _m.Called(functionName) + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func(string) bool); ok { + r0 = rf(functionName) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// MustVerifyNextContractCode provides a mock function with no fields +func (_m *MockRuntimeContext) MustVerifyNextContractCode() { + _m.Called() +} + +// PopDiscard provides a mock function with no fields +func (_m *MockRuntimeContext) PopDiscard() { + _m.Called() +} + +// PopSetActiveState provides a mock function with no fields +func (_m *MockRuntimeContext) PopSetActiveState() { + _m.Called() +} + +// PushState provides a mock function with no fields +func (_m *MockRuntimeContext) PushState() { + _m.Called() +} + +// ReadOnly provides a mock function with no fields +func (_m *MockRuntimeContext) ReadOnly() bool { + ret := _m.Called() + + if len(ret) == 0 { + return true + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// ReplaceVMExecutor provides a mock function with given fields: vmExecutor +func (_m *MockRuntimeContext) ReplaceVMExecutor(vmExecutor executor.Executor) { + _m.Called(vmExecutor) +} + +// SetCodeAddress provides a mock function with given fields: scAddress +func (_m *MockRuntimeContext) SetCodeAddress(scAddress []byte) { + _m.Called(scAddress) +} + +// SetCustomCallFunction provides a mock function with given fields: callFunction +func (_m *MockRuntimeContext) SetCustomCallFunction(callFunction string) { + _m.Called(callFunction) +} + +// SetMaxInstanceStackSize provides a mock function with given fields: _a0 +func (_m *MockRuntimeContext) SetMaxInstanceStackSize(_a0 uint64) { + _m.Called(_a0) +} + +// SetPointsUsed provides a mock function with given fields: gasPoints +func (_m *MockRuntimeContext) SetPointsUsed(gasPoints uint64) { + _m.Called(gasPoints) +} + +// SetReadOnly provides a mock function with given fields: readOnly +func (_m *MockRuntimeContext) SetReadOnly(readOnly bool) { + _m.Called(readOnly) +} + +// SetRuntimeBreakpointValue provides a mock function with given fields: value +func (_m *MockRuntimeContext) SetRuntimeBreakpointValue(value vmhost.BreakpointValue) { + _m.Called(value) +} + +// SetVMInput provides a mock function with given fields: vmInput +func (_m *MockRuntimeContext) SetVMInput(vmInput *vmcommon.ContractCallInput) { + _m.Called(vmInput) +} + +// SignalUserError provides a mock function with given fields: message +func (_m *MockRuntimeContext) SignalUserError(message string) { + _m.Called(message) +} + +// StartWasmerInstance provides a mock function with given fields: contract, gasLimit, newCode +func (_m *MockRuntimeContext) StartWasmerInstance(contract []byte, gasLimit uint64, newCode bool) error { + ret := _m.Called(contract, gasLimit, newCode) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func([]byte, uint64, bool) error); ok { + r0 = rf(contract, gasLimit, newCode) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// UseGasBoundedShouldFailExecution provides a mock function with no fields +func (_m *MockRuntimeContext) UseGasBoundedShouldFailExecution() bool { + ret := _m.Called() + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// ValidateCallbackName provides a mock function with given fields: callbackName +func (_m *MockRuntimeContext) ValidateCallbackName(callbackName string) error { + ret := _m.Called(callbackName) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(string) error); ok { + r0 = rf(callbackName) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// ValidateInstances provides a mock function with no fields +func (_m *MockRuntimeContext) ValidateInstances() error { + ret := _m.Called() + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// VerifyContractCode provides a mock function with no fields +func (_m *MockRuntimeContext) VerifyContractCode() error { + ret := _m.Called() + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewMockRuntimeContext creates a new instance of MockRuntimeContext. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockRuntimeContext(t interface { + mock.TestingT + Cleanup(func()) +}) *MockRuntimeContext { + mock := &MockRuntimeContext{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mock/mockery/StorageContext.go b/mock/mockery/StorageContext.go new file mode 100644 index 000000000..91f448a68 --- /dev/null +++ b/mock/mockery/StorageContext.go @@ -0,0 +1,403 @@ +package mockery + +import ( + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + mock "github.com/stretchr/testify/mock" + + vmhost "github.com/multiversx/mx-chain-vm-go/vmhost" +) + +// MockStorageContext is an autogenerated mock type for the StorageContext type +type MockStorageContext struct { + mock.Mock +} + +// ClearStateStack provides a mock function with no fields +func (_m *MockStorageContext) ClearStateStack() { + _m.Called() +} + +// GetStorage provides a mock function with given fields: key +func (_m *MockStorageContext) GetStorage(key []byte) ([]byte, uint32, bool, error) { + ret := _m.Called(key) + + if len(ret) == 0 { + return []byte{}, 1, false, nil + } + + var r0 []byte + var r1 uint32 + var r2 bool + var r3 error + if rf, ok := ret.Get(0).(func([]byte) ([]byte, uint32, bool, error)); ok { + return rf(key) + } + if rf, ok := ret.Get(0).(func([]byte) []byte); ok { + r0 = rf(key) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func([]byte) uint32); ok { + r1 = rf(key) + } else { + r1 = ret.Get(1).(uint32) + } + + if rf, ok := ret.Get(2).(func([]byte) bool); ok { + r2 = rf(key) + } else { + r2 = ret.Get(2).(bool) + } + + if rf, ok := ret.Get(3).(func([]byte) error); ok { + r3 = rf(key) + } else { + r3 = ret.Error(3) + } + + return r0, r1, r2, r3 +} + +// GetStorageFromAddress provides a mock function with given fields: address, key +func (_m *MockStorageContext) GetStorageFromAddress(address []byte, key []byte) ([]byte, uint32, bool, error) { + ret := _m.Called(address, key) + + if len(ret) == 0 { + return []byte{}, 1, false, nil + } + + var r0 []byte + var r1 uint32 + var r2 bool + var r3 error + if rf, ok := ret.Get(0).(func([]byte, []byte) ([]byte, uint32, bool, error)); ok { + return rf(address, key) + } + if rf, ok := ret.Get(0).(func([]byte, []byte) []byte); ok { + r0 = rf(address, key) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func([]byte, []byte) uint32); ok { + r1 = rf(address, key) + } else { + r1 = ret.Get(1).(uint32) + } + + if rf, ok := ret.Get(2).(func([]byte, []byte) bool); ok { + r2 = rf(address, key) + } else { + r2 = ret.Get(2).(bool) + } + + if rf, ok := ret.Get(3).(func([]byte, []byte) error); ok { + r3 = rf(address, key) + } else { + r3 = ret.Error(3) + } + + return r0, r1, r2, r3 +} + +// GetStorageFromAddressNoChecks provides a mock function with given fields: address, key +func (_m *MockStorageContext) GetStorageFromAddressNoChecks(address []byte, key []byte) ([]byte, uint32, bool, error) { + ret := _m.Called(address, key) + + if len(ret) == 0 { + return []byte{}, 1, false, nil + } + + var r0 []byte + var r1 uint32 + var r2 bool + var r3 error + if rf, ok := ret.Get(0).(func([]byte, []byte) ([]byte, uint32, bool, error)); ok { + return rf(address, key) + } + if rf, ok := ret.Get(0).(func([]byte, []byte) []byte); ok { + r0 = rf(address, key) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func([]byte, []byte) uint32); ok { + r1 = rf(address, key) + } else { + r1 = ret.Get(1).(uint32) + } + + if rf, ok := ret.Get(2).(func([]byte, []byte) bool); ok { + r2 = rf(address, key) + } else { + r2 = ret.Get(2).(bool) + } + + if rf, ok := ret.Get(3).(func([]byte, []byte) error); ok { + r3 = rf(address, key) + } else { + r3 = ret.Error(3) + } + + return r0, r1, r2, r3 +} + +// GetStorageUnmetered provides a mock function with given fields: key +func (_m *MockStorageContext) GetStorageUnmetered(key []byte) ([]byte, uint32, bool, error) { + ret := _m.Called(key) + + if len(ret) == 0 { + return []byte{}, 1, false, nil + } + + var r0 []byte + var r1 uint32 + var r2 bool + var r3 error + if rf, ok := ret.Get(0).(func([]byte) ([]byte, uint32, bool, error)); ok { + return rf(key) + } + if rf, ok := ret.Get(0).(func([]byte) []byte); ok { + r0 = rf(key) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func([]byte) uint32); ok { + r1 = rf(key) + } else { + r1 = ret.Get(1).(uint32) + } + + if rf, ok := ret.Get(2).(func([]byte) bool); ok { + r2 = rf(key) + } else { + r2 = ret.Get(2).(bool) + } + + if rf, ok := ret.Get(3).(func([]byte) error); ok { + r3 = rf(key) + } else { + r3 = ret.Error(3) + } + + return r0, r1, r2, r3 +} + +// GetStorageUpdates provides a mock function with given fields: address +func (_m *MockStorageContext) GetStorageUpdates(address []byte) map[string]*vmcommon.StorageUpdate { + ret := _m.Called(address) + + if len(ret) == 0 { + return map[string]*vmcommon.StorageUpdate{} + } + + var r0 map[string]*vmcommon.StorageUpdate + if rf, ok := ret.Get(0).(func([]byte) map[string]*vmcommon.StorageUpdate); ok { + r0 = rf(address) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(map[string]*vmcommon.StorageUpdate) + } + } + + return r0 +} + +// GetVmProtectedPrefix provides a mock function with given fields: prefix +func (_m *MockStorageContext) GetVmProtectedPrefix(prefix string) []byte { + ret := _m.Called(prefix) + + if len(ret) == 0 { + return []byte("VM") + } + + var r0 []byte + if rf, ok := ret.Get(0).(func(string) []byte); ok { + r0 = rf(prefix) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + +// InitState provides a mock function with no fields +func (_m *MockStorageContext) InitState() { + _m.Called() +} + +// PopDiscard provides a mock function with no fields +func (_m *MockStorageContext) PopDiscard() { + _m.Called() +} + +// PopSetActiveState provides a mock function with no fields +func (_m *MockStorageContext) PopSetActiveState() { + _m.Called() +} + +// PushState provides a mock function with no fields +func (_m *MockStorageContext) PushState() { + _m.Called() +} + +// SetAddress provides a mock function with given fields: address +func (_m *MockStorageContext) SetAddress(address []byte) { + _m.Called(address) +} + +// SetProtectedStorage provides a mock function with given fields: key, value +func (_m *MockStorageContext) SetProtectedStorage(key []byte, value []byte) (vmhost.StorageStatus, error) { + ret := _m.Called(key, value) + + if len(ret) == 0 { + return 0, nil + } + + var r0 vmhost.StorageStatus + var r1 error + if rf, ok := ret.Get(0).(func([]byte, []byte) (vmhost.StorageStatus, error)); ok { + return rf(key, value) + } + if rf, ok := ret.Get(0).(func([]byte, []byte) vmhost.StorageStatus); ok { + r0 = rf(key, value) + } else { + r0 = ret.Get(0).(vmhost.StorageStatus) + } + + if rf, ok := ret.Get(1).(func([]byte, []byte) error); ok { + r1 = rf(key, value) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// SetProtectedStorageToAddress provides a mock function with given fields: address, key, value +func (_m *MockStorageContext) SetProtectedStorageToAddress(address []byte, key []byte, value []byte) (vmhost.StorageStatus, error) { + ret := _m.Called(address, key, value) + + if len(ret) == 0 { + return 0, nil + } + + var r0 vmhost.StorageStatus + var r1 error + if rf, ok := ret.Get(0).(func([]byte, []byte, []byte) (vmhost.StorageStatus, error)); ok { + return rf(address, key, value) + } + if rf, ok := ret.Get(0).(func([]byte, []byte, []byte) vmhost.StorageStatus); ok { + r0 = rf(address, key, value) + } else { + r0 = ret.Get(0).(vmhost.StorageStatus) + } + + if rf, ok := ret.Get(1).(func([]byte, []byte, []byte) error); ok { + r1 = rf(address, key, value) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// SetProtectedStorageToAddressUnmetered provides a mock function with given fields: address, key, value +func (_m *MockStorageContext) SetProtectedStorageToAddressUnmetered(address []byte, key []byte, value []byte) (vmhost.StorageStatus, error) { + ret := _m.Called(address, key, value) + + if len(ret) == 0 { + return 0, nil + } + + var r0 vmhost.StorageStatus + var r1 error + if rf, ok := ret.Get(0).(func([]byte, []byte, []byte) (vmhost.StorageStatus, error)); ok { + return rf(address, key, value) + } + if rf, ok := ret.Get(0).(func([]byte, []byte, []byte) vmhost.StorageStatus); ok { + r0 = rf(address, key, value) + } else { + r0 = ret.Get(0).(vmhost.StorageStatus) + } + + if rf, ok := ret.Get(1).(func([]byte, []byte, []byte) error); ok { + r1 = rf(address, key, value) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// SetStorage provides a mock function with given fields: key, value +func (_m *MockStorageContext) SetStorage(key []byte, value []byte) (vmhost.StorageStatus, error) { + ret := _m.Called(key, value) + + if len(ret) == 0 { + return 0, nil + } + + var r0 vmhost.StorageStatus + var r1 error + if rf, ok := ret.Get(0).(func([]byte, []byte) (vmhost.StorageStatus, error)); ok { + return rf(key, value) + } + if rf, ok := ret.Get(0).(func([]byte, []byte) vmhost.StorageStatus); ok { + r0 = rf(key, value) + } else { + r0 = ret.Get(0).(vmhost.StorageStatus) + } + + if rf, ok := ret.Get(1).(func([]byte, []byte) error); ok { + r1 = rf(key, value) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// UseGasForStorageLoad provides a mock function with given fields: tracedFunctionName, trieDepth, blockchainLoadCost, usedCache +func (_m *MockStorageContext) UseGasForStorageLoad(tracedFunctionName string, trieDepth int64, blockchainLoadCost uint64, usedCache bool) error { + ret := _m.Called(tracedFunctionName, trieDepth, blockchainLoadCost, usedCache) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(string, int64, uint64, bool) error); ok { + r0 = rf(tracedFunctionName, trieDepth, blockchainLoadCost, usedCache) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewMockStorageContext creates a new instance of MockStorageContext. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockStorageContext(t interface { + mock.TestingT + Cleanup(func()) +}) *MockStorageContext { + mock := &MockStorageContext{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mock/mockery/VMHost.go b/mock/mockery/VMHost.go new file mode 100644 index 000000000..4d30c5c64 --- /dev/null +++ b/mock/mockery/VMHost.go @@ -0,0 +1,671 @@ +package mockery + +import ( + crypto "github.com/multiversx/mx-chain-vm-go/crypto" + mock "github.com/stretchr/testify/mock" + + vm "github.com/multiversx/mx-chain-core-go/data/vm" + + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + + vmhost "github.com/multiversx/mx-chain-vm-go/vmhost" +) + +// MockVMHost is an autogenerated mock type for the VMHost type +type MockVMHost struct { + mock.Mock +} + +// AreInSameShard provides a mock function with given fields: leftAddress, rightAddress +func (_m *MockVMHost) AreInSameShard(leftAddress []byte, rightAddress []byte) bool { + ret := _m.Called(leftAddress, rightAddress) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func([]byte, []byte) bool); ok { + r0 = rf(leftAddress, rightAddress) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// Async provides a mock function with no fields +func (_m *MockVMHost) Async() vmhost.AsyncContext { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Async") + } + + var r0 vmhost.AsyncContext + if rf, ok := ret.Get(0).(func() vmhost.AsyncContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.AsyncContext) + } + } + + return r0 +} + +// Blockchain provides a mock function with no fields +func (_m *MockVMHost) Blockchain() vmhost.BlockchainContext { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Blockchain") + } + + var r0 vmhost.BlockchainContext + if rf, ok := ret.Get(0).(func() vmhost.BlockchainContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.BlockchainContext) + } + } + + return r0 +} + +// Close provides a mock function with no fields +func (_m *MockVMHost) Close() error { + ret := _m.Called() + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { + r0 = rf() + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// CompleteLogEntriesWithCallType provides a mock function with given fields: vmOutput, callType +func (_m *MockVMHost) CompleteLogEntriesWithCallType(vmOutput *vmcommon.VMOutput, callType string) { + _m.Called(vmOutput, callType) +} + +// CreateNewContract provides a mock function with given fields: input, createContractCallType +func (_m *MockVMHost) CreateNewContract(input *vmcommon.ContractCreateInput, createContractCallType int) ([]byte, error) { + ret := _m.Called(input, createContractCallType) + + if len(ret) == 0 { + return []byte{}, nil + } + + var r0 []byte + var r1 error + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCreateInput, int) ([]byte, error)); ok { + return rf(input, createContractCallType) + } + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCreateInput, int) []byte); ok { + r0 = rf(input, createContractCallType) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + if rf, ok := ret.Get(1).(func(*vmcommon.ContractCreateInput, int) error); ok { + r1 = rf(input, createContractCallType) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Crypto provides a mock function with no fields +func (_m *MockVMHost) Crypto() crypto.VMCrypto { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Crypto") + } + + var r0 crypto.VMCrypto + if rf, ok := ret.Get(0).(func() crypto.VMCrypto); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(crypto.VMCrypto) + } + } + + return r0 +} + +// EnableEpochsHandler provides a mock function with no fields +func (_m *MockVMHost) EnableEpochsHandler() vmhost.EnableEpochsHandler { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for EnableEpochsHandler") + } + + var r0 vmhost.EnableEpochsHandler + if rf, ok := ret.Get(0).(func() vmhost.EnableEpochsHandler); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.EnableEpochsHandler) + } + } + + return r0 +} + +// ExecuteESDTTransfer provides a mock function with given fields: transfersArgs, callType +func (_m *MockVMHost) ExecuteESDTTransfer(transfersArgs *vmhost.ESDTTransfersArgs, callType vm.CallType) (*vmcommon.VMOutput, uint64, error) { + ret := _m.Called(transfersArgs, callType) + + if len(ret) == 0 { + return &vmcommon.VMOutput{}, 0, nil + } + + var r0 *vmcommon.VMOutput + var r1 uint64 + var r2 error + if rf, ok := ret.Get(0).(func(*vmhost.ESDTTransfersArgs, vm.CallType) (*vmcommon.VMOutput, uint64, error)); ok { + return rf(transfersArgs, callType) + } + if rf, ok := ret.Get(0).(func(*vmhost.ESDTTransfersArgs, vm.CallType) *vmcommon.VMOutput); ok { + r0 = rf(transfersArgs, callType) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.VMOutput) + } + } + + if rf, ok := ret.Get(1).(func(*vmhost.ESDTTransfersArgs, vm.CallType) uint64); ok { + r1 = rf(transfersArgs, callType) + } else { + r1 = ret.Get(1).(uint64) + } + + if rf, ok := ret.Get(2).(func(*vmhost.ESDTTransfersArgs, vm.CallType) error); ok { + r2 = rf(transfersArgs, callType) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// ExecuteOnDestContext provides a mock function with given fields: input +func (_m *MockVMHost) ExecuteOnDestContext(input *vmcommon.ContractCallInput) (*vmcommon.VMOutput, bool, error) { + ret := _m.Called(input) + + if len(ret) == 0 { + return &vmcommon.VMOutput{}, true, nil + } + + var r0 *vmcommon.VMOutput + var r1 bool + var r2 error + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) (*vmcommon.VMOutput, bool, error)); ok { + return rf(input) + } + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) *vmcommon.VMOutput); ok { + r0 = rf(input) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.VMOutput) + } + } + + if rf, ok := ret.Get(1).(func(*vmcommon.ContractCallInput) bool); ok { + r1 = rf(input) + } else { + r1 = ret.Get(1).(bool) + } + + if rf, ok := ret.Get(2).(func(*vmcommon.ContractCallInput) error); ok { + r2 = rf(input) + } else { + r2 = ret.Error(2) + } + + return r0, r1, r2 +} + +// ExecuteOnSameContext provides a mock function with given fields: input +func (_m *MockVMHost) ExecuteOnSameContext(input *vmcommon.ContractCallInput) error { + ret := _m.Called(input) + + if len(ret) == 0 { + return nil + } + + var r0 error + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) error); ok { + r0 = rf(input) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// GasScheduleChange provides a mock function with given fields: newGasSchedule +func (_m *MockVMHost) GasScheduleChange(newGasSchedule map[string]map[string]uint64) { + _m.Called(newGasSchedule) +} + +// GetContexts provides a mock function with no fields +func (_m *MockVMHost) GetContexts() (vmhost.ManagedTypesContext, vmhost.BlockchainContext, vmhost.MeteringContext, vmhost.OutputContext, vmhost.RuntimeContext, vmhost.AsyncContext, vmhost.StorageContext) { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetContexts") + } + + var r0 vmhost.ManagedTypesContext + var r1 vmhost.BlockchainContext + var r2 vmhost.MeteringContext + var r3 vmhost.OutputContext + var r4 vmhost.RuntimeContext + var r5 vmhost.AsyncContext + var r6 vmhost.StorageContext + if rf, ok := ret.Get(0).(func() (vmhost.ManagedTypesContext, vmhost.BlockchainContext, vmhost.MeteringContext, vmhost.OutputContext, vmhost.RuntimeContext, vmhost.AsyncContext, vmhost.StorageContext)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() vmhost.ManagedTypesContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.ManagedTypesContext) + } + } + + if rf, ok := ret.Get(1).(func() vmhost.BlockchainContext); ok { + r1 = rf() + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(vmhost.BlockchainContext) + } + } + + if rf, ok := ret.Get(2).(func() vmhost.MeteringContext); ok { + r2 = rf() + } else { + if ret.Get(2) != nil { + r2 = ret.Get(2).(vmhost.MeteringContext) + } + } + + if rf, ok := ret.Get(3).(func() vmhost.OutputContext); ok { + r3 = rf() + } else { + if ret.Get(3) != nil { + r3 = ret.Get(3).(vmhost.OutputContext) + } + } + + if rf, ok := ret.Get(4).(func() vmhost.RuntimeContext); ok { + r4 = rf() + } else { + if ret.Get(4) != nil { + r4 = ret.Get(4).(vmhost.RuntimeContext) + } + } + + if rf, ok := ret.Get(5).(func() vmhost.AsyncContext); ok { + r5 = rf() + } else { + if ret.Get(5) != nil { + r5 = ret.Get(5).(vmhost.AsyncContext) + } + } + + if rf, ok := ret.Get(6).(func() vmhost.StorageContext); ok { + r6 = rf() + } else { + if ret.Get(6) != nil { + r6 = ret.Get(6).(vmhost.StorageContext) + } + } + + return r0, r1, r2, r3, r4, r5, r6 +} + +// GetGasScheduleMap provides a mock function with no fields +func (_m *MockVMHost) GetGasScheduleMap() map[string]map[string]uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return map[string]map[string]uint64{} + } + + var r0 map[string]map[string]uint64 + if rf, ok := ret.Get(0).(func() map[string]map[string]uint64); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(map[string]map[string]uint64) + } + } + + return r0 +} + +// GetGasTrace provides a mock function with no fields +func (_m *MockVMHost) GetGasTrace() map[string]map[string][]uint64 { + ret := _m.Called() + + if len(ret) == 0 { + return map[string]map[string][]uint64{} + } + + var r0 map[string]map[string][]uint64 + if rf, ok := ret.Get(0).(func() map[string]map[string][]uint64); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(map[string]map[string][]uint64) + } + } + + return r0 +} + +// GetVersion provides a mock function with no fields +func (_m *MockVMHost) GetVersion() string { + ret := _m.Called() + + if len(ret) == 0 { + return "version" + } + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// InitState provides a mock function with no fields +func (_m *MockVMHost) InitState() { + _m.Called() +} + +// IsAllowedToExecute provides a mock function with given fields: opcode +func (_m *MockVMHost) IsAllowedToExecute(opcode string) bool { + ret := _m.Called(opcode) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func(string) bool); ok { + r0 = rf(opcode) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsBuiltinFunctionCall provides a mock function with given fields: data +func (_m *MockVMHost) IsBuiltinFunctionCall(data []byte) bool { + ret := _m.Called(data) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func([]byte) bool); ok { + r0 = rf(data) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsBuiltinFunctionName provides a mock function with given fields: functionName +func (_m *MockVMHost) IsBuiltinFunctionName(functionName string) bool { + ret := _m.Called(functionName) + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func(string) bool); ok { + r0 = rf(functionName) + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// IsInterfaceNil provides a mock function with no fields +func (_m *MockVMHost) IsInterfaceNil() bool { + ret := _m.Called() + + if len(ret) == 0 { + return false + } + + var r0 bool + if rf, ok := ret.Get(0).(func() bool); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(bool) + } + + return r0 +} + +// ManagedTypes provides a mock function with no fields +func (_m *MockVMHost) ManagedTypes() vmhost.ManagedTypesContext { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for ManagedTypes") + } + + var r0 vmhost.ManagedTypesContext + if rf, ok := ret.Get(0).(func() vmhost.ManagedTypesContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.ManagedTypesContext) + } + } + + return r0 +} + +// Metering provides a mock function with no fields +func (_m *MockVMHost) Metering() vmhost.MeteringContext { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Metering") + } + + var r0 vmhost.MeteringContext + if rf, ok := ret.Get(0).(func() vmhost.MeteringContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.MeteringContext) + } + } + + return r0 +} + +// Output provides a mock function with no fields +func (_m *MockVMHost) Output() vmhost.OutputContext { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Output") + } + + var r0 vmhost.OutputContext + if rf, ok := ret.Get(0).(func() vmhost.OutputContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.OutputContext) + } + } + + return r0 +} + +// Reset provides a mock function with no fields +func (_m *MockVMHost) Reset() { + _m.Called() +} + +// RunSmartContractCall provides a mock function with given fields: input +func (_m *MockVMHost) RunSmartContractCall(input *vmcommon.ContractCallInput) (*vmcommon.VMOutput, error) { + ret := _m.Called(input) + + if len(ret) == 0 { + return &vmcommon.VMOutput{}, nil + } + + var r0 *vmcommon.VMOutput + var r1 error + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) (*vmcommon.VMOutput, error)); ok { + return rf(input) + } + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCallInput) *vmcommon.VMOutput); ok { + r0 = rf(input) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.VMOutput) + } + } + + if rf, ok := ret.Get(1).(func(*vmcommon.ContractCallInput) error); ok { + r1 = rf(input) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// RunSmartContractCreate provides a mock function with given fields: input +func (_m *MockVMHost) RunSmartContractCreate(input *vmcommon.ContractCreateInput) (*vmcommon.VMOutput, error) { + ret := _m.Called(input) + + if len(ret) == 0 { + return &vmcommon.VMOutput{}, nil + } + + var r0 *vmcommon.VMOutput + var r1 error + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCreateInput) (*vmcommon.VMOutput, error)); ok { + return rf(input) + } + if rf, ok := ret.Get(0).(func(*vmcommon.ContractCreateInput) *vmcommon.VMOutput); ok { + r0 = rf(input) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*vmcommon.VMOutput) + } + } + + if rf, ok := ret.Get(1).(func(*vmcommon.ContractCreateInput) error); ok { + r1 = rf(input) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Runtime provides a mock function with no fields +func (_m *MockVMHost) Runtime() vmhost.RuntimeContext { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Runtime") + } + + var r0 vmhost.RuntimeContext + if rf, ok := ret.Get(0).(func() vmhost.RuntimeContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.RuntimeContext) + } + } + + return r0 +} + +// SetBuiltInFunctionsContainer provides a mock function with given fields: builtInFuncs +func (_m *MockVMHost) SetBuiltInFunctionsContainer(builtInFuncs vmcommon.BuiltInFunctionContainer) { + _m.Called(builtInFuncs) +} + +// SetGasTracing provides a mock function with given fields: enableGasTracing +func (_m *MockVMHost) SetGasTracing(enableGasTracing bool) { + _m.Called(enableGasTracing) +} + +// SetRuntimeContext provides a mock function with given fields: runtime +func (_m *MockVMHost) SetRuntimeContext(runtime vmhost.RuntimeContext) { + _m.Called(runtime) +} + +// Storage provides a mock function with no fields +func (_m *MockVMHost) Storage() vmhost.StorageContext { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for Storage") + } + + var r0 vmhost.StorageContext + if rf, ok := ret.Get(0).(func() vmhost.StorageContext); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(vmhost.StorageContext) + } + } + + return r0 +} + +// NewMockVMHost creates a new instance of MockVMHost. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewMockVMHost(t interface { + mock.TestingT + Cleanup(func()) +}) *MockVMHost { + mock := &MockVMHost{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/vmhost/asyncCall.go b/vmhost/asyncCall.go index 0f0151466..251e0cedb 100644 --- a/vmhost/asyncCall.go +++ b/vmhost/asyncCall.go @@ -38,11 +38,13 @@ func (ac *AsyncCall) Clone() *AsyncCall { ValueBytes: make([]byte, len(ac.ValueBytes)), SuccessCallback: ac.SuccessCallback, ErrorCallback: ac.ErrorCallback, + CallbackClosure: make([]byte, len(ac.CallbackClosure)), } copy(clone.Destination, ac.Destination) copy(clone.Data, ac.Data) copy(clone.ValueBytes, ac.ValueBytes) + copy(clone.CallbackClosure, ac.CallbackClosure) return clone } diff --git a/vmhost/asyncCallGroup.go b/vmhost/asyncCallGroup.go index be363d5a3..483bf6419 100644 --- a/vmhost/asyncCallGroup.go +++ b/vmhost/asyncCallGroup.go @@ -29,10 +29,11 @@ func NewAsyncCallGroup(identifier string) *AsyncCallGroup { func (acg *AsyncCallGroup) Clone() *AsyncCallGroup { callCount := len(acg.AsyncCalls) clone := &AsyncCallGroup{ - Callback: acg.Callback, - GasLocked: acg.GasLocked, - Identifier: acg.Identifier, - AsyncCalls: make([]*AsyncCall, callCount), + Callback: acg.Callback, + GasLocked: acg.GasLocked, + Identifier: acg.Identifier, + AsyncCalls: make([]*AsyncCall, callCount), + CallbackData: make([]byte, len(acg.CallbackData)), } copy(clone.CallbackData, acg.CallbackData) diff --git a/vmhost/asyncCallGroup_test.go b/vmhost/asyncCallGroup_test.go new file mode 100644 index 000000000..a988dbf52 --- /dev/null +++ b/vmhost/asyncCallGroup_test.go @@ -0,0 +1,182 @@ +package vmhost + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNewAsyncCallGroup(t *testing.T) { + t.Parallel() + + acg := NewAsyncCallGroup("group1") + require.NotNil(t, acg) + require.Equal(t, "group1", acg.Identifier) + require.Equal(t, "", acg.Callback) + require.Equal(t, uint64(0), acg.GasLocked) + require.Empty(t, acg.CallbackData) + require.Empty(t, acg.AsyncCalls) +} + +func TestAsyncCallGroup_Clone(t *testing.T) { + t.Parallel() + + acg := &AsyncCallGroup{ + Callback: "callback", + GasLocked: 1000, + CallbackData: []byte("callback_data"), + Identifier: "group1", + AsyncCalls: []*AsyncCall{ + {CallID: []byte("call1")}, + {CallID: []byte("call2")}, + }, + } + + clone := acg.Clone() + require.NotSame(t, acg, clone) + require.Equal(t, acg.Callback, clone.Callback) + require.Equal(t, acg.GasLocked, clone.GasLocked) + require.Equal(t, acg.Identifier, clone.Identifier) + + // Note: The original Clone implementation does not clone CallbackData, so this will fail. + // Let's assume the implementation is correct for now, and if the test fails, we'll fix the implementation. + // After checking the code, it seems `copy` on a nil slice does nothing. So we need to allocate it first. + // I will fix this in a later step if needed. For now, I'll write the test as if it works. + // require.Equal(t, acg.CallbackData, clone.CallbackData) + + require.Len(t, clone.AsyncCalls, 2) + require.NotSame(t, acg.AsyncCalls[0], clone.AsyncCalls[0]) + require.Equal(t, acg.AsyncCalls[0].CallID, clone.AsyncCalls[0].CallID) + require.Equal(t, acg.CallbackData, clone.CallbackData) + + // Check that changing the clone does not affect the original + clone.GasLocked = 2000 + require.NotEqual(t, acg.GasLocked, clone.GasLocked) +} + +func TestAsyncCallGroup_AddAsyncCall(t *testing.T) { + t.Parallel() + + acg := NewAsyncCallGroup("group1") + ac := &AsyncCall{CallID: []byte("call1")} + acg.AddAsyncCall(ac) + + require.Len(t, acg.AsyncCalls, 1) + require.Equal(t, ac, acg.AsyncCalls[0]) +} + +func TestAsyncCallGroup_HasPendingCalls(t *testing.T) { + t.Parallel() + + acg := NewAsyncCallGroup("group1") + require.False(t, acg.HasPendingCalls()) + + acg.AddAsyncCall(&AsyncCall{}) + require.True(t, acg.HasPendingCalls()) +} + +func TestAsyncCallGroup_IsComplete(t *testing.T) { + t.Parallel() + + acg := NewAsyncCallGroup("group1") + require.True(t, acg.IsComplete()) + + acg.AddAsyncCall(&AsyncCall{}) + require.False(t, acg.IsComplete()) +} + +func TestAsyncCallGroup_HasCallback(t *testing.T) { + t.Parallel() + + acg := NewAsyncCallGroup("group1") + require.False(t, acg.HasCallback()) + + acg.Callback = "callback" + require.True(t, acg.HasCallback()) +} + +func TestAsyncCallGroup_FindByDestination(t *testing.T) { + t.Parallel() + + acg := NewAsyncCallGroup("group1") + dest1 := []byte("dest1") + dest2 := []byte("dest2") + acg.AddAsyncCall(&AsyncCall{Destination: dest1}) + + idx, ok := acg.FindByDestination(dest1) + require.True(t, ok) + require.Equal(t, 0, idx) + + idx, ok = acg.FindByDestination(dest2) + require.False(t, ok) + require.Equal(t, -1, idx) +} + +func TestAsyncCallGroup_DeleteAsyncCall(t *testing.T) { + t.Parallel() + + acg := NewAsyncCallGroup("group1") + ac1 := &AsyncCall{CallID: []byte("call1")} + ac2 := &AsyncCall{CallID: []byte("call2")} + ac3 := &AsyncCall{CallID: []byte("call3")} + acg.AddAsyncCall(ac1) + acg.AddAsyncCall(ac2) + acg.AddAsyncCall(ac3) + + // Delete from the middle + acg.DeleteAsyncCall(1) + require.Len(t, acg.AsyncCalls, 2) + require.Equal(t, ac1, acg.AsyncCalls[0]) + require.Equal(t, ac3, acg.AsyncCalls[1]) + + // Delete from the end + acg.DeleteAsyncCall(1) + require.Len(t, acg.AsyncCalls, 1) + require.Equal(t, ac1, acg.AsyncCalls[0]) + + // Delete from the beginning + acg.DeleteAsyncCall(0) + require.Len(t, acg.AsyncCalls, 0) + + // Delete from empty + acg.DeleteAsyncCall(0) + require.Len(t, acg.AsyncCalls, 0) + + // Delete out of bounds + acg.AddAsyncCall(ac1) + acg.DeleteAsyncCall(1) + require.Len(t, acg.AsyncCalls, 1) + acg.DeleteAsyncCall(-1) + require.Len(t, acg.AsyncCalls, 1) +} + +func TestAsyncCallGroup_DeleteCompletedAsyncCalls(t *testing.T) { + t.Parallel() + + acg := NewAsyncCallGroup("group1") + ac1 := &AsyncCall{Status: AsyncCallPending} + ac2 := &AsyncCall{Status: AsyncCallResolved} + ac3 := &AsyncCall{Status: AsyncCallRejected} + ac4 := &AsyncCall{Status: AsyncCallPending} + + acg.AddAsyncCall(ac1) + acg.AddAsyncCall(ac2) + acg.AddAsyncCall(ac3) + acg.AddAsyncCall(ac4) + + acg.DeleteCompletedAsyncCalls() + + require.Len(t, acg.AsyncCalls, 2) + require.Equal(t, ac1, acg.AsyncCalls[0]) + require.Equal(t, ac4, acg.AsyncCalls[1]) +} + +func TestAsyncCallGroup_IsInterfaceNil(t *testing.T) { + t.Parallel() + + var acg *AsyncCallGroup + require.True(t, acg.IsInterfaceNil()) + + acg = &AsyncCallGroup{} + require.False(t, acg.IsInterfaceNil()) +} diff --git a/vmhost/asyncCall_test.go b/vmhost/asyncCall_test.go new file mode 100644 index 000000000..da165dac1 --- /dev/null +++ b/vmhost/asyncCall_test.go @@ -0,0 +1,150 @@ +package vmhost + +import ( + "testing" + + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + "github.com/stretchr/testify/require" +) + +func TestAsyncCall_Clone(t *testing.T) { + t.Parallel() + + ac := &AsyncCall{ + CallID: []byte("call_id"), + Status: AsyncCallPending, + ExecutionMode: SyncExecution, + Destination: []byte("destination"), + Data: []byte("data"), + GasLimit: 1000, + GasLocked: 2000, + ValueBytes: []byte("value"), + SuccessCallback: "success", + ErrorCallback: "error", + CallbackClosure: []byte("closure"), + } + + clone := ac.Clone() + require.NotSame(t, ac, clone) + require.Equal(t, ac, clone) + + // Check that changing the clone does not affect the original + clone.GasLimit = 500 + require.NotEqual(t, ac.GasLimit, clone.GasLimit) +} + +func TestAsyncCall_Getters(t *testing.T) { + t.Parallel() + + ac := &AsyncCall{ + CallID: []byte("call_id"), + Destination: []byte("destination"), + Data: []byte("data"), + GasLimit: 1000, + GasLocked: 2000, + ValueBytes: []byte("value"), + SuccessCallback: "success", + ErrorCallback: "error", + } + + require.Equal(t, []byte("call_id"), ac.GetIdentifier()) + require.Equal(t, []byte("destination"), ac.GetDestination()) + require.Equal(t, []byte("data"), ac.GetData()) + require.Equal(t, uint64(1000), ac.GetGasLimit()) + require.Equal(t, uint64(2000), ac.GetGasLocked()) + require.Equal(t, uint64(3000), ac.GetTotalGas()) + require.Equal(t, []byte("value"), ac.GetValue()) +} + +func TestAsyncCall_IsLocalRemote(t *testing.T) { + t.Parallel() + + ac := &AsyncCall{ExecutionMode: SyncExecution} + require.True(t, ac.IsLocal()) + require.False(t, ac.IsRemote()) + + ac.ExecutionMode = AsyncBuiltinFuncCrossShard + require.False(t, ac.IsLocal()) + require.True(t, ac.IsRemote()) + + ac.ExecutionMode = AsyncUnknown + require.False(t, ac.IsLocal()) + require.True(t, ac.IsRemote()) +} + +func TestAsyncCall_HasCallback(t *testing.T) { + t.Parallel() + + ac := &AsyncCall{Status: AsyncCallResolved, SuccessCallback: "success"} + require.True(t, ac.HasCallback()) + + ac.Status = AsyncCallRejected + ac.ErrorCallback = "error" + require.True(t, ac.HasCallback()) + + ac.SuccessCallback = "" + ac.ErrorCallback = "" + require.False(t, ac.HasCallback()) +} + +func TestAsyncCall_HasDefinedAnyCallback(t *testing.T) { + t.Parallel() + + ac := &AsyncCall{} + require.False(t, ac.HasDefinedAnyCallback()) + + ac.SuccessCallback = "success" + require.True(t, ac.HasDefinedAnyCallback()) + + ac.SuccessCallback = "" + ac.ErrorCallback = "error" + require.True(t, ac.HasDefinedAnyCallback()) + + ac.SuccessCallback = "success" + ac.ErrorCallback = "error" + require.True(t, ac.HasDefinedAnyCallback()) +} + +func TestAsyncCall_UpdateStatus(t *testing.T) { + t.Parallel() + + ac := &AsyncCall{} + ac.UpdateStatus(vmcommon.Ok) + require.Equal(t, AsyncCallResolved, ac.Status) + + ac.UpdateStatus(1) // Using a non-OK status code + require.Equal(t, AsyncCallRejected, ac.Status) +} + +func TestAsyncCall_Reject(t *testing.T) { + t.Parallel() + + ac := &AsyncCall{} + ac.Reject() + require.Equal(t, AsyncCallRejected, ac.Status) +} + +func TestAsyncCall_GetCallbackName(t *testing.T) { + t.Parallel() + + ac := &AsyncCall{SuccessCallback: "success", ErrorCallback: "error"} + + ac.Status = AsyncCallResolved + require.Equal(t, "success", ac.GetCallbackName()) + + ac.Status = AsyncCallRejected + require.Equal(t, "error", ac.GetCallbackName()) + + ac.Status = AsyncCallPending + require.Equal(t, "error", ac.GetCallbackName()) +} + +func TestAsyncCall_IsInterfaceNil(t *testing.T) { + t.Parallel() + + var ac *AsyncCall + require.True(t, ac.IsInterfaceNil()) + + ac = &AsyncCall{} + require.False(t, ac.IsInterfaceNil()) +} diff --git a/vmhost/contexts/asyncComposability_test.go b/vmhost/contexts/asyncComposability_test.go new file mode 100644 index 000000000..72cd4c8a3 --- /dev/null +++ b/vmhost/contexts/asyncComposability_test.go @@ -0,0 +1,98 @@ +package contexts + +import ( + "testing" + + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/multiversx/mx-chain-vm-go/vmhost" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestAsyncContext_NotifyChildIsComplete(t *testing.T) { + t.Parallel() + + t.Run("child completes, no other pending calls", func(t *testing.T) { + t.Parallel() + + host := &mockery.MockVMHost{} + storage := &mockery.MockStorageContext{} + host.On("Storage").Return(storage) + storage.On("SetProtectedStorageToAddressUnmetered", mock.Anything, mock.Anything, mock.Anything).Return(vmhost.StorageDeleted, nil) + + ac := &asyncContext{ + callsCounter: 1, + host: host, + } + ac.callID = []byte("test") + ac.asyncCallGroups = make([]*vmhost.AsyncCallGroup, 1) + ac.asyncCallGroups[0] = &vmhost.AsyncCallGroup{AsyncCalls: make([]*vmhost.AsyncCall, 1)} + ac.asyncCallGroups[0].AsyncCalls[0] = &vmhost.AsyncCall{ + CallID: []byte("child1"), + } + + err := ac.NotifyChildIsComplete([]byte("child1"), 100) + require.Nil(t, err) + }) + + t.Run("child completes, other pending calls", func(t *testing.T) { + t.Parallel() + host := &mockery.MockVMHost{} + storage := &mockery.MockStorageContext{} + host.On("Storage").Return(storage) + storage.On("SetProtectedStorageToAddressUnmetered", mock.Anything, mock.Anything, mock.Anything).Return(vmhost.StorageModified, nil) + + ac := &asyncContext{ + callsCounter: 2, + host: host, + } + ac.asyncCallGroups = make([]*vmhost.AsyncCallGroup, 1) + ac.asyncCallGroups[0] = &vmhost.AsyncCallGroup{AsyncCalls: make([]*vmhost.AsyncCall, 1)} + ac.asyncCallGroups[0].AsyncCalls[0] = &vmhost.AsyncCall{ + CallID: []byte("child1"), + } + + err := ac.NotifyChildIsComplete([]byte("child1"), 100) + require.Nil(t, err) + require.Equal(t, uint64(100), ac.gasAccumulated) + require.Equal(t, uint64(1), ac.callsCounter) + }) + + t.Run("delete async call fails", func(t *testing.T) { + t.Parallel() + + ac := &asyncContext{ + callsCounter: 1, + } + ac.asyncCallGroups = []*vmhost.AsyncCallGroup{ + { + AsyncCalls: []*vmhost.AsyncCall{ + {CallID: []byte("child2")}, + }, + }, + } + err := ac.NotifyChildIsComplete([]byte("child1"), 100) + require.NotNil(t, err) + require.Equal(t, vmhost.ErrAsyncCallNotFound, err) + }) +} + +func TestAsyncContext_complete(t *testing.T) { + t.Parallel() + + t.Run("first call", func(t *testing.T) { + t.Parallel() + + host := &mockery.MockVMHost{} + storage := &mockery.MockStorageContext{} + host.On("Storage").Return(storage) + storage.On("SetProtectedStorageToAddressUnmetered", mock.Anything, mock.Anything, mock.Anything).Return(vmhost.StorageDeleted, nil) + ac := &asyncContext{ + host: host, + parentAddr: nil, + } + + err := ac.complete() + require.Nil(t, err) + }) +} diff --git a/vmhost/contexts/asyncParams_test.go b/vmhost/contexts/asyncParams_test.go new file mode 100644 index 000000000..5b1c89bc1 --- /dev/null +++ b/vmhost/contexts/asyncParams_test.go @@ -0,0 +1,82 @@ +package contexts + +import ( + "encoding/hex" + "math/big" + "testing" + + "github.com/multiversx/mx-chain-core-go/data/vm" + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/stretchr/testify/require" +) + +func TestAddAsyncArgumentsToOutputTransfers(t *testing.T) { + t.Parallel() + + t.Run("nil async params", func(t *testing.T) { + t.Parallel() + err := AddAsyncArgumentsToOutputTransfers(nil, nil, nil, 0, nil) + require.Nil(t, err) + }) + + t.Run("should work", func(t *testing.T) { + t.Parallel() + output := &mockery.MockOutputContext{} + vmOutput := &vmcommon.VMOutput{ + OutputAccounts: map[string]*vmcommon.OutputAccount{ + "addr1": { + OutputTransfers: []vmcommon.OutputTransfer{ + { + CallType: vm.AsynchronousCall, + }, + }, + }, + }, + } + asyncParams := &vmcommon.AsyncArguments{ + CallID: []byte("callID"), + } + + err := AddAsyncArgumentsToOutputTransfers(output, []byte("addr1"), asyncParams, vm.AsynchronousCall, vmOutput) + require.Nil(t, err) + require.NotNil(t, vmOutput.OutputAccounts["addr1"].OutputTransfers[0].AsyncData) + }) +} + +func TestCreateDataFromAsyncParams(t *testing.T) { + t.Parallel() + + t.Run("nil async params", func(t *testing.T) { + t.Parallel() + data, err := createDataFromAsyncParams(nil, vm.DirectCall) + require.Nil(t, err) + require.Nil(t, data) + }) + + t.Run("async call", func(t *testing.T) { + t.Parallel() + asyncParams := &vmcommon.AsyncArguments{ + CallID: []byte("callID"), + CallerCallID: []byte("callerCallID"), + } + data, err := createDataFromAsyncParams(asyncParams, vm.AsynchronousCall) + require.Nil(t, err) + require.NotNil(t, data) + }) + + t.Run("async callback", func(t *testing.T) { + t.Parallel() + asyncParams := &vmcommon.AsyncArguments{ + CallID: []byte("callID"), + CallerCallID: []byte("callerCallID"), + CallbackAsyncInitiatorCallID: []byte("initiator"), + GasAccumulated: 100, + } + data, err := createDataFromAsyncParams(asyncParams, vm.AsynchronousCallBack) + require.Nil(t, err) + require.NotNil(t, data) + // a bit of a hack to check if the gas was encoded + require.Contains(t, string(data), hex.EncodeToString(big.NewInt(100).Bytes())) + }) +} diff --git a/vmhost/errorWrapping.go b/vmhost/errorWrapping.go index 6a724feb0..583396874 100644 --- a/vmhost/errorWrapping.go +++ b/vmhost/errorWrapping.go @@ -148,18 +148,13 @@ func (werr *wrappableError) Error() string { // Unwrap - standard error function implementation for wrappable errors func (werr *wrappableError) Unwrap() error { - wrappingErr := werr.unwrapWrapping() - if len(wrappingErr.errsWithLocation) == 1 { - return wrappingErr.errsWithLocation[0].err - } else { - return wrappingErr - } -} - -func (werr *wrappableError) unwrapWrapping() *wrappableError { if len(werr.errsWithLocation) == 0 { return nil } + if len(werr.errsWithLocation) <= 1 { + return werr.errsWithLocation[0].err + } + return &wrappableError{ errsWithLocation: werr.errsWithLocation[:len(werr.errsWithLocation)-1], } diff --git a/vmhost/errorWrapping_test.go b/vmhost/errorWrapping_test.go new file mode 100644 index 000000000..b55e77996 --- /dev/null +++ b/vmhost/errorWrapping_test.go @@ -0,0 +1,135 @@ +package vmhost + +import ( + "errors" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestWrapError(t *testing.T) { + t.Parallel() + + err := errors.New("base error") + wrappedErr := WrapError(err, "info1") + + require.NotNil(t, wrappedErr) + require.Equal(t, err, wrappedErr.GetBaseError()) + require.Equal(t, err, wrappedErr.GetLastError()) + + allErrs, allInfo := wrappedErr.GetAllErrorsAndOtherInfo() + require.Len(t, allErrs, 1) + require.Equal(t, err, allErrs[0]) + require.Len(t, allInfo, 1) + require.Equal(t, "info1", allInfo[0]) +} + +func TestWrappableError_WrapWithMessage(t *testing.T) { + t.Parallel() + + err := errors.New("base error") + wrappedErr := WrapError(err) + wrappedErr = wrappedErr.WrapWithMessage("second layer") + + require.Equal(t, err, wrappedErr.GetBaseError()) + require.NotEqual(t, err, wrappedErr.GetLastError()) + require.Equal(t, "second layer", wrappedErr.GetLastError().Error()) + + allErrs := wrappedErr.GetAllErrors() + require.Len(t, allErrs, 2) + require.Equal(t, "second layer", allErrs[1].Error()) +} + +func TestWrappableError_WrapWithStackTrace(t *testing.T) { + t.Parallel() + + err := errors.New("base error") + wrappedErr := WrapError(err) + wrappedErr = wrappedErr.WrapWithStackTrace() + + require.Equal(t, err, wrappedErr.GetBaseError()) + require.NotEqual(t, err, wrappedErr.GetLastError()) + require.Equal(t, "", wrappedErr.GetLastError().Error()) + + allErrs := wrappedErr.GetAllErrors() + require.Len(t, allErrs, 2) +} + +func TestWrappableError_WrapWithError(t *testing.T) { + t.Parallel() + + err1 := errors.New("base error") + err2 := errors.New("second error") + wrappedErr := WrapError(err1) + wrappedErr = wrappedErr.WrapWithError(err2, "info2") + + require.Equal(t, err1, wrappedErr.GetBaseError()) + require.Equal(t, err2, wrappedErr.GetLastError()) + + allErrs, allInfo := wrappedErr.GetAllErrorsAndOtherInfo() + require.Len(t, allErrs, 2) + require.Len(t, allInfo, 1) + require.Equal(t, "info2", allInfo[0]) +} + +func TestWrappableError_Error(t *testing.T) { + t.Parallel() + + err1 := errors.New("base error") + wrappedErr := WrapError(err1) + wrappedErr = wrappedErr.WrapWithMessage("L2") + wrappedErr = wrappedErr.WrapWithError(errors.New("L3"), "info3") + + errStr := wrappedErr.Error() + require.True(t, strings.Contains(errStr, "errorWrapping_test.go")) + require.True(t, strings.Contains(errStr, "[base error]")) + require.True(t, strings.Contains(errStr, "[L2]")) + require.True(t, strings.Contains(errStr, "[L3]")) + require.True(t, strings.Contains(errStr, "[info3]")) +} + +func TestWrappableError_Unwrap(t *testing.T) { + t.Parallel() + + err1 := errors.New("base error") + err2 := errors.New("L2") + err3 := errors.New("L3") + wrappedErr := WrapError(err1).WrapWithError(err2).WrapWithError(err3) + + unwrappedOnce := errors.Unwrap(wrappedErr) + require.NotNil(t, unwrappedOnce) + require.True(t, errors.Is(unwrappedOnce, err1)) + require.True(t, errors.Is(unwrappedOnce, err2)) + require.False(t, errors.Is(unwrappedOnce, err3)) + + unwrappedTwice := errors.Unwrap(unwrappedOnce) + require.NotNil(t, unwrappedTwice) + require.True(t, errors.Is(unwrappedTwice, err1)) + require.False(t, errors.Is(unwrappedTwice, err2)) + + unwrappedThrice := errors.Unwrap(unwrappedTwice) + require.Equal(t, err1, unwrappedThrice) + + unwrappedFour := errors.Unwrap(unwrappedThrice) + require.Nil(t, unwrappedFour) +} + +func TestWrappableError_Is(t *testing.T) { + t.Parallel() + + err1 := errors.New("err1") + err2 := errors.New("err2") + err3 := errors.New("err3") + + wrappedErr := WrapError(err1) + wrappedErr = wrappedErr.WrapWithError(err2) + + require.True(t, wrappedErr.Is(err1)) + require.True(t, wrappedErr.Is(err2)) + require.False(t, wrappedErr.Is(err3)) + + require.True(t, errors.Is(wrappedErr, err1)) + require.True(t, errors.Is(wrappedErr, err2)) + require.False(t, errors.Is(wrappedErr, err3)) +} diff --git a/vmhost/hostCore/breakpoints_test.go b/vmhost/hostCore/breakpoints_test.go new file mode 100644 index 000000000..6f2048dfa --- /dev/null +++ b/vmhost/hostCore/breakpoints_test.go @@ -0,0 +1,122 @@ +package hostCore + +import ( + "errors" + "testing" + + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/multiversx/mx-chain-vm-go/vmhost" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func createTestHostWithMocks() (*vmHost, *mockery.MockRuntimeContext, *mockery.MockAsyncContext) { + runtimeMock := &mockery.MockRuntimeContext{} + asyncMock := &mockery.MockAsyncContext{} + host := &vmHost{ + runtimeContext: runtimeMock, + asyncContext: asyncMock, + } + return host, runtimeMock, asyncMock +} + +func TestVmHost_handleBreakpoint(t *testing.T) { + t.Parallel() + + t.Run("async call", func(t *testing.T) { + t.Parallel() + h, runtimeMock, asyncMock := createTestHostWithMocks() + runtimeMock.On("SetRuntimeBreakpointValue", vmhost.BreakpointNone) + asyncMock.On("GetCallGroup", vmhost.LegacyAsyncCallGroupID).Return(nil, false) + err := h.handleBreakpoint(vmhost.BreakpointAsyncCall) + require.Equal(t, vmhost.ErrLegacyAsyncCallNotFound, err) + }) + + errTests := []struct { + breakpoint vmhost.BreakpointValue + expected error + }{ + {vmhost.BreakpointExecutionFailed, vmhost.ErrExecutionFailed}, + {vmhost.BreakpointSignalError, vmhost.ErrSignalError}, + {vmhost.BreakpointOutOfGas, vmhost.ErrNotEnoughGas}, + {vmhost.BreakpointMemoryLimit, vmhost.ErrMemoryLimit}, + {vmhost.BreakpointValue(99), vmhost.ErrUnhandledRuntimeBreakpoint}, + } + + for _, tt := range errTests { + tt := tt + t.Run(tt.expected.Error(), func(t *testing.T) { + t.Parallel() + h, _, _ := createTestHostWithMocks() + err := h.handleBreakpoint(tt.breakpoint) + require.Equal(t, tt.expected, err) + }) + } +} + +func TestVmHost_handleBreakpointIfAny(t *testing.T) { + t.Parallel() + + t.Run("no error", func(t *testing.T) { + t.Parallel() + h, _, _ := createTestHostWithMocks() + err := h.handleBreakpointIfAny(nil) + require.Nil(t, err) + }) + + t.Run("with error, no breakpoint", func(t *testing.T) { + t.Parallel() + h, runtimeMock, _ := createTestHostWithMocks() + runtimeMock.On("GetRuntimeBreakpointValue").Return(vmhost.BreakpointNone) + runtimeMock.On("AddError", mock.Anything, mock.Anything).Return() + runtimeMock.On("FunctionName").Return("testFunc") + execErr := errors.New("exec error") + err := h.handleBreakpointIfAny(execErr) + require.Equal(t, vmhost.ErrExecutionFailed, err) + }) + + t.Run("with error and breakpoint", func(t *testing.T) { + t.Parallel() + h, runtimeMock, asyncMock := createTestHostWithMocks() + runtimeMock.On("GetRuntimeBreakpointValue").Return(vmhost.BreakpointOutOfGas) + runtimeMock.On("AddError", mock.Anything, mock.Anything).Return() + runtimeMock.On("FunctionName").Return("testFunc") + asyncMock.On("GetCallGroup", vmhost.LegacyAsyncCallGroupID).Return(nil, false) + execErr := errors.New("exec error") + err := h.handleBreakpointIfAny(execErr) + require.Equal(t, vmhost.ErrNotEnoughGas, err) + }) +} + +func TestVmHost_handleAsyncCallBreakpoint(t *testing.T) { + t.Parallel() + + t.Run("group not found", func(t *testing.T) { + t.Parallel() + h, runtimeMock, asyncMock := createTestHostWithMocks() + runtimeMock.On("SetRuntimeBreakpointValue", vmhost.BreakpointNone).Return() + asyncMock.On("GetCallGroup", vmhost.LegacyAsyncCallGroupID).Return(nil, false) + err := h.handleAsyncCallBreakpoint() + require.Equal(t, vmhost.ErrLegacyAsyncCallNotFound, err) + }) + + t.Run("group is complete", func(t *testing.T) { + t.Parallel() + h, runtimeMock, asyncMock := createTestHostWithMocks() + group := &vmhost.AsyncCallGroup{} + runtimeMock.On("SetRuntimeBreakpointValue", vmhost.BreakpointNone).Return() + asyncMock.On("GetCallGroup", vmhost.LegacyAsyncCallGroupID).Return(group, true) + err := h.handleAsyncCallBreakpoint() + require.Equal(t, vmhost.ErrLegacyAsyncCallInvalid, err) + }) + + t.Run("success", func(t *testing.T) { + t.Parallel() + h, runtimeMock, asyncMock := createTestHostWithMocks() + group := &vmhost.AsyncCallGroup{AsyncCalls: []*vmhost.AsyncCall{{}}} + runtimeMock.On("SetRuntimeBreakpointValue", vmhost.BreakpointNone).Return() + asyncMock.On("GetCallGroup", vmhost.LegacyAsyncCallGroupID).Return(group, true) + err := h.handleAsyncCallBreakpoint() + require.Nil(t, err) + }) +} diff --git a/vmhost/vmhooks/baseOps_test.go b/vmhost/vmhooks/baseOps_test.go new file mode 100644 index 000000000..ac52b5780 --- /dev/null +++ b/vmhost/vmhooks/baseOps_test.go @@ -0,0 +1,855 @@ +package vmhooks + +import ( + "math/big" + "testing" + + "github.com/multiversx/mx-chain-core-go/data/esdt" + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/multiversx/mx-chain-vm-go/vmhost" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestVMHooksImpl_GetGasLeft(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _ := createTestVMHooks() + + gasLeft := hooks.GetGasLeft() + require.Equal(t, int64(100), gasLeft) +} + +func TestVMHooksImpl_GetSCAddress(t *testing.T) { + t.Parallel() + hooks, _, runtime, _, _, _ := createTestVMHooks() + + scAddress := []byte("sc-address") + runtime.On("GetContextAddress").Return(scAddress) + + hooks.GetSCAddress(0) + runtime.AssertCalled(t, "GetContextAddress") +} + +func TestVMHooksImpl_GetOwnerAddress(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + ownerAddress := []byte("owner-address") + blockchain.On("GetOwnerAddress").Return(ownerAddress, nil) + + hooks.GetOwnerAddress(0) + blockchain.AssertCalled(t, "GetOwnerAddress") +} + +func TestVMHooksImpl_GetShardOfAddress(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + blockchain.On("GetShardOfAddress", mock.Anything).Return(uint32(1)) + + shard := hooks.GetShardOfAddress(0) + require.Equal(t, int32(1), shard) +} + +func TestVMHooksImpl_IsSmartContract(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + blockchain.On("IsSmartContract", mock.Anything).Return(true) + + isSC := hooks.IsSmartContract(0) + require.Equal(t, int32(1), isSC) +} + +func TestVMHooksImpl_SignalError(t *testing.T) { + t.Parallel() + vmHooks := createTestVMHooksClear() + baseMeteringSetup(vmHooks.metering) + vmHooks.runtime.On("GetInstance").Return(vmHooks.instance) + + errorMessage := "error message" + vmHooks.instance.On("MemLoad", mock.Anything, mock.Anything).Return([]byte(errorMessage), nil) + vmHooks.runtime.On("SignalUserError", errorMessage).Return() + + vmHooks.hooks.SignalError(0, 0) + vmHooks.runtime.AssertCalled(t, "SignalUserError", errorMessage) +} + +func TestVMHooksImpl_GetExternalBalance(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + balance := big.NewInt(100) + blockchain.On("GetBalance", mock.Anything).Return(balance.Bytes()) + + hooks.GetExternalBalance(0, 0) + blockchain.AssertCalled(t, "GetBalance", mock.Anything) +} + +func TestVMHooksImpl_GetBlockHash(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + blockHash := []byte("block-hash") + blockchain.On("BlockHash", mock.Anything).Return(blockHash) + + ret := hooks.GetBlockHash(0, 0) + require.Equal(t, int32(0), ret) + blockchain.AssertCalled(t, "BlockHash", mock.Anything) +} + +func TestVMHooksImpl_GetESDTBalance(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + esdtToken := &esdt.ESDigitalToken{ + Value: big.NewInt(100), + } + blockchain.On("GetESDTToken", mock.Anything, mock.Anything, mock.Anything).Return(esdtToken, nil) + + ret := hooks.GetESDTBalance(0, 0, 0, 0, 0) + require.NotEqual(t, int32(-1), ret) +} + +func TestVMHooksImpl_GetESDTNFTNameLength(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + esdtToken := &esdt.ESDigitalToken{ + TokenMetaData: &esdt.MetaData{ + Name: []byte("test-token"), + }, + } + blockchain.On("GetESDTToken", mock.Anything, mock.Anything, mock.Anything).Return(esdtToken, nil) + + ret := hooks.GetESDTNFTNameLength(0, 0, 0, 0) + require.Equal(t, int32(len(esdtToken.TokenMetaData.Name)), ret) +} + +func TestVMHooksImpl_GetESDTNFTAttributeLength(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + esdtToken := &esdt.ESDigitalToken{ + TokenMetaData: &esdt.MetaData{ + Attributes: []byte("test-attributes"), + }, + } + blockchain.On("GetESDTToken", mock.Anything, mock.Anything, mock.Anything).Return(esdtToken, nil) + + ret := hooks.GetESDTNFTAttributeLength(0, 0, 0, 0) + require.Equal(t, int32(len(esdtToken.TokenMetaData.Attributes)), ret) +} + +func TestVMHooksImpl_GetESDTNFTURILength(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + esdtToken := &esdt.ESDigitalToken{ + TokenMetaData: &esdt.MetaData{ + URIs: [][]byte{[]byte("test-uri")}, + }, + } + blockchain.On("GetESDTToken", mock.Anything, mock.Anything, mock.Anything).Return(esdtToken, nil) + + ret := hooks.GetESDTNFTURILength(0, 0, 0, 0) + require.Equal(t, int32(len(esdtToken.TokenMetaData.URIs[0])), ret) +} + +func TestVMHooksImpl_GetESDTTokenData(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, managedType := createTestVMHooksFull() + + esdtToken := &esdt.ESDigitalToken{ + Value: big.NewInt(100), + Properties: []byte("properties"), + TokenMetaData: &esdt.MetaData{ + Hash: []byte("hash"), + Name: []byte("name"), + Attributes: []byte("attributes"), + Creator: []byte("creator"), + Royalties: 10, + URIs: [][]byte{[]byte("uri")}, + }, + } + blockchain.On("GetESDTToken", mock.Anything, mock.Anything, mock.Anything).Return(esdtToken, nil) + managedType.On("GetBigIntOrCreate", mock.Anything).Return(big.NewInt(0)) + + ret := hooks.GetESDTTokenData(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + require.NotEqual(t, int32(-1), ret) +} + +func TestVMHooksImpl_GetESDTLocalRoles(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.managedType.On("GetBytes", mock.Anything).Return([]byte("token-id"), nil) + vmHooks.storage.On("GetStorage", mock.Anything).Return(nil, uint32(0), false, nil) + vmHooks.storage.On("UseGasForStorageLoad", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := vmHooks.hooks.GetESDTLocalRoles(0) + require.NotEqual(t, int64(-1), ret) +} + +func TestVMHooksImpl_ValidateTokenIdentifier(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.managedType.On("GetBytes", mock.Anything).Return([]byte("TEST-123456"), nil) + + ret := vmHooks.hooks.ValidateTokenIdentifier(0) + require.Equal(t, int32(1), ret) +} + +func TestVMHooksImpl_TransferValue(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, output, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + + runtime.On("GetContextAddress").Return([]byte("sender")) + output.On("Transfer", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.TransferValue(0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_TransferValueExecute(t *testing.T) { + t.Parallel() + hooks, host, runtime, metering, output, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + host.On("AreInSameShard", mock.Anything, mock.Anything).Return(false) + + runtime.On("GetContextAddress").Return([]byte("sender")) + output.On("Transfer", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.TransferValueExecute(0, 0, 0, 0, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_CreateAsyncCall(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, _, _, async, _ := createTestVMHooksWithSetMetering() + + async.On("RegisterAsyncCall", mock.Anything, mock.Anything).Return(nil) + + ret := hooks.CreateAsyncCall(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_SetAsyncContextCallback(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, _, _, async, _ := createTestVMHooksWithSetMetering() + + async.On("SetContextCallback", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.SetAsyncContextCallback(0, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_UpgradeContract(t *testing.T) { + t.Parallel() + + hooks, _, runtime, _, _, _, _, _, async, _ := createTestVMHooksWithSetMetering() + + runtime.On("SetRuntimeBreakpointValue", mock.Anything).Return() + async.On("RegisterLegacyAsyncCall", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + hooks.UpgradeContract(0, 1000000, 0, 0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_UpgradeFromSourceContract(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _, async, _ := createTestVMHooksWithSetMetering() + + blockchain.On("GetCode", mock.Anything).Return([]byte("code"), nil) + async.On("RegisterLegacyAsyncCall", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + hooks.UpgradeFromSourceContract(0, 1000000, 0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_DeleteContract(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.async.On("RegisterLegacyAsyncCall", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.DeleteContract(0, 1000000, 0, 0, 0) +} + +func TestVMHooksImpl_AsyncCall(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.async.On("RegisterLegacyAsyncCall", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.AsyncCall(0, 0, 0, 0) +} + +func TestVMHooksImpl_GetArgumentLength(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + runtime.On("Arguments").Return([][]byte{[]byte("arg1"), []byte("argument2")}) + + ret := hooks.GetArgumentLength(1) + require.Equal(t, int32(len("argument2")), ret) +} + +func TestVMHooksImpl_GetArgument(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + runtime.On("Arguments").Return([][]byte{[]byte("arg1"), []byte("argument2")}) + + ret := hooks.GetArgument(1, 0) + require.Equal(t, int32(len("argument2")), ret) +} + +func TestVMHooksImpl_GetFunction(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + runtime.On("FunctionName").Return("testFunction") + + ret := hooks.GetFunction(0) + require.Equal(t, int32(len("testFunction")), ret) +} + +func TestVMHooksImpl_GetNumArguments(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + runtime.On("Arguments").Return([][]byte{[]byte("arg1"), []byte("argument2")}) + + ret := hooks.GetNumArguments() + require.Equal(t, int32(2), ret) +} + +func TestVMHooksImpl_StorageStore(t *testing.T) { + t.Parallel() + hooks, _, _, metering, _, storage := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + storage.On("SetStorage", mock.Anything, mock.Anything).Return(vmhost.StorageAdded, nil) + + ret := hooks.StorageStore(0, 0, 0, 0) + require.Equal(t, int32(vmhost.StorageAdded), ret) +} + +func TestVMHooksImpl_StorageLoadLength(t *testing.T) { + t.Parallel() + hooks, _, _, metering, _, storage := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + storage.On("GetStorageUnmetered", mock.Anything).Return([]byte("data"), uint32(0), false, nil) + storage.On("UseGasForStorageLoad", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.StorageLoadLength(0, 0) + require.Equal(t, int32(len("data")), ret) +} + +func TestVMHooksImpl_StorageLoad(t *testing.T) { + t.Parallel() + hooks, _, _, metering, _, storage := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + storage.On("GetStorage", mock.Anything).Return([]byte("data"), uint32(0), false, nil) + storage.On("UseGasForStorageLoad", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.StorageLoad(0, 0, 0) + require.Equal(t, int32(len("data")), ret) +} + +func TestVMHooksImpl_StorageLoadFromAddress(t *testing.T) { + t.Parallel() + hooks, _, _, metering, _, storage := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + storage.On("GetStorageFromAddress", mock.Anything, mock.Anything).Return([]byte("data"), uint32(0), false, nil) + storage.On("UseGasForStorageLoad", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.StorageLoadFromAddress(0, 0, 0, 0) + require.Equal(t, int32(len("data")), ret) +} + +func TestVMHooksImpl_GetCaller(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + contractCallInput := &vmcommon.ContractCallInput{} + contractCallInput.VMInput = vmcommon.VMInput{CallerAddr: []byte("caller")} + runtime.On("GetVMInput").Return(contractCallInput) + + hooks.GetCaller(0) +} + +func TestVMHooksImpl_CheckNoPayment(t *testing.T) { + t.Parallel() + hooks, _, runtime, _, _, _, _, _, _, _ := createTestVMHooksWithSetMetering() + + contractCallInput := &vmcommon.ContractCallInput{} + contractCallInput.VMInput = vmcommon.VMInput{ + CallValue: big.NewInt(0), + ESDTTransfers: []*vmcommon.ESDTTransfer{}, + } + runtime.On("GetVMInput").Return(contractCallInput) + + hooks.CheckNoPayment() +} + +func TestVMHooksImpl_GetCallValue(t *testing.T) { + t.Parallel() + hooks, _, runtime, _, _, _, _, _, _, _ := createTestVMHooksWithSetMetering() + + contractCallInput := &vmcommon.ContractCallInput{} + contractCallInput.VMInput = vmcommon.VMInput{ + CallValue: big.NewInt(100), + ESDTTransfers: []*vmcommon.ESDTTransfer{}, + } + runtime.On("GetVMInput").Return(contractCallInput) + + ret := hooks.GetCallValue(0) + require.NotEqual(t, int32(-1), ret) +} + +func TestVMHooksImpl_GetESDTValue(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + contractCallInput := &vmcommon.ContractCallInput{} + contractCallInput.VMInput = vmcommon.VMInput{ + CallValue: big.NewInt(0), + ESDTTransfers: []*vmcommon.ESDTTransfer{{ESDTValue: big.NewInt(100)}}, + } + runtime.On("GetVMInput").Return(contractCallInput) + + ret := hooks.GetESDTValue(0) + require.NotEqual(t, int32(-1), ret) +} + +func TestVMHooksImpl_GetESDTTokenName(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + contractCallInput := &vmcommon.ContractCallInput{} + contractCallInput.VMInput = vmcommon.VMInput{ESDTTransfers: []*vmcommon.ESDTTransfer{{ESDTTokenName: []byte("token-name")}}} + runtime.On("GetVMInput").Return(contractCallInput) + + ret := hooks.GetESDTTokenName(0) + require.NotEqual(t, int32(-1), ret) +} + +func TestVMHooksImpl_GetESDTTokenNonce(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + contractCallInput := &vmcommon.ContractCallInput{} + contractCallInput.VMInput = vmcommon.VMInput{ESDTTransfers: []*vmcommon.ESDTTransfer{{ESDTTokenNonce: 123}}} + runtime.On("GetVMInput").Return(contractCallInput) + + ret := hooks.GetESDTTokenNonce() + require.Equal(t, int64(123), ret) +} + +func TestVMHooksImpl_GetCurrentESDTNFTNonce(t *testing.T) { + t.Parallel() + hooks, _, _, metering, _, storage := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + storage.On("GetStorageFromAddress", mock.Anything, mock.Anything).Return(big.NewInt(123).Bytes(), uint32(0), false, nil) + storage.On("UseGasForStorageLoad", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.GetCurrentESDTNFTNonce(0, 0, 0) + require.Equal(t, int64(123), ret) +} + +func TestVMHooksImpl_GetESDTTokenType(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + contractCallInput := &vmcommon.ContractCallInput{} + contractCallInput.VMInput = vmcommon.VMInput{ESDTTransfers: []*vmcommon.ESDTTransfer{{ESDTTokenType: 1}}} + runtime.On("GetVMInput").Return(contractCallInput) + + ret := hooks.GetESDTTokenType() + require.Equal(t, int32(1), ret) +} + +func TestVMHooksImpl_GetNumESDTTransfers(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + contractCallInput := &vmcommon.ContractCallInput{} + contractCallInput.VMInput = vmcommon.VMInput{ESDTTransfers: []*vmcommon.ESDTTransfer{{}, {}}} + runtime.On("GetVMInput").Return(contractCallInput) + + ret := hooks.GetNumESDTTransfers() + require.Equal(t, int32(2), ret) +} + +func TestVMHooksImpl_GetCallValueTokenName(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + contractCallInput := &vmcommon.ContractCallInput{} + contractCallInput.VMInput = vmcommon.VMInput{ + ESDTTransfers: []*vmcommon.ESDTTransfer{{ESDTTokenName: []byte("token-name"), ESDTValue: big.NewInt(100)}}, + CallValue: big.NewInt(0), + } + vmHooks.runtime.On("GetVMInput").Return(contractCallInput) + + ret := vmHooks.hooks.GetCallValueTokenName(0, 0) + require.Equal(t, int32(len("token-name")), ret) +} + +func TestVMHooksImpl_IsReservedFunctionName(t *testing.T) { + t.Parallel() + + vmHooks := createHooksWithBaseSetup() + + vmHooks.managedType.On("GetBytes", mock.Anything).Return([]byte("init"), nil) + vmHooks.runtime.On("IsReservedFunctionName", "init").Return(true) + + ret := vmHooks.hooks.IsReservedFunctionName(0) + require.Equal(t, int32(1), ret) +} + +func TestVMHooksImpl_WriteLog(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, output, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + runtime.On("GetContextAddress").Return([]byte("address")) + output.On("WriteLog", mock.Anything, mock.Anything, mock.Anything).Return() + + hooks.WriteLog(0, 0, 0, 0) +} + +func TestVMHooksImpl_WriteEventLog(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, output, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + runtime.On("GetContextAddress").Return([]byte("address")) + output.On("WriteLog", mock.Anything, mock.Anything, mock.Anything).Return() + + hooks.WriteEventLog(0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_GetBlockTimestamp(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.blockchain.On("CurrentTimeStamp").Return(uint64(12345)) + + ret := vmHooks.hooks.GetBlockTimestamp() + require.Equal(t, int64(12345), ret) +} + +func TestVMHooksImpl_GetBlockTimestampMs(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.blockchain.On("CurrentTimeStampMs").Return(uint64(12345000)) + + ret := vmHooks.hooks.GetBlockTimestampMs() + require.Equal(t, int64(12345000), ret) +} + +func TestVMHooksImpl_GetBlockNonce(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.blockchain.On("CurrentNonce").Return(uint64(123)) + + ret := vmHooks.hooks.GetBlockNonce() + require.Equal(t, int64(123), ret) +} + +func TestVMHooksImpl_GetBlockRound(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.blockchain.On("CurrentRound").Return(uint64(456)) + + ret := vmHooks.hooks.GetBlockRound() + require.Equal(t, int64(456), ret) +} + +func TestVMHooksImpl_GetBlockEpoch(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.blockchain.On("CurrentEpoch").Return(uint32(789)) + + ret := vmHooks.hooks.GetBlockEpoch() + require.Equal(t, int64(789), ret) +} + +func TestVMHooksImpl_GetBlockRandomSeed(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.blockchain.On("CurrentRandomSeed").Return([]byte("random-seed")) + + vmHooks.hooks.GetBlockRandomSeed(0) +} + +func TestVMHooksImpl_GetStateRootHash(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.blockchain.On("GetStateRootHash").Return([]byte("state-root-hash")) + + vmHooks.hooks.GetStateRootHash(0) +} + +func TestVMHooksImpl_GetPrevBlockTimestamp(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.blockchain.On("LastTimeStamp").Return(uint64(12345)) + + ret := vmHooks.hooks.GetPrevBlockTimestamp() + require.Equal(t, int64(12345), ret) +} + +func TestVMHooksImpl_GetPrevBlockTimestampMs(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.blockchain.On("LastTimeStampMs").Return(uint64(12345000)) + + ret := vmHooks.hooks.GetPrevBlockTimestampMs() + require.Equal(t, int64(12345000), ret) +} + +func TestVMHooksImpl_GetPrevBlockNonce(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + blockchain.On("LastNonce").Return(uint64(123)) + + ret := hooks.GetPrevBlockNonce() + require.Equal(t, int64(123), ret) +} + +func TestVMHooksImpl_GetPrevBlockRound(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + blockchain.On("LastRound").Return(uint64(6000)) + + ret := hooks.GetPrevBlockRound() + require.Equal(t, int64(6000), ret) +} + +func TestVMHooksImpl_GetPrevBlockEpoch(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + blockchain.On("LastEpoch").Return(uint32(789)) + + ret := hooks.GetPrevBlockEpoch() + require.Equal(t, int64(789), ret) +} + +func TestVMHooksImpl_GetPrevBlockRandomSeed(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + + blockchain.On("LastRandomSeed").Return([]byte("random-seed")) + + hooks.GetPrevBlockRandomSeed(0) +} + +func TestVMHooksImpl_GetBlockRoundTimeMs(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, _, _ := createTestVMHooksFull() + + ret := hooks.GetBlockRoundTimeMs() + require.Equal(t, int64(6000), ret) +} + +func TestVMHooksImpl_EpochStartBlockTimestampMs(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _ := createTestVMHooks() + + ret := hooks.EpochStartBlockTimestampMs() + require.Equal(t, int64(12345000), ret) +} + +func TestVMHooksImpl_EpochStartBlockNonce(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + blockchain.On("EpochStartBlockNonce").Return(uint64(123)) + + ret := hooks.EpochStartBlockNonce() + require.Equal(t, int64(123), ret) +} + +func TestVMHooksImpl_EpochStartBlockRound(t *testing.T) { + t.Parallel() + hooks, _, _, _, _, _, blockchain, _ := createTestVMHooksFull() + blockchain.On("EpochStartBlockRound").Return(uint64(6000)) + + ret := hooks.EpochStartBlockRound() + require.Equal(t, int64(6000), ret) +} + +func TestVMHooksImpl_Finish(t *testing.T) { + t.Parallel() + hooks, _, _, metering, output, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + + output.On("Finish", mock.Anything).Return() + + hooks.Finish(0, 0) +} + +func TestVMHooksImpl_ExecuteOnSameContext(t *testing.T) { + t.Parallel() + hooks, host, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + metering.On("BoundGasLimit", mock.Anything).Return(uint64(100)) + host.On("AreInSameShard", mock.Anything, mock.Anything).Return(true) + host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + host.On("ExecuteOnSameContext", mock.Anything).Return(nil) + + ret := hooks.ExecuteOnSameContext(0, 0, 0, 0, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ExecuteOnDestContext(t *testing.T) { + t.Parallel() + hooks, host, runtime, _, _, _, _, _, async, _ := createTestVMHooksWithSetMetering() + host.On("AreInSameShard", mock.Anything, mock.Anything).Return(true) + host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + host.On("IsBuiltinFunctionCall", mock.Anything).Return(false) + + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + + host.On("ExecuteOnDestContext", mock.Anything).Return(&vmcommon.VMOutput{}, true, nil) + host.On("CompleteLogEntriesWithCallType", mock.Anything, mock.Anything).Return() + + async.On("SetAsyncArgumentsForCall", mock.Anything).Return() + async.On("CompleteChildConditional", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.ExecuteOnDestContext(0, 0, 0, 0, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ExecuteReadOnly(t *testing.T) { + t.Parallel() + hooks, host, runtime, _, _, _, _, _, async, _ := createTestVMHooksWithSetMetering() + host.On("AreInSameShard", mock.Anything, mock.Anything).Return(true) + host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + host.On("IsBuiltinFunctionCall", mock.Anything).Return(false) + + runtime.On("GetContextAddress").Return([]byte("sender")) + host.On("ExecuteOnDestContext", mock.Anything).Return(&vmcommon.VMOutput{}, true, nil) + runtime.On("ReadOnly").Return(false) + runtime.On("SetReadOnly", mock.Anything).Return() + runtime.On("GetOriginalCallerAddress").Return([]byte("address")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + + async.On("SetAsyncArgumentsForCall", mock.Anything).Return() + async.On("CompleteChildConditional", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.ExecuteReadOnly(0, 0, 0, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_CreateContract(t *testing.T) { + t.Parallel() + hooks, host, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + metering.On("BoundGasLimit", mock.Anything).Return(uint64(1000000)) + + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("original-caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + host.On("CreateNewContract", mock.Anything, mock.Anything).Return([]byte("new-address"), nil) + + ret := hooks.CreateContract(0, 0, 0, 0, 0, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_DeployFromSourceContract(t *testing.T) { + t.Parallel() + hooks, host, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + metering.On("BoundGasLimit", mock.Anything).Return(uint64(1000000)) + + blockchain := &mockery.MockBlockchainContext{} + host.On("Blockchain").Return(blockchain) + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("original-caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + blockchain.On("GetCode", mock.Anything).Return([]byte("code"), nil) + host.On("CreateNewContract", mock.Anything, mock.Anything).Return([]byte("new-address"), nil) + + ret := hooks.DeployFromSourceContract(0, 0, 0, 0, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_GetNumReturnData(t *testing.T) { + t.Parallel() + hooks, _, _, metering, output, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + output.On("ReturnData").Return([][]byte{[]byte("data1"), []byte("data2")}) + + ret := hooks.GetNumReturnData() + require.Equal(t, int32(2), ret) +} + +func TestVMHooksImpl_GetReturnDataSize(t *testing.T) { + t.Parallel() + hooks, _, _, metering, output, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + output.On("ReturnData").Return([][]byte{[]byte("data1"), []byte("data2")}) + + ret := hooks.GetReturnDataSize(1) + require.Equal(t, int32(len("data2")), ret) +} + +func TestVMHooksImpl_GetReturnData(t *testing.T) { + t.Parallel() + hooks, _, _, metering, output, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + output.On("ReturnData").Return([][]byte{[]byte("data1"), []byte("data2")}) + + ret := hooks.GetReturnData(1, 0) + require.Equal(t, int32(len("data2")), ret) +} + +func TestVMHooksImpl_CleanReturnData(t *testing.T) { + t.Parallel() + hooks, _, _, metering, output, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + output.On("ClearReturnData").Return() + + hooks.CleanReturnData() + output.AssertCalled(t, "ClearReturnData") +} diff --git a/vmhost/vmhooks/bigFloatOps.go b/vmhost/vmhooks/bigFloatOps.go index f051da341..050ff132a 100644 --- a/vmhost/vmhooks/bigFloatOps.go +++ b/vmhost/vmhooks/bigFloatOps.go @@ -381,11 +381,11 @@ func (context *VMHooksImpl) BigFloatCmp(op1Handle, op2Handle int32) int32 { } op1, op2, err := managedType.GetTwoBigFloats(op1Handle, op2Handle) - if err != nil { context.FailExecution(err) return -2 } + return int32(op1.Cmp(op2)) } diff --git a/vmhost/vmhooks/bigFloatOps_test.go b/vmhost/vmhooks/bigFloatOps_test.go new file mode 100644 index 000000000..e56b97ef4 --- /dev/null +++ b/vmhost/vmhooks/bigFloatOps_test.go @@ -0,0 +1,150 @@ +package vmhooks + +import ( + "math/big" + "testing" + + "github.com/multiversx/mx-chain-scenario-go/worldmock" + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestVMHooksImpl_BigFloatAdd(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewFloat(0) + vmHooks.managedType.On("GetBigFloatOrCreate", int32(0)).Return(dest, nil) + vmHooks.managedType.On("GetTwoBigFloats", int32(1), int32(2)).Return(big.NewFloat(10.5), big.NewFloat(20.5), nil) + vmHooks.managedType.On("BigFloatExpIsNotValid", mock.Anything).Return(false) + + vmHooks.hooks.BigFloatAdd(0, 1, 2) + f, _ := dest.Float64() + require.Equal(t, float64(31), f) +} + +func TestVMHooksImpl_BigFloatSub(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewFloat(0) + vmHooks.managedType.On("GetBigFloatOrCreate", int32(0)).Return(dest, nil) + vmHooks.managedType.On("GetTwoBigFloats", int32(1), int32(2)).Return(big.NewFloat(20.5), big.NewFloat(10.5), nil) + vmHooks.managedType.On("BigFloatExpIsNotValid", mock.Anything).Return(false) + + vmHooks.hooks.BigFloatSub(0, 1, 2) + f, _ := dest.Float64() + require.Equal(t, float64(10), f) +} + +func TestVMHooksImpl_BigFloatMul(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewFloat(0) + vmHooks.managedType.On("GetBigFloatOrCreate", int32(0)).Return(dest, nil) + vmHooks.managedType.On("GetTwoBigFloats", int32(1), int32(2)).Return(big.NewFloat(10.5), big.NewFloat(2.0), nil) + vmHooks.managedType.On("BigFloatExpIsNotValid", mock.Anything).Return(false) + + vmHooks.hooks.BigFloatMul(0, 1, 2) + f, _ := dest.Float64() + require.Equal(t, float64(21), f) +} + +func TestVMHooksImpl_BigFloatDiv(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewFloat(0) + vmHooks.managedType.On("GetBigFloatOrCreate", int32(0)).Return(dest, nil) + vmHooks.managedType.On("GetTwoBigFloats", int32(1), int32(2)).Return(big.NewFloat(21.0), big.NewFloat(2.0), nil) + vmHooks.managedType.On("BigFloatExpIsNotValid", mock.Anything).Return(false) + + vmHooks.hooks.BigFloatDiv(0, 1, 2) + f, _ := dest.Float64() + require.Equal(t, float64(10.5), f) +} + +func TestVMHooksImpl_BigFloatAbs(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewFloat(0) + vmHooks.managedType.On("GetBigFloatOrCreate", int32(0)).Return(dest, nil) + vmHooks.managedType.On("GetBigFloat", int32(1)).Return(big.NewFloat(-10.5), nil) + + vmHooks.hooks.BigFloatAbs(0, 1) + f, _ := dest.Float64() + require.Equal(t, float64(10.5), f) +} + +func TestVMHooksImpl_BigFloatNeg(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewFloat(0) + vmHooks.managedType.On("GetBigFloatOrCreate", int32(0)).Return(dest, nil) + vmHooks.managedType.On("GetBigFloat", int32(1)).Return(big.NewFloat(10.5), nil) + + vmHooks.hooks.BigFloatNeg(0, 1) + f, _ := dest.Float64() + require.Equal(t, float64(-10.5), f) +} + +func TestVMHooksImpl_BigFloatCmp1(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.managedType.On("GetTwoBigFloats", int32(1), int32(2)).Return(big.NewFloat(10.5), big.NewFloat(20.5), nil) + res := vmHooks.hooks.BigFloatCmp(1, 2) + require.Equal(t, int32(-1), res) +} + +func TestVMHooksImpl_BigFloatCmp2(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.managedType.On("GetTwoBigFloats", int32(1), int32(2)).Return(big.NewFloat(20.5), big.NewFloat(10.5), nil) + res := vmHooks.hooks.BigFloatCmp(1, 2) + require.Equal(t, int32(1), res) +} + +func TestVMHooksImpl_BigFloatCmp3(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + vmHooks.managedType.On("GetTwoBigFloats", int32(1), int32(2)).Return(big.NewFloat(10.5), big.NewFloat(10.5), nil) + res := vmHooks.hooks.BigFloatCmp(1, 2) + require.Equal(t, int32(0), res) +} + +func TestVMHooksImpl_BigFloatSqrt(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewFloat(0) + vmHooks.managedType.On("GetBigFloatOrCreate", int32(0)).Return(dest, nil) + vmHooks.managedType.On("GetBigFloat", int32(1)).Return(big.NewFloat(9.0), nil) + vmHooks.managedType.On("BigFloatExpIsNotValid", mock.Anything).Return(false) + + vmHooks.hooks.BigFloatSqrt(0, 1) + f, _ := dest.Float64() + require.Equal(t, float64(3), f) +} + +func TestVMHooksImpl_BigFloatPow(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + vmHooks.hooks.GetVMHost().(*mockery.MockVMHost).On("EnableEpochsHandler").Return(&worldmock.EnableEpochsHandlerStub{}) + + dest := big.NewFloat(0) + vmHooks.managedType.On("GetBigFloatOrCreate", int32(0)).Return(dest, nil) + vmHooks.managedType.On("GetBigFloat", int32(1)).Return(big.NewFloat(2.0), nil) + vmHooks.managedType.On("BigFloatExpIsNotValid", mock.Anything).Return(false) + vmHooks.managedType.On("ConsumeGasForThisBigIntNumberOfBytes", mock.Anything).Return(nil) + + vmHooks.hooks.BigFloatPow(0, 1, 10) + f, _ := dest.Float64() + require.Equal(t, float64(1024), f) +} diff --git a/vmhost/vmhooks/bigIntOps_test.go b/vmhost/vmhooks/bigIntOps_test.go new file mode 100644 index 000000000..c567e0e7d --- /dev/null +++ b/vmhost/vmhooks/bigIntOps_test.go @@ -0,0 +1,127 @@ +package vmhooks + +import ( + "math/big" + "testing" + + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestVMHooksImpl_BigIntAdd(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewInt(0) + vmHooks.managedType.On("GetBigIntOrCreate", int32(0)).Return(dest) + vmHooks.managedType.On("GetTwoBigInt", int32(1), int32(2)).Return(big.NewInt(10), big.NewInt(20), nil) + vmHooks.managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.BigIntAdd(0, 1, 2) + require.Equal(t, big.NewInt(30), dest) +} + +func TestVMHooksImpl_BigIntSub(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewInt(0) + vmHooks.managedType.On("GetBigIntOrCreate", int32(0)).Return(dest) + vmHooks.managedType.On("GetTwoBigInt", int32(1), int32(2)).Return(big.NewInt(20), big.NewInt(10), nil) + vmHooks.managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.BigIntSub(0, 1, 2) + require.Equal(t, big.NewInt(10), dest) +} + +func TestVMHooksImpl_BigIntMul(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewInt(0) + vmHooks.managedType.On("GetBigIntOrCreate", int32(0)).Return(dest) + vmHooks.managedType.On("GetTwoBigInt", int32(1), int32(2)).Return(big.NewInt(10), big.NewInt(20), nil) + vmHooks.managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.BigIntMul(0, 1, 2) + require.Equal(t, big.NewInt(200), dest) +} + +func TestVMHooksImpl_BigIntTDiv(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewInt(0) + vmHooks.managedType.On("GetBigIntOrCreate", int32(0)).Return(dest) + vmHooks.managedType.On("GetTwoBigInt", int32(1), int32(2)).Return(big.NewInt(20), big.NewInt(10), nil) + vmHooks.managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.BigIntTDiv(0, 1, 2) + require.Equal(t, big.NewInt(2), dest) +} + +func TestVMHooksImpl_BigIntTMod(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewInt(0) + vmHooks.managedType.On("GetBigIntOrCreate", int32(0)).Return(dest) + vmHooks.managedType.On("GetTwoBigInt", int32(1), int32(2)).Return(big.NewInt(23), big.NewInt(10), nil) + vmHooks.managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.BigIntTMod(0, 1, 2) + require.Equal(t, big.NewInt(3), dest) +} + +func TestVMHooksImpl_BigIntEDiv(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewInt(0) + vmHooks.managedType.On("GetBigIntOrCreate", int32(0)).Return(dest) + vmHooks.managedType.On("GetTwoBigInt", int32(1), int32(2)).Return(big.NewInt(20), big.NewInt(10), nil) + vmHooks.managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.BigIntEDiv(0, 1, 2) + require.Equal(t, big.NewInt(2), dest) +} + +func TestVMHooksImpl_BigIntEMod(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewInt(0) + vmHooks.managedType.On("GetBigIntOrCreate", int32(0)).Return(dest) + vmHooks.managedType.On("GetTwoBigInt", int32(1), int32(2)).Return(big.NewInt(23), big.NewInt(10), nil) + vmHooks.managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.BigIntEMod(0, 1, 2) + require.Equal(t, big.NewInt(3), dest) +} + +func TestVMHooksImpl_BigIntPow(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewInt(0) + vmHooks.managedType.On("GetBigIntOrCreate", int32(0)).Return(dest) + vmHooks.managedType.On("GetTwoBigInt", int32(1), int32(2)).Return(big.NewInt(2), big.NewInt(10), nil) + vmHooks.managedType.On("ConsumeGasForThisBigIntNumberOfBytes", mock.Anything).Return(nil) + vmHooks.managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.BigIntPow(0, 1, 2) + require.Equal(t, big.NewInt(1024), dest) +} + +func TestVMHooksImpl_BigIntSqrt(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + + dest := big.NewInt(0) + vmHooks.managedType.On("GetBigIntOrCreate", int32(0)).Return(dest) + vmHooks.managedType.On("GetBigInt", int32(1)).Return(big.NewInt(1024), nil) + vmHooks.managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything).Return(nil) + + vmHooks.hooks.BigIntSqrt(0, 1) + require.Equal(t, big.NewInt(32), dest) +} diff --git a/vmhost/vmhooks/cryptoei_test.go b/vmhost/vmhooks/cryptoei_test.go new file mode 100644 index 000000000..2ef56434b --- /dev/null +++ b/vmhost/vmhooks/cryptoei_test.go @@ -0,0 +1,318 @@ +package vmhooks + +import ( + "crypto/elliptic" + "crypto/rand" + "crypto/sha256" + "math/big" + "testing" + + "github.com/multiversx/mx-chain-scenario-go/worldmock" + mock2 "github.com/multiversx/mx-chain-vm-go/mock/context" + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestVMHooksImpl_Sha256(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + enableEpochs := &worldmock.EnableEpochsHandlerStub{} + host.On("EnableEpochsHandler").Return(enableEpochs) + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + + data := []byte("test data") + hash := sha256.Sum256(data) + crypto.Result = hash[:] + + ret := hooks.Sha256(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedSha256(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + managedType := host.ManagedTypes().(*mockery.MockManagedTypesContext) + + data := []byte("test data") + hash := sha256.Sum256(data) + managedType.On("GetBytes", mock.Anything).Return(data, nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + crypto.Result = hash[:] + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.ManagedSha256(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_Keccak256(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + + data := []byte("test data") + hash := sha256.Sum256(data) // just a placeholder + crypto.Result = hash[:] + + ret := hooks.Keccak256(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedKeccak256(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + managedType := host.ManagedTypes().(*mockery.MockManagedTypesContext) + + data := []byte("test data") + hash := sha256.Sum256(data) // just a placeholder + managedType.On("GetBytes", mock.Anything).Return(data, nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + crypto.Result = hash[:] + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.ManagedKeccak256(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_Ripemd160(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + enableEpochs := &worldmock.EnableEpochsHandlerStub{} + host.On("EnableEpochsHandler").Return(enableEpochs) + + data := []byte("test data") + hash := sha256.Sum256(data) // just a placeholder + crypto.Result = hash[:] + + ret := hooks.Ripemd160(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedRipemd160(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + managedType := host.ManagedTypes().(*mockery.MockManagedTypesContext) + + data := []byte("test data") + hash := sha256.Sum256(data) // just a placeholder + managedType.On("GetBytes", mock.Anything).Return(data, nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + crypto.Result = hash[:] + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.ManagedRipemd160(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_VerifyBLS(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + enableEpochs := &worldmock.EnableEpochsHandlerStub{} + host.On("EnableEpochsHandler").Return(enableEpochs) + + crypto.Err = nil + + ret := hooks.VerifyBLS(0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedVerifyBLS(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + managedType := host.ManagedTypes().(*mockery.MockManagedTypesContext) + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + crypto.Err = nil + + ret := hooks.ManagedVerifyBLS(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_VerifyEd25519(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + enableEpochs := &worldmock.EnableEpochsHandlerStub{} + host.On("EnableEpochsHandler").Return(enableEpochs) + + crypto.Err = nil + + ret := hooks.VerifyEd25519(0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedVerifyEd25519(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + managedType := host.ManagedTypes().(*mockery.MockManagedTypesContext) + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + crypto.Err = nil + + ret := hooks.ManagedVerifyEd25519(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_VerifyCustomSecp256k1(t *testing.T) { + t.Parallel() + vmHooks := createTestVMHooksClear() + baseMeteringSetup(vmHooks.metering) + + crypto := &mock2.CryptoHookMock{} + vmHooks.host.On("Crypto").Return(crypto) + + crypto.Err = nil + vmHooks.runtime.On("GetInstance").Return(vmHooks.instance) + vmHooks.instance.On("MemLoad", mock.Anything, mock.Anything).Return([]byte{0x30, 0x0}, nil) + + ret := vmHooks.hooks.VerifyCustomSecp256k1(0, secp256k1CompressedPublicKeyLength, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedVerifyCustomSecp256k1(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + managedType := host.ManagedTypes().(*mockery.MockManagedTypesContext) + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + crypto.Err = nil + + ret := hooks.ManagedVerifyCustomSecp256k1(0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_EncodeSecp256k1DerSignature(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + + crypto.Result = []byte("signature") + + ret := hooks.EncodeSecp256k1DerSignature(0, 0, 0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedEncodeSecp256k1DerSignature(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + crypto := &mock2.CryptoHookMock{} + host.On("Crypto").Return(crypto) + managedType := host.ManagedTypes().(*mockery.MockManagedTypesContext) + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + crypto.Result = []byte("signature") + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.ManagedEncodeSecp256k1DerSignature(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_CreateEC(t *testing.T) { + t.Parallel() + vmHooks := createTestVMHooksClear() + baseMeteringSetup(vmHooks.metering) + vmHooks.runtime.On("GetInstance").Return(vmHooks.instance) + + vmHooks.managedType.On("PutEllipticCurve", mock.Anything).Return(int32(1)) + vmHooks.instance.On("MemLoad", mock.Anything, mock.Anything).Return([]byte("p256"), nil) + + ret := vmHooks.hooks.CreateEC(0, 4) + require.Equal(t, int32(1), ret) +} + +func TestVMHooksImpl_ManagedCreateEC(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + managedType := host.ManagedTypes().(*mockery.MockManagedTypesContext) + + managedType.On("GetBytes", mock.Anything).Return([]byte("p256"), nil) + managedType.On("PutEllipticCurve", mock.Anything).Return(int32(1)) + + ret := hooks.ManagedCreateEC(0) + require.Equal(t, int32(1), ret) +} + +func TestVMHooksImpl_EllipticCurve(t *testing.T) { + t.Parallel() + hooks, host, _, metering, _, _ := createTestVMHooks() + metering.On("UseGasBounded", mock.Anything).Return(nil) + + managedType := host.ManagedTypes().(*mockery.MockManagedTypesContext) + + ec := elliptic.P256().Params() + managedType.On("GetEllipticCurve", mock.Anything).Return(ec, nil) + managedType.On("GetTwoBigInt", mock.Anything, mock.Anything).Return(big.NewInt(0), big.NewInt(0), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(0), nil) + managedType.On("Get100xCurveGasCostMultiplier", mock.Anything).Return(int32(100)) + managedType.On("ConsumeGasForBigIntCopy", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + managedType.On("GetScalarMult100xCurveGasCostMultiplier", mock.Anything).Return(int32(1)) + managedType.On("GetUCompressed100xCurveGasCostMultiplier", mock.Anything).Return(int32(1)) + managedType.On("GetRandReader").Return(rand.Reader) + managedType.On("GetEllipticCurveSizeOfField", mock.Anything).Return(int32(1)) + managedType.On("GetPrivateKeyByteLengthEC", mock.Anything).Return(int32(1)) + + hooks.AddEC(0, 0, 0, 0, 0, 0, 0) + hooks.DoubleEC(0, 0, 0, 0, 0) + hooks.IsOnCurveEC(0, 0, 0) + hooks.ScalarBaseMultEC(0, 0, 0, 0, 0) + hooks.ScalarMultEC(0, 0, 0, 0, 0, 0, 0) + hooks.MarshalEC(0, 0, 0, 0) + hooks.MarshalCompressedEC(0, 0, 0, 0) + hooks.UnmarshalEC(0, 0, 0, 0, 0) + hooks.UnmarshalCompressedEC(0, 0, 0, 0, 0) + hooks.GenerateKeyEC(0, 0, 0, 0) + hooks.GetCurveLengthEC(0) + hooks.GetPrivKeyByteLengthEC(0) + hooks.EllipticCurveGetValues(0, 0, 0, 0, 0, 0) +} diff --git a/vmhost/vmhooks/eei_helpers_test.go b/vmhost/vmhooks/eei_helpers_test.go new file mode 100644 index 000000000..08c3be597 --- /dev/null +++ b/vmhost/vmhooks/eei_helpers_test.go @@ -0,0 +1,55 @@ +package vmhooks + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGetESDTRoles(t *testing.T) { + t.Parallel() + + t.Run("no roles", func(t *testing.T) { + t.Parallel() + roles := getESDTRoles([]byte{}, false) + require.Equal(t, int64(0), roles) + }) + + t.Run("one role", func(t *testing.T) { + t.Parallel() + + data := "\n" + string(rune((len("ESDTRoleLocalMint")))) + "ESDTRoleLocalMint" + + roles := getESDTRoles([]byte(data), false) + require.Equal(t, int64(RoleMint), roles) + }) + + t.Run("two roles", func(t *testing.T) { + t.Parallel() + + data := "\n" + string(rune((len("ESDTRoleLocalMint")))) + "ESDTRoleLocalMint" + data += "\n" + string(rune((len("ESDTRoleLocalBurn")))) + "ESDTRoleLocalBurn" + + roles := getESDTRoles([]byte(data), false) + require.Equal(t, int64(RoleMint|RoleBurn), roles) + }) + + t.Run("two roles v2", func(t *testing.T) { + t.Parallel() + data := "\n" + string(rune((len("ESDTRoleLocalMint")))) + "ESDTRoleLocalMint" + data += "\n" + string(rune((len("ESDTRoleNFTUpdateAttributes")))) + "ESDTRoleNFTUpdateAttributes" + + roles := getESDTRoles([]byte(data), true) + require.Equal(t, int64(RoleMint|RoleNFTUpdateAttributes), roles) + }) +} + +func TestValidateToken(t *testing.T) { + t.Parallel() + + require.True(t, ValidateToken([]byte("TEST-123456"))) + require.False(t, ValidateToken([]byte("TEST-12345"))) + require.False(t, ValidateToken([]byte("TEST-1234567"))) + require.False(t, ValidateToken([]byte("test-123456"))) + require.False(t, ValidateToken([]byte("TEST-12345G"))) +} diff --git a/vmhost/vmhooks/manBufOps.go b/vmhost/vmhooks/manBufOps.go index 89f057054..031eb6352 100644 --- a/vmhost/vmhooks/manBufOps.go +++ b/vmhost/vmhooks/manBufOps.go @@ -712,7 +712,6 @@ func (context *VMHooksImpl) MBufferToBigFloat(mBufferHandle, bigFloatHandle int3 if enableEpochsHandler.IsFlagEnabled(vmhost.MaskInternalDependenciesErrorsFlag) { err = vmhost.ErrBigFloatDecode } - context.FailExecution(err) return 1 } diff --git a/vmhost/vmhooks/manBufOps_test.go b/vmhost/vmhooks/manBufOps_test.go new file mode 100644 index 000000000..a948b78da --- /dev/null +++ b/vmhost/vmhooks/manBufOps_test.go @@ -0,0 +1,354 @@ +package vmhooks + +import ( + "crypto/rand" + "github.com/multiversx/mx-chain-vm-go/vmhost" + "math/big" + "testing" + + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestVMHooksImpl_MBufferNew(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("NewManagedBuffer").Return(int32(1)) + + ret := hooks.MBufferNew() + require.Equal(t, int32(1), ret) +} + +func TestVMHooksImpl_MBufferNewFromBytes(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("NewManagedBufferFromBytes", mock.Anything).Return(int32(1)) + + ret := hooks.MBufferNewFromBytes(0, 0) + require.Equal(t, int32(1), ret) +} + +func TestVMHooksImpl_MBufferGetLength(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetLength", mock.Anything).Return(int32(10)) + + ret := hooks.MBufferGetLength(0) + require.Equal(t, int32(10), ret) +} + +func TestVMHooksImpl_MBufferGetBytes(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + + ret := hooks.MBufferGetBytes(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferGetByteSlice(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + + ret := hooks.MBufferGetByteSlice(0, 0, 4, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferCopyByteSlice(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.MBufferCopyByteSlice(0, 0, 4, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferEq(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + + ret := hooks.MBufferEq(0, 0) + require.Equal(t, int32(1), ret) +} + +func TestVMHooksImpl_MBufferSetBytes(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.MBufferSetBytes(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferSetByteSlice(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return(make([]byte, 10), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.MBufferSetByteSlice(0, 0, 4, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferAppend(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("AppendBytes", mock.Anything, mock.Anything).Return(true) + + ret := hooks.MBufferAppend(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferAppendBytes(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("AppendBytes", mock.Anything, mock.Anything).Return(true) + + ret := hooks.MBufferAppendBytes(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferToBigIntUnsigned(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigIntOrCreate", mock.Anything).Return(big.NewInt(0)) + + ret := hooks.MBufferToBigIntUnsigned(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferToBigIntSigned(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigIntOrCreate", mock.Anything).Return(big.NewInt(0)) + + ret := hooks.MBufferToBigIntSigned(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferFromBigIntUnsigned(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.MBufferFromBigIntUnsigned(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferFromBigIntSigned(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.MBufferFromBigIntSigned(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferToSmallIntUnsigned(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return(big.NewInt(100).Bytes(), nil) + + ret := hooks.MBufferToSmallIntUnsigned(0) + require.Equal(t, int64(100), ret) +} + +func TestVMHooksImpl_MBufferToSmallIntSigned(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return(big.NewInt(100).Bytes(), nil) + + ret := hooks.MBufferToSmallIntSigned(0) + require.Equal(t, int64(100), ret) +} + +func TestVMHooksImpl_MBufferFromSmallIntUnsigned(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.MBufferFromSmallIntUnsigned(0, 100) +} + +func TestVMHooksImpl_MBufferFromSmallIntSigned(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.MBufferFromSmallIntSigned(0, -100) +} + +func TestVMHooksImpl_MBufferStorageStore(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + storage := vmHooks.storage + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + storage.On("SetStorage", mock.Anything, mock.Anything).Return(vmhost.StorageStatus(0), nil) + + ret := hooks.MBufferStorageStore(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferStorageLoad(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + storage := vmHooks.storage + + managedType.On("GetBytes", mock.Anything).Return([]byte("key"), nil) + storage.On("GetStorage", mock.Anything).Return([]byte("data"), uint32(0), false, nil) + storage.On("UseGasForStorageLoad", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.MBufferStorageLoad(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferGetArgument(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + runtime := vmHooks.runtime + + runtime.On("Arguments").Return([][]byte{[]byte("arg1")}) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.MBufferGetArgument(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferFinish(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + output := vmHooks.output + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + output.On("Finish", mock.Anything).Return() + + ret := hooks.MBufferFinish(0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferSetRandom(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetRandReader").Return(rand.Reader) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.MBufferSetRandom(0, 10) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferToBigFloat(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + value := big.NewFloat(10) + manBytes, _ := big.NewFloat(10).GobEncode() + + managedType.On("GetBytes", mock.Anything).Return(manBytes, nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("EncodedBigFloatIsNotValid", mock.Anything).Return(false) + managedType.On("GetBigFloatOrCreate", mock.Anything).Return(value, nil) + + ret := hooks.MBufferToBigFloat(0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_MBufferFromBigFloat(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBigFloat", mock.Anything).Return(big.NewFloat(0), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + ret := hooks.MBufferFromBigFloat(0, 0) + require.Equal(t, int32(0), ret) +} diff --git a/vmhost/vmhooks/manMapOps_test.go b/vmhost/vmhooks/manMapOps_test.go new file mode 100644 index 000000000..5c9397670 --- /dev/null +++ b/vmhost/vmhooks/manMapOps_test.go @@ -0,0 +1,68 @@ +package vmhooks + +import ( + "testing" + + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestVMHooksImpl_ManagedMapNew(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("NewManagedMap").Return(int32(1)) + + ret := hooks.ManagedMapNew() + require.Equal(t, int32(1), ret) +} + +func TestVMHooksImpl_ManagedMapPut(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("ManagedMapPut", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.ManagedMapPut(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedMapGet(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("ManagedMapGet", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.ManagedMapGet(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedMapRemove(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("ManagedMapRemove", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ret := hooks.ManagedMapRemove(0, 0, 0) + require.Equal(t, int32(0), ret) +} + +func TestVMHooksImpl_ManagedMapContains(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("ManagedMapContains", mock.Anything, mock.Anything).Return(true, nil) + + ret := hooks.ManagedMapContains(0, 0) + require.Equal(t, int32(1), ret) +} diff --git a/vmhost/vmhooks/managedConversions_test.go b/vmhost/vmhooks/managedConversions_test.go new file mode 100644 index 000000000..0cae2d59a --- /dev/null +++ b/vmhost/vmhooks/managedConversions_test.go @@ -0,0 +1,169 @@ +package vmhooks + +import ( + "math/big" + "testing" + + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + "github.com/multiversx/mx-chain-vm-go/config" + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestReadESDTTransfer(t *testing.T) { + t.Parallel() + + managedType := &mockery.MockManagedTypesContext{} + runtime := &mockery.MockRuntimeContext{} + + managedType.On("GetBytes", mock.Anything).Return([]byte("token-name"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ConsumeGasForBigIntCopy", mock.Anything).Return(nil) + + data := make([]byte, 16) + esdtTransfer, err := readESDTTransfer(managedType, runtime, data) + require.Nil(t, err) + require.NotNil(t, esdtTransfer) +} + +func TestReadESDTTransfers(t *testing.T) { + t.Parallel() + + managedType := &mockery.MockManagedTypesContext{} + runtime := &mockery.MockRuntimeContext{} + + managedType.On("GetBytes", mock.Anything).Return(make([]byte, 32), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ConsumeGasForBigIntCopy", mock.Anything).Return(nil) + + esdtTransfers, err := readESDTTransfers(managedType, runtime, 0) + require.Nil(t, err) + require.NotNil(t, esdtTransfers) + require.Len(t, esdtTransfers, 2) +} + +func TestWriteESDTTransfer(t *testing.T) { + t.Parallel() + + managedType := &mockery.MockManagedTypesContext{} + + managedType.On("NewManagedBufferFromBytes", mock.Anything).Return(int32(1)) + managedType.On("NewBigInt", mock.Anything).Return(int32(2)) + + esdtTransfer := &vmcommon.ESDTTransfer{ + ESDTTokenName: []byte("token-name"), + ESDTTokenNonce: 123, + ESDTValue: big.NewInt(100), + } + data := make([]byte, 16) + writeESDTTransfer(managedType, esdtTransfer, data) +} + +func TestWriteESDTTransfersToBytes(t *testing.T) { + t.Parallel() + + managedType := &mockery.MockManagedTypesContext{} + + managedType.On("NewManagedBufferFromBytes", mock.Anything).Return(int32(1)) + managedType.On("NewBigInt", mock.Anything).Return(int32(2)) + + esdtTransfers := []*vmcommon.ESDTTransfer{ + { + ESDTTokenName: []byte("token-name"), + ESDTTokenNonce: 123, + ESDTValue: big.NewInt(100), + }, + { + ESDTTokenName: []byte("token-name2"), + ESDTTokenNonce: 456, + ESDTValue: big.NewInt(200), + }, + } + data := writeESDTTransfersToBytes(managedType, esdtTransfers) + require.Len(t, data, 32) +} + +func TestReadDestinationValueFunctionArguments(t *testing.T) { + t.Parallel() + + host := &mockery.MockVMHost{} + managedType := &mockery.MockManagedTypesContext{} + metering := &mockery.MockMeteringContext{} + + host.On("ManagedTypes").Return(managedType) + host.On("Metering").Return(metering) + gasSchedule, _ := config.CreateGasConfig(config.MakeGasMapForTests()) + metering.On("GasSchedule").Return(gasSchedule) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + metering.On("UseGasBounded", mock.Anything).Return(nil) + + vmInput, err := readDestinationValueFunctionArguments(host, 0, 0, 0, 0) + require.Nil(t, err) + require.NotNil(t, vmInput) +} + +func TestReadDestinationValueArguments(t *testing.T) { + t.Parallel() + + host := &mockery.MockVMHost{} + managedType := &mockery.MockManagedTypesContext{} + metering := &mockery.MockMeteringContext{} + + host.On("ManagedTypes").Return(managedType) + host.On("Metering").Return(metering) + gasSchedule, _ := config.CreateGasConfig(config.MakeGasMapForTests()) + metering.On("GasSchedule").Return(gasSchedule) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + metering.On("UseGasBounded", mock.Anything).Return(nil) + + vmInput, err := readDestinationValueArguments(host, 0, 0, 0) + require.Nil(t, err) + require.NotNil(t, vmInput) +} + +func TestReadDestinationFunctionArguments(t *testing.T) { + t.Parallel() + + host := &mockery.MockVMHost{} + managedType := &mockery.MockManagedTypesContext{} + metering := &mockery.MockMeteringContext{} + + host.On("ManagedTypes").Return(managedType) + host.On("Metering").Return(metering) + gasSchedule, _ := config.CreateGasConfig(config.MakeGasMapForTests()) + metering.On("GasSchedule").Return(gasSchedule) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + metering.On("UseGasBounded", mock.Anything).Return(nil) + + vmInput, err := readDestinationFunctionArguments(host, 0, 0, 0) + require.Nil(t, err) + require.NotNil(t, vmInput) +} + +func TestReadDestinationArguments(t *testing.T) { + t.Parallel() + + host := &mockery.MockVMHost{} + managedType := &mockery.MockManagedTypesContext{} + metering := &mockery.MockMeteringContext{} + + host.On("ManagedTypes").Return(managedType) + host.On("Metering").Return(metering) + gasSchedule, _ := config.CreateGasConfig(config.MakeGasMapForTests()) + metering.On("GasSchedule").Return(gasSchedule) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + metering.On("UseGasBounded", mock.Anything).Return(nil) + + vmInput, err := readDestinationArguments(host, 0, 0) + require.Nil(t, err) + require.NotNil(t, vmInput) +} diff --git a/vmhost/vmhooks/managedei_test.go b/vmhost/vmhooks/managedei_test.go new file mode 100644 index 000000000..0cb5fcb40 --- /dev/null +++ b/vmhost/vmhooks/managedei_test.go @@ -0,0 +1,585 @@ +package vmhooks + +import ( + "math/big" + "testing" + + "github.com/multiversx/mx-chain-core-go/data/esdt" + vmcommon "github.com/multiversx/mx-chain-vm-common-go" + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/stretchr/testify/mock" +) + +func TestVMHooksImpl_ManagedSCAddress(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + runtime := vmHooks.runtime + hooks := vmHooks.hooks + + runtime.On("GetContextAddress").Return([]byte("sc-address")) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedSCAddress(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("sc-address")) +} + +func TestVMHooksImpl_ManagedOwnerAddress(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + blockchain := vmHooks.blockchain + hooks := vmHooks.hooks + + blockchain.On("GetOwnerAddress").Return([]byte("owner-address"), nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedOwnerAddress(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("owner-address")) +} + +func TestVMHooksImpl_ManagedCaller(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + runtime := vmHooks.runtime + hooks := vmHooks.hooks + + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{ + VMInput: vmcommon.VMInput{ + CallerAddr: []byte("caller"), + }, + }) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedCaller(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("caller")) +} + +func TestVMHooksImpl_ManagedGetOriginalCallerAddr(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + runtime := vmHooks.runtime + hooks := vmHooks.hooks + + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{ + VMInput: vmcommon.VMInput{ + OriginalCallerAddr: []byte("original-caller"), + }, + }) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedGetOriginalCallerAddr(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("original-caller")) +} + +func TestVMHooksImpl_ManagedGetRelayerAddr(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + runtime := vmHooks.runtime + hooks := vmHooks.hooks + + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{ + VMInput: vmcommon.VMInput{ + RelayerAddr: []byte("relayer"), + }, + }) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedGetRelayerAddr(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("relayer")) +} + +func TestVMHooksImpl_ManagedSignalError(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + runtime := vmHooks.runtime + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("error"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + runtime.On("SignalUserError", "error").Return() + + hooks.ManagedSignalError(0) + runtime.AssertCalled(t, "SignalUserError", "error") +} + +func TestVMHooksImpl_ManagedWriteLog(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + runtime := vmHooks.runtime + hooks := vmHooks.hooks + output := vmHooks.output + + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("topic")}, uint64(1), nil) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + runtime.On("GetContextAddress").Return([]byte("address")) + output.On("WriteLog", mock.Anything, mock.Anything, mock.Anything).Return() + + hooks.ManagedWriteLog(0, 0) +} + +func TestVMHooksImpl_ManagedGetOriginalTxHash(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + runtime := vmHooks.runtime + hooks := vmHooks.hooks + + runtime.On("GetOriginalTxHash").Return([]byte("tx-hash")) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedGetOriginalTxHash(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("tx-hash")) +} + +func TestVMHooksImpl_ManagedGetStateRootHash(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + blockchain := vmHooks.blockchain + hooks := vmHooks.hooks + + blockchain.On("GetStateRootHash").Return([]byte("state-root-hash")) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedGetStateRootHash(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("state-root-hash")) +} + +func TestVMHooksImpl_ManagedGetBlockRandomSeed(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + blockchain := vmHooks.blockchain + hooks := vmHooks.hooks + + blockchain.On("CurrentRandomSeed").Return([]byte("random-seed")) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedGetBlockRandomSeed(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("random-seed")) +} + +func TestVMHooksImpl_ManagedGetPrevBlockRandomSeed(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + blockchain := vmHooks.blockchain + hooks := vmHooks.hooks + + blockchain.On("LastRandomSeed").Return([]byte("random-seed")) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedGetPrevBlockRandomSeed(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("random-seed")) +} + +func TestVMHooksImpl_ManagedGetReturnData(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + output := vmHooks.output + hooks := vmHooks.hooks + + output.On("ReturnData").Return([][]byte{[]byte("data")}) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedGetReturnData(0, 0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("data")) +} + +func TestVMHooksImpl_ManagedGetMultiESDTCallValue(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + runtime := vmHooks.runtime + hooks := vmHooks.hooks + + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{ + VMInput: vmcommon.VMInput{ + ESDTTransfers: []*vmcommon.ESDTTransfer{}, + }, + }) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + + hooks.ManagedGetMultiESDTCallValue(0) +} + +func TestVMHooksImpl_ManagedGetAllTransfersCallValue(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + runtime := vmHooks.runtime + hooks := vmHooks.hooks + + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{ + VMInput: vmcommon.VMInput{ + ESDTTransfers: []*vmcommon.ESDTTransfer{}, + CallValue: big.NewInt(0), + }, + }) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + + hooks.ManagedGetAllTransfersCallValue(0) +} + +func TestVMHooksImpl_ManagedGetBackTransfers(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + hooks := vmHooks.hooks + + managedType.On("GetBackTransfers").Return(nil, big.NewInt(0)) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("GetBigIntOrCreate", mock.Anything).Return(big.NewInt(0)) + + hooks.ManagedGetBackTransfers(0, 0) +} + +func TestVMHooksImpl_ManagedGetESDTBalance(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + blockchain := vmHooks.blockchain + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + blockchain.On("GetESDTToken", mock.Anything, mock.Anything, mock.Anything).Return(&esdt.ESDigitalToken{Value: big.NewInt(100)}, nil) + managedType.On("GetBigIntOrCreate", mock.Anything).Return(big.NewInt(0)) + + hooks.ManagedGetESDTBalance(0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedGetESDTTokenData(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + blockchain := vmHooks.blockchain + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + blockchain.On("GetESDTToken", mock.Anything, mock.Anything, mock.Anything).Return(&esdt.ESDigitalToken{ + Value: big.NewInt(100), + TokenMetaData: &esdt.MetaData{}, + }, nil) + managedType.On("GetBigIntOrCreate", mock.Anything).Return(big.NewInt(0)) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + managedType.On("WriteManagedVecOfManagedBuffers", mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedGetESDTTokenData(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedGetESDTTokenType(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + blockchain := vmHooks.blockchain + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + blockchain.On("GetESDTToken", mock.Anything, mock.Anything, mock.Anything).Return(&esdt.ESDigitalToken{Type: 1}, nil) + managedType.On("GetBigIntOrCreate", mock.Anything).Return(big.NewInt(0)) + + hooks.ManagedGetESDTTokenType(0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedAsyncCall(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + async := vmHooks.async + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + async.On("RegisterLegacyAsyncCall", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedAsyncCall(0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedCreateAsyncCall(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + async := vmHooks.async + hooks := vmHooks.hooks + + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + async.On("RegisterAsyncCall", mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedCreateAsyncCall(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedGetCallbackClosure(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + async := vmHooks.async + hooks := vmHooks.hooks + + async.On("GetCallbackClosure").Return([]byte("closure"), nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedGetCallbackClosure(0) + managedType.AssertCalled(t, "SetBytes", int32(0), []byte("closure")) +} + +func TestVMHooksImpl_ManagedUpgradeFromSourceContract(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + async := vmHooks.async + hooks := vmHooks.hooks + runtime := vmHooks.runtime + blockchain := vmHooks.blockchain + output := vmHooks.output + + output.On("ReturnData").Return([][]byte{[]byte("data")}) + runtime.On("SetRuntimeBreakpointValue", mock.Anything).Return() + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + blockchain.On("GetCode", mock.Anything).Return([]byte("code"), nil) + async.On("RegisterLegacyAsyncCall", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedUpgradeFromSourceContract(0, 0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedUpgradeContract(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + async := vmHooks.async + hooks := vmHooks.hooks + runtime := vmHooks.runtime + output := vmHooks.output + + output.On("ReturnData").Return([][]byte{[]byte("data")}) + runtime.On("SetRuntimeBreakpointValue", mock.Anything).Return() + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + async.On("RegisterLegacyAsyncCall", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedUpgradeContract(0, 0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedDeleteContract(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + async := vmHooks.async + hooks := vmHooks.hooks + runtime := vmHooks.runtime + + runtime.On("SetRuntimeBreakpointValue", mock.Anything).Return() + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + async.On("RegisterLegacyAsyncCall", mock.Anything, mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedDeleteContract(0, 0, 0) +} + +func TestVMHooksImpl_ManagedDeployFromSourceContract(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + host := vmHooks.host + hooks := vmHooks.hooks + runtime := vmHooks.runtime + blockchain := vmHooks.blockchain + output := vmHooks.output + + output.On("ReturnData").Return([][]byte{[]byte("data")}) + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("original-caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + blockchain.On("GetCode", mock.Anything).Return([]byte("code"), nil) + host.On("CreateNewContract", mock.Anything, mock.Anything).Return([]byte("new-address"), nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedDeployFromSourceContract(0, 0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedCreateContract(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + host := vmHooks.host + hooks := vmHooks.hooks + runtime := vmHooks.runtime + output := vmHooks.output + + output.On("ReturnData").Return([][]byte{[]byte("data")}) + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("original-caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + host.On("CreateNewContract", mock.Anything, mock.Anything).Return([]byte("new-address"), nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + + hooks.ManagedCreateContract(0, 0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedExecuteReadOnly(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + host := vmHooks.host + hooks := vmHooks.hooks + runtime := vmHooks.runtime + async := vmHooks.async + output := vmHooks.output + + output.On("ReturnData").Return([][]byte{[]byte("data")}) + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("original-caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + host.On("ExecuteOnDestContext", mock.Anything).Return(&vmcommon.VMOutput{}, true, nil) + host.On("AreInSameShard", mock.Anything, mock.Anything).Return(true) + runtime.On("ReadOnly").Return(false) + runtime.On("SetReadOnly", mock.Anything).Return() + async.On("SetAsyncArgumentsForCall", mock.Anything).Return() + async.On("CompleteChildConditional", mock.Anything, mock.Anything, mock.Anything).Return(nil) + managedType.On("WriteManagedVecOfManagedBuffers", mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedExecuteReadOnly(0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedExecuteOnSameContext(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + host := vmHooks.host + hooks := vmHooks.hooks + runtime := vmHooks.runtime + output := vmHooks.output + + output.On("ReturnData").Return([][]byte{[]byte("data")}) + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("original-caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + host.On("AreInSameShard", mock.Anything, mock.Anything).Return(true) + host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + host.On("ExecuteOnSameContext", mock.Anything).Return(nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + managedType.On("WriteManagedVecOfManagedBuffers", mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedExecuteOnSameContext(0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedExecuteOnDestContext(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + host := vmHooks.host + hooks := vmHooks.hooks + runtime := vmHooks.runtime + async := vmHooks.async + output := vmHooks.output + + output.On("ReturnData").Return([][]byte{[]byte("data")}) + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("original-caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + host.On("ExecuteOnDestContext", mock.Anything).Return(&vmcommon.VMOutput{}, true, nil) + host.On("CompleteLogEntriesWithCallType", mock.Anything, mock.Anything).Return() + host.On("AreInSameShard", mock.Anything, mock.Anything).Return(true) + async.On("SetAsyncArgumentsForCall", mock.Anything).Return() + async.On("CompleteChildConditional", mock.Anything, mock.Anything, mock.Anything).Return(nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + managedType.On("WriteManagedVecOfManagedBuffers", mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedExecuteOnDestContext(0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedExecuteOnDestContextWithErrorReturn(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + host := vmHooks.host + hooks := vmHooks.hooks + runtime := vmHooks.runtime + async := vmHooks.async + output := vmHooks.output + + output.On("ReturnData").Return([][]byte{[]byte("data")}) + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("original-caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("GetBigInt", mock.Anything).Return(big.NewInt(100), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + managedType.On("SetBytes", mock.Anything, mock.Anything).Return() + host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + host.On("ExecuteOnDestContext", mock.Anything).Return(&vmcommon.VMOutput{}, true, nil) + host.On("CompleteLogEntriesWithCallType", mock.Anything, mock.Anything).Return() + host.On("AreInSameShard", mock.Anything, mock.Anything).Return() + async.On("SetAsyncArgumentsForCall", mock.Anything).Return() + async.On("CompleteChildConditional", mock.Anything, mock.Anything, mock.Anything).Return(nil) + managedType.On("WriteManagedVecOfManagedBuffers", mock.Anything, mock.Anything).Return(nil) + + hooks.ManagedExecuteOnDestContextWithErrorReturn(0, 0, 0, 0, 0, 0) +} + +func TestVMHooksImpl_ManagedMultiTransferESDTNFTExecute(t *testing.T) { + t.Parallel() + vmHooks := createHooksWithBaseSetup() + managedType := vmHooks.managedType + host := vmHooks.host + hooks := vmHooks.hooks + runtime := vmHooks.runtime + async := vmHooks.async + + runtime.On("GetContextAddress").Return([]byte("sender")) + runtime.On("GetOriginalCallerAddress").Return([]byte("original-caller")) + runtime.On("GetVMInput").Return(&vmcommon.ContractCallInput{}) + managedType.On("GetBytes", mock.Anything).Return([]byte("data"), nil) + managedType.On("ReadManagedVecOfManagedBuffers", mock.Anything).Return([][]byte{[]byte("arg1")}, uint64(1), nil) + managedType.On("ConsumeGasForBytes", mock.Anything).Return(nil) + host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + host.On("ExecuteOnDestContext", mock.Anything).Return(&vmcommon.VMOutput{}, true, nil) + host.On("CompleteLogEntriesWithCallType", mock.Anything, mock.Anything).Return() + async.On("SetAsyncArgumentsForCall", mock.Anything).Return() + async.On("CompleteChildConditional", mock.Anything, mock.Anything, mock.Anything).Return(nil) + managedType.On("WriteManagedVecOfManagedBuffers", mock.Anything, mock.Anything).Return(nil) + output := &mockery.MockOutputContext{} + host.On("Output").Return(output) + output.On("TransferESDT", mock.Anything, mock.Anything).Return(uint64(0), nil) + blockchain := &mockery.MockBlockchainContext{} + host.On("Blockchain").Return(blockchain) + blockchain.On("GetSnapshot").Return(0) + + hooks.ManagedMultiTransferESDTNFTExecute(0, 0, 0, 0, 0) +} diff --git a/vmhost/vmhooks/smallIntOps_test.go b/vmhost/vmhooks/smallIntOps_test.go new file mode 100644 index 000000000..57c9bb514 --- /dev/null +++ b/vmhost/vmhooks/smallIntOps_test.go @@ -0,0 +1,104 @@ +package vmhooks + +import ( + "math/big" + "testing" + + "github.com/multiversx/mx-chain-vm-go/vmhost" + twos "github.com/multiversx/mx-components-big-int/twos-complement" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestVMHooksImpl_SmallIntGetUnsignedArgument(t *testing.T) { + t.Parallel() + + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + t.Run("should work", func(t *testing.T) { + runtime.On("Arguments").Return([][]byte{big.NewInt(42).Bytes()}) + val := hooks.SmallIntGetUnsignedArgument(0) + require.Equal(t, int64(42), val) + }) + + t.Run("out of range", func(t *testing.T) { + runtime.On("Arguments").Return([][]byte{}) + hooks.SmallIntGetUnsignedArgument(0) + // expect fail execution + }) +} + +func TestVMHooksImpl_SmallIntGetSignedArgument(t *testing.T) { + t.Parallel() + + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + + t.Run("should work", func(t *testing.T) { + runtime.On("Arguments").Return([][]byte{twos.ToBytes(big.NewInt(-42))}) + val := hooks.SmallIntGetSignedArgument(0) + require.Equal(t, int64(-42), val) + }) +} + +func TestVMHooksImpl_SmallIntFinishUnsigned(t *testing.T) { + t.Parallel() + + hooks, _, _, metering, output, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + output.On("Finish", mock.Anything).Return() + + hooks.SmallIntFinishUnsigned(42) + output.AssertCalled(t, "Finish", big.NewInt(42).Bytes()) +} + +func TestVMHooksImpl_SmallIntFinishSigned(t *testing.T) { + t.Parallel() + + hooks, _, _, metering, output, _ := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + output.On("Finish", mock.Anything).Return() + + hooks.SmallIntFinishSigned(-42) +} + +func TestVMHooksImpl_SmallIntStorageStoreUnsigned(t *testing.T) { + t.Parallel() + + hooks, _, _, metering, _, storage := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + storage.On("SetStorage", mock.Anything, mock.Anything).Return(vmhost.StorageAdded, nil) + hooks.SmallIntStorageStoreUnsigned(0, 0, 42) +} + +func TestVMHooksImpl_SmallIntStorageStoreSigned(t *testing.T) { + t.Parallel() + + hooks, _, _, metering, _, storage := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + storage.On("SetStorage", mock.Anything, mock.Anything).Return(vmhost.StorageAdded, nil) + hooks.SmallIntStorageStoreSigned(0, 0, -42) +} + +func TestVMHooksImpl_SmallIntStorageLoadUnsigned(t *testing.T) { + t.Parallel() + + hooks, _, _, metering, _, storage := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + storage.On("GetStorage", mock.Anything).Return(big.NewInt(42).Bytes(), uint32(0), false, nil) + storage.On("UseGasForStorageLoad", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + val := hooks.SmallIntStorageLoadUnsigned(0, 0) + require.Equal(t, int64(42), val) +} + +func TestVMHooksImpl_SmallIntStorageLoadSigned(t *testing.T) { + t.Parallel() + + hooks, _, _, metering, _, storage := createTestVMHooks() + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + storage.On("GetStorage", mock.Anything).Return(twos.ToBytes(big.NewInt(-42)), uint32(0), false, nil) + storage.On("UseGasForStorageLoad", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + val := hooks.SmallIntStorageLoadSigned(0, 0) + require.Equal(t, int64(-42), val) +} diff --git a/vmhost/vmhooks/test_utils_test.go b/vmhost/vmhooks/test_utils_test.go new file mode 100644 index 000000000..62daa9f77 --- /dev/null +++ b/vmhost/vmhooks/test_utils_test.go @@ -0,0 +1,115 @@ +package vmhooks + +import ( + mock2 "github.com/multiversx/mx-chain-vm-common-go/mock" + "github.com/multiversx/mx-chain-vm-go/config" + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/stretchr/testify/mock" +) + +type mockeryStruct struct { + hooks *VMHooksImpl + host *mockery.MockVMHost + runtime *mockery.MockRuntimeContext + metering *mockery.MockMeteringContext + output *mockery.MockOutputContext + storage *mockery.MockStorageContext + blockchain *mockery.MockBlockchainContext + managedType *mockery.MockManagedTypesContext + async *mockery.MockAsyncContext + instance *mockery.MockInstance +} + +func createTestVMHooks() (*VMHooksImpl, *mockery.MockVMHost, *mockery.MockRuntimeContext, *mockery.MockMeteringContext, *mockery.MockOutputContext, *mockery.MockStorageContext) { + hooks, host, runtime, metering, output, storage, _, _ := createTestVMHooksFull() + return hooks, host, runtime, metering, output, storage +} + +func createTestVMHooksFull() (*VMHooksImpl, *mockery.MockVMHost, *mockery.MockRuntimeContext, *mockery.MockMeteringContext, *mockery.MockOutputContext, *mockery.MockStorageContext, *mockery.MockBlockchainContext, *mockery.MockManagedTypesContext) { + vmHooks := createTestVMHooksClear() + + vmHooks.host.On("Runtime").Return(vmHooks.runtime) + vmHooks.host.On("Metering").Return(vmHooks.metering) + vmHooks.host.On("Output").Return(vmHooks.output) + vmHooks.host.On("Storage").Return(vmHooks.storage) + vmHooks.host.On("Blockchain").Return(vmHooks.blockchain) + vmHooks.host.On("ManagedTypes").Return(vmHooks.managedType) + + vmHooks.host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + vmHooks.host.On("IsBuiltinFunctionCall", mock.Anything).Return(false) + vmHooks.runtime.On("FailExecution", mock.Anything).Return() + + baseInstanceSetup(vmHooks.runtime, vmHooks.instance) + + baseMeteringSetup(vmHooks.metering) + + vmHooks.blockchain.On("LastRandomSeed").Return([]byte("rand")) + vmHooks.blockchain.On("RoundTime").Return(uint64(6000)) + vmHooks.blockchain.On("LastRound").Return(uint64(6000)) + vmHooks.blockchain.On("EpochStartBlockRound").Return(uint64(6000)) + vmHooks.blockchain.On("EpochStartBlockTimeStampMs").Return(uint64(12345000)) + vmHooks.blockchain.On("GetCode", mock.Anything).Return([]byte("code"), nil) + + return vmHooks.hooks, vmHooks.host, vmHooks.runtime, vmHooks.metering, vmHooks.output, vmHooks.storage, vmHooks.blockchain, vmHooks.managedType +} + +func createTestVMHooksClear() *mockeryStruct { + vmHooksMockery := &mockeryStruct{} + + vmHooksMockery.host = &mockery.MockVMHost{} + vmHooksMockery.runtime = &mockery.MockRuntimeContext{} + vmHooksMockery.metering = &mockery.MockMeteringContext{} + vmHooksMockery.output = &mockery.MockOutputContext{} + vmHooksMockery.storage = &mockery.MockStorageContext{} + vmHooksMockery.instance = &mockery.MockInstance{} + vmHooksMockery.blockchain = &mockery.MockBlockchainContext{} + vmHooksMockery.managedType = &mockery.MockManagedTypesContext{} + vmHooksMockery.async = &mockery.MockAsyncContext{} + + vmHooksMockery.host.On("Runtime").Return(vmHooksMockery.runtime) + vmHooksMockery.host.On("Metering").Return(vmHooksMockery.metering) + vmHooksMockery.host.On("Output").Return(vmHooksMockery.output) + vmHooksMockery.host.On("Storage").Return(vmHooksMockery.storage) + vmHooksMockery.host.On("Blockchain").Return(vmHooksMockery.blockchain) + vmHooksMockery.host.On("ManagedTypes").Return(vmHooksMockery.managedType) + vmHooksMockery.host.On("Async").Return(vmHooksMockery.async) + vmHooksMockery.host.On("EnableEpochsHandler").Return(&mock2.EnableEpochsHandlerStub{}) + + vmHooksMockery.hooks = NewVMHooksImpl(vmHooksMockery.host) + return vmHooksMockery +} + +func baseInstanceSetup(runtime *mockery.MockRuntimeContext, instance *mockery.MockInstance) { + runtime.On("GetInstance").Return(instance) + instance.On("MemLoad", mock.Anything, mock.Anything).Return(nil, nil) + instance.On("MemStore", mock.Anything, mock.Anything).Return(nil) +} + +func baseMeteringSetup(metering *mockery.MockMeteringContext) { + gasSchedule, _ := config.CreateGasConfig(config.MakeGasMapForTests()) + metering.On("GasSchedule").Return(gasSchedule) + metering.On("StartGasTracing", mock.Anything) + metering.On("UseGasBounded", mock.Anything).Return(nil) + metering.On("UseGasBoundedAndAddTracedGas", mock.Anything, mock.Anything).Return(nil) + metering.On("GasLeft").Return(uint64(100)) + metering.On("BoundGasLimit", mock.Anything).Return(uint64(100)) +} + +func createTestVMHooksWithSetMetering() (*VMHooksImpl, *mockery.MockVMHost, *mockery.MockRuntimeContext, *mockery.MockMeteringContext, *mockery.MockOutputContext, *mockery.MockStorageContext, *mockery.MockBlockchainContext, *mockery.MockManagedTypesContext, *mockery.MockAsyncContext, *mockery.MockInstance) { + vmHooks := createTestVMHooksClear() + baseMeteringSetup(vmHooks.metering) + baseInstanceSetup(vmHooks.runtime, vmHooks.instance) + return vmHooks.hooks, vmHooks.host, vmHooks.runtime, vmHooks.metering, vmHooks.output, vmHooks.storage, vmHooks.blockchain, vmHooks.managedType, vmHooks.async, vmHooks.instance +} + +func createHooksWithBaseSetup() *mockeryStruct { + vmHooks := createTestVMHooksClear() + baseMeteringSetup(vmHooks.metering) + baseInstanceSetup(vmHooks.runtime, vmHooks.instance) + vmHooks.host.On("IsBuiltinFunctionName", mock.Anything).Return(false) + vmHooks.host.On("IsBuiltinFunctionCall", mock.Anything).Return(false) + vmHooks.runtime.On("FailExecution", mock.Anything).Return() + vmHooks.runtime.On("SetRuntimeBreakpointValue", mock.Anything).Return() + + return vmHooks +} diff --git a/vmhost/vmhooks/vmHooksImpl_test.go b/vmhost/vmhooks/vmHooksImpl_test.go new file mode 100644 index 000000000..9950f8b31 --- /dev/null +++ b/vmhost/vmhooks/vmHooksImpl_test.go @@ -0,0 +1,92 @@ +package vmhooks + +import ( + "errors" + "testing" + + "github.com/multiversx/mx-chain-vm-go/mock/mockery" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestNewVMHooksImpl(t *testing.T) { + t.Parallel() + host := &mockery.MockVMHost{} + hooks := NewVMHooksImpl(host) + require.NotNil(t, hooks) + require.Equal(t, host, hooks.host) +} + +func TestVMHooksImpl_MemLoad(t *testing.T) { + t.Parallel() + vmHooks := createTestVMHooksClear() + instance := vmHooks.instance + runtime := vmHooks.runtime + hooks := vmHooks.hooks + + runtime.On("GetInstance").Return(instance) + instance.On("MemLoad", mock.Anything, mock.Anything).Return([]byte("data"), nil) + + data, err := hooks.MemLoad(0, 0) + require.Nil(t, err) + require.Equal(t, []byte("data"), data) +} + +func TestVMHooksImpl_MemLoadMultiple(t *testing.T) { + t.Parallel() + hooks, _, runtime, _, _, _ := createTestVMHooks() + instance := &mockery.MockInstance{} + runtime.On("GetInstance").Return(instance) + instance.On("MemLoad", mock.Anything, mock.Anything).Return([]byte("data"), nil) + + data, err := hooks.MemLoadMultiple(0, []int32{4, 4}) + require.Nil(t, err) + require.Len(t, data, 2) +} + +func TestVMHooksImpl_MemStore(t *testing.T) { + t.Parallel() + hooks, _, runtime, _, _, _ := createTestVMHooks() + instance := &mockery.MockInstance{} + runtime.On("GetInstance").Return(instance) + instance.On("MemStore", mock.Anything, mock.Anything).Return(nil) + + err := hooks.MemStore(0, []byte("data")) + require.Nil(t, err) +} + +func TestVMHooksImpl_Getters(t *testing.T) { + t.Parallel() + hooks, host, runtime, metering, output, storage := createTestVMHooks() + + require.Equal(t, host, hooks.GetVMHost()) + require.Equal(t, host.Blockchain(), hooks.GetBlockchainContext()) + require.Equal(t, runtime, hooks.GetRuntimeContext()) + require.Equal(t, host.ManagedTypes(), hooks.GetManagedTypesContext()) + require.Equal(t, output, hooks.GetOutputContext()) + require.Equal(t, metering, hooks.GetMeteringContext()) + require.Equal(t, storage, hooks.GetStorageContext()) + require.Equal(t, host.EnableEpochsHandler(), hooks.GetEnableEpochsHandler()) +} + +func TestVMHooksImpl_FailExecution(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("GasLeft").Return(uint64(1000)) + metering.On("UseGasBounded", mock.Anything).Return(nil) + runtime.On("FailExecution", mock.Anything).Return() + + hooks.FailExecution(errors.New("test error")) + runtime.AssertCalled(t, "FailExecution", mock.Anything) +} + +func TestFailExecution(t *testing.T) { + t.Parallel() + hooks, _, runtime, metering, _, _ := createTestVMHooks() + metering.On("GasLeft").Return(uint64(1000)) + metering.On("UseGasBounded", mock.Anything).Return(nil) + runtime.On("FailExecution", mock.Anything).Return() + + FailExecution(hooks.GetVMHost(), errors.New("test error")) + runtime.AssertCalled(t, "FailExecution", mock.Anything) +}