vm: make (*VM).GasLimit public
VM is inherently single-threaded and replacing setter/getter methods with public field simplifies code a bit.
This commit is contained in:
parent
1f97f3abd8
commit
a7d4fff897
4 changed files with 8 additions and 14 deletions
|
@ -566,7 +566,7 @@ func (bc *Blockchain) storeBlock(block *block.Block) error {
|
||||||
v.LoadScriptWithFlags(tx.Script, smartcontract.All)
|
v.LoadScriptWithFlags(tx.Script, smartcontract.All)
|
||||||
v.SetPriceGetter(getPrice)
|
v.SetPriceGetter(getPrice)
|
||||||
if bc.config.FreeGasLimit > 0 {
|
if bc.config.FreeGasLimit > 0 {
|
||||||
v.SetGasLimit(bc.config.FreeGasLimit + tx.SystemFee)
|
v.GasLimit = bc.config.FreeGasLimit + tx.SystemFee
|
||||||
}
|
}
|
||||||
|
|
||||||
err := v.Run()
|
err := v.Run()
|
||||||
|
|
|
@ -906,7 +906,7 @@ func (s *Server) invokescript(reqParams request.Params) (interface{}, *response.
|
||||||
// result.
|
// result.
|
||||||
func (s *Server) runScriptInVM(script []byte, tx *transaction.Transaction) *result.Invoke {
|
func (s *Server) runScriptInVM(script []byte, tx *transaction.Transaction) *result.Invoke {
|
||||||
vm := s.chain.GetTestVM(tx)
|
vm := s.chain.GetTestVM(tx)
|
||||||
vm.SetGasLimit(s.config.MaxGasInvoke)
|
vm.GasLimit = s.config.MaxGasInvoke
|
||||||
vm.LoadScriptWithFlags(script, smartcontract.All)
|
vm.LoadScriptWithFlags(script, smartcontract.All)
|
||||||
_ = vm.Run()
|
_ = vm.Run()
|
||||||
result := &result.Invoke{
|
result := &result.Invoke{
|
||||||
|
|
12
pkg/vm/vm.go
12
pkg/vm/vm.go
|
@ -78,7 +78,7 @@ type VM struct {
|
||||||
refs *refCounter
|
refs *refCounter
|
||||||
|
|
||||||
gasConsumed util.Fixed8
|
gasConsumed util.Fixed8
|
||||||
gasLimit util.Fixed8
|
GasLimit util.Fixed8
|
||||||
|
|
||||||
trigger trigger.Type
|
trigger trigger.Type
|
||||||
|
|
||||||
|
@ -134,16 +134,10 @@ func (v *VM) GasConsumed() util.Fixed8 {
|
||||||
return v.gasConsumed
|
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.
|
// AddGas consumes specified amount of gas. It returns true iff gas limit wasn't exceeded.
|
||||||
func (v *VM) AddGas(gas util.Fixed8) bool {
|
func (v *VM) AddGas(gas util.Fixed8) bool {
|
||||||
v.gasConsumed += gas
|
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.
|
// 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) {
|
if v.getPrice != nil && ctx.ip < len(ctx.prog) {
|
||||||
v.gasConsumed += v.getPrice(v, op, parameter)
|
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")
|
panic("gas limit is exceeded")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,7 +90,7 @@ func TestVM_SetPriceGetter(t *testing.T) {
|
||||||
|
|
||||||
t.Run("with sufficient gas limit", func(t *testing.T) {
|
t.Run("with sufficient gas limit", func(t *testing.T) {
|
||||||
v.Load(prog)
|
v.Load(prog)
|
||||||
v.SetGasLimit(9)
|
v.GasLimit = 9
|
||||||
runVM(t, v)
|
runVM(t, v)
|
||||||
|
|
||||||
require.EqualValues(t, 9, v.GasConsumed())
|
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) {
|
t.Run("with small gas limit", func(t *testing.T) {
|
||||||
v.Load(prog)
|
v.Load(prog)
|
||||||
v.SetGasLimit(8)
|
v.GasLimit = 8
|
||||||
checkVMFailed(t, v)
|
checkVMFailed(t, v)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddGas(t *testing.T) {
|
func TestAddGas(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
v.SetGasLimit(10)
|
v.GasLimit = 10
|
||||||
require.True(t, v.AddGas(5))
|
require.True(t, v.AddGas(5))
|
||||||
require.True(t, v.AddGas(5))
|
require.True(t, v.AddGas(5))
|
||||||
require.False(t, v.AddGas(5))
|
require.False(t, v.AddGas(5))
|
||||||
|
|
Loading…
Reference in a new issue