diff --git a/go.mod b/go.mod index fd874e34e..b2e3cf4af 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/multiversx/mx-chain-crypto-go v1.2.8 github.com/multiversx/mx-chain-logger-go v1.0.13 github.com/multiversx/mx-chain-scenario-go v1.2.1 - github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230828083058-8acb3c9d0f87 + github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230901130135-4d211b1a41d2 github.com/multiversx/mx-components-big-int v1.0.0 github.com/pelletier/go-toml v1.9.3 github.com/stretchr/testify v1.8.1 diff --git a/go.sum b/go.sum index 44c1877c1..71a1cf546 100644 --- a/go.sum +++ b/go.sum @@ -89,8 +89,8 @@ github.com/multiversx/mx-chain-logger-go v1.0.13 h1:eru/TETo0MkO4ZTnXsQDKf4PBRpA github.com/multiversx/mx-chain-logger-go v1.0.13/go.mod h1:MZJhTAtZTJxT+yK2EHc4ZW3YOHUc1UdjCD0iahRNBZk= github.com/multiversx/mx-chain-scenario-go v1.2.1 h1:9eC6VcOEAKRRKZ7EbSWPLzCdNIMWwuNBtAZlgR4cSMA= github.com/multiversx/mx-chain-scenario-go v1.2.1/go.mod h1:EuZY7DpNFHVNSxJR8dKE1z2I8gBYfEFFPSwNUOXptqE= -github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230828083058-8acb3c9d0f87 h1:66UVkqfJPw+IOtCtYfFkl5MLfopzLUCa7FpxWInHTwU= -github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230828083058-8acb3c9d0f87/go.mod h1:uDaefuPXa9p0sb+wrUk16J0z+U324Hh894Y5oVXeVdI= +github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230901130135-4d211b1a41d2 h1:5ZyyNUvyV4QLIjz/tHgqSDC99FmF3H2q2p3NajOTkVc= +github.com/multiversx/mx-chain-vm-common-go v1.5.6-0.20230901130135-4d211b1a41d2/go.mod h1:uDaefuPXa9p0sb+wrUk16J0z+U324Hh894Y5oVXeVdI= github.com/multiversx/mx-components-big-int v1.0.0 h1:Wkr8lSzK2nDqixOrrBa47VNuqdhV1m/aJhaP1EMaiS8= github.com/multiversx/mx-components-big-int v1.0.0/go.mod h1:maIEMgHlNE2u78JaDD0oLzri+ShgU4okHfzP3LWGdQM= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= diff --git a/mock/context/vmHostMock.go b/mock/context/vmHostMock.go index d0bb8f4a4..b2666938e 100644 --- a/mock/context/vmHostMock.go +++ b/mock/context/vmHostMock.go @@ -23,7 +23,7 @@ type VMHostMock struct { OutputContext vmhost.OutputContext MeteringContext vmhost.MeteringContext StorageContext vmhost.StorageContext - EnableEpochsHandlerField vmcommon.EnableEpochsHandler + EnableEpochsHandlerField vmhost.EnableEpochsHandler ManagedTypesContext vmhost.ManagedTypesContext IsBuiltinFunc bool @@ -71,7 +71,7 @@ func (host *VMHostMock) Storage() vmhost.StorageContext { } // EnableEpochsHandler mocked method -func (host *VMHostMock) EnableEpochsHandler() vmcommon.EnableEpochsHandler { +func (host *VMHostMock) EnableEpochsHandler() vmhost.EnableEpochsHandler { return host.EnableEpochsHandlerField } diff --git a/mock/context/vmHostStub.go b/mock/context/vmHostStub.go index 9b4557592..22d1b5533 100644 --- a/mock/context/vmHostStub.go +++ b/mock/context/vmHostStub.go @@ -25,7 +25,7 @@ type VMHostStub struct { MeteringCalled func() vmhost.MeteringContext AsyncCalled func() vmhost.AsyncContext StorageCalled func() vmhost.StorageContext - EnableEpochsHandlerCalled func() vmcommon.EnableEpochsHandler + EnableEpochsHandlerCalled func() vmhost.EnableEpochsHandler GetContextsCalled func() (vmhost.ManagedTypesContext, vmhost.BlockchainContext, vmhost.MeteringContext, vmhost.OutputContext, vmhost.RuntimeContext, vmhost.AsyncContext, vmhost.StorageContext) ManagedTypesCalled func() vmhost.ManagedTypesContext @@ -160,7 +160,7 @@ func (vhs *VMHostStub) Storage() vmhost.StorageContext { } // EnableEpochsHandler mocked method -func (vhs *VMHostStub) EnableEpochsHandler() vmcommon.EnableEpochsHandler { +func (vhs *VMHostStub) EnableEpochsHandler() vmhost.EnableEpochsHandler { if vhs.EnableEpochsHandlerCalled != nil { return vhs.EnableEpochsHandlerCalled() } diff --git a/mock/world/builtinFunctionsWrapper.go b/mock/world/builtinFunctionsWrapper.go index 1871957ab..d19592e2b 100644 --- a/mock/world/builtinFunctionsWrapper.go +++ b/mock/world/builtinFunctionsWrapper.go @@ -3,10 +3,12 @@ package worldmock import ( "bytes" + "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-core-go/marshal" vmcommon "github.com/multiversx/mx-chain-vm-common-go" "github.com/multiversx/mx-chain-vm-common-go/builtInFunctions" + "github.com/multiversx/mx-chain-vm-common-go/mock" "github.com/multiversx/mx-chain-vm-go/config" ) @@ -33,13 +35,30 @@ func NewBuiltinFunctionsWrapper( dnsMap := makeDNSAddresses(numDNSAddresses) argsBuiltIn := builtInFunctions.ArgsCreateBuiltInFunctionContainer{ - GasMap: gasMap, - MapDNSAddresses: dnsMap, - MapDNSV2Addresses: dnsMap, - Marshalizer: WorldMarshalizer, - Accounts: world.AccountsAdapter, - ShardCoordinator: world, - EnableEpochsHandler: EnableEpochsHandlerStubAllFlags(), + GasMap: gasMap, + MapDNSAddresses: dnsMap, + MapDNSV2Addresses: dnsMap, + Marshalizer: WorldMarshalizer, + Accounts: world.AccountsAdapter, + ShardCoordinator: world, + EnableEpochsHandler: &mock.EnableEpochsHandlerStub{ + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == builtInFunctions.CheckCorrectTokenIDForTransferRoleFlag || + flag == builtInFunctions.ESDTTransferRoleFlag || + flag == builtInFunctions.GlobalMintBurnFlag || + flag == builtInFunctions.TransferToMetaFlag || + flag == builtInFunctions.CheckFrozenCollectionFlag || + flag == builtInFunctions.FixAsyncCallbackCheckFlag || + flag == builtInFunctions.ESDTNFTImprovementV1Flag || + flag == builtInFunctions.SaveToSystemAccountFlag || + flag == builtInFunctions.ValueLengthCheckFlag || + flag == builtInFunctions.CheckFunctionArgumentFlag || + flag == builtInFunctions.FixOldTokenLiquidityFlag || + flag == builtInFunctions.AlwaysSaveTokenMetaDataFlag || + flag == builtInFunctions.SetGuardianFlag || + flag == builtInFunctions.ScToScLogEventFlag + }, + }, GuardedAccountHandler: world.GuardedAccountHandler, MaxNumOfAddressesForTransferRole: 100, } diff --git a/mock/world/enableEpochsHandlerStub.go b/mock/world/enableEpochsHandlerStub.go index 615d8cb53..5b36ed100 100644 --- a/mock/world/enableEpochsHandlerStub.go +++ b/mock/world/enableEpochsHandlerStub.go @@ -1,305 +1,50 @@ package worldmock -import vmcommon "github.com/multiversx/mx-chain-vm-common-go" +import ( + "github.com/multiversx/mx-chain-core-go/core" + "github.com/multiversx/mx-chain-vm-go/vmhost" +) -var _ vmcommon.EnableEpochsHandler = (*EnableEpochsHandlerStub)(nil) +var _ vmhost.EnableEpochsHandler = (*EnableEpochsHandlerStub)(nil) // EnableEpochsHandlerStub - type EnableEpochsHandlerStub struct { - CheckExecuteReadOnlyEnableEpochField uint32 - CreateNFTThroughExecByCallerEnableEpochField uint32 - DisableExecByCallerEnableEpochField uint32 - FixFailExecutionOnErrorEnableEpochField uint32 - FixOOGReturnCodeEnableEpochField uint32 - IsAheadOfTimeGasUsageFlagEnabledField bool - IsBuiltInFunctionsFlagEnabledField bool - IsCheckCorrectTokenIDForTransferRoleFlagEnabledField bool - IsCheckExecuteOnReadOnlyFlagEnabledField bool - IsCheckFrozenCollectionFlagEnabledField bool - IsCheckFunctionArgumentFlagEnabledField bool - IsCheckTransferFlagEnabledField bool - IsCreateNFTThroughExecByCallerFlagEnabledField bool - IsDisableExecByCallerFlagEnabledField bool - IsESDTNFTImprovementV1FlagEnabledField bool - IsESDTTransferRoleFlagEnabledField bool - IsFailExecutionOnEveryAPIErrorFlagEnabledField bool - IsFixAsyncCallbackCheckFlagEnabledField bool - IsFixOOGReturnCodeFlagEnabledField bool - IsFixOldTokenLiquidityEnabledField bool - IsGlobalMintBurnFlagEnabledField bool - IsManagedCryptoAPIsFlagEnabledField bool - IsMultiESDTTransferFixOnCallBackFlagEnabledField bool - IsRefactorContextFlagEnabledField bool - IsRemoveNonUpdatedStorageFlagEnabledField bool - IsRepairCallbackFlagEnabledField bool - IsSCDeployFlagEnabledField bool - IsSaveToSystemAccountFlagEnabledField bool - IsSendAlwaysFlagEnabledField bool - IsStorageAPICostOptimizationFlagEnabledField bool - IsTransferToMetaFlagEnabledField bool - IsValueLengthCheckFlagEnabledField bool - ManagedCryptoAPIEnableEpochField uint32 - MultiESDTTransferAsyncCallBackEnableEpochField uint32 - RefactorContextEnableEpochField uint32 - RemoveNonUpdatedStorageEnableEpochField uint32 - StorageAPICostOptimizationEnableEpochField uint32 - IsRuntimeMemStoreLimitEnabledField bool - IsMaxBlockchainHookCountersFlagEnabledField bool - IsWipeSingleNFTLiquidityDecreaseEnabledField bool - IsAlwaysSaveTokenMetaDataEnabledField bool - IsDynamicGasCostForDataTrieStorageLoadEnabledField bool - IsRuntimeCodeSizeFixEnabledField bool - IsSetGuardianEnabledField bool - IsGuardAccountEnabledField bool - IsChangeUsernameEnabledField bool - IsConsistentTokensValuesLengthCheckEnabledField bool - IsAutoBalanceDataTriesEnabledField bool - ScToScLogEventEnabledField bool + IsFlagDefinedCalled func(flag core.EnableEpochFlag) bool + IsFlagEnabledCalled func(flag core.EnableEpochFlag) bool + IsFlagEnabledInEpochCalled func(flag core.EnableEpochFlag, epoch uint32) bool + GetActivationEpochCalled func(flag core.EnableEpochFlag) uint32 } -// IsGuardAccountEnabled - -func (stub *EnableEpochsHandlerStub) IsGuardAccountEnabled() bool { - return stub.IsGuardAccountEnabledField -} - -// IsGlobalMintBurnFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsGlobalMintBurnFlagEnabled() bool { - return stub.IsGlobalMintBurnFlagEnabledField -} - -// IsESDTTransferRoleFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsESDTTransferRoleFlagEnabled() bool { - return stub.IsESDTTransferRoleFlagEnabledField -} - -// IsBuiltInFunctionsFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsBuiltInFunctionsFlagEnabled() bool { - return stub.IsBuiltInFunctionsFlagEnabledField -} - -// IsCheckCorrectTokenIDForTransferRoleFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsCheckCorrectTokenIDForTransferRoleFlagEnabled() bool { - return stub.IsCheckCorrectTokenIDForTransferRoleFlagEnabledField -} - -// IsMultiESDTTransferFixOnCallBackFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsMultiESDTTransferFixOnCallBackFlagEnabled() bool { - return stub.IsMultiESDTTransferFixOnCallBackFlagEnabledField -} - -// IsFixOOGReturnCodeFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsFixOOGReturnCodeFlagEnabled() bool { - return stub.IsFixOOGReturnCodeFlagEnabledField -} - -// IsRemoveNonUpdatedStorageFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsRemoveNonUpdatedStorageFlagEnabled() bool { - return stub.IsRemoveNonUpdatedStorageFlagEnabledField -} - -// IsCreateNFTThroughExecByCallerFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsCreateNFTThroughExecByCallerFlagEnabled() bool { - return stub.IsCreateNFTThroughExecByCallerFlagEnabledField -} - -// IsStorageAPICostOptimizationFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsStorageAPICostOptimizationFlagEnabled() bool { - return stub.IsStorageAPICostOptimizationFlagEnabledField -} - -// IsFailExecutionOnEveryAPIErrorFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsFailExecutionOnEveryAPIErrorFlagEnabled() bool { - return stub.IsFailExecutionOnEveryAPIErrorFlagEnabledField -} - -// IsManagedCryptoAPIsFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsManagedCryptoAPIsFlagEnabled() bool { - return stub.IsManagedCryptoAPIsFlagEnabledField -} - -// IsSCDeployFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsSCDeployFlagEnabled() bool { - return stub.IsSCDeployFlagEnabledField -} - -// IsAheadOfTimeGasUsageFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsAheadOfTimeGasUsageFlagEnabled() bool { - return stub.IsAheadOfTimeGasUsageFlagEnabledField -} - -// IsRepairCallbackFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsRepairCallbackFlagEnabled() bool { - return stub.IsRepairCallbackFlagEnabledField -} - -// IsDisableExecByCallerFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsDisableExecByCallerFlagEnabled() bool { - return stub.IsDisableExecByCallerFlagEnabledField -} - -// IsRefactorContextFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsRefactorContextFlagEnabled() bool { - return stub.IsRefactorContextFlagEnabledField -} - -// IsCheckFunctionArgumentFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsCheckFunctionArgumentFlagEnabled() bool { - return stub.IsCheckFunctionArgumentFlagEnabledField -} - -// IsCheckExecuteOnReadOnlyFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsCheckExecuteOnReadOnlyFlagEnabled() bool { - return stub.IsCheckExecuteOnReadOnlyFlagEnabledField -} - -// IsFixAsyncCallbackCheckFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsFixAsyncCallbackCheckFlagEnabled() bool { - return stub.IsFixAsyncCallbackCheckFlagEnabledField -} - -// IsSaveToSystemAccountFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsSaveToSystemAccountFlagEnabled() bool { - return stub.IsSaveToSystemAccountFlagEnabledField -} - -// IsCheckFrozenCollectionFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsCheckFrozenCollectionFlagEnabled() bool { - return stub.IsCheckFrozenCollectionFlagEnabledField -} - -// IsSendAlwaysFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsSendAlwaysFlagEnabled() bool { - return stub.IsSendAlwaysFlagEnabledField -} - -// IsValueLengthCheckFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsValueLengthCheckFlagEnabled() bool { - return stub.IsValueLengthCheckFlagEnabledField -} - -// IsCheckTransferFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsCheckTransferFlagEnabled() bool { - return stub.IsCheckTransferFlagEnabledField -} - -// IsTransferToMetaFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsTransferToMetaFlagEnabled() bool { - return stub.IsTransferToMetaFlagEnabledField -} - -// IsESDTNFTImprovementV1FlagEnabled - -func (stub *EnableEpochsHandlerStub) IsESDTNFTImprovementV1FlagEnabled() bool { - return stub.IsESDTNFTImprovementV1FlagEnabledField -} - -// IsFixOldTokenLiquidityEnabled - -func (stub *EnableEpochsHandlerStub) IsFixOldTokenLiquidityEnabled() bool { - return stub.IsFixOldTokenLiquidityEnabledField -} - -// MultiESDTTransferAsyncCallBackEnableEpoch - -func (stub *EnableEpochsHandlerStub) MultiESDTTransferAsyncCallBackEnableEpoch() uint32 { - return stub.MultiESDTTransferAsyncCallBackEnableEpochField -} - -// FixOOGReturnCodeEnableEpoch - -func (stub *EnableEpochsHandlerStub) FixOOGReturnCodeEnableEpoch() uint32 { - return stub.FixOOGReturnCodeEnableEpochField -} - -// RemoveNonUpdatedStorageEnableEpoch - -func (stub *EnableEpochsHandlerStub) RemoveNonUpdatedStorageEnableEpoch() uint32 { - return stub.RemoveNonUpdatedStorageEnableEpochField -} - -// CreateNFTThroughExecByCallerEnableEpoch - -func (stub *EnableEpochsHandlerStub) CreateNFTThroughExecByCallerEnableEpoch() uint32 { - return stub.CreateNFTThroughExecByCallerEnableEpochField -} - -// FixFailExecutionOnErrorEnableEpoch - -func (stub *EnableEpochsHandlerStub) FixFailExecutionOnErrorEnableEpoch() uint32 { - return stub.FixFailExecutionOnErrorEnableEpochField -} - -// ManagedCryptoAPIEnableEpoch - -func (stub *EnableEpochsHandlerStub) ManagedCryptoAPIEnableEpoch() uint32 { - return stub.ManagedCryptoAPIEnableEpochField -} - -// DisableExecByCallerEnableEpoch - -func (stub *EnableEpochsHandlerStub) DisableExecByCallerEnableEpoch() uint32 { - return stub.DisableExecByCallerEnableEpochField -} - -// RefactorContextEnableEpoch - -func (stub *EnableEpochsHandlerStub) RefactorContextEnableEpoch() uint32 { - return stub.RefactorContextEnableEpochField -} - -// CheckExecuteReadOnlyEnableEpoch - -func (stub *EnableEpochsHandlerStub) CheckExecuteReadOnlyEnableEpoch() uint32 { - return stub.CheckExecuteReadOnlyEnableEpochField -} - -// StorageAPICostOptimizationEnableEpoch - -func (stub *EnableEpochsHandlerStub) StorageAPICostOptimizationEnableEpoch() uint32 { - return stub.StorageAPICostOptimizationEnableEpochField -} - -// IsRuntimeMemStoreLimitEnabled - -func (stub *EnableEpochsHandlerStub) IsRuntimeMemStoreLimitEnabled() bool { - return stub.IsRuntimeMemStoreLimitEnabledField -} - -// IsRuntimeCodeSizeFixEnabled - -func (stub *EnableEpochsHandlerStub) IsRuntimeCodeSizeFixEnabled() bool { - return stub.IsRuntimeMemStoreLimitEnabledField -} - -// IsMaxBlockchainHookCountersFlagEnabled - -func (stub *EnableEpochsHandlerStub) IsMaxBlockchainHookCountersFlagEnabled() bool { - return stub.IsMaxBlockchainHookCountersFlagEnabledField -} - -// IsWipeSingleNFTLiquidityDecreaseEnabled - -func (stub *EnableEpochsHandlerStub) IsWipeSingleNFTLiquidityDecreaseEnabled() bool { - return stub.IsWipeSingleNFTLiquidityDecreaseEnabledField -} - -// IsAlwaysSaveTokenMetaDataEnabled - -func (stub *EnableEpochsHandlerStub) IsAlwaysSaveTokenMetaDataEnabled() bool { - return stub.IsAlwaysSaveTokenMetaDataEnabledField -} - -// IsDynamicGasCostForDataTrieStorageLoadEnabled - -func (stub *EnableEpochsHandlerStub) IsDynamicGasCostForDataTrieStorageLoadEnabled() bool { - return stub.IsDynamicGasCostForDataTrieStorageLoadEnabledField -} - -// IsSetGuardianEnabled - -func (stub *EnableEpochsHandlerStub) IsSetGuardianEnabled() bool { - return stub.IsSetGuardianEnabledField -} - -// IsChangeUsernameEnabled - -func (stub *EnableEpochsHandlerStub) IsChangeUsernameEnabled() bool { - return stub.IsChangeUsernameEnabledField +// IsFlagDefined - +func (stub *EnableEpochsHandlerStub) IsFlagDefined(flag core.EnableEpochFlag) bool { + if stub.IsFlagDefinedCalled != nil { + return stub.IsFlagDefinedCalled(flag) + } + return true } -// IsConsistentTokensValuesLengthCheckEnabled - -func (stub *EnableEpochsHandlerStub) IsConsistentTokensValuesLengthCheckEnabled() bool { - return stub.IsConsistentTokensValuesLengthCheckEnabledField +// IsFlagEnabled - +func (stub *EnableEpochsHandlerStub) IsFlagEnabled(flag core.EnableEpochFlag) bool { + if stub.IsFlagEnabledCalled != nil { + return stub.IsFlagEnabledCalled(flag) + } + return false } -// IsAutoBalanceDataTriesEnabled - -func (stub *EnableEpochsHandlerStub) IsAutoBalanceDataTriesEnabled() bool { - return stub.IsAutoBalanceDataTriesEnabledField +// IsFlagEnabledInEpoch - +func (stub *EnableEpochsHandlerStub) IsFlagEnabledInEpoch(flag core.EnableEpochFlag, epoch uint32) bool { + if stub.IsFlagEnabledInEpochCalled != nil { + return stub.IsFlagEnabledInEpochCalled(flag, epoch) + } + return false } -// IsScToScEventLogEnabled - -func (stub *EnableEpochsHandlerStub) IsScToScEventLogEnabled() bool { - return stub.ScToScLogEventEnabledField +// GetActivationEpoch - +func (stub *EnableEpochsHandlerStub) GetActivationEpoch(flag core.EnableEpochFlag) uint32 { + if stub.GetActivationEpochCalled != nil { + return stub.GetActivationEpochCalled(flag) + } + return 0 } // IsInterfaceNil - @@ -310,34 +55,17 @@ func (stub *EnableEpochsHandlerStub) IsInterfaceNil() bool { // EnableEpochsHandlerStubAllFlags creates a new EnableEpochsHandlerStub with all flags enabled func EnableEpochsHandlerStubAllFlags() *EnableEpochsHandlerStub { return &EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, - IsMultiESDTTransferFixOnCallBackFlagEnabledField: true, - IsFixOOGReturnCodeFlagEnabledField: true, - IsRemoveNonUpdatedStorageFlagEnabledField: true, - IsCreateNFTThroughExecByCallerFlagEnabledField: true, - IsManagedCryptoAPIsFlagEnabledField: true, - IsFailExecutionOnEveryAPIErrorFlagEnabledField: true, - IsRefactorContextFlagEnabledField: true, - IsCheckCorrectTokenIDForTransferRoleFlagEnabledField: true, - IsDisableExecByCallerFlagEnabledField: true, - IsESDTTransferRoleFlagEnabledField: true, - IsGlobalMintBurnFlagEnabledField: true, - IsTransferToMetaFlagEnabledField: true, - IsCheckFrozenCollectionFlagEnabledField: true, - IsFixAsyncCallbackCheckFlagEnabledField: true, - IsESDTNFTImprovementV1FlagEnabledField: true, - IsSaveToSystemAccountFlagEnabledField: true, - IsValueLengthCheckFlagEnabledField: true, - IsSCDeployFlagEnabledField: true, - IsRepairCallbackFlagEnabledField: true, - IsAheadOfTimeGasUsageFlagEnabledField: true, - IsCheckFunctionArgumentFlagEnabledField: true, - IsCheckExecuteOnReadOnlyFlagEnabledField: true, - IsFixOldTokenLiquidityEnabledField: true, - IsAlwaysSaveTokenMetaDataEnabledField: true, - IsRuntimeCodeSizeFixEnabledField: true, - IsSetGuardianEnabledField: true, - ScToScLogEventEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag || + flag == vmhost.FixOOGReturnCodeFlag || + flag == vmhost.RemoveNonUpdatedStorageFlag || + flag == vmhost.CreateNFTThroughExecByCallerFlag || + flag == vmhost.ManagedCryptoAPIsFlag || + flag == vmhost.FailExecutionOnEveryAPIErrorFlag || + flag == vmhost.RefactorContextFlag || + flag == vmhost.DisableExecByCallerFlag || + flag == vmhost.CheckExecuteOnReadOnlyFlag + }, } } diff --git a/vmhost/common.go b/vmhost/common.go index 4e72ce469..682005e5b 100644 --- a/vmhost/common.go +++ b/vmhost/common.go @@ -173,7 +173,7 @@ type VMHostParameters struct { ProtectedKeyPrefix []byte WasmerSIGSEGVPassthrough bool EpochNotifier vmcommon.EpochNotifier - EnableEpochsHandler vmcommon.EnableEpochsHandler + EnableEpochsHandler EnableEpochsHandler Hasher HashComputer TimeOutForSCExecutionInMilliseconds uint32 } diff --git a/vmhost/contexts/runtime.go b/vmhost/contexts/runtime.go index e2c0a04cd..8504ef1b9 100644 --- a/vmhost/contexts/runtime.go +++ b/vmhost/contexts/runtime.go @@ -653,7 +653,7 @@ func (context *runtimeContext) VerifyContractCode() error { } enableEpochsHandler := context.host.EnableEpochsHandler() - if enableEpochsHandler.IsManagedCryptoAPIsFlagEnabled() { + if enableEpochsHandler.IsFlagEnabled(vmhost.ManagedCryptoAPIsFlag) { err = context.validator.verifyProtectedFunctions(context.iTracker.Instance()) if err != nil { logRuntime.Trace("verify contract code", "error", err) diff --git a/vmhost/contexts/storage.go b/vmhost/contexts/storage.go index 7b202d8f0..611aee723 100644 --- a/vmhost/contexts/storage.go +++ b/vmhost/contexts/storage.go @@ -128,7 +128,7 @@ func (context *storageContext) GetStorage(key []byte) ([]byte, uint32, bool, err func (context *storageContext) useGasForValueIfNeeded(value []byte, usedCache bool) { metering := context.host.Metering() enableEpochsHandler := context.host.EnableEpochsHandler() - gasFlagSet := enableEpochsHandler.IsStorageAPICostOptimizationFlagEnabled() + gasFlagSet := enableEpochsHandler.IsFlagEnabled(vmhost.StorageAPICostOptimizationFlag) if !usedCache || !gasFlagSet { costPerByte := metering.GasSchedule().BaseOperationCost.DataCopyPerByte gasToUse := math.MulUint64(costPerByte, uint64(len(value))) @@ -144,7 +144,7 @@ func (context *storageContext) useExtraGasForKeyIfNeeded(key []byte, usedCache b return } enableEpochsHandler := context.host.EnableEpochsHandler() - gasFlagSet := enableEpochsHandler.IsStorageAPICostOptimizationFlagEnabled() + gasFlagSet := enableEpochsHandler.IsFlagEnabled(vmhost.StorageAPICostOptimizationFlag) if !gasFlagSet || !usedCache { gasToUse := math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(extraBytes)) // TODO replace UseGas with UseGasBounded @@ -193,7 +193,7 @@ func (context *storageContext) getStorageFromAddressUnmetered(address []byte, ke var trieDepth uint32 enableEpochsHandler := context.host.EnableEpochsHandler() - if context.isProtocolProtectedKey(key) && enableEpochsHandler.IsStorageAPICostOptimizationFlagEnabled() { + if context.isProtocolProtectedKey(key) && enableEpochsHandler.IsFlagEnabled(vmhost.StorageAPICostOptimizationFlag) { value, trieDepth, err = context.readFromBlockchain(address, key) return value, trieDepth, false, err } @@ -434,7 +434,7 @@ func (context *storageContext) computeGasForUnchangedValue(length int, usedCache metering := context.host.Metering() useGas := uint64(0) enableEpochsHandler := context.host.EnableEpochsHandler() - if !usedCache || !enableEpochsHandler.IsStorageAPICostOptimizationFlagEnabled() { + if !usedCache || !enableEpochsHandler.IsFlagEnabled(vmhost.StorageAPICostOptimizationFlag) { useGas = math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(length)) } return useGas @@ -469,7 +469,7 @@ func (context *storageContext) computeGasForKey(key []byte, usedCache bool) uint extraKeyLenGas := uint64(0) enableEpochsHandler := context.host.EnableEpochsHandler() if extraBytes > 0 && - (!usedCache || !enableEpochsHandler.IsStorageAPICostOptimizationFlagEnabled()) { + (!usedCache || !enableEpochsHandler.IsFlagEnabled(vmhost.StorageAPICostOptimizationFlag)) { extraKeyLenGas = math.MulUint64(metering.GasSchedule().BaseOperationCost.DataCopyPerByte, uint64(extraBytes)) } return extraKeyLenGas @@ -487,7 +487,7 @@ func (context *storageContext) UseGasForStorageLoad(tracedFunctionName string, t func (context *storageContext) getBlockchainLoadCost(trieDepth int64, staticGasCost uint64, usedCache bool) (uint64, error) { enableEpochsHandler := context.host.EnableEpochsHandler() - if enableEpochsHandler.IsStorageAPICostOptimizationFlagEnabled() && usedCache { + if enableEpochsHandler.IsFlagEnabled(vmhost.StorageAPICostOptimizationFlag) && usedCache { return context.host.Metering().GasSchedule().BaseOpsAPICost.CachedStorageLoad, nil } @@ -497,7 +497,7 @@ func (context *storageContext) getBlockchainLoadCost(trieDepth int64, staticGasC // IsUseDifferentGasCostFlagSet - getter for flag func (context *storageContext) IsUseDifferentGasCostFlagSet() bool { enableEpochsHandler := context.host.EnableEpochsHandler() - return enableEpochsHandler.IsStorageAPICostOptimizationFlagEnabled() + return enableEpochsHandler.IsFlagEnabled(vmhost.StorageAPICostOptimizationFlag) } // IsInterfaceNil returns true if there is no value under the interface @@ -512,7 +512,7 @@ func (context *storageContext) GetVmProtectedPrefix(prefix string) []byte { // GetStorageLoadCost returns the gas cost for the storage load operation func (context *storageContext) GetStorageLoadCost(trieDepth int64, staticGasCost uint64) (uint64, error) { - if context.host.EnableEpochsHandler().IsDynamicGasCostForDataTrieStorageLoadEnabled() { + if context.host.EnableEpochsHandler().IsFlagEnabled(vmhost.DynamicGasCostForDataTrieStorageLoadFlag) { return computeGasForStorageLoadBasedOnTrieDepth( trieDepth, context.host.Metering().GasSchedule().DynamicStorageLoad, diff --git a/vmhost/contexts/storage_test.go b/vmhost/contexts/storage_test.go index e1ec0b839..89a26eed5 100644 --- a/vmhost/contexts/storage_test.go +++ b/vmhost/contexts/storage_test.go @@ -6,6 +6,7 @@ import ( "math/big" "testing" + "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" vmcommon "github.com/multiversx/mx-chain-vm-common-go" "github.com/multiversx/mx-chain-vm-go/config" @@ -52,7 +53,9 @@ func TestNewStorageContext(t *testing.T) { t.Parallel() enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag + }, } host := &contextmock.VMHostMock{ EnableEpochsHandlerField: enableEpochsHandler, @@ -101,7 +104,9 @@ func TestStorageContext_SetAddress(t *testing.T) { mockMetering.BlockGasLimitMock = uint64(15000) mockMetering.GasLeftMock = 20000 enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag + }, } host := &contextmock.VMHostMock{ @@ -161,7 +166,9 @@ func TestStorageContext_GetStorageUpdates(t *testing.T) { } enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag + }, } host := &contextmock.VMHostMock{ @@ -194,7 +201,9 @@ func TestStorageContext_SetStorage(t *testing.T) { mockMetering.GasLeftMock = 20000 enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag + }, } host := &contextmock.VMHostMock{ @@ -345,7 +354,9 @@ func TestStorageContext_SetStorage_GasUsage(t *testing.T) { mockMetering.BlockGasLimitMock = uint64(15000) enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag + }, } host := &contextmock.VMHostMock{ OutputContext: mockOutput, @@ -413,7 +424,9 @@ func TestStorageContext_StorageProtection(t *testing.T) { mockMetering.GasLeftMock = 20000 enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag + }, } host := &contextmock.VMHostMock{ @@ -463,7 +476,9 @@ func TestStorageContext_GetStorageFromAddress(t *testing.T) { nonreadable := []byte("nonreadable") enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag + }, } mockOutput := &contextmock.OutputContextMock{} @@ -570,7 +585,9 @@ func TestStorageContext_StoreGasPerKey(t *testing.T) { func TestStorageContext_PopSetActiveStateIfStackIsEmptyShouldNotPanic(t *testing.T) { t.Parallel() enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag + }, } host := &contextmock.VMHostMock{ @@ -586,7 +603,9 @@ func TestStorageContext_PopSetActiveStateIfStackIsEmptyShouldNotPanic(t *testing func TestStorageContext_PopDiscardIfStackIsEmptyShouldNotPanic(t *testing.T) { t.Parallel() enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsStorageAPICostOptimizationFlagEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.StorageAPICostOptimizationFlag + }, } host := &contextmock.VMHostMock{ @@ -623,7 +642,9 @@ func TestStorageContext_GetStorageLoadCost(t *testing.T) { t.Parallel() enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsDynamicGasCostForDataTrieStorageLoadEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.DynamicGasCostForDataTrieStorageLoadFlag + }, } mockMetering := &contextmock.MeteringContextMock{ GasCost: &config.GasCost{ @@ -653,7 +674,9 @@ func TestStorageContext_GetStorageLoadCost(t *testing.T) { t.Parallel() enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsDynamicGasCostForDataTrieStorageLoadEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.DynamicGasCostForDataTrieStorageLoadFlag + }, } mockMetering := &contextmock.MeteringContextMock{ GasCost: &config.GasCost{ @@ -683,7 +706,9 @@ func TestStorageContext_GetStorageLoadCost(t *testing.T) { t.Parallel() enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsDynamicGasCostForDataTrieStorageLoadEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.DynamicGasCostForDataTrieStorageLoadFlag + }, } mockMetering := &contextmock.MeteringContextMock{ GasCost: &config.GasCost{ @@ -713,7 +738,9 @@ func TestStorageContext_GetStorageLoadCost(t *testing.T) { t.Parallel() enableEpochsHandler := &worldmock.EnableEpochsHandlerStub{ - IsDynamicGasCostForDataTrieStorageLoadEnabledField: true, + IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool { + return flag == vmhost.DynamicGasCostForDataTrieStorageLoadFlag + }, } mockMetering := &contextmock.MeteringContextMock{ GasCost: &config.GasCost{ diff --git a/vmhost/flags.go b/vmhost/flags.go new file mode 100644 index 000000000..ad1a4cbfe --- /dev/null +++ b/vmhost/flags.go @@ -0,0 +1,32 @@ +package vmhost + +import "github.com/multiversx/mx-chain-core-go/core" + +const ( + // MultiESDTTransferFixOnCallBackFlag defines the flag that activates the multi esdt transfer fix on callback + MultiESDTTransferFixOnCallBackFlag core.EnableEpochFlag = "MultiESDTTransferFixOnCallBackFlag" + // RemoveNonUpdatedStorageFlag defines the flag that activates the remove non updated storage fix + RemoveNonUpdatedStorageFlag core.EnableEpochFlag = "RemoveNonUpdatedStorageFlag" + // CreateNFTThroughExecByCallerFlag defines the flag that activates the create nft through exec by caller fix + CreateNFTThroughExecByCallerFlag core.EnableEpochFlag = "CreateNFTThroughExecByCallerFlag" + // StorageAPICostOptimizationFlag defines the flag that activates the storage api cost optimization + StorageAPICostOptimizationFlag core.EnableEpochFlag = "StorageAPICostOptimizationFlag" + // CheckExecuteOnReadOnlyFlag defines the flag that activates the check execute on read only + CheckExecuteOnReadOnlyFlag core.EnableEpochFlag = "CheckExecuteOnReadOnlyFlag" + // FailExecutionOnEveryAPIErrorFlag defines the flag that activates the fail execution on every api error + FailExecutionOnEveryAPIErrorFlag core.EnableEpochFlag = "FailExecutionOnEveryAPIErrorFlag" + // ManagedCryptoAPIsFlag defines the flag that activates the manage crypto apis + ManagedCryptoAPIsFlag core.EnableEpochFlag = "ManagedCryptoAPIsFlag" + // DisableExecByCallerFlag defines the flag that activates disable exec by caller + DisableExecByCallerFlag core.EnableEpochFlag = "DisableExecByCallerFlag" + // RefactorContextFlag defines the flag that activates the refactor context + RefactorContextFlag core.EnableEpochFlag = "RefactorContextFlag" + // RuntimeMemStoreLimitFlag defines the flag that activates the runtime mem store limit + RuntimeMemStoreLimitFlag core.EnableEpochFlag = "RuntimeMemStoreLimitFlag" + // RuntimeCodeSizeFixFlag defines the flag that activates the runtime code size fix + RuntimeCodeSizeFixFlag core.EnableEpochFlag = "RuntimeCodeSizeFixFlag" + // FixOOGReturnCodeFlag defines the flag that activates the fix oog return code + FixOOGReturnCodeFlag core.EnableEpochFlag = "FixOOGReturnCodeFlag" + // DynamicGasCostForDataTrieStorageLoadFlag defines the flag that activates the dynamic gas cost for data trie storage load + DynamicGasCostForDataTrieStorageLoadFlag core.EnableEpochFlag = "DynamicGasCostForDataTrieStorageLoadFlag" +) diff --git a/vmhost/hostCore/execution.go b/vmhost/hostCore/execution.go index 5a825e386..decd02fcd 100644 --- a/vmhost/hostCore/execution.go +++ b/vmhost/hostCore/execution.go @@ -107,7 +107,7 @@ func (host *vmHost) performCodeDeployment(input vmhost.CodeDeployInput, initFunc } output.DeployCode(input) - if host.enableEpochsHandler.IsRemoveNonUpdatedStorageFlagEnabled() { + if host.enableEpochsHandler.IsFlagEnabled(vmhost.RemoveNonUpdatedStorageFlag) { output.RemoveNonUpdatedStorage() } @@ -270,7 +270,7 @@ func (host *vmHost) doRunSmartContractCall(input *vmcommon.ContractCallInput) *v return vmOutput } - if host.enableEpochsHandler.IsRemoveNonUpdatedStorageFlagEnabled() { + if host.enableEpochsHandler.IsFlagEnabled(vmhost.RemoveNonUpdatedStorageFlag) { output.RemoveNonUpdatedStorage() } vmOutput = output.GetVMOutput() @@ -534,7 +534,7 @@ func (host *vmHost) ExecuteOnSameContext(input *vmcommon.ContractCallInput) erro librarySCAddress := make([]byte, len(input.RecipientAddr)) copy(librarySCAddress, input.RecipientAddr) - if host.enableEpochsHandler.IsRefactorContextFlagEnabled() { + if host.enableEpochsHandler.IsFlagEnabled(vmhost.RefactorContextFlag) { input.RecipientAddr = input.CallerAddr } diff --git a/vmhost/hostCore/host.go b/vmhost/hostCore/host.go index 31401953a..bbc3faa3b 100644 --- a/vmhost/hostCore/host.go +++ b/vmhost/hostCore/host.go @@ -6,6 +6,7 @@ import ( "sync" "time" + "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-core-go/marshal" logger "github.com/multiversx/mx-chain-logger-go" @@ -33,6 +34,23 @@ var _ vmhost.VMHost = (*vmHost)(nil) const minExecutionTimeout = time.Second const internalVMErrors = "internalVMErrors" +// allFlags must have all flags used by mx-chain-vm-go in the current version +var allFlags = []core.EnableEpochFlag{ + vmhost.MultiESDTTransferFixOnCallBackFlag, + vmhost.RemoveNonUpdatedStorageFlag, + vmhost.CreateNFTThroughExecByCallerFlag, + vmhost.StorageAPICostOptimizationFlag, + vmhost.CheckExecuteOnReadOnlyFlag, + vmhost.FailExecutionOnEveryAPIErrorFlag, + vmhost.ManagedCryptoAPIsFlag, + vmhost.DisableExecByCallerFlag, + vmhost.RefactorContextFlag, + vmhost.RuntimeMemStoreLimitFlag, + vmhost.RuntimeCodeSizeFixFlag, + vmhost.FixOOGReturnCodeFlag, + vmhost.DynamicGasCostForDataTrieStorageLoadFlag, +} + // vmHost implements HostContext interface. type vmHost struct { cryptoHook crypto.VMCrypto @@ -54,7 +72,7 @@ type vmHost struct { builtInFuncContainer vmcommon.BuiltInFunctionContainer esdtTransferParser vmcommon.ESDTTransferParser callArgsParser vmhost.CallArgsParser - enableEpochsHandler vmcommon.EnableEpochsHandler + enableEpochsHandler vmhost.EnableEpochsHandler activationEpochMap map[uint32]struct{} transferLogIdentifiers map[string]bool @@ -84,6 +102,10 @@ func NewVMHost( if check.IfNil(hostParameters.EnableEpochsHandler) { return nil, vmhost.ErrNilEnableEpochsHandler } + err := core.CheckHandlerCompatibility(hostParameters.EnableEpochsHandler, allFlags) + if err != nil { + return nil, err + } if check.IfNil(hostParameters.Hasher) { return nil, vmhost.ErrNilHasher } @@ -112,7 +134,6 @@ func NewVMHost( host.executionTimeout = newExecutionTimeout } - var err error host.blockchainContext, err = contexts.NewBlockchainContext(host, blockChainHook) if err != nil { return nil, err @@ -242,7 +263,7 @@ func (host *vmHost) Storage() vmhost.StorageContext { } // EnableEpochsHandler returns the enableEpochsHandler instance of the host -func (host *vmHost) EnableEpochsHandler() vmcommon.EnableEpochsHandler { +func (host *vmHost) EnableEpochsHandler() vmhost.EnableEpochsHandler { return host.enableEpochsHandler } @@ -539,27 +560,27 @@ func (host *vmHost) EpochConfirmed(epoch uint32, _ uint64) { // FixOOGReturnCodeEnabled returns true if the corresponding flag is set func (host *vmHost) FixOOGReturnCodeEnabled() bool { - return host.enableEpochsHandler.IsFixOOGReturnCodeFlagEnabled() + return host.enableEpochsHandler.IsFlagEnabled(vmhost.FixOOGReturnCodeFlag) } // FixFailExecutionEnabled returns true if the corresponding flag is set func (host *vmHost) FixFailExecutionEnabled() bool { - return host.enableEpochsHandler.IsFailExecutionOnEveryAPIErrorFlagEnabled() + return host.enableEpochsHandler.IsFlagEnabled(vmhost.FailExecutionOnEveryAPIErrorFlag) } // CreateNFTOnExecByCallerEnabled returns true if the corresponding flag is set func (host *vmHost) CreateNFTOnExecByCallerEnabled() bool { - return host.enableEpochsHandler.IsCreateNFTThroughExecByCallerFlagEnabled() + return host.enableEpochsHandler.IsFlagEnabled(vmhost.CreateNFTThroughExecByCallerFlag) } // DisableExecByCaller returns true if the corresponding flag is set func (host *vmHost) DisableExecByCaller() bool { - return host.enableEpochsHandler.IsDisableExecByCallerFlagEnabled() + return host.enableEpochsHandler.IsFlagEnabled(vmhost.DisableExecByCallerFlag) } // CheckExecuteReadOnly returns true if the corresponding flag is set func (host *vmHost) CheckExecuteReadOnly() bool { - return host.enableEpochsHandler.IsCheckExecuteOnReadOnlyFlagEnabled() + return host.enableEpochsHandler.IsFlagEnabled(vmhost.CheckExecuteOnReadOnlyFlag) } func (host *vmHost) setGasTracerEnabledIfLogIsTrace() { diff --git a/vmhost/hostCore/host_test.go b/vmhost/hostCore/host_test.go index b4eea05a3..881191e05 100644 --- a/vmhost/hostCore/host_test.go +++ b/vmhost/hostCore/host_test.go @@ -3,6 +3,7 @@ package hostCore import ( "testing" + "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-vm-common-go/builtInFunctions" "github.com/multiversx/mx-chain-vm-common-go/parsers" worldmock "github.com/multiversx/mx-chain-vm-go/mock/world" @@ -69,6 +70,17 @@ func TestNewVMHost(t *testing.T) { require.Nil(t, host) require.ErrorIs(t, err, vmhost.ErrNilEnableEpochsHandler) }) + t.Run("InvalidEnableEpochsHandler", func(t *testing.T) { + hostParameters := makeHostParameters() + hostParameters.EnableEpochsHandler = &worldmock.EnableEpochsHandlerStub{ + IsFlagDefinedCalled: func(flag core.EnableEpochFlag) bool { + return false + }, + } + host, err := NewVMHost(blockchainHook, hostParameters) + require.Nil(t, host) + require.ErrorIs(t, err, core.ErrInvalidEnableEpochsHandler) + }) t.Run("NilHasher", func(t *testing.T) { hostParameters := makeHostParameters() hostParameters.Hasher = nil diff --git a/vmhost/hosttest/execution_test.go b/vmhost/hosttest/execution_test.go index 496a8ca4a..fb3a8b918 100644 --- a/vmhost/hosttest/execution_test.go +++ b/vmhost/hosttest/execution_test.go @@ -9,6 +9,7 @@ import ( "math/big" "testing" + "github.com/multiversx/mx-chain-core-go/core" logger "github.com/multiversx/mx-chain-logger-go" vmcommon "github.com/multiversx/mx-chain-vm-common-go" "github.com/multiversx/mx-chain-vm-go/config" @@ -1014,7 +1015,9 @@ func runTestMBufferSetByteSliceDeploy(t *testing.T, enabled bool, retCode vmcomm WithSetup(func(host vmhost.VMHost, stubBlockchainHook *contextmock.BlockchainHookStub) { if !enabled { enableEpochsHandler, _ := host.EnableEpochsHandler().(*worldmock.EnableEpochsHandlerStub) - enableEpochsHandler.IsStorageAPICostOptimizationFlagEnabledField = false + enableEpochsHandler.IsFlagEnabledCalled = func(flag core.EnableEpochFlag) bool { + return false + } } }). AndAssertResults(func(blockchainHook *contextmock.BlockchainHookStub, verify *test.VMOutputVerifier) { @@ -1043,7 +1046,9 @@ func runTestMBufferSetByteSlice( WithSetup(func(host vmhost.VMHost, stubBlockchainHook *contextmock.BlockchainHookStub) { if !enabled { enableEpochsHandler, _ := host.EnableEpochsHandler().(*worldmock.EnableEpochsHandlerStub) - enableEpochsHandler.IsStorageAPICostOptimizationFlagEnabledField = false + enableEpochsHandler.IsFlagEnabledCalled = func(flag core.EnableEpochFlag) bool { + return false + } } }). AndAssertResults(func(host vmhost.VMHost, stubBlockchainHook *contextmock.BlockchainHookStub, verify *test.VMOutputVerifier) { diff --git a/vmhost/interface.go b/vmhost/interface.go index ee8dcc10f..bcfa03888 100644 --- a/vmhost/interface.go +++ b/vmhost/interface.go @@ -5,6 +5,7 @@ import ( "io" "math/big" + "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/data/esdt" "github.com/multiversx/mx-chain-core-go/data/vm" vmcommon "github.com/multiversx/mx-chain-vm-common-go" @@ -39,7 +40,7 @@ type VMHost interface { Output() OutputContext Metering() MeteringContext Storage() StorageContext - EnableEpochsHandler() vmcommon.EnableEpochsHandler + EnableEpochsHandler() EnableEpochsHandler ExecuteESDTTransfer(transfersArgs *ESDTTransfersArgs, callType vm.CallType) (*vmcommon.VMOutput, uint64, error) CreateNewContract(input *vmcommon.ContractCreateInput, createContractCallType int) ([]byte, error) @@ -428,3 +429,12 @@ type HashComputer interface { Size() int IsInterfaceNil() bool } + +// EnableEpochsHandler is used to verify which flags are set in a specific epoch based on EnableEpochs config +type EnableEpochsHandler interface { + IsFlagDefined(flag core.EnableEpochFlag) bool + IsFlagEnabled(flag core.EnableEpochFlag) bool + IsFlagEnabledInEpoch(flag core.EnableEpochFlag, epoch uint32) bool + GetActivationEpoch(flag core.EnableEpochFlag) uint32 + IsInterfaceNil() bool +}