2020-03-19 15:52:37 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
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/config/netmode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-03-19 15:52:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-07-22 16:03:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
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/core/transaction"
|
2020-08-05 11:26:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-03-19 15:52:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"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-03-19 15:52:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-08-05 11:26:09 +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-06-17 10:57:54 +00:00
|
|
|
func (tn *testNative) OnPersist(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
if ic.Trigger != trigger.System {
|
|
|
|
panic("invalid trigger")
|
|
|
|
}
|
2020-03-19 15:52:37 +00:00
|
|
|
select {
|
|
|
|
case tn.blocks <- ic.Block.Index:
|
2020-06-17 10:57:54 +00:00
|
|
|
return stackitem.NewBool(true)
|
2020-03-19 15:52:37 +00:00
|
|
|
default:
|
2020-06-17 10:57:54 +00:00
|
|
|
return stackitem.NewBool(false)
|
2020-03-19 15:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2020-03-19 15:52:37 +00:00
|
|
|
|
2020-06-17 13:50:37 +00:00
|
|
|
const testSumPrice = 1000000
|
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
func newTestNative() *testNative {
|
|
|
|
tn := &testNative{
|
2020-04-22 20:00:18 +00:00
|
|
|
meta: *interop.NewContractMD("Test.Native.Sum"),
|
2020-03-19 15:52:37 +00:00
|
|
|
blocks: make(chan uint32, 1),
|
|
|
|
}
|
|
|
|
desc := &manifest.Method{
|
|
|
|
Name: "sum",
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("addend1", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("addend2", smartcontract.IntegerType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
}
|
2020-04-22 20:00:18 +00:00
|
|
|
md := &interop.MethodAndPrice{
|
2020-03-19 15:52:37 +00:00
|
|
|
Func: tn.sum,
|
2020-06-17 13:50:37 +00:00
|
|
|
Price: testSumPrice,
|
2020-03-19 15:52:37 +00:00
|
|
|
RequiredFlags: smartcontract.NoneFlag,
|
|
|
|
}
|
|
|
|
tn.meta.AddMethod(md, desc, true)
|
|
|
|
|
2020-08-05 11:26:09 +00:00
|
|
|
desc = &manifest.Method{
|
|
|
|
Name: "callOtherContractWithoutArgs",
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("contractHash", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("method", smartcontract.StringType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.AnyType,
|
|
|
|
}
|
|
|
|
md = &interop.MethodAndPrice{
|
|
|
|
Func: tn.callOtherContractWithoutArgs,
|
|
|
|
Price: testSumPrice,
|
|
|
|
RequiredFlags: smartcontract.NoneFlag}
|
|
|
|
tn.meta.AddMethod(md, desc, true)
|
|
|
|
|
|
|
|
desc = &manifest.Method{
|
|
|
|
Name: "callOtherContractWithArg",
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("contractHash", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("method", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("arg", smartcontract.ArrayType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.AnyType,
|
|
|
|
}
|
|
|
|
md = &interop.MethodAndPrice{
|
|
|
|
Func: tn.callOtherContractWithArg,
|
|
|
|
Price: testSumPrice,
|
|
|
|
RequiredFlags: smartcontract.NoneFlag}
|
|
|
|
tn.meta.AddMethod(md, desc, true)
|
|
|
|
|
2020-06-17 10:57:54 +00:00
|
|
|
desc = &manifest.Method{Name: "onPersist", ReturnType: smartcontract.BoolType}
|
|
|
|
md = &interop.MethodAndPrice{Func: tn.OnPersist, RequiredFlags: smartcontract.AllowModifyStates}
|
|
|
|
tn.meta.AddMethod(md, desc, false)
|
|
|
|
|
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-08-05 11:26:09 +00:00
|
|
|
func (tn *testNative) callOtherContractWithoutArgs(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
vm := ic.VM
|
|
|
|
vm.Estack().PushVal(stackitem.NewArray([]stackitem.Item{})) // no args
|
|
|
|
vm.Estack().PushVal(args[1]) // method
|
|
|
|
vm.Estack().PushVal(args[0]) // contract hash
|
|
|
|
err := contractCall(ic)
|
|
|
|
if err != nil {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(-1))
|
|
|
|
}
|
|
|
|
_ = vm.Run()
|
|
|
|
if vm.HasFailed() {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(-2))
|
|
|
|
}
|
|
|
|
return vm.Estack().Pop().Item()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tn *testNative) callOtherContractWithArg(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
vm := ic.VM
|
|
|
|
vm.Estack().PushVal(stackitem.NewArray([]stackitem.Item{args[2]})) // arg
|
|
|
|
vm.Estack().PushVal(args[1]) // method
|
|
|
|
vm.Estack().PushVal(args[0]) // contract hash
|
|
|
|
err := contractCall(ic)
|
|
|
|
if err != nil {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(-1))
|
|
|
|
}
|
|
|
|
_ = vm.Run()
|
|
|
|
if vm.HasFailed() {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(-2))
|
|
|
|
}
|
|
|
|
return vm.Estack().Pop().Item()
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
func TestNativeContract_Invoke(t *testing.T) {
|
|
|
|
chain := newTestChain(t)
|
|
|
|
defer chain.Close()
|
|
|
|
|
|
|
|
tn := newTestNative()
|
2020-04-22 20:00:18 +00:00
|
|
|
chain.registerNative(tn)
|
2020-03-19 15:52:37 +00:00
|
|
|
|
2020-07-23 15:13:02 +00:00
|
|
|
err := chain.dao.PutContractState(&state.Contract{
|
|
|
|
Script: tn.meta.Script,
|
|
|
|
Manifest: tn.meta.Manifest,
|
|
|
|
})
|
2020-05-07 11:11:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.AppCallWithOperationAndArgs(w.BinWriter, tn.Metadata().Hash, "sum", int64(14), int64(28))
|
|
|
|
script := w.Bytes()
|
2020-06-17 13:50:37 +00:00
|
|
|
// System.Contract.Call + "sum" itself + opcodes for pushing arguments (PACK is 7000)
|
|
|
|
tx := transaction.New(chain.GetConfig().Magic, script, testSumPrice*2+10000)
|
2020-03-19 15:52:37 +00:00
|
|
|
validUntil := chain.blockHeight + 1
|
|
|
|
tx.ValidUntilBlock = validUntil
|
2020-07-29 16:57:38 +00:00
|
|
|
addSigners(tx)
|
2020-04-22 17:42:38 +00:00
|
|
|
require.NoError(t, signTx(chain, tx))
|
2020-06-18 19:43:04 +00:00
|
|
|
|
|
|
|
// Enough for Call and other opcodes, but not enough for "sum" call.
|
|
|
|
tx2 := transaction.New(chain.GetConfig().Magic, script, testSumPrice*2)
|
|
|
|
tx2.ValidUntilBlock = chain.blockHeight + 1
|
2020-07-29 16:57:38 +00:00
|
|
|
addSigners(tx2)
|
2020-06-18 19:43:04 +00:00
|
|
|
require.NoError(t, signTx(chain, tx2))
|
|
|
|
|
|
|
|
b := chain.newBlock(tx, tx2)
|
2020-03-19 15:52:37 +00:00
|
|
|
require.NoError(t, chain.AddBlock(b))
|
|
|
|
|
|
|
|
res, err := chain.GetAppExecResult(tx.Hash())
|
|
|
|
require.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
require.Equal(t, vm.HaltState, res.VMState)
|
2020-03-19 15:52:37 +00:00
|
|
|
require.Equal(t, 1, len(res.Stack))
|
2020-07-31 12:48:35 +00:00
|
|
|
require.Equal(t, big.NewInt(42), res.Stack[0].Value())
|
2020-03-19 15:52:37 +00:00
|
|
|
|
2020-06-18 19:43:04 +00:00
|
|
|
res, err = chain.GetAppExecResult(tx2.Hash())
|
|
|
|
require.NoError(t, err)
|
2020-07-27 14:57:53 +00:00
|
|
|
require.Equal(t, vm.FaultState, res.VMState)
|
2020-06-18 19:43:04 +00:00
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
require.NoError(t, chain.persist())
|
|
|
|
select {
|
|
|
|
case index := <-tn.blocks:
|
|
|
|
require.Equal(t, chain.blockHeight, index)
|
|
|
|
default:
|
|
|
|
require.Fail(t, "onPersist wasn't called")
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 16:03:05 +00:00
|
|
|
|
|
|
|
func TestNativeContract_InvokeInternal(t *testing.T) {
|
|
|
|
chain := newTestChain(t)
|
|
|
|
defer chain.Close()
|
|
|
|
|
|
|
|
tn := newTestNative()
|
|
|
|
chain.registerNative(tn)
|
|
|
|
|
|
|
|
err := chain.dao.PutContractState(&state.Contract{
|
|
|
|
Script: tn.meta.Script,
|
|
|
|
Manifest: tn.meta.Manifest,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ic := chain.newInteropContext(trigger.Application,
|
|
|
|
dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet), nil, nil)
|
2020-08-07 11:37:49 +00:00
|
|
|
v := ic.SpawnVM()
|
2020-07-22 16:03:05 +00:00
|
|
|
|
|
|
|
t.Run("fail, bad current script hash", func(t *testing.T) {
|
|
|
|
v.LoadScriptWithHash([]byte{1}, util.Uint160{1, 2, 3}, smartcontract.All)
|
|
|
|
v.Estack().PushVal(stackitem.NewArray([]stackitem.Item{stackitem.NewBigInteger(big.NewInt(14)), stackitem.NewBigInteger(big.NewInt(28))}))
|
|
|
|
v.Estack().PushVal("sum")
|
|
|
|
v.Estack().PushVal(tn.Metadata().Name)
|
|
|
|
|
|
|
|
// it's prohibited to call natives directly
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, native.Call(ic))
|
2020-07-22 16:03:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
v.LoadScriptWithHash([]byte{1}, tn.Metadata().Hash, smartcontract.All)
|
|
|
|
v.Estack().PushVal(stackitem.NewArray([]stackitem.Item{stackitem.NewBigInteger(big.NewInt(14)), stackitem.NewBigInteger(big.NewInt(28))}))
|
|
|
|
v.Estack().PushVal("sum")
|
|
|
|
v.Estack().PushVal(tn.Metadata().Name)
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, native.Call(ic))
|
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)
|
|
|
|
defer chain.Close()
|
|
|
|
|
|
|
|
tn := newTestNative()
|
|
|
|
chain.registerNative(tn)
|
|
|
|
|
|
|
|
err := chain.dao.PutContractState(&state.Contract{
|
|
|
|
Script: tn.meta.Script,
|
|
|
|
Manifest: tn.meta.Manifest,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("native Policy, getFeePerByte", func(t *testing.T) {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.AppCallWithOperationAndArgs(w.BinWriter, tn.Metadata().Hash, "callOtherContractWithoutArgs", chain.contracts.Policy.Hash, "getFeePerByte")
|
|
|
|
require.NoError(t, w.Err)
|
|
|
|
script := w.Bytes()
|
|
|
|
tx := transaction.New(chain.GetConfig().Magic, script, testSumPrice*4+10000)
|
|
|
|
validUntil := chain.blockHeight + 1
|
|
|
|
tx.ValidUntilBlock = validUntil
|
|
|
|
addSigners(tx)
|
|
|
|
require.NoError(t, signTx(chain, tx))
|
|
|
|
|
|
|
|
b := chain.newBlock(tx)
|
|
|
|
require.NoError(t, chain.AddBlock(b))
|
|
|
|
|
|
|
|
res, err := chain.GetAppExecResult(tx.Hash())
|
|
|
|
require.NoError(t, err)
|
|
|
|
// we expect it to be FeePerByte from Policy contract
|
|
|
|
require.Equal(t, vm.HaltState, res.VMState)
|
|
|
|
require.Equal(t, 1, len(res.Stack))
|
|
|
|
require.Equal(t, big.NewInt(1000), res.Stack[0].Value())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("native Policy, setFeePerByte", func(t *testing.T) {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.AppCallWithOperationAndArgs(w.BinWriter, tn.Metadata().Hash, "callOtherContractWithArg", chain.contracts.Policy.Hash, "setFeePerByte", int64(500))
|
|
|
|
require.NoError(t, w.Err)
|
|
|
|
script := w.Bytes()
|
|
|
|
tx := transaction.New(chain.GetConfig().Magic, script, testSumPrice*5+10000)
|
|
|
|
validUntil := chain.blockHeight + 1
|
|
|
|
tx.ValidUntilBlock = validUntil
|
|
|
|
addSigners(tx)
|
|
|
|
// to pass policy.checkValidators
|
|
|
|
tx.Signers[0].Scopes = transaction.Global
|
|
|
|
require.NoError(t, signTx(chain, tx))
|
|
|
|
|
|
|
|
b := chain.newBlock(tx)
|
|
|
|
require.NoError(t, chain.AddBlock(b))
|
|
|
|
|
|
|
|
require.NoError(t, chain.persist())
|
|
|
|
|
|
|
|
res, err := chain.GetAppExecResult(tx.Hash())
|
|
|
|
require.NoError(t, err)
|
|
|
|
// we expect it to be `true` which means that native policy value was successfully updated
|
|
|
|
require.Equal(t, vm.HaltState, res.VMState)
|
|
|
|
require.Equal(t, 1, len(res.Stack))
|
|
|
|
require.Equal(t, true, res.Stack[0].Value())
|
|
|
|
|
|
|
|
require.NoError(t, chain.persist())
|
|
|
|
|
|
|
|
// check that feePerByte was updated
|
|
|
|
n := chain.contracts.Policy.GetFeePerByteInternal(chain.dao)
|
|
|
|
require.Equal(t, 500, int(n))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("non-native contract", func(t *testing.T) {
|
|
|
|
// put some other contract into chain (this contract just pushes `5` on stack)
|
|
|
|
avm := []byte{byte(opcode.PUSH5), byte(opcode.RET)}
|
|
|
|
contractHash := hash.Hash160(avm)
|
|
|
|
m := manifest.NewManifest(contractHash)
|
|
|
|
m.ABI.Methods = []manifest.Method{
|
|
|
|
{
|
|
|
|
Name: "five",
|
|
|
|
Offset: 0,
|
|
|
|
Parameters: []manifest.Parameter{},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = chain.dao.PutContractState(&state.Contract{
|
|
|
|
Script: avm,
|
|
|
|
Manifest: *m,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.AppCallWithOperationAndArgs(w.BinWriter, tn.Metadata().Hash, "callOtherContractWithoutArgs", contractHash, "five")
|
|
|
|
require.NoError(t, w.Err)
|
|
|
|
script := w.Bytes()
|
|
|
|
tx := transaction.New(chain.GetConfig().Magic, script, testSumPrice*4+10000)
|
|
|
|
validUntil := chain.blockHeight + 1
|
|
|
|
tx.ValidUntilBlock = validUntil
|
|
|
|
addSigners(tx)
|
|
|
|
require.NoError(t, signTx(chain, tx))
|
|
|
|
|
|
|
|
b := chain.newBlock(tx)
|
|
|
|
require.NoError(t, chain.AddBlock(b))
|
|
|
|
|
|
|
|
res, err := chain.GetAppExecResult(tx.Hash())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, vm.HaltState, res.VMState)
|
|
|
|
require.Equal(t, 1, len(res.Stack))
|
|
|
|
require.Equal(t, int64(5), res.Stack[0].Value().(*big.Int).Int64())
|
|
|
|
})
|
|
|
|
}
|