Skip to content

Commit

Permalink
Merge pull request #4057 from ElrondNetwork/max-gas-limit-vmquery-con…
Browse files Browse the repository at this point in the history
…fig-value

extracted max gas limit for vm query to config
  • Loading branch information
iulianpascalau authored May 5, 2022
2 parents 668938b + 5fdf992 commit 4365190
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cmd/node/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,11 @@
{ StartEpoch = 1, Version = "v1.4" },
]

[VirtualMachine.GasConfig]
# MaxGasPerVmQuery defines the maximum amount of gas to be allocated for VM Queries coming from API
# If set to 0, then MaxUInt64 will be used
MaxGasPerVmQuery = 1500000000 #1.5b

[Hardfork]
EnableTrigger = true
EnableTriggerFromP2P = true
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ type IncreaseFactorConfig struct {
type VirtualMachineServicesConfig struct {
Execution VirtualMachineConfig
Querying QueryVirtualMachineConfig
GasConfig VirtualMachineGasConfig
}

// VirtualMachineConfig holds configuration for a Virtual Machine service
Expand All @@ -374,6 +375,11 @@ type QueryVirtualMachineConfig struct {
NumConcurrentVMs int
}

// VirtualMachineGasConfig holds the configuration for the virtual machine(s) gas operations
type VirtualMachineGasConfig struct {
MaxGasPerVmQuery uint64
}

// HardforkConfig holds the configuration for the hardfork trigger
type HardforkConfig struct {
ExportStateStorageConfig StorageConfig
Expand Down
6 changes: 6 additions & 0 deletions config/tomlConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func TestTomlParser(t *testing.T) {
NumConcurrentVMs: 16,
VirtualMachineConfig: vmConfig,
},
GasConfig: VirtualMachineGasConfig{
MaxGasPerVmQuery: 1_500_000_000,
},
},
Debug: DebugConfig{
InterceptorResolver: InterceptorResolverDebugConfig{
Expand Down Expand Up @@ -188,6 +191,9 @@ func TestTomlParser(t *testing.T) {
{ StartEpoch = 88, Version = "v1.2" },
]
[VirtualMachine.GasConfig]
MaxGasPerVmQuery = 1500000000
[Debug]
[Debug.InterceptorResolver]
Enabled = true
Expand Down
1 change: 1 addition & 0 deletions factory/apiResolverFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ func createScQueryElement(
ArwenChangeLocker: args.coreComponents.ArwenChangeLocker(),
Bootstrapper: args.bootstrapper,
AllowExternalQueriesChan: args.allowVMQueriesChan,
MaxGasLimitPerQuery: args.generalConfig.VirtualMachine.GasConfig.MaxGasPerVmQuery,
}

return smartContract.NewSCQueryService(argsNewSCQueryService)
Expand Down
7 changes: 6 additions & 1 deletion process/smartContract/scQueryService.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ArgsNewSCQueryService struct {
ArwenChangeLocker common.Locker
Bootstrapper process.Bootstrapper
AllowExternalQueriesChan chan struct{}
MaxGasLimitPerQuery uint64
}

// NewSCQueryService returns a new instance of SCQueryService
Expand Down Expand Up @@ -70,14 +71,18 @@ func NewSCQueryService(
return nil, process.ErrNilAllowExternalQueriesChan
}

gasForQuery := uint64(math.MaxUint64)
if args.MaxGasLimitPerQuery > 0 {
gasForQuery = args.MaxGasLimitPerQuery
}
return &SCQueryService{
vmContainer: args.VmContainer,
economicsFee: args.EconomicsFee,
blockChain: args.BlockChain,
blockChainHook: args.BlockChainHook,
arwenChangeLocker: args.ArwenChangeLocker,
bootstrapper: args.Bootstrapper,
gasForQuery: math.MaxUint64,
gasForQuery: gasForQuery,
allowExternalQueriesChan: args.AllowExternalQueriesChan,
}, nil
}
Expand Down
79 changes: 79 additions & 0 deletions process/smartContract/scQueryService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,85 @@ func TestExecuteQuery_ReturnsCorrectly(t *testing.T) {
assert.Equal(t, d[1], vmOutput.ReturnData[1])
}

func TestExecuteQuery_GasProvidedShouldBeApplied(t *testing.T) {
t.Parallel()

t.Run("no gas defined, should use max uint64", func(t *testing.T) {
t.Parallel()

runSCWasCalled := false
mockVM := &mock.VMExecutionHandlerStub{
RunSmartContractCallCalled: func(input *vmcommon.ContractCallInput) (output *vmcommon.VMOutput, e error) {
require.Equal(t, uint64(math.MaxUint64), input.GasProvided)
runSCWasCalled = true
return &vmcommon.VMOutput{}, nil
},
}
argsNewSCQuery := createMockArgumentsForSCQuery()
argsNewSCQuery.VmContainer = &mock.VMContainerMock{
GetCalled: func(key []byte) (handler vmcommon.VMExecutionHandler, e error) {
return mockVM, nil
},
}
argsNewSCQuery.EconomicsFee = &mock.FeeHandlerStub{
MaxGasLimitPerBlockCalled: func() uint64 {
return uint64(math.MaxUint64)
},
}

target, _ := NewSCQueryService(argsNewSCQuery)

query := process.SCQuery{
ScAddress: []byte(DummyScAddress),
FuncName: "function",
Arguments: [][]byte{},
}

_, err := target.ExecuteQuery(&query)
require.Nil(t, err)
require.True(t, runSCWasCalled)
})

t.Run("custom gas defined, should use it", func(t *testing.T) {
t.Parallel()

maxGasLimit := uint64(1_500_000_000)
runSCWasCalled := false
mockVM := &mock.VMExecutionHandlerStub{
RunSmartContractCallCalled: func(input *vmcommon.ContractCallInput) (output *vmcommon.VMOutput, e error) {
require.Equal(t, maxGasLimit, input.GasProvided)
runSCWasCalled = true
return &vmcommon.VMOutput{}, nil
},
}
argsNewSCQuery := createMockArgumentsForSCQuery()
argsNewSCQuery.VmContainer = &mock.VMContainerMock{
GetCalled: func(key []byte) (handler vmcommon.VMExecutionHandler, e error) {
return mockVM, nil
},
}
argsNewSCQuery.EconomicsFee = &mock.FeeHandlerStub{
MaxGasLimitPerBlockCalled: func() uint64 {
return uint64(math.MaxUint64)
},
}

argsNewSCQuery.MaxGasLimitPerQuery = maxGasLimit

target, _ := NewSCQueryService(argsNewSCQuery)

query := process.SCQuery{
ScAddress: []byte(DummyScAddress),
FuncName: "function",
Arguments: [][]byte{},
}

_, err := target.ExecuteQuery(&query)
require.Nil(t, err)
require.True(t, runSCWasCalled)
})
}

func TestExecuteQuery_WhenNotOkCodeShouldNotErr(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 4365190

Please sign in to comment.