2020-03-19 15:52:37 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-12-13 18:36:06 +00:00
|
|
|
"errors"
|
2020-07-22 16:03:05 +00:00
|
|
|
"math/big"
|
2020-03-19 15:52:37 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-07-22 16:03:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-12-11 12:22:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/fee"
|
2020-03-19 15:52:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-09-21 14:00:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
|
2020-05-07 11:11:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-07-22 16:03:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2020-03-19 15:52:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-03-19 15:52:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-06-17 10:57:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2020-07-22 16:03:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-07-27 14:57:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-12-11 12:22:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-19 15:52:37 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testNative struct {
|
2020-04-22 20:00:18 +00:00
|
|
|
meta interop.ContractMD
|
2020-03-19 15:52:37 +00:00
|
|
|
blocks chan uint32
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
func (tn *testNative) Initialize(_ *interop.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tn *testNative) Metadata() *interop.ContractMD {
|
2020-03-19 15:52:37 +00:00
|
|
|
return &tn.meta
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:36:06 +00:00
|
|
|
func (tn *testNative) OnPersist(ic *interop.Context) error {
|
2020-03-19 15:52:37 +00:00
|
|
|
select {
|
|
|
|
case tn.blocks <- ic.Block.Index:
|
2020-12-13 18:36:06 +00:00
|
|
|
return nil
|
2020-03-19 15:52:37 +00:00
|
|
|
default:
|
2020-12-13 18:36:06 +00:00
|
|
|
return errors.New("can't send index")
|
2020-03-19 15:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:36:06 +00:00
|
|
|
func (tn *testNative) PostPersist(ic *interop.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
var _ interop.Contract = (*testNative)(nil)
|
|
|
|
|
|
|
|
// registerNative registers native contract in the blockchain.
|
|
|
|
func (bc *Blockchain) registerNative(c interop.Contract) {
|
|
|
|
bc.contracts.Contracts = append(bc.contracts.Contracts, c)
|
2021-03-11 11:39:51 +00:00
|
|
|
bc.config.NativeUpdateHistories[c.Metadata().Name] = c.Metadata().UpdateHistory
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
2020-03-19 15:52:37 +00:00
|
|
|
|
2021-03-05 11:53:46 +00:00
|
|
|
const (
|
|
|
|
testSumCPUFee = 1 << 15 // same as contract.Call
|
|
|
|
testSumStorageFee = 200
|
|
|
|
)
|
2020-06-17 13:50:37 +00:00
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
func newTestNative() *testNative {
|
2021-03-11 11:39:51 +00:00
|
|
|
cMD := interop.NewContractMD("Test.Native.Sum", 0)
|
|
|
|
cMD.UpdateHistory = []uint32{0}
|
2020-03-19 15:52:37 +00:00
|
|
|
tn := &testNative{
|
2021-03-11 11:39:51 +00:00
|
|
|
meta: *cMD,
|
2020-03-19 15:52:37 +00:00
|
|
|
blocks: make(chan uint32, 1),
|
|
|
|
}
|
2021-02-15 13:40:44 +00:00
|
|
|
defer tn.meta.UpdateHash()
|
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
desc := &manifest.Method{
|
|
|
|
Name: "sum",
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("addend1", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("addend2", smartcontract.IntegerType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
2020-12-08 10:27:41 +00:00
|
|
|
Safe: true,
|
2020-03-19 15:52:37 +00:00
|
|
|
}
|
2020-04-22 20:00:18 +00:00
|
|
|
md := &interop.MethodAndPrice{
|
2020-03-19 15:52:37 +00:00
|
|
|
Func: tn.sum,
|
2021-03-05 10:30:16 +00:00
|
|
|
CPUFee: testSumCPUFee,
|
2021-03-05 11:53:46 +00:00
|
|
|
StorageFee: testSumStorageFee,
|
2020-12-29 10:45:49 +00:00
|
|
|
RequiredFlags: callflag.NoneFlag,
|
2020-03-19 15:52:37 +00:00
|
|
|
}
|
2020-12-08 10:27:41 +00:00
|
|
|
tn.meta.AddMethod(md, desc)
|
2020-03-19 15:52:37 +00:00
|
|
|
|
2020-08-05 11:26:09 +00:00
|
|
|
desc = &manifest.Method{
|
2020-11-19 15:02:21 +00:00
|
|
|
Name: "callOtherContractNoReturn",
|
2020-08-05 11:26:09 +00:00
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("contractHash", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("method", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("arg", smartcontract.ArrayType),
|
|
|
|
},
|
2020-11-19 15:02:21 +00:00
|
|
|
ReturnType: smartcontract.VoidType,
|
2020-12-08 10:27:41 +00:00
|
|
|
Safe: true,
|
2020-08-05 11:26:09 +00:00
|
|
|
}
|
|
|
|
md = &interop.MethodAndPrice{
|
2020-11-19 15:02:21 +00:00
|
|
|
Func: tn.callOtherContractNoReturn,
|
2021-03-05 10:30:16 +00:00
|
|
|
CPUFee: testSumCPUFee,
|
2020-12-29 10:45:49 +00:00
|
|
|
RequiredFlags: callflag.NoneFlag}
|
2020-12-08 10:27:41 +00:00
|
|
|
tn.meta.AddMethod(md, desc)
|
2020-08-05 11:26:09 +00:00
|
|
|
|
2020-12-09 12:16:49 +00:00
|
|
|
desc = &manifest.Method{
|
|
|
|
Name: "callOtherContractWithReturn",
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("contractHash", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("method", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("arg", smartcontract.ArrayType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
}
|
|
|
|
md = &interop.MethodAndPrice{
|
|
|
|
Func: tn.callOtherContractWithReturn,
|
2021-03-05 10:30:16 +00:00
|
|
|
CPUFee: testSumCPUFee,
|
2020-12-29 10:45:49 +00:00
|
|
|
RequiredFlags: callflag.NoneFlag}
|
2020-12-09 12:16:49 +00:00
|
|
|
tn.meta.AddMethod(md, desc)
|
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
return tn
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func (tn *testNative) sum(_ *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-03-19 15:52:37 +00:00
|
|
|
s1, err := args[0].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
s2, err := args[1].TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewBigInteger(s1.Add(s1, s2))
|
2020-03-19 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:02:21 +00:00
|
|
|
func toUint160(item stackitem.Item) util.Uint160 {
|
|
|
|
bs, err := item.TryBytes()
|
2020-08-05 11:26:09 +00:00
|
|
|
if err != nil {
|
2020-11-19 15:02:21 +00:00
|
|
|
panic(err)
|
2020-08-05 11:26:09 +00:00
|
|
|
}
|
2020-11-19 15:02:21 +00:00
|
|
|
u, err := util.Uint160DecodeBytesBE(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-08-05 11:26:09 +00:00
|
|
|
}
|
2020-11-19 15:02:21 +00:00
|
|
|
return u
|
2020-08-05 11:26:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 10:44:07 +00:00
|
|
|
func (tn *testNative) call(ic *interop.Context, args []stackitem.Item, hasReturn bool) {
|
2020-12-13 15:26:35 +00:00
|
|
|
cs, err := ic.GetContract(toUint160(args[0]))
|
2020-11-19 15:02:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
bs, err := args[1].TryBytes()
|
2020-08-05 11:26:09 +00:00
|
|
|
if err != nil {
|
2020-11-19 15:02:21 +00:00
|
|
|
panic(err)
|
2020-08-05 11:26:09 +00:00
|
|
|
}
|
2020-12-29 10:44:07 +00:00
|
|
|
err = contract.CallFromNative(ic, tn.meta.Hash, cs, string(bs), args[2].Value().([]stackitem.Item), hasReturn)
|
2020-11-19 15:02:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-08-05 11:26:09 +00:00
|
|
|
}
|
2020-11-19 15:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tn *testNative) callOtherContractNoReturn(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-12-29 10:44:07 +00:00
|
|
|
tn.call(ic, args, false)
|
2020-11-19 15:02:21 +00:00
|
|
|
return stackitem.Null{}
|
2020-08-05 11:26:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 12:16:49 +00:00
|
|
|
func (tn *testNative) callOtherContractWithReturn(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-12-29 10:44:07 +00:00
|
|
|
tn.call(ic, args, true)
|
2020-12-09 12:16:49 +00:00
|
|
|
bi := ic.VM.Estack().Pop().BigInt()
|
|
|
|
return stackitem.Make(bi.Add(bi, big.NewInt(1)))
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
func TestNativeContract_Invoke(t *testing.T) {
|
|
|
|
chain := newTestChain(t)
|
|
|
|
|
|
|
|
tn := newTestNative()
|
2020-04-22 20:00:18 +00:00
|
|
|
chain.registerNative(tn)
|
2020-03-19 15:52:37 +00:00
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
err := chain.contracts.Management.PutContractState(chain.dao, &state.Contract{
|
2021-02-09 09:02:38 +00:00
|
|
|
ContractBase: state.ContractBase{
|
|
|
|
ID: 1,
|
|
|
|
NEF: tn.meta.NEF,
|
|
|
|
Hash: tn.meta.Hash,
|
|
|
|
Manifest: tn.meta.Manifest,
|
|
|
|
},
|
2020-07-23 15:13:02 +00:00
|
|
|
})
|
2020-05-07 11:11:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-11 12:22:49 +00:00
|
|
|
// System.Contract.Call + "sum" itself + opcodes for pushing arguments.
|
2021-03-05 10:30:16 +00:00
|
|
|
price := int64(testSumCPUFee * chain.GetBaseExecFee() * 2)
|
2021-03-05 11:53:46 +00:00
|
|
|
price += testSumStorageFee * chain.GetStoragePrice()
|
2021-01-15 21:17:31 +00:00
|
|
|
price += 3 * fee.Opcode(chain.GetBaseExecFee(), opcode.PUSHINT8)
|
|
|
|
price += 2 * fee.Opcode(chain.GetBaseExecFee(), opcode.SYSCALL, opcode.PUSHDATA1, opcode.PUSHINT8)
|
2020-12-11 12:22:49 +00:00
|
|
|
price += fee.Opcode(chain.GetBaseExecFee(), opcode.PACK)
|
|
|
|
res, err := invokeContractMethod(chain, price, tn.Metadata().Hash, "sum", int64(14), int64(28))
|
2020-06-18 19:43:04 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-25 15:10:14 +00:00
|
|
|
checkResult(t, res, stackitem.Make(42))
|
2021-07-30 20:47:48 +00:00
|
|
|
_, err = chain.persist()
|
|
|
|
require.NoError(t, err)
|
2020-11-25 15:10:14 +00:00
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
select {
|
|
|
|
case index := <-tn.blocks:
|
|
|
|
require.Equal(t, chain.blockHeight, index)
|
|
|
|
default:
|
|
|
|
require.Fail(t, "onPersist wasn't called")
|
|
|
|
}
|
2020-11-25 15:10:14 +00:00
|
|
|
|
|
|
|
// Enough for Call and other opcodes, but not enough for "sum" call.
|
2020-12-11 12:22:49 +00:00
|
|
|
res, err = invokeContractMethod(chain, price-1, tn.Metadata().Hash, "sum", int64(14), int64(28))
|
2020-11-25 15:10:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
checkFAULTState(t, res)
|
2020-03-19 15:52:37 +00:00
|
|
|
}
|
2020-07-22 16:03:05 +00:00
|
|
|
|
|
|
|
func TestNativeContract_InvokeInternal(t *testing.T) {
|
|
|
|
chain := newTestChain(t)
|
|
|
|
|
|
|
|
tn := newTestNative()
|
|
|
|
chain.registerNative(tn)
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
err := chain.contracts.Management.PutContractState(chain.dao, &state.Contract{
|
2021-02-09 09:02:38 +00:00
|
|
|
ContractBase: state.ContractBase{
|
|
|
|
ID: 1,
|
|
|
|
NEF: tn.meta.NEF,
|
|
|
|
Manifest: tn.meta.Manifest,
|
|
|
|
},
|
2020-07-22 16:03:05 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-25 19:32:16 +00:00
|
|
|
d := dao.NewSimple(storage.NewMemoryStore(), chain.config.StateRootInHeader)
|
2020-11-17 12:57:50 +00:00
|
|
|
ic := chain.newInteropContext(trigger.Application, d, nil, nil)
|
2021-02-15 13:40:44 +00:00
|
|
|
|
|
|
|
sumOffset := 0
|
|
|
|
for _, md := range tn.Metadata().Methods {
|
|
|
|
if md.MD.Name == "sum" {
|
|
|
|
sumOffset = md.MD.Offset
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 16:03:05 +00:00
|
|
|
|
|
|
|
t.Run("fail, bad current script hash", func(t *testing.T) {
|
2021-02-15 13:40:44 +00:00
|
|
|
v := ic.SpawnVM()
|
|
|
|
v.LoadScriptWithHash(tn.Metadata().NEF.Script, util.Uint160{1, 2, 3}, callflag.All)
|
2020-12-29 10:44:07 +00:00
|
|
|
v.Estack().PushVal(14)
|
|
|
|
v.Estack().PushVal(28)
|
2021-02-15 13:40:44 +00:00
|
|
|
v.Jump(v.Context(), sumOffset)
|
2020-07-22 16:03:05 +00:00
|
|
|
|
|
|
|
// it's prohibited to call natives directly
|
2021-02-15 13:40:44 +00:00
|
|
|
require.Error(t, v.Run())
|
2020-07-22 16:03:05 +00:00
|
|
|
})
|
|
|
|
|
2021-03-11 11:39:51 +00:00
|
|
|
t.Run("fail, bad NativeUpdateHistory height", func(t *testing.T) {
|
|
|
|
tn.Metadata().UpdateHistory = []uint32{chain.blockHeight + 1}
|
|
|
|
v := ic.SpawnVM()
|
|
|
|
v.LoadScriptWithHash(tn.Metadata().NEF.Script, tn.Metadata().Hash, callflag.All)
|
|
|
|
v.Estack().PushVal(14)
|
|
|
|
v.Estack().PushVal(28)
|
|
|
|
v.Jump(v.Context(), sumOffset)
|
|
|
|
|
|
|
|
// it's prohibited to call natives before NativeUpdateHistory[0] height
|
|
|
|
require.Error(t, v.Run())
|
|
|
|
|
|
|
|
// set the value back to 0
|
|
|
|
tn.Metadata().UpdateHistory = []uint32{0}
|
|
|
|
})
|
|
|
|
|
2020-07-22 16:03:05 +00:00
|
|
|
t.Run("success", func(t *testing.T) {
|
2021-02-15 13:40:44 +00:00
|
|
|
v := ic.SpawnVM()
|
|
|
|
v.LoadScriptWithHash(tn.Metadata().NEF.Script, tn.Metadata().Hash, callflag.All)
|
2020-12-29 10:44:07 +00:00
|
|
|
v.Estack().PushVal(14)
|
|
|
|
v.Estack().PushVal(28)
|
2021-02-15 13:40:44 +00:00
|
|
|
v.Jump(v.Context(), sumOffset)
|
|
|
|
require.NoError(t, v.Run())
|
2020-07-22 16:03:05 +00:00
|
|
|
|
|
|
|
value := v.Estack().Pop().BigInt()
|
|
|
|
require.Equal(t, int64(42), value.Int64())
|
|
|
|
})
|
|
|
|
}
|
2020-08-05 11:26:09 +00:00
|
|
|
|
|
|
|
func TestNativeContract_InvokeOtherContract(t *testing.T) {
|
|
|
|
chain := newTestChain(t)
|
|
|
|
|
|
|
|
tn := newTestNative()
|
|
|
|
chain.registerNative(tn)
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
err := chain.contracts.Management.PutContractState(chain.dao, &state.Contract{
|
2021-02-09 09:02:38 +00:00
|
|
|
ContractBase: state.ContractBase{
|
|
|
|
ID: 1,
|
|
|
|
Hash: tn.meta.Hash,
|
|
|
|
NEF: tn.meta.NEF,
|
|
|
|
Manifest: tn.meta.Manifest,
|
|
|
|
},
|
2020-08-05 11:26:09 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-13 18:36:06 +00:00
|
|
|
var drainTN = func(t *testing.T) {
|
|
|
|
select {
|
|
|
|
case <-tn.blocks:
|
|
|
|
default:
|
|
|
|
require.Fail(t, "testNative didn't send us block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
cs, _ := getTestContractState(chain)
|
|
|
|
require.NoError(t, chain.contracts.Management.PutContractState(chain.dao, cs))
|
2020-08-05 11:26:09 +00:00
|
|
|
|
2021-03-05 10:30:16 +00:00
|
|
|
baseFee := chain.GetBaseExecFee()
|
2020-11-19 15:02:21 +00:00
|
|
|
t.Run("non-native, no return", func(t *testing.T) {
|
2021-03-05 10:30:16 +00:00
|
|
|
res, err := invokeContractMethod(chain, testSumCPUFee*baseFee*4+10000, tn.Metadata().Hash, "callOtherContractNoReturn", cs.Hash, "justReturn", []interface{}{})
|
2020-08-05 11:26:09 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-13 18:36:06 +00:00
|
|
|
drainTN(t)
|
2020-12-29 10:44:07 +00:00
|
|
|
require.Equal(t, vm.HaltState, res.VMState, res.FaultException)
|
2020-11-25 15:10:14 +00:00
|
|
|
checkResult(t, res, stackitem.Null{}) // simple call is done with EnsureNotEmpty
|
2020-08-05 11:26:09 +00:00
|
|
|
})
|
2020-12-09 12:16:49 +00:00
|
|
|
t.Run("non-native, with return", func(t *testing.T) {
|
2021-03-05 10:30:16 +00:00
|
|
|
res, err := invokeContractMethod(chain, testSumCPUFee*baseFee*4+10000, tn.Metadata().Hash,
|
2020-12-09 12:16:49 +00:00
|
|
|
"callOtherContractWithReturn", cs.Hash, "ret7", []interface{}{})
|
|
|
|
require.NoError(t, err)
|
2020-12-13 18:36:06 +00:00
|
|
|
drainTN(t)
|
2020-12-09 12:16:49 +00:00
|
|
|
checkResult(t, res, stackitem.Make(8))
|
|
|
|
})
|
2020-08-05 11:26:09 +00:00
|
|
|
}
|