diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 51462f5c5..f0db627c4 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -566,7 +566,7 @@ func (bc *Blockchain) storeBlock(block *block.Block) error { v.LoadScriptWithFlags(tx.Script, smartcontract.All) v.SetPriceGetter(getPrice) if bc.config.FreeGasLimit > 0 { - v.SetGasLimit(bc.config.FreeGasLimit + tx.SystemFee) + v.GasLimit = bc.config.FreeGasLimit + tx.SystemFee } err := v.Run() diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 1e05c32d7..55db4fd97 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -906,7 +906,7 @@ func (s *Server) invokescript(reqParams request.Params) (interface{}, *response. // result. func (s *Server) runScriptInVM(script []byte, tx *transaction.Transaction) *result.Invoke { vm := s.chain.GetTestVM(tx) - vm.SetGasLimit(s.config.MaxGasInvoke) + vm.GasLimit = s.config.MaxGasInvoke vm.LoadScriptWithFlags(script, smartcontract.All) _ = vm.Run() result := &result.Invoke{ diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 5bd960b62..1c201dfd7 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -78,7 +78,7 @@ type VM struct { refs *refCounter gasConsumed util.Fixed8 - gasLimit util.Fixed8 + GasLimit util.Fixed8 trigger trigger.Type @@ -134,16 +134,10 @@ func (v *VM) GasConsumed() util.Fixed8 { return v.gasConsumed } -// SetGasLimit sets maximum amount of gas which v can spent. -// If max <= 0, no limit is imposed. -func (v *VM) SetGasLimit(max util.Fixed8) { - v.gasLimit = max -} - // AddGas consumes specified amount of gas. It returns true iff gas limit wasn't exceeded. func (v *VM) AddGas(gas util.Fixed8) bool { v.gasConsumed += gas - return v.gasLimit == 0 || v.gasConsumed <= v.gasLimit + return v.GasLimit == 0 || v.gasConsumed <= v.GasLimit } // Estack returns the evaluation stack so interop hooks can utilize this. @@ -520,7 +514,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro if v.getPrice != nil && ctx.ip < len(ctx.prog) { v.gasConsumed += v.getPrice(v, op, parameter) - if v.gasLimit > 0 && v.gasConsumed > v.gasLimit { + if v.GasLimit > 0 && v.gasConsumed > v.GasLimit { panic("gas limit is exceeded") } } diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 83fa9f04c..ca93b2bd7 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -90,7 +90,7 @@ func TestVM_SetPriceGetter(t *testing.T) { t.Run("with sufficient gas limit", func(t *testing.T) { v.Load(prog) - v.SetGasLimit(9) + v.GasLimit = 9 runVM(t, v) require.EqualValues(t, 9, v.GasConsumed()) @@ -98,14 +98,14 @@ func TestVM_SetPriceGetter(t *testing.T) { t.Run("with small gas limit", func(t *testing.T) { v.Load(prog) - v.SetGasLimit(8) + v.GasLimit = 8 checkVMFailed(t, v) }) } func TestAddGas(t *testing.T) { v := New() - v.SetGasLimit(10) + v.GasLimit = 10 require.True(t, v.AddGas(5)) require.True(t, v.AddGas(5)) require.False(t, v.AddGas(5))