From ab89e16a223a4e8d1a67b01fae163dd647cc28cc Mon Sep 17 00:00:00 2001 From: Meng-Ying Yang Date: Thu, 18 Apr 2019 14:58:57 +0800 Subject: core: vm: sqlvm: change function id from bytes to uint16 --- core/vm/sqlvm/runtime/functions.go | 22 +++++++++++----------- core/vm/sqlvm/runtime/instructions.go | 25 +++++++++++++++---------- 2 files changed, 26 insertions(+), 21 deletions(-) (limited to 'core/vm') diff --git a/core/vm/sqlvm/runtime/functions.go b/core/vm/sqlvm/runtime/functions.go index d9c0f94f4..7aed05b19 100644 --- a/core/vm/sqlvm/runtime/functions.go +++ b/core/vm/sqlvm/runtime/functions.go @@ -16,22 +16,22 @@ import ( // function identifier const ( - BLOCKHASH = "BLOCK_HASH" - BLOCKNUMBER = "BLOCK_NUMBER" - BLOCKTIMESTAMP = "BLOCK_TIMESTAMP" - BLOCKCOINBASE = "BLOCK_COINBASE" - BLOCKGASLIMIT = "BLOCK_GAS_LIMIT" - MSGSENDER = "MSG_SENDER" - MSGDATA = "MSG_DATA" - TXORIGIN = "TX_ORIGIN" - NOW = "NOW" - RAND = "RAND" + BLOCKHASH uint16 = iota + BLOCKNUMBER + BLOCKTIMESTAMP + BLOCKCOINBASE + BLOCKGASLIMIT + MSGSENDER + MSGDATA + TXORIGIN + NOW + RAND ) type fn func(*common.Context, []*Operand, uint64) (*Operand, error) var ( - fnTable = map[string]fn{ + fnTable = []fn{ BLOCKHASH: fnBlockHash, BLOCKNUMBER: fnBlockNumber, BLOCKTIMESTAMP: fnBlockTimestamp, diff --git a/core/vm/sqlvm/runtime/instructions.go b/core/vm/sqlvm/runtime/instructions.go index 013c700f8..8c303c160 100644 --- a/core/vm/sqlvm/runtime/instructions.go +++ b/core/vm/sqlvm/runtime/instructions.go @@ -1849,23 +1849,28 @@ func opFunc(ctx *common.Context, ops, registers []*Operand, output uint) (err er } var ( - op, funcID = ops[0], ops[1] - op2 *Operand - length uint64 + opLength, opFuncID = ops[0], ops[1] + result *Operand + length uint64 ) - if op.IsImmediate { - length, err = ast.DecimalToUint64(op.Data[0][0].Value) + if opLength.IsImmediate { + length, err = ast.DecimalToUint64(opLength.Data[0][0].Value) if err != nil { return } } else { - length = uint64(len(op.Data)) + length = uint64(len(opLength.Data)) } - fn, ok := fnTable[string(funcID.Data[0][0].Bytes)] - if !ok { - err = se.ErrorCodeNoSuchFunction + funcID, err := ast.DecimalToUint64(opFuncID.Data[0][0].Value) + if err != nil { + return + } + + id := uint16(funcID) + if uint64(id) != funcID { + err = se.ErrorCodeIndexOutOfRange return } @@ -1874,6 +1879,6 @@ func opFunc(ctx *common.Context, ops, registers []*Operand, output uint) (err er return } - registers[output] = op2 + registers[output] = result return } -- cgit v1.2.3