2022-06-08 19:25:50 +00:00
|
|
|
package native_test
|
2020-03-19 15:52:37 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-11 14:47:59 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-06-08 19:25:50 +00:00
|
|
|
"path/filepath"
|
2022-03-11 14:47:59 +00:00
|
|
|
"strings"
|
2020-03-19 15:52:37 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-03-15 16:16:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/contracts"
|
2022-03-11 14:47:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2020-12-11 12:22:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/fee"
|
2022-03-11 14:47:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2020-05-07 11:11:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-03-11 14:47:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
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-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"
|
|
|
|
)
|
|
|
|
|
2022-06-08 19:25:50 +00:00
|
|
|
var pathToInternalContracts = filepath.Join("..", "..", "..", "internal", "contracts")
|
|
|
|
|
2020-03-19 15:52:37 +00:00
|
|
|
func TestNativeContract_Invoke(t *testing.T) {
|
2022-03-11 14:47:59 +00:00
|
|
|
const (
|
|
|
|
transferCPUFee = 1 << 17
|
|
|
|
transferStorageFee = 50
|
|
|
|
systemContractCallPrice = 1 << 15
|
|
|
|
)
|
|
|
|
bc, validator, committee := chain.NewMulti(t)
|
|
|
|
e := neotest.NewExecutor(t, bc, validator, committee)
|
|
|
|
gasHash := e.NativeHash(t, nativenames.Gas)
|
|
|
|
|
|
|
|
baseExecFee := bc.GetBaseExecFee()
|
|
|
|
price := fee.Opcode(baseExecFee, opcode.SYSCALL, // System.Contract.Call
|
|
|
|
opcode.PUSHDATA1, // contract hash (20 byte)
|
|
|
|
opcode.PUSHDATA1, // method
|
|
|
|
opcode.PUSH15, // call flags
|
|
|
|
// `transfer` args:
|
|
|
|
opcode.PUSHDATA1, // from
|
|
|
|
opcode.PUSHDATA1, // to
|
|
|
|
opcode.PUSH1, // amount
|
|
|
|
opcode.PUSHNULL, // data
|
|
|
|
// end args
|
|
|
|
opcode.PUSH4, // amount of args
|
|
|
|
opcode.PACK, // pack args
|
|
|
|
)
|
|
|
|
price += systemContractCallPrice*baseExecFee + // System.Contract.Call price
|
|
|
|
transferCPUFee*baseExecFee + // `transfer` itself
|
|
|
|
transferStorageFee*bc.GetStoragePrice() // `transfer` storage price
|
|
|
|
|
|
|
|
tx := e.NewUnsignedTx(t, gasHash, "transfer", validator.ScriptHash(), validator.ScriptHash(), 1, nil)
|
|
|
|
e.SignTx(t, tx, -1, validator)
|
|
|
|
e.AddNewBlock(t, tx)
|
|
|
|
e.CheckHalt(t, tx.Hash(), stackitem.Make(true))
|
|
|
|
|
|
|
|
// Enough for Call and other opcodes, but not enough for "transfer" call.
|
|
|
|
tx = e.NewUnsignedTx(t, gasHash, "transfer", validator.ScriptHash(), validator.ScriptHash(), 1, nil)
|
|
|
|
e.SignTx(t, tx, price-1, validator)
|
|
|
|
e.AddNewBlock(t, tx)
|
|
|
|
e.CheckFault(t, tx.Hash(), "gas limit exceeded")
|
2020-03-19 15:52:37 +00:00
|
|
|
}
|
2020-07-22 16:03:05 +00:00
|
|
|
|
|
|
|
func TestNativeContract_InvokeInternal(t *testing.T) {
|
2022-03-11 14:47:59 +00:00
|
|
|
bc, validator, committee := chain.NewMulti(t)
|
|
|
|
e := neotest.NewExecutor(t, bc, validator, committee)
|
|
|
|
clState := bc.GetContractState(e.NativeHash(t, nativenames.CryptoLib))
|
|
|
|
require.NotNil(t, clState)
|
|
|
|
md := clState.Manifest.ABI.GetMethod("ripemd160", 1)
|
|
|
|
require.NotNil(t, md)
|
2020-07-22 16:03:05 +00:00
|
|
|
|
|
|
|
t.Run("fail, bad current script hash", func(t *testing.T) {
|
2022-10-06 10:24:57 +00:00
|
|
|
ic, err := bc.GetTestVM(trigger.Application, nil, nil)
|
|
|
|
require.NoError(t, err)
|
2021-02-15 13:40:44 +00:00
|
|
|
v := ic.SpawnVM()
|
2022-03-11 14:47:59 +00:00
|
|
|
fakeH := util.Uint160{1, 2, 3}
|
|
|
|
v.LoadScriptWithHash(clState.NEF.Script, fakeH, callflag.All)
|
|
|
|
input := []byte{1, 2, 3, 4}
|
|
|
|
v.Estack().PushVal(input)
|
|
|
|
v.Context().Jump(md.Offset)
|
|
|
|
|
|
|
|
// Bad current script hash
|
2022-10-06 10:24:57 +00:00
|
|
|
err = v.Run()
|
2022-03-11 14:47:59 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.Contains(err.Error(), fmt.Sprintf("native contract %s (version 0) not found", fakeH.StringLE())), err.Error())
|
2020-07-22 16:03:05 +00:00
|
|
|
})
|
|
|
|
|
2023-11-21 09:35:18 +00:00
|
|
|
/*
|
|
|
|
t.Run("fail, bad NativeUpdateHistory height", func(t *testing.T) {
|
|
|
|
bcBad, validatorBad, committeeBad := chain.NewMultiWithCustomConfig(t, func(c *config.Blockchain) {
|
|
|
|
c.NativeUpdateHistories = map[string][]uint32{
|
|
|
|
nativenames.Policy: {0},
|
|
|
|
nativenames.Neo: {0},
|
|
|
|
nativenames.Gas: {0},
|
|
|
|
nativenames.Designation: {0},
|
|
|
|
nativenames.StdLib: {0},
|
|
|
|
nativenames.Management: {0},
|
|
|
|
nativenames.Oracle: {0},
|
|
|
|
nativenames.Ledger: {0},
|
|
|
|
nativenames.CryptoLib: {1},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
eBad := neotest.NewExecutor(t, bcBad, validatorBad, committeeBad)
|
|
|
|
|
|
|
|
ic, err := bcBad.GetTestVM(trigger.Application, nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
v := ic.SpawnVM()
|
|
|
|
v.LoadScriptWithHash(clState.NEF.Script, clState.Hash, callflag.All) // hash is not affected by native update history
|
|
|
|
input := []byte{1, 2, 3, 4}
|
|
|
|
v.Estack().PushVal(input)
|
|
|
|
v.Context().Jump(md.Offset)
|
|
|
|
|
|
|
|
// It's prohibited to call natives before NativeUpdateHistory[0] height.
|
|
|
|
err = v.Run()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.Contains(err.Error(), "native contract CryptoLib is active after height = 1"))
|
|
|
|
|
|
|
|
// Add new block => CryptoLib should be active now.
|
|
|
|
eBad.AddNewBlock(t)
|
|
|
|
ic, err = bcBad.GetTestVM(trigger.Application, nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
v = ic.SpawnVM()
|
|
|
|
v.LoadScriptWithHash(clState.NEF.Script, clState.Hash, callflag.All) // hash is not affected by native update history
|
|
|
|
v.Estack().PushVal(input)
|
|
|
|
v.Context().Jump(md.Offset)
|
|
|
|
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
value := v.Estack().Pop().Bytes()
|
|
|
|
require.Equal(t, hash.RipeMD160(input).BytesBE(), value)
|
2022-03-11 14:47:59 +00:00
|
|
|
})
|
2023-11-21 09:35:18 +00:00
|
|
|
*/
|
2021-03-11 11:39:51 +00:00
|
|
|
|
2022-12-16 20:36:45 +00:00
|
|
|
manState := bc.GetContractState(e.NativeHash(t, nativenames.Management))
|
|
|
|
require.NotNil(t, manState)
|
|
|
|
mdDeploy := manState.Manifest.ABI.GetMethod("deploy", 2)
|
|
|
|
require.NotNil(t, mdDeploy)
|
|
|
|
t.Run("fail, bad call flag", func(t *testing.T) {
|
|
|
|
ic, err := bc.GetTestVM(trigger.Application, nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
v := ic.SpawnVM()
|
|
|
|
v.LoadScriptWithHash(manState.NEF.Script, manState.Hash, callflag.States|callflag.AllowNotify)
|
|
|
|
input := []byte{1, 2, 3, 4}
|
|
|
|
v.Estack().PushVal(input)
|
|
|
|
v.Estack().PushVal(input)
|
|
|
|
v.Context().Jump(mdDeploy.Offset)
|
|
|
|
|
|
|
|
// Can't call with these flags, Aspidochelone is active.
|
|
|
|
err = v.Run()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.Contains(err.Error(), "missing call flags for native 0 `deploy` operation call"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("good, pre-aspidochelone deploy", func(t *testing.T) {
|
|
|
|
bc, _, _ := chain.NewMultiWithCustomConfig(t, func(c *config.Blockchain) {
|
|
|
|
c.Hardforks = map[string]uint32{
|
|
|
|
config.HFAspidochelone.String(): 100500,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ic, err := bc.GetTestVM(trigger.Application, nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
v := ic.SpawnVM()
|
|
|
|
v.LoadScriptWithHash(manState.NEF.Script, manState.Hash, callflag.States|callflag.AllowNotify)
|
|
|
|
input := []byte{1, 2, 3, 4}
|
|
|
|
v.Estack().PushVal(input)
|
|
|
|
v.Estack().PushVal(input)
|
|
|
|
v.Context().Jump(mdDeploy.Offset)
|
|
|
|
|
|
|
|
// We have an invalid input, but call flags are OK.
|
|
|
|
err = v.Run()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.Contains(err.Error(), "invalid NEF file"))
|
|
|
|
})
|
|
|
|
|
2020-07-22 16:03:05 +00:00
|
|
|
t.Run("success", func(t *testing.T) {
|
2022-10-06 10:24:57 +00:00
|
|
|
ic, err := bc.GetTestVM(trigger.Application, nil, nil)
|
|
|
|
require.NoError(t, err)
|
2021-02-15 13:40:44 +00:00
|
|
|
v := ic.SpawnVM()
|
2022-03-11 14:47:59 +00:00
|
|
|
v.LoadScriptWithHash(clState.NEF.Script, clState.Hash, callflag.All)
|
|
|
|
input := []byte{1, 2, 3, 4}
|
|
|
|
v.Estack().PushVal(input)
|
|
|
|
v.Context().Jump(md.Offset)
|
|
|
|
|
2021-02-15 13:40:44 +00:00
|
|
|
require.NoError(t, v.Run())
|
2020-07-22 16:03:05 +00:00
|
|
|
|
2022-03-11 14:47:59 +00:00
|
|
|
value := v.Estack().Pop().Bytes()
|
|
|
|
require.Equal(t, hash.RipeMD160(input).BytesBE(), value)
|
2020-07-22 16:03:05 +00:00
|
|
|
})
|
|
|
|
}
|
2020-08-05 11:26:09 +00:00
|
|
|
|
|
|
|
func TestNativeContract_InvokeOtherContract(t *testing.T) {
|
2022-03-11 14:47:59 +00:00
|
|
|
bc, validator, committee := chain.NewMulti(t)
|
|
|
|
e := neotest.NewExecutor(t, bc, validator, committee)
|
|
|
|
managementInvoker := e.ValidatorInvoker(e.NativeHash(t, nativenames.Management))
|
|
|
|
gasInvoker := e.ValidatorInvoker(e.NativeHash(t, nativenames.Gas))
|
|
|
|
|
|
|
|
cs, _ := contracts.GetTestContractState(t, pathToInternalContracts, 1, 2, validator.ScriptHash())
|
|
|
|
cs.Hash = state.CreateContractHash(validator.ScriptHash(), cs.NEF.Checksum, cs.Manifest.Name) // set proper hash
|
|
|
|
manifB, err := json.Marshal(cs.Manifest)
|
2020-08-05 11:26:09 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-11 14:47:59 +00:00
|
|
|
nefB, err := cs.NEF.Bytes()
|
|
|
|
require.NoError(t, err)
|
|
|
|
si, err := cs.ToStackItem()
|
|
|
|
require.NoError(t, err)
|
|
|
|
managementInvoker.Invoke(t, si, "deploy", nefB, manifB)
|
2020-08-05 11:26:09 +00:00
|
|
|
|
2020-11-19 15:02:21 +00:00
|
|
|
t.Run("non-native, no return", func(t *testing.T) {
|
2022-03-11 14:47:59 +00:00
|
|
|
// `onNEP17Payment` will be invoked on test contract from GAS contract.
|
|
|
|
gasInvoker.Invoke(t, true, "transfer", validator.ScriptHash(), cs.Hash, 1, nil)
|
2020-12-09 12:16:49 +00:00
|
|
|
})
|
2020-08-05 11:26:09 +00:00
|
|
|
}
|