diff options
author | Meng-Ying Yang <garfield@dexon.org> | 2019-04-15 12:17:37 +0800 |
---|---|---|
committer | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-05-06 10:44:05 +0800 |
commit | ba6e85977e88195d8c5dccc512d70b066265930a (patch) | |
tree | aaea38cdd79d32a82f7b25ef33cf8a46c09fc218 | |
parent | 439168f1a1e3a05c489cdf97d91684c6f498abb4 (diff) | |
download | dexon-ba6e85977e88195d8c5dccc512d70b066265930a.tar.gz dexon-ba6e85977e88195d8c5dccc512d70b066265930a.tar.zst dexon-ba6e85977e88195d8c5dccc512d70b066265930a.zip |
core: vm: sqlvm: add built-in function TX_ORIGIN()
-rw-r--r-- | core/vm/sqlvm/runtime/functions.go | 11 | ||||
-rw-r--r-- | core/vm/sqlvm/runtime/functions_test.go | 54 |
2 files changed, 65 insertions, 0 deletions
diff --git a/core/vm/sqlvm/runtime/functions.go b/core/vm/sqlvm/runtime/functions.go index 6708ad542..d72ee5bd8 100644 --- a/core/vm/sqlvm/runtime/functions.go +++ b/core/vm/sqlvm/runtime/functions.go @@ -21,6 +21,7 @@ const ( BLOCKGASLIMIT = "BLOCK_GAS_LIMIT" MSGSENDER = "MSG_SENDER" MSGDATA = "MSG_DATA" + TXORIGIN = "TX_ORIGIN" NOW = "NOW" ) @@ -36,6 +37,7 @@ var ( BLOCKGASLIMIT: fnBlockGasLimit, MSGSENDER: fnMsgSender, MSGDATA: fnMsgData, + TXORIGIN: fnTxOrigin, } ) @@ -154,3 +156,12 @@ func fnMsgData(ctx *common.Context, ops []*Operand, length uint64) (result *Oper return } +func fnTxOrigin(ctx *common.Context, ops []*Operand, length uint64) (result *Operand, err error) { + r := &Raw{Bytes: ctx.Origin.Bytes()} + result = assignFuncResult( + []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)}, + r.clone, length, + ) + return +} + diff --git a/core/vm/sqlvm/runtime/functions_test.go b/core/vm/sqlvm/runtime/functions_test.go index bd9d460b5..54fa75816 100644 --- a/core/vm/sqlvm/runtime/functions_test.go +++ b/core/vm/sqlvm/runtime/functions_test.go @@ -409,3 +409,57 @@ func (s *FunctionSuite) TestFnMsgData() { } } } + +func (s *FunctionSuite) TestFnTxOrigin() { + type blockTxOriginCase struct { + Name string + Address dexCommon.Address + Length uint64 + Res []byte + Err error + } + + res := []byte{ + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0x01, 0x23, 0x45, 0x67} + address := dexCommon.BytesToAddress(res) + + testcases := []blockTxOriginCase{ + {"address with length 1", address, 1, res, nil}, + {"address with length 10", address, 10, res, nil}, + {"address with length 0", address, 0, res, nil}, + } + + callFn := func(c blockTxOriginCase) (*Operand, error) { + return fnTxOrigin( + &common.Context{ + Context: vm.Context{Origin: c.Address}, + }, + nil, + c.Length) + } + + meta := []ast.DataType{ast.ComposeDataType(ast.DataTypeMajorAddress, 0)} + + for idx, tCase := range testcases { + r, err := callFn(tCase) + s.Require().Equal( + tCase.Err, err, + "Index: %v. Error not expected: %v != %v", idx, tCase.Err, err) + s.Require().Equal( + meta, r.Meta, + "Index: %v. Meta not equal: %v != %v", idx, meta, r.Meta) + s.Require().Equal( + uint64(len(r.Data)), tCase.Length, + "Index: %v. Length not equal: %v != %v", idx, len(r.Data), tCase.Length) + + for i := 0; i < len(r.Data); i++ { + s.Require().Equal( + tCase.Res, r.Data[i][0].Bytes, + "Index: %v. Data Index: %v. Value not equal: %v != %v", + idx, i, tCase.Res, r.Data[i][0].Value) + } + } +} + |