diff options
author | Meng-Ying Yang <garfield@dexon.org> | 2019-05-06 08:35:00 +0800 |
---|---|---|
committer | Meng-Ying Yang <garfield@dexon.org> | 2019-05-06 17:38:26 +0800 |
commit | 9368e90380636880b63ebce6c2863b76be5d3cd8 (patch) | |
tree | 41f9bbc5fd2c9467680834ed9ee2d963275e8a6d | |
parent | 3e5eb2c1d551905d93d1cd8dd36028a28efbd03c (diff) | |
download | dexon-wip/gy-sqlvm-gas-model.tar.gz dexon-wip/gy-sqlvm-gas-model.tar.zst dexon-wip/gy-sqlvm-gas-model.zip |
core: vm: sqlvm: apply gas computation through built-in functionswip/gy-sqlvm-gas-model
-rw-r--r-- | core/vm/sqlvm/runtime/functions.go | 18 | ||||
-rw-r--r-- | core/vm/sqlvm/runtime/functions_test.go | 50 |
2 files changed, 54 insertions, 14 deletions
diff --git a/core/vm/sqlvm/runtime/functions.go b/core/vm/sqlvm/runtime/functions.go index 1dc9e76ba..5d41fb986 100644 --- a/core/vm/sqlvm/runtime/functions.go +++ b/core/vm/sqlvm/runtime/functions.go @@ -160,6 +160,8 @@ func fnBlockHash(ctx *common.Context, in Instruction, length uint64) (result *Op result.Data[i] = Tuple{r} } } + + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -169,6 +171,7 @@ func fnBlockNumber(ctx *common.Context, in Instruction, length uint64) (result * []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 31)}, r.clone, length, ) + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -178,6 +181,7 @@ func fnBlockTimestamp(ctx *common.Context, in Instruction, length uint64) (resul []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 31)}, r.clone, length, ) + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -187,6 +191,7 @@ func fnBlockCoinBase(ctx *common.Context, in Instruction, length uint64) (result []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)}, r.clone, length, ) + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -204,6 +209,7 @@ func fnBlockGasLimit(ctx *common.Context, in Instruction, length uint64) (result []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 7)}, r.clone, length, ) + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -213,6 +219,7 @@ func fnMsgSender(ctx *common.Context, in Instruction, length uint64) (result *Op []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)}, r.clone, length, ) + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -222,6 +229,7 @@ func fnMsgData(ctx *common.Context, in Instruction, length uint64) (result *Oper []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorDynamicBytes, 0)}, r.clone, length, ) + err = applyGas(ctx, in.GasFunc, result.bytesCount()) return } @@ -231,6 +239,7 @@ func fnTxOrigin(ctx *common.Context, in Instruction, length uint64) (result *Ope []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)}, r.clone, length, ) + err = applyGas(ctx, in.GasFunc, result.bytesCount()) return } @@ -259,6 +268,7 @@ func fnRand(ctx *common.Context, in Instruction, length uint64) (result *Operand []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorUint, 31)}, fn, length, ) + err = applyGas(ctx, in.GasFunc, result.bytesCount()) return } @@ -351,6 +361,7 @@ func fnBitAnd(ctx *common.Context, in Instruction, length uint64) (result *Opera func(b1, b2 byte) byte { return b1 & b2 }, ) } + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -395,6 +406,7 @@ func fnBitOr(ctx *common.Context, in Instruction, length uint64) (result *Operan func(b1, b2 byte) byte { return b1 | b2 }, ) } + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -413,6 +425,7 @@ func fnBitXor(ctx *common.Context, in Instruction, length uint64) (result *Opera func(b1, b2 byte) byte { return b1 ^ b2 }, ) } + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -438,6 +451,7 @@ func fnBitNot(ctx *common.Context, in Instruction, length uint64) (result *Opera func(b byte) byte { return ^b }, ) } + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -492,6 +506,8 @@ func fnOctetLength(ctx *common.Context, in Instruction, length uint64) (result * result.Data[i][j] = &Raw{Value: decimal.New(int64(len(op.Data[i][j].Bytes)), 0)} } } + + err = applyGas(ctx, in.GasFunc, result.elementCount()) return } @@ -553,5 +569,7 @@ func fnSubString(ctx *common.Context, in Instruction, length uint64) (result *Op result.Data[i][j] = &Raw{Bytes: op.Data[i][j].Bytes[start : start+end]} } } + + err = applyGas(ctx, in.GasFunc, result.bytesCount()) return } diff --git a/core/vm/sqlvm/runtime/functions_test.go b/core/vm/sqlvm/runtime/functions_test.go index 29a67e10f..66d570477 100644 --- a/core/vm/sqlvm/runtime/functions_test.go +++ b/core/vm/sqlvm/runtime/functions_test.go @@ -65,9 +65,13 @@ func (s *FunctionSuite) TestFnBlockHash() { Context: vm.Context{ GetHash: mockGetHashFunc, BlockNumber: c.Cur, + GasLimit: math.MaxUint64, }, }, - Instruction{Input: c.Ops}, + Instruction{ + Input: c.Ops, + GasFunc: fnTable[BLOCKHASH].GasFunc, + }, c.Length, ) } @@ -115,9 +119,12 @@ func (s *FunctionSuite) TestFnBlockNumber() { callFn := func(c blockNumberCase) (*Operand, error) { return fnBlockNumber( &common.Context{ - Context: vm.Context{BlockNumber: c.RawNum}, + Context: vm.Context{ + BlockNumber: c.RawNum, + GasLimit: math.MaxUint64, + }, }, - Instruction{}, + Instruction{GasFunc: fnTable[BLOCKNUMBER].GasFunc}, c.Length) } @@ -171,9 +178,12 @@ func (s *FunctionSuite) TestFnBlockTimestamp() { callFn := func(c blockTimestampCase) (*Operand, error) { return fnBlockTimestamp( &common.Context{ - Context: vm.Context{Time: c.Timestamp}, + Context: vm.Context{ + Time: c.Timestamp, + GasLimit: math.MaxUint64, + }, }, - Instruction{}, + Instruction{GasFunc: fnTable[BLOCKTIMESTAMP].GasFunc}, c.Length) } @@ -231,9 +241,12 @@ func (s *FunctionSuite) TestFnCoinBase() { callFn := func(c blockCoinBaseCase) (*Operand, error) { return fnBlockCoinBase( &common.Context{ - Context: vm.Context{Coinbase: c.Address}, + Context: vm.Context{ + Coinbase: c.Address, + GasLimit: math.MaxUint64, + }, }, - Instruction{}, + Instruction{GasFunc: fnTable[BLOCKCOINBASE].GasFunc}, c.Length) } @@ -279,7 +292,7 @@ func (s *FunctionSuite) TestFnGasLimit() { &common.Context{ Context: vm.Context{GasLimit: c.Limit}, }, - Instruction{}, + Instruction{GasFunc: fnTable[BLOCKGASLIMIT].GasFunc}, c.Length) } @@ -330,9 +343,10 @@ func (s *FunctionSuite) TestFnMsgSender() { callFn := func(c txMsgSenderCase) (*Operand, error) { return fnMsgSender( &common.Context{ + Context: vm.Context{GasLimit: math.MaxUint64}, Contract: &vm.Contract{CallerAddress: c.Address}, }, - Instruction{}, + Instruction{GasFunc: fnTable[MSGSENDER].GasFunc}, c.Length) } @@ -381,9 +395,10 @@ func (s *FunctionSuite) TestFnMsgData() { callFn := func(c txMsgDataCase) (*Operand, error) { return fnMsgData( &common.Context{ + Context: vm.Context{GasLimit: math.MaxUint64}, Contract: &vm.Contract{Input: c.Res}, }, - Instruction{}, + Instruction{GasFunc: fnTable[MSGDATA].GasFunc}, c.Length) } @@ -434,9 +449,12 @@ func (s *FunctionSuite) TestFnTxOrigin() { callFn := func(c blockTxOriginCase) (*Operand, error) { return fnTxOrigin( &common.Context{ - Context: vm.Context{Origin: c.Address}, + Context: vm.Context{ + Origin: c.Address, + GasLimit: math.MaxUint64, + }, }, - Instruction{}, + Instruction{GasFunc: fnTable[TXORIGIN].GasFunc}, c.Length) } @@ -485,10 +503,14 @@ func (s *FunctionSuite) TestFnRand() { callFn := func(c blockRandCase) (*Operand, error) { return fnRand( &common.Context{ - Context: vm.Context{Origin: c.Origin, Randomness: res}, + Context: vm.Context{ + Origin: c.Origin, + Randomness: res, + GasLimit: math.MaxUint64, + }, Storage: newStorage(), }, - Instruction{}, + Instruction{GasFunc: fnTable[RAND].GasFunc}, c.Length) } |