2020-06-08 15:36:19 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-11-13 18:46:26 +00:00
|
|
|
"encoding/json"
|
2020-10-05 09:32:04 +00:00
|
|
|
"errors"
|
2020-06-08 15:36:19 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-16 08:17:18 +00:00
|
|
|
"github.com/nspcc-dev/dbft/crypto"
|
2020-11-23 11:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/random"
|
2020-07-16 06:56:57 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-07-28 10:17:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-07-27 08:06:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/callback"
|
2020-09-21 14:00:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
|
2020-10-02 09:56:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-06-16 09:04:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
|
2020-06-16 08:17:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-07-16 06:56:57 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-06-16 09:47:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-06-16 08:17:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-10-02 08:37:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-07-16 08:32:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-06-16 08:25:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-07-23 15:13:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-10-02 08:37:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-06-16 08:17:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-06-08 15:36:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBCGetTransaction(t *testing.T) {
|
|
|
|
v, tx, context, chain := createVMAndTX(t)
|
|
|
|
defer chain.Close()
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
2020-08-31 19:11:49 +00:00
|
|
|
require.NoError(t, context.DAO.StoreAsTransaction(tx, 0, nil))
|
2020-06-08 15:36:19 +00:00
|
|
|
v.Estack().PushVal(tx.Hash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetTransaction(context)
|
2020-06-08 15:36:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := v.Estack().Pop().Value()
|
|
|
|
actual, ok := value.([]stackitem.Item)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 8, len(actual))
|
|
|
|
require.Equal(t, tx.Hash().BytesBE(), actual[0].Value().([]byte))
|
|
|
|
require.Equal(t, int64(tx.Version), actual[1].Value().(*big.Int).Int64())
|
|
|
|
require.Equal(t, int64(tx.Nonce), actual[2].Value().(*big.Int).Int64())
|
2020-07-29 16:57:38 +00:00
|
|
|
require.Equal(t, tx.Sender().BytesBE(), actual[3].Value().([]byte))
|
2020-06-08 15:36:19 +00:00
|
|
|
require.Equal(t, int64(tx.SystemFee), actual[4].Value().(*big.Int).Int64())
|
|
|
|
require.Equal(t, int64(tx.NetworkFee), actual[5].Value().(*big.Int).Int64())
|
|
|
|
require.Equal(t, int64(tx.ValidUntilBlock), actual[6].Value().(*big.Int).Int64())
|
|
|
|
require.Equal(t, tx.Script, actual[7].Value().([]byte))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("isn't traceable", func(t *testing.T) {
|
2020-08-31 19:11:49 +00:00
|
|
|
require.NoError(t, context.DAO.StoreAsTransaction(tx, 1, nil))
|
2020-06-08 15:36:19 +00:00
|
|
|
v.Estack().PushVal(tx.Hash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetTransaction(context)
|
2020-06-08 15:36:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok := v.Estack().Pop().Item().(stackitem.Null)
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad hash", func(t *testing.T) {
|
2020-08-31 19:11:49 +00:00
|
|
|
require.NoError(t, context.DAO.StoreAsTransaction(tx, 1, nil))
|
2020-06-08 15:36:19 +00:00
|
|
|
v.Estack().PushVal(tx.Hash().BytesLE())
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetTransaction(context)
|
2020-06-08 15:36:19 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok := v.Estack().Pop().Item().(stackitem.Null)
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
}
|
2020-06-09 09:18:08 +00:00
|
|
|
|
|
|
|
func TestBCGetTransactionFromBlock(t *testing.T) {
|
|
|
|
v, block, context, chain := createVMAndBlock(t)
|
|
|
|
defer chain.Close()
|
|
|
|
require.NoError(t, chain.AddBlock(chain.newBlock()))
|
2020-08-31 19:11:49 +00:00
|
|
|
require.NoError(t, context.DAO.StoreAsBlock(block, nil))
|
2020-06-09 09:18:08 +00:00
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(0)
|
|
|
|
v.Estack().PushVal(block.Hash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetTransactionFromBlock(context)
|
2020-06-09 09:18:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := v.Estack().Pop().Value()
|
|
|
|
actual, ok := value.([]byte)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, block.Transactions[0].Hash().BytesBE(), actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid block hash", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(0)
|
|
|
|
v.Estack().PushVal(block.Hash().BytesBE()[:10])
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetTransactionFromBlock(context)
|
2020-06-09 09:18:08 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("isn't traceable", func(t *testing.T) {
|
|
|
|
block.Index = 2
|
2020-08-31 19:11:49 +00:00
|
|
|
require.NoError(t, context.DAO.StoreAsBlock(block, nil))
|
2020-06-09 09:18:08 +00:00
|
|
|
v.Estack().PushVal(0)
|
|
|
|
v.Estack().PushVal(block.Hash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetTransactionFromBlock(context)
|
2020-06-09 09:18:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok := v.Estack().Pop().Item().(stackitem.Null)
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad block hash", func(t *testing.T) {
|
|
|
|
block.Index = 1
|
2020-08-31 19:11:49 +00:00
|
|
|
require.NoError(t, context.DAO.StoreAsBlock(block, nil))
|
2020-06-09 09:18:08 +00:00
|
|
|
v.Estack().PushVal(0)
|
|
|
|
v.Estack().PushVal(block.Hash().BytesLE())
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetTransactionFromBlock(context)
|
2020-06-09 09:18:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok := v.Estack().Pop().Item().(stackitem.Null)
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad transaction index", func(t *testing.T) {
|
2020-08-31 19:11:49 +00:00
|
|
|
require.NoError(t, context.DAO.StoreAsBlock(block, nil))
|
2020-06-09 09:18:08 +00:00
|
|
|
v.Estack().PushVal(1)
|
|
|
|
v.Estack().PushVal(block.Hash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetTransactionFromBlock(context)
|
2020-06-09 09:18:08 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2020-06-09 10:18:32 +00:00
|
|
|
|
|
|
|
func TestBCGetBlock(t *testing.T) {
|
|
|
|
v, context, chain := createVM(t)
|
|
|
|
defer chain.Close()
|
|
|
|
block := chain.newBlock()
|
|
|
|
require.NoError(t, chain.AddBlock(block))
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(block.Hash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetBlock(context)
|
2020-06-09 10:18:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := v.Estack().Pop().Value()
|
|
|
|
actual, ok := value.([]stackitem.Item)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 8, len(actual))
|
|
|
|
require.Equal(t, block.Hash().BytesBE(), actual[0].Value().([]byte))
|
|
|
|
require.Equal(t, int64(block.Version), actual[1].Value().(*big.Int).Int64())
|
|
|
|
require.Equal(t, block.PrevHash.BytesBE(), actual[2].Value().([]byte))
|
|
|
|
require.Equal(t, block.MerkleRoot.BytesBE(), actual[3].Value().([]byte))
|
|
|
|
require.Equal(t, int64(block.Timestamp), actual[4].Value().(*big.Int).Int64())
|
|
|
|
require.Equal(t, int64(block.Index), actual[5].Value().(*big.Int).Int64())
|
|
|
|
require.Equal(t, block.NextConsensus.BytesBE(), actual[6].Value().([]byte))
|
|
|
|
require.Equal(t, int64(len(block.Transactions)), actual[7].Value().(*big.Int).Int64())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad hash", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(block.Hash().BytesLE())
|
2020-08-07 11:37:49 +00:00
|
|
|
err := bcGetTransaction(context)
|
2020-06-09 10:18:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok := v.Estack().Pop().Item().(stackitem.Null)
|
|
|
|
require.True(t, ok)
|
|
|
|
})
|
|
|
|
}
|
2020-06-16 08:17:18 +00:00
|
|
|
|
|
|
|
func TestContractIsStandard(t *testing.T) {
|
|
|
|
v, ic, chain := createVM(t)
|
|
|
|
defer chain.Close()
|
|
|
|
|
2020-07-16 06:56:57 +00:00
|
|
|
t.Run("contract not stored", func(t *testing.T) {
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
tx := transaction.New(netmode.TestNet, []byte{1, 2, 3}, 1)
|
|
|
|
tx.Scripts = []transaction.Witness{
|
|
|
|
{
|
|
|
|
InvocationScript: []byte{1, 2, 3},
|
|
|
|
VerificationScript: pub.GetVerificationScript(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ic.Container = tx
|
|
|
|
|
|
|
|
t.Run("true", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(pub.GetScriptHash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractIsStandard(ic))
|
2020-07-16 06:56:57 +00:00
|
|
|
require.True(t, v.Estack().Pop().Bool())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("false", func(t *testing.T) {
|
|
|
|
tx.Scripts[0].VerificationScript = []byte{9, 8, 7}
|
|
|
|
v.Estack().PushVal(pub.GetScriptHash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractIsStandard(ic))
|
2020-07-16 06:56:57 +00:00
|
|
|
require.False(t, v.Estack().Pop().Bool())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("contract stored, true", func(t *testing.T) {
|
2020-06-16 08:17:18 +00:00
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
err = ic.DAO.PutContractState(&state.Contract{ID: 42, Script: pub.GetVerificationScript()})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
v.Estack().PushVal(pub.GetScriptHash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractIsStandard(ic))
|
2020-06-16 08:17:18 +00:00
|
|
|
require.True(t, v.Estack().Pop().Bool())
|
|
|
|
})
|
2020-07-16 06:56:57 +00:00
|
|
|
t.Run("contract stored, false", func(t *testing.T) {
|
2020-06-16 08:17:18 +00:00
|
|
|
script := []byte{byte(opcode.PUSHT)}
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(&state.Contract{ID: 24, Script: script}))
|
|
|
|
|
|
|
|
v.Estack().PushVal(crypto.Hash160(script).BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractIsStandard(ic))
|
2020-06-16 08:17:18 +00:00
|
|
|
require.False(t, v.Estack().Pop().Bool())
|
|
|
|
})
|
|
|
|
}
|
2020-06-16 08:25:30 +00:00
|
|
|
|
|
|
|
func TestContractCreateAccount(t *testing.T) {
|
|
|
|
v, ic, chain := createVM(t)
|
|
|
|
defer chain.Close()
|
|
|
|
t.Run("Good", func(t *testing.T) {
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
v.Estack().PushVal(pub.Bytes())
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractCreateStandardAccount(ic))
|
2020-06-16 08:25:30 +00:00
|
|
|
|
|
|
|
value := v.Estack().Pop().Bytes()
|
|
|
|
u, err := util.Uint160DecodeBytesBE(value)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, pub.GetScriptHash(), u)
|
|
|
|
})
|
|
|
|
t.Run("InvalidKey", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal([]byte{1, 2, 3})
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractCreateStandardAccount(ic))
|
2020-06-16 08:25:30 +00:00
|
|
|
})
|
|
|
|
}
|
2020-06-16 09:04:08 +00:00
|
|
|
|
|
|
|
func TestRuntimeGasLeft(t *testing.T) {
|
|
|
|
v, ic, chain := createVM(t)
|
|
|
|
defer chain.Close()
|
|
|
|
|
|
|
|
v.GasLimit = 100
|
|
|
|
v.AddGas(58)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, runtime.GasLeft(ic))
|
2020-06-16 09:04:08 +00:00
|
|
|
require.EqualValues(t, 42, v.Estack().Pop().BigInt().Int64())
|
|
|
|
}
|
2020-06-16 09:30:25 +00:00
|
|
|
|
|
|
|
func TestRuntimeGetNotifications(t *testing.T) {
|
|
|
|
v, ic, chain := createVM(t)
|
|
|
|
defer chain.Close()
|
|
|
|
|
|
|
|
ic.Notifications = []state.NotificationEvent{
|
2020-06-29 08:25:32 +00:00
|
|
|
{ScriptHash: util.Uint160{1}, Name: "Event1", Item: stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray([]byte{11})})},
|
|
|
|
{ScriptHash: util.Uint160{2}, Name: "Event2", Item: stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray([]byte{22})})},
|
|
|
|
{ScriptHash: util.Uint160{1}, Name: "Event1", Item: stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray([]byte{33})})},
|
2020-06-16 09:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("NoFilter", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(stackitem.Null{})
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, runtime.GetNotifications(ic))
|
2020-06-16 09:30:25 +00:00
|
|
|
|
|
|
|
arr := v.Estack().Pop().Array()
|
|
|
|
require.Equal(t, len(ic.Notifications), len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
elem := arr[i].Value().([]stackitem.Item)
|
|
|
|
require.Equal(t, ic.Notifications[i].ScriptHash.BytesBE(), elem[0].Value())
|
2020-07-29 08:18:51 +00:00
|
|
|
name, err := stackitem.ToString(elem[1])
|
2020-06-29 08:25:32 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-29 08:18:51 +00:00
|
|
|
require.Equal(t, ic.Notifications[i].Name, name)
|
2020-06-29 08:25:32 +00:00
|
|
|
require.Equal(t, ic.Notifications[i].Item, elem[2])
|
2020-06-16 09:30:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("WithFilter", func(t *testing.T) {
|
|
|
|
h := util.Uint160{2}.BytesBE()
|
|
|
|
v.Estack().PushVal(h)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, runtime.GetNotifications(ic))
|
2020-06-16 09:30:25 +00:00
|
|
|
|
|
|
|
arr := v.Estack().Pop().Array()
|
|
|
|
require.Equal(t, 1, len(arr))
|
|
|
|
elem := arr[0].Value().([]stackitem.Item)
|
|
|
|
require.Equal(t, h, elem[0].Value())
|
2020-07-29 08:18:51 +00:00
|
|
|
name, err := stackitem.ToString(elem[1])
|
2020-06-29 08:25:32 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-29 08:18:51 +00:00
|
|
|
require.Equal(t, ic.Notifications[1].Name, name)
|
2020-06-29 08:25:32 +00:00
|
|
|
require.Equal(t, ic.Notifications[1].Item, elem[2])
|
2020-06-16 09:30:25 +00:00
|
|
|
})
|
|
|
|
}
|
2020-06-16 09:47:42 +00:00
|
|
|
|
|
|
|
func TestRuntimeGetInvocationCounter(t *testing.T) {
|
|
|
|
v, ic, chain := createVM(t)
|
|
|
|
defer chain.Close()
|
|
|
|
|
2020-10-05 10:08:55 +00:00
|
|
|
ic.VM.Invocations[hash.Hash160([]byte{2})] = 42
|
2020-06-16 09:47:42 +00:00
|
|
|
|
2020-10-02 15:42:23 +00:00
|
|
|
t.Run("No invocations", func(t *testing.T) {
|
2020-06-16 09:47:42 +00:00
|
|
|
v.LoadScript([]byte{1})
|
2020-10-02 15:42:23 +00:00
|
|
|
// do not return an error in this case.
|
|
|
|
require.NoError(t, runtime.GetInvocationCounter(ic))
|
|
|
|
require.EqualValues(t, 1, v.Estack().Pop().BigInt().Int64())
|
2020-06-16 09:47:42 +00:00
|
|
|
})
|
|
|
|
t.Run("NonZero", func(t *testing.T) {
|
|
|
|
v.LoadScript([]byte{2})
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, runtime.GetInvocationCounter(ic))
|
2020-06-16 09:47:42 +00:00
|
|
|
require.EqualValues(t, 42, v.Estack().Pop().BigInt().Int64())
|
|
|
|
})
|
|
|
|
}
|
2020-07-15 09:53:09 +00:00
|
|
|
|
|
|
|
func TestBlockchainGetContractState(t *testing.T) {
|
|
|
|
v, cs, ic, bc := createVMAndContractState(t)
|
|
|
|
defer bc.Close()
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(cs.ScriptHash().BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, bcGetContract(ic))
|
2020-07-15 09:53:09 +00:00
|
|
|
|
2020-07-15 11:39:20 +00:00
|
|
|
actual := v.Estack().Pop().Item()
|
|
|
|
compareContractStates(t, cs, actual)
|
2020-07-15 09:53:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("uncknown contract state", func(t *testing.T) {
|
|
|
|
v.Estack().PushVal(util.Uint160{1, 2, 3}.BytesBE())
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, bcGetContract(ic))
|
2020-07-15 09:53:09 +00:00
|
|
|
|
|
|
|
actual := v.Estack().Pop().Item()
|
|
|
|
require.Equal(t, stackitem.Null{}, actual)
|
|
|
|
})
|
|
|
|
}
|
2020-07-15 11:39:20 +00:00
|
|
|
|
2020-10-05 09:32:04 +00:00
|
|
|
func TestStoragePut(t *testing.T) {
|
|
|
|
_, cs, ic, bc := createVMAndContractState(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
|
|
|
|
initVM := func(t *testing.T, key, value []byte, gas int64) {
|
|
|
|
v := ic.SpawnVM()
|
|
|
|
v.LoadScript(cs.Script)
|
|
|
|
v.GasLimit = gas
|
|
|
|
v.Estack().PushVal(value)
|
|
|
|
v.Estack().PushVal(key)
|
|
|
|
require.NoError(t, storageGetContext(ic))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("create, not enough gas", func(t *testing.T) {
|
|
|
|
initVM(t, []byte{1}, []byte{2, 3}, 2*StoragePrice)
|
|
|
|
err := storagePut(ic)
|
|
|
|
require.True(t, errors.Is(err, errGasLimitExceeded), "got: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
initVM(t, []byte{4}, []byte{5, 6}, 3*StoragePrice)
|
|
|
|
require.NoError(t, storagePut(ic))
|
|
|
|
|
|
|
|
t.Run("update", func(t *testing.T) {
|
|
|
|
t.Run("not enough gas", func(t *testing.T) {
|
|
|
|
initVM(t, []byte{4}, []byte{5, 6, 7, 8}, StoragePrice)
|
|
|
|
err := storagePut(ic)
|
|
|
|
require.True(t, errors.Is(err, errGasLimitExceeded), "got: %v", err)
|
|
|
|
})
|
2020-11-03 19:50:40 +00:00
|
|
|
initVM(t, []byte{4}, []byte{5, 6, 7, 8}, 3*StoragePrice)
|
|
|
|
require.NoError(t, storagePut(ic))
|
|
|
|
initVM(t, []byte{4}, []byte{5, 6}, StoragePrice)
|
2020-10-05 09:32:04 +00:00
|
|
|
require.NoError(t, storagePut(ic))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:34:14 +00:00
|
|
|
// getTestContractState returns 2 contracts second of which is allowed to call the first.
|
|
|
|
func getTestContractState() (*state.Contract, *state.Contract) {
|
2020-10-02 08:37:11 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.ABORT)
|
|
|
|
addOff := w.Len()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.ADD, opcode.RET)
|
|
|
|
ret7Off := w.Len()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.PUSH7, opcode.RET)
|
|
|
|
dropOff := w.Len()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.DROP, opcode.RET)
|
|
|
|
initOff := w.Len()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.INITSSLOT, 1, opcode.PUSH3, opcode.STSFLD0, opcode.RET)
|
|
|
|
add3Off := w.Len()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.LDSFLD0, opcode.ADD, opcode.RET)
|
|
|
|
invalidRetOff := w.Len()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.PUSH1, opcode.PUSH2, opcode.RET)
|
|
|
|
justRetOff := w.Len()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
|
|
|
verifyOff := w.Len()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.LDSFLD0, opcode.SUB,
|
|
|
|
opcode.CONVERT, opcode.Opcode(stackitem.BooleanT), opcode.RET)
|
2020-10-02 09:56:51 +00:00
|
|
|
deployOff := w.Len()
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.JMPIF, 2+8+3)
|
|
|
|
emit.String(w.BinWriter, "create")
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.CALL, 3+8+3, opcode.RET)
|
|
|
|
emit.String(w.BinWriter, "update")
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.CALL, 3, opcode.RET)
|
|
|
|
putValOff := w.Len()
|
|
|
|
emit.String(w.BinWriter, "initial")
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemStorageGetContext)
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemStoragePut)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
|
|
|
getValOff := w.Len()
|
|
|
|
emit.String(w.BinWriter, "initial")
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemStorageGetContext)
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemStorageGet)
|
2020-11-19 15:01:42 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
|
|
|
onPaymentOff := w.Len()
|
|
|
|
emit.Int(w.BinWriter, 3)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.PACK)
|
|
|
|
emit.String(w.BinWriter, "LastPayment")
|
|
|
|
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeNotify)
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
2020-10-02 08:37:11 +00:00
|
|
|
|
|
|
|
script := w.Bytes()
|
2020-07-23 15:13:02 +00:00
|
|
|
h := hash.Hash160(script)
|
2020-11-20 08:02:58 +00:00
|
|
|
m := manifest.NewManifest(h, "TestMain")
|
2020-07-23 15:13:02 +00:00
|
|
|
m.ABI.Methods = []manifest.Method{
|
|
|
|
{
|
|
|
|
Name: "add",
|
2020-10-02 08:37:11 +00:00
|
|
|
Offset: addOff,
|
2020-07-23 15:13:02 +00:00
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("addend1", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("addend2", smartcontract.IntegerType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ret7",
|
2020-10-02 08:37:11 +00:00
|
|
|
Offset: ret7Off,
|
2020-07-23 15:13:02 +00:00
|
|
|
Parameters: []manifest.Parameter{},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
},
|
2020-07-24 10:09:02 +00:00
|
|
|
{
|
|
|
|
Name: "drop",
|
2020-10-02 08:37:11 +00:00
|
|
|
Offset: dropOff,
|
2020-07-24 10:09:02 +00:00
|
|
|
ReturnType: smartcontract.VoidType,
|
|
|
|
},
|
2020-07-24 10:40:54 +00:00
|
|
|
{
|
|
|
|
Name: manifest.MethodInit,
|
2020-10-02 08:37:11 +00:00
|
|
|
Offset: initOff,
|
2020-07-24 10:40:54 +00:00
|
|
|
ReturnType: smartcontract.VoidType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "add3",
|
2020-10-02 08:37:11 +00:00
|
|
|
Offset: add3Off,
|
2020-07-24 10:40:54 +00:00
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("addend", smartcontract.IntegerType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
},
|
2020-08-10 08:52:32 +00:00
|
|
|
{
|
|
|
|
Name: "invalidReturn",
|
2020-10-02 08:37:11 +00:00
|
|
|
Offset: invalidRetOff,
|
2020-08-10 08:52:32 +00:00
|
|
|
ReturnType: smartcontract.IntegerType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "justReturn",
|
2020-10-02 08:37:11 +00:00
|
|
|
Offset: justRetOff,
|
2020-11-19 15:02:21 +00:00
|
|
|
ReturnType: smartcontract.VoidType,
|
2020-08-10 08:52:32 +00:00
|
|
|
},
|
2020-08-13 15:42:53 +00:00
|
|
|
{
|
|
|
|
Name: manifest.MethodVerify,
|
2020-10-02 08:37:11 +00:00
|
|
|
Offset: verifyOff,
|
2020-08-13 15:42:53 +00:00
|
|
|
ReturnType: smartcontract.BoolType,
|
|
|
|
},
|
2020-10-02 09:56:51 +00:00
|
|
|
{
|
|
|
|
Name: manifest.MethodDeploy,
|
|
|
|
Offset: deployOff,
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("isUpdate", smartcontract.BoolType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.VoidType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "getValue",
|
|
|
|
Offset: getValOff,
|
|
|
|
ReturnType: smartcontract.StringType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "putValue",
|
|
|
|
Offset: putValOff,
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("value", smartcontract.StringType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.VoidType,
|
|
|
|
},
|
2020-11-19 15:01:42 +00:00
|
|
|
{
|
|
|
|
Name: "onPayment",
|
|
|
|
Offset: onPaymentOff,
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
manifest.NewParameter("from", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("amount", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("data", smartcontract.AnyType),
|
|
|
|
},
|
|
|
|
ReturnType: smartcontract.VoidType,
|
|
|
|
},
|
2020-07-23 15:13:02 +00:00
|
|
|
}
|
2020-07-28 10:34:14 +00:00
|
|
|
cs := &state.Contract{
|
2020-07-23 15:13:02 +00:00
|
|
|
Script: script,
|
|
|
|
Manifest: *m,
|
|
|
|
ID: 42,
|
|
|
|
}
|
2020-07-28 10:34:14 +00:00
|
|
|
|
2020-07-27 09:23:48 +00:00
|
|
|
currScript := []byte{byte(opcode.RET)}
|
2020-11-20 08:02:58 +00:00
|
|
|
m = manifest.NewManifest(hash.Hash160(currScript), "TestAux")
|
2020-07-28 10:34:14 +00:00
|
|
|
perm := manifest.NewPermission(manifest.PermissionHash, h)
|
|
|
|
perm.Methods.Add("add")
|
|
|
|
perm.Methods.Add("drop")
|
|
|
|
perm.Methods.Add("add3")
|
2020-08-10 08:52:32 +00:00
|
|
|
perm.Methods.Add("invalidReturn")
|
|
|
|
perm.Methods.Add("justReturn")
|
2020-10-02 09:56:51 +00:00
|
|
|
perm.Methods.Add("getValue")
|
2020-07-28 10:34:14 +00:00
|
|
|
m.Permissions = append(m.Permissions, *perm)
|
|
|
|
|
|
|
|
return cs, &state.Contract{
|
|
|
|
Script: currScript,
|
|
|
|
Manifest: *m,
|
|
|
|
ID: 123,
|
|
|
|
}
|
2020-07-23 15:13:02 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
func loadScript(ic *interop.Context, script []byte, args ...interface{}) {
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.SpawnVM()
|
|
|
|
ic.VM.LoadScriptWithFlags(script, smartcontract.AllowCall)
|
2020-07-28 10:29:15 +00:00
|
|
|
for i := range args {
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.VM.Estack().PushVal(args[i])
|
2020-07-28 10:29:15 +00:00
|
|
|
}
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.VM.GasLimit = -1
|
2020-07-28 10:29:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 14:11:41 +00:00
|
|
|
func loadScriptWithHashAndFlags(ic *interop.Context, script []byte, hash util.Uint160, f smartcontract.CallFlag, args ...interface{}) {
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.SpawnVM()
|
|
|
|
ic.VM.LoadScriptWithHash(script, hash, f)
|
2020-10-22 14:11:41 +00:00
|
|
|
for i := range args {
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.VM.Estack().PushVal(args[i])
|
2020-10-22 14:11:41 +00:00
|
|
|
}
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.VM.GasLimit = -1
|
2020-10-22 14:11:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:13:02 +00:00
|
|
|
func TestContractCall(t *testing.T) {
|
2020-07-28 10:29:15 +00:00
|
|
|
_, ic, bc := createVM(t)
|
2020-07-23 15:13:02 +00:00
|
|
|
defer bc.Close()
|
|
|
|
|
2020-07-28 10:34:14 +00:00
|
|
|
cs, currCs := getTestContractState()
|
2020-07-23 15:13:02 +00:00
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
2020-07-28 10:34:14 +00:00
|
|
|
require.NoError(t, ic.DAO.PutContractState(currCs))
|
2020-07-23 15:13:02 +00:00
|
|
|
|
2020-07-28 10:34:14 +00:00
|
|
|
currScript := currCs.Script
|
2020-07-23 15:13:02 +00:00
|
|
|
h := cs.Manifest.ABI.Hash
|
|
|
|
|
|
|
|
addArgs := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(2)})
|
|
|
|
t.Run("Good", func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, currScript, 42)
|
|
|
|
ic.VM.Estack().PushVal(addArgs)
|
|
|
|
ic.VM.Estack().PushVal("add")
|
|
|
|
ic.VM.Estack().PushVal(h.BytesBE())
|
2020-09-21 14:00:33 +00:00
|
|
|
require.NoError(t, contract.Call(ic))
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, ic.VM.Run())
|
|
|
|
require.Equal(t, 2, ic.VM.Estack().Len())
|
|
|
|
require.Equal(t, big.NewInt(3), ic.VM.Estack().Pop().Value())
|
|
|
|
require.Equal(t, big.NewInt(42), ic.VM.Estack().Pop().Value())
|
2020-07-23 15:13:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CallExInvalidFlag", func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, currScript, 42)
|
|
|
|
ic.VM.Estack().PushVal(byte(0xFF))
|
|
|
|
ic.VM.Estack().PushVal(addArgs)
|
|
|
|
ic.VM.Estack().PushVal("add")
|
|
|
|
ic.VM.Estack().PushVal(h.BytesBE())
|
2020-09-21 14:00:33 +00:00
|
|
|
require.Error(t, contract.CallEx(ic))
|
2020-07-23 15:13:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
runInvalid := func(args ...interface{}) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, currScript, 42)
|
2020-07-23 15:13:02 +00:00
|
|
|
for i := range args {
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().PushVal(args[i])
|
2020-07-23 15:13:02 +00:00
|
|
|
}
|
2020-07-29 08:24:06 +00:00
|
|
|
// interops can both return error and panic,
|
2020-08-14 09:16:24 +00:00
|
|
|
// we don't care which kind of error has occurred
|
2020-07-29 08:24:06 +00:00
|
|
|
require.Panics(t, func() {
|
2020-09-21 14:00:33 +00:00
|
|
|
err := contract.Call(ic)
|
2020-07-29 08:24:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
2020-07-23 15:13:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Invalid", func(t *testing.T) {
|
|
|
|
t.Run("Hash", runInvalid(addArgs, "add", h.BytesBE()[1:]))
|
|
|
|
t.Run("MissingHash", runInvalid(addArgs, "add", util.Uint160{}.BytesBE()))
|
|
|
|
t.Run("Method", runInvalid(addArgs, stackitem.NewInterop("add"), h.BytesBE()))
|
|
|
|
t.Run("MissingMethod", runInvalid(addArgs, "sub", h.BytesBE()))
|
|
|
|
t.Run("DisallowedMethod", runInvalid(stackitem.NewArray(nil), "ret7", h.BytesBE()))
|
|
|
|
t.Run("Arguments", runInvalid(1, "add", h.BytesBE()))
|
|
|
|
t.Run("NotEnoughArguments", runInvalid(
|
|
|
|
stackitem.NewArray([]stackitem.Item{stackitem.Make(1)}), "add", h.BytesBE()))
|
|
|
|
})
|
2020-07-24 10:09:02 +00:00
|
|
|
|
2020-08-10 08:52:32 +00:00
|
|
|
t.Run("ReturnValues", func(t *testing.T) {
|
|
|
|
t.Run("Many", func(t *testing.T) {
|
|
|
|
loadScript(ic, currScript, 42)
|
|
|
|
ic.VM.Estack().PushVal(stackitem.NewArray(nil))
|
|
|
|
ic.VM.Estack().PushVal("invalidReturn")
|
|
|
|
ic.VM.Estack().PushVal(h.BytesBE())
|
2020-09-21 14:00:33 +00:00
|
|
|
require.NoError(t, contract.Call(ic))
|
2020-08-10 08:52:32 +00:00
|
|
|
require.Error(t, ic.VM.Run())
|
|
|
|
})
|
|
|
|
t.Run("Void", func(t *testing.T) {
|
|
|
|
loadScript(ic, currScript, 42)
|
|
|
|
ic.VM.Estack().PushVal(stackitem.NewArray(nil))
|
|
|
|
ic.VM.Estack().PushVal("justReturn")
|
|
|
|
ic.VM.Estack().PushVal(h.BytesBE())
|
2020-09-21 14:00:33 +00:00
|
|
|
require.NoError(t, contract.Call(ic))
|
2020-08-10 08:52:32 +00:00
|
|
|
require.NoError(t, ic.VM.Run())
|
|
|
|
require.Equal(t, 2, ic.VM.Estack().Len())
|
|
|
|
require.Equal(t, stackitem.Null{}, ic.VM.Estack().Pop().Item())
|
|
|
|
require.Equal(t, big.NewInt(42), ic.VM.Estack().Pop().Value())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-07-24 10:09:02 +00:00
|
|
|
t.Run("IsolatedStack", func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, currScript, 42)
|
|
|
|
ic.VM.Estack().PushVal(stackitem.NewArray(nil))
|
|
|
|
ic.VM.Estack().PushVal("drop")
|
|
|
|
ic.VM.Estack().PushVal(h.BytesBE())
|
2020-09-21 14:00:33 +00:00
|
|
|
require.NoError(t, contract.Call(ic))
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, ic.VM.Run())
|
2020-07-24 10:09:02 +00:00
|
|
|
})
|
2020-07-24 10:40:54 +00:00
|
|
|
|
|
|
|
t.Run("CallInitialize", func(t *testing.T) {
|
|
|
|
t.Run("Directly", runInvalid(stackitem.NewArray([]stackitem.Item{}), "_initialize", h.BytesBE()))
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, currScript, 42)
|
|
|
|
ic.VM.Estack().PushVal(stackitem.NewArray([]stackitem.Item{stackitem.Make(5)}))
|
|
|
|
ic.VM.Estack().PushVal("add3")
|
|
|
|
ic.VM.Estack().PushVal(h.BytesBE())
|
2020-09-21 14:00:33 +00:00
|
|
|
require.NoError(t, contract.Call(ic))
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, ic.VM.Run())
|
|
|
|
require.Equal(t, 2, ic.VM.Estack().Len())
|
|
|
|
require.Equal(t, big.NewInt(8), ic.VM.Estack().Pop().Value())
|
|
|
|
require.Equal(t, big.NewInt(42), ic.VM.Estack().Pop().Value())
|
2020-07-24 10:40:54 +00:00
|
|
|
})
|
2020-07-23 15:13:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:39:20 +00:00
|
|
|
func TestContractCreate(t *testing.T) {
|
|
|
|
v, cs, ic, bc := createVMAndContractState(t)
|
|
|
|
v.GasLimit = -1
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
putArgsOnStack := func() {
|
2020-11-13 18:46:26 +00:00
|
|
|
manifest, err := json.Marshal(cs.Manifest)
|
2020-07-15 11:39:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
v.Estack().PushVal(manifest)
|
|
|
|
v.Estack().PushVal(cs.Script)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
putArgsOnStack()
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractCreate(ic))
|
2020-07-15 11:39:20 +00:00
|
|
|
actual := v.Estack().Pop().Item()
|
|
|
|
compareContractStates(t, cs, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid scripthash", func(t *testing.T) {
|
|
|
|
cs.Script = append(cs.Script, 0x01)
|
|
|
|
putArgsOnStack()
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractCreate(ic))
|
2020-07-15 11:39:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("contract already exists", func(t *testing.T) {
|
|
|
|
cs.Script = cs.Script[:len(cs.Script)-1]
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
putArgsOnStack()
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractCreate(ic))
|
2020-07-15 11:39:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareContractStates(t *testing.T, expected *state.Contract, actual stackitem.Item) {
|
|
|
|
act, ok := actual.Value().([]stackitem.Item)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2020-11-13 18:46:26 +00:00
|
|
|
expectedManifest, err := json.Marshal(expected.Manifest)
|
2020-07-15 11:39:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-13 18:26:23 +00:00
|
|
|
require.Equal(t, 2, len(act))
|
2020-07-15 11:39:20 +00:00
|
|
|
require.Equal(t, expected.Script, act[0].Value().([]byte))
|
|
|
|
require.Equal(t, expectedManifest, act[1].Value().([]byte))
|
|
|
|
}
|
2020-07-16 08:32:32 +00:00
|
|
|
|
|
|
|
func TestContractUpdate(t *testing.T) {
|
|
|
|
v, cs, ic, bc := createVMAndContractState(t)
|
|
|
|
defer bc.Close()
|
|
|
|
v.GasLimit = -1
|
|
|
|
|
2020-10-05 13:12:08 +00:00
|
|
|
putArgsOnStack := func(script, manifest interface{}) {
|
2020-07-16 08:32:32 +00:00
|
|
|
v.Estack().PushVal(manifest)
|
|
|
|
v.Estack().PushVal(script)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no args", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
2020-10-05 13:12:08 +00:00
|
|
|
putArgsOnStack(stackitem.Null{}, stackitem.Null{})
|
|
|
|
require.Error(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no contract", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, util.Uint160{8, 9, 7}, smartcontract.All)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("too large script", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
2020-10-05 13:12:08 +00:00
|
|
|
putArgsOnStack(make([]byte, MaxContractScriptSize+1), stackitem.Null{})
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("too large manifest", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
2020-10-05 13:12:08 +00:00
|
|
|
putArgsOnStack(stackitem.Null{}, make([]byte, manifest.MaxManifestSize+1))
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("gas limit exceeded", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
v.GasLimit = 0
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
|
|
|
putArgsOnStack([]byte{1}, []byte{2})
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update script, the same script", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
v.GasLimit = -1
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
2020-10-05 13:12:08 +00:00
|
|
|
putArgsOnStack(cs.Script, stackitem.Null{})
|
2020-07-16 08:32:32 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update script, already exists", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
duplicateScript := []byte{byte(opcode.PUSHDATA4)}
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(&state.Contract{
|
|
|
|
ID: 95,
|
|
|
|
Script: duplicateScript,
|
|
|
|
Manifest: manifest.Manifest{
|
|
|
|
ABI: manifest.ABI{
|
|
|
|
Hash: hash.Hash160(duplicateScript),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
2020-10-05 13:12:08 +00:00
|
|
|
putArgsOnStack(duplicateScript, stackitem.Null{})
|
2020-07-16 08:32:32 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update script, positive", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
2020-10-05 13:12:08 +00:00
|
|
|
t.Run("empty manifest", func(t *testing.T) {
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
|
|
|
newScript := []byte{9, 8, 7, 6, 5}
|
|
|
|
putArgsOnStack(newScript, []byte{})
|
|
|
|
require.Error(t, contractUpdate(ic))
|
|
|
|
})
|
|
|
|
|
2020-07-16 08:32:32 +00:00
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
|
|
|
newScript := []byte{9, 8, 7, 6, 5}
|
2020-10-05 13:12:08 +00:00
|
|
|
putArgsOnStack(newScript, stackitem.Null{})
|
2020-07-16 08:32:32 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
|
|
|
|
// updated contract should have new scripthash
|
|
|
|
actual, err := ic.DAO.GetContractState(hash.Hash160(newScript))
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := &state.Contract{
|
|
|
|
ID: cs.ID,
|
|
|
|
Script: newScript,
|
|
|
|
Manifest: cs.Manifest,
|
|
|
|
}
|
|
|
|
expected.Manifest.ABI.Hash = hash.Hash160(newScript)
|
|
|
|
_ = expected.ScriptHash()
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
|
|
|
// old contract should be deleted
|
|
|
|
_, err = ic.DAO.GetContractState(cs.ScriptHash())
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update manifest, bad manifest", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
2020-10-05 13:12:08 +00:00
|
|
|
putArgsOnStack(stackitem.Null{}, []byte{1, 2, 3})
|
2020-07-16 08:32:32 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update manifest, bad contract hash", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
|
|
|
manifest := &manifest.Manifest{
|
|
|
|
ABI: manifest.ABI{
|
|
|
|
Hash: util.Uint160{4, 5, 6},
|
|
|
|
},
|
|
|
|
}
|
2020-11-13 18:46:26 +00:00
|
|
|
manifestBytes, err := json.Marshal(manifest)
|
2020-07-16 08:32:32 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-05 13:12:08 +00:00
|
|
|
putArgsOnStack(stackitem.Null{}, manifestBytes)
|
2020-07-16 08:32:32 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update manifest, positive", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
manifest := &manifest.Manifest{
|
|
|
|
ABI: manifest.ABI{
|
|
|
|
Hash: cs.ScriptHash(),
|
|
|
|
},
|
|
|
|
}
|
2020-11-13 18:46:26 +00:00
|
|
|
manifestBytes, err := json.Marshal(manifest)
|
2020-07-16 08:32:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-05 13:12:08 +00:00
|
|
|
t.Run("empty script", func(t *testing.T) {
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
|
|
|
putArgsOnStack([]byte{}, manifestBytes)
|
|
|
|
require.Error(t, contractUpdate(ic))
|
|
|
|
})
|
|
|
|
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
|
|
|
putArgsOnStack(stackitem.Null{}, manifestBytes)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
|
|
|
|
// updated contract should have new scripthash
|
|
|
|
actual, err := ic.DAO.GetContractState(cs.ScriptHash())
|
2020-08-14 09:08:57 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-16 08:32:32 +00:00
|
|
|
expected := &state.Contract{
|
|
|
|
ID: cs.ID,
|
|
|
|
Script: cs.Script,
|
|
|
|
Manifest: *manifest,
|
|
|
|
}
|
|
|
|
_ = expected.ScriptHash()
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update both script and manifest", func(t *testing.T) {
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, cs.ScriptHash(), smartcontract.All)
|
|
|
|
newScript := []byte{12, 13, 14}
|
|
|
|
newManifest := manifest.Manifest{
|
|
|
|
ABI: manifest.ABI{
|
|
|
|
Hash: hash.Hash160(newScript),
|
|
|
|
},
|
|
|
|
}
|
2020-11-13 18:46:26 +00:00
|
|
|
newManifestBytes, err := json.Marshal(newManifest)
|
2020-07-16 08:32:32 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
putArgsOnStack(newScript, newManifestBytes)
|
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractUpdate(ic))
|
2020-07-16 08:32:32 +00:00
|
|
|
|
|
|
|
// updated contract should have new script and manifest
|
|
|
|
actual, err := ic.DAO.GetContractState(hash.Hash160(newScript))
|
|
|
|
require.NoError(t, err)
|
|
|
|
expected := &state.Contract{
|
|
|
|
ID: cs.ID,
|
|
|
|
Script: newScript,
|
|
|
|
Manifest: newManifest,
|
|
|
|
}
|
|
|
|
expected.Manifest.ABI.Hash = hash.Hash160(newScript)
|
|
|
|
_ = expected.ScriptHash()
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
|
|
|
// old contract should be deleted
|
|
|
|
_, err = ic.DAO.GetContractState(cs.ScriptHash())
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2020-07-16 09:13:55 +00:00
|
|
|
|
2020-10-02 09:56:51 +00:00
|
|
|
// TestContractCreateDeploy checks that `_deploy` method was called
|
|
|
|
// during contract creation or update.
|
|
|
|
func TestContractCreateDeploy(t *testing.T) {
|
|
|
|
v, ic, bc := createVM(t)
|
|
|
|
defer bc.Close()
|
|
|
|
v.GasLimit = -1
|
|
|
|
|
|
|
|
putArgs := func(cs *state.Contract) {
|
2020-11-13 18:46:26 +00:00
|
|
|
rawManifest, err := json.Marshal(cs.Manifest)
|
2020-10-02 09:56:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
v.Estack().PushVal(rawManifest)
|
|
|
|
v.Estack().PushVal(cs.Script)
|
|
|
|
}
|
|
|
|
cs, currCs := getTestContractState()
|
|
|
|
|
|
|
|
v.LoadScriptWithFlags([]byte{byte(opcode.RET)}, smartcontract.All)
|
|
|
|
putArgs(cs)
|
|
|
|
require.NoError(t, contractCreate(ic))
|
|
|
|
require.NoError(t, ic.VM.Run())
|
|
|
|
|
|
|
|
v.LoadScriptWithFlags(currCs.Script, smartcontract.All)
|
2020-10-12 11:32:27 +00:00
|
|
|
err := contract.CallExInternal(ic, cs, "getValue", nil, smartcontract.All, vm.EnsureNotEmpty)
|
2020-10-02 09:56:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
require.Equal(t, "create", v.Estack().Pop().String())
|
|
|
|
|
|
|
|
v.LoadScriptWithFlags(cs.Script, smartcontract.All)
|
|
|
|
md := cs.Manifest.ABI.GetMethod("justReturn")
|
|
|
|
v.Jump(v.Context(), md.Offset)
|
|
|
|
|
|
|
|
t.Run("Update", func(t *testing.T) {
|
|
|
|
newCs := &state.Contract{
|
|
|
|
ID: cs.ID,
|
|
|
|
Script: append(cs.Script, byte(opcode.RET)),
|
|
|
|
Manifest: cs.Manifest,
|
|
|
|
}
|
|
|
|
newCs.Manifest.ABI.Hash = hash.Hash160(newCs.Script)
|
|
|
|
putArgs(newCs)
|
|
|
|
require.NoError(t, contractUpdate(ic))
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
|
|
|
|
v.LoadScriptWithFlags(currCs.Script, smartcontract.All)
|
2020-10-12 11:32:27 +00:00
|
|
|
err = contract.CallExInternal(ic, newCs, "getValue", nil, smartcontract.All, vm.EnsureNotEmpty)
|
2020-10-02 09:56:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
require.Equal(t, "update", v.Estack().Pop().String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-16 09:13:55 +00:00
|
|
|
func TestContractGetCallFlags(t *testing.T) {
|
|
|
|
v, ic, bc := createVM(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
v.LoadScriptWithHash([]byte{byte(opcode.RET)}, util.Uint160{1, 2, 3}, smartcontract.All)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, contractGetCallFlags(ic))
|
2020-07-16 09:13:55 +00:00
|
|
|
require.Equal(t, int64(smartcontract.All), v.Estack().Pop().Value().(*big.Int).Int64())
|
|
|
|
}
|
2020-07-27 08:06:06 +00:00
|
|
|
|
|
|
|
func TestPointerCallback(t *testing.T) {
|
|
|
|
_, ic, bc := createVM(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
script := []byte{
|
|
|
|
byte(opcode.NOP), byte(opcode.INC), byte(opcode.RET),
|
|
|
|
byte(opcode.DIV), byte(opcode.RET),
|
|
|
|
}
|
|
|
|
t.Run("Good", func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, script, 2, stackitem.NewPointer(3, script))
|
|
|
|
ic.VM.Estack().PushVal(ic.VM.Context())
|
|
|
|
require.NoError(t, callback.Create(ic))
|
2020-07-27 08:06:06 +00:00
|
|
|
|
|
|
|
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(3), stackitem.Make(12)})
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().InsertAt(vm.NewElement(args), 1)
|
|
|
|
require.NoError(t, callback.Invoke(ic))
|
2020-07-27 08:06:06 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, ic.VM.Run())
|
|
|
|
require.Equal(t, 1, ic.VM.Estack().Len())
|
|
|
|
require.Equal(t, big.NewInt(5), ic.VM.Estack().Pop().Item().Value())
|
2020-07-27 08:06:06 +00:00
|
|
|
})
|
|
|
|
t.Run("Invalid", func(t *testing.T) {
|
|
|
|
t.Run("NotEnoughParameters", func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, script, 2, stackitem.NewPointer(3, script))
|
|
|
|
ic.VM.Estack().PushVal(ic.VM.Context())
|
|
|
|
require.NoError(t, callback.Create(ic))
|
2020-07-27 08:06:06 +00:00
|
|
|
|
|
|
|
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(3)})
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().InsertAt(vm.NewElement(args), 1)
|
|
|
|
require.Error(t, callback.Invoke(ic))
|
2020-07-27 08:06:06 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2020-07-27 09:23:48 +00:00
|
|
|
|
|
|
|
func TestMethodCallback(t *testing.T) {
|
|
|
|
_, ic, bc := createVM(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
cs, currCs := getTestContractState()
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(cs))
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(currCs))
|
|
|
|
|
|
|
|
ic.Functions = append(ic.Functions, systemInterops)
|
|
|
|
rawHash := cs.Manifest.ABI.Hash.BytesBE()
|
|
|
|
|
|
|
|
t.Run("Invalid", func(t *testing.T) {
|
|
|
|
runInvalid := func(args ...interface{}) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, currCs.Script, 42)
|
2020-07-27 09:23:48 +00:00
|
|
|
for i := range args {
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().PushVal(args[i])
|
2020-07-27 09:23:48 +00:00
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, callback.CreateFromMethod(ic))
|
2020-07-27 09:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Run("Hash", runInvalid("add", rawHash[1:]))
|
|
|
|
t.Run("MissingHash", runInvalid("add", util.Uint160{}.BytesBE()))
|
|
|
|
t.Run("MissingMethod", runInvalid("sub", rawHash))
|
|
|
|
t.Run("DisallowedMethod", runInvalid("ret7", rawHash))
|
|
|
|
t.Run("Initialize", runInvalid("_initialize", rawHash))
|
|
|
|
t.Run("NotEnoughArguments", func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, currCs.Script, 42, "add", rawHash)
|
|
|
|
require.NoError(t, callback.CreateFromMethod(ic))
|
2020-07-27 09:23:48 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().InsertAt(vm.NewElement(stackitem.NewArray([]stackitem.Item{stackitem.Make(1)})), 1)
|
|
|
|
require.Error(t, callback.Invoke(ic))
|
2020-07-27 09:23:48 +00:00
|
|
|
})
|
|
|
|
t.Run("CallIsNotAllowed", func(t *testing.T) {
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.SpawnVM()
|
|
|
|
ic.VM.Load(currCs.Script)
|
|
|
|
ic.VM.Estack().PushVal("add")
|
|
|
|
ic.VM.Estack().PushVal(rawHash)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, callback.CreateFromMethod(ic))
|
2020-07-27 09:23:48 +00:00
|
|
|
|
|
|
|
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(5)})
|
2020-10-29 16:10:28 +00:00
|
|
|
ic.VM.Estack().InsertAt(vm.NewElement(args), 1)
|
2020-08-07 11:37:49 +00:00
|
|
|
require.Error(t, callback.Invoke(ic))
|
2020-07-27 09:23:48 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Good", func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, currCs.Script, 42, "add", rawHash)
|
|
|
|
require.NoError(t, callback.CreateFromMethod(ic))
|
2020-07-27 09:23:48 +00:00
|
|
|
|
|
|
|
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(5)})
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().InsertAt(vm.NewElement(args), 1)
|
2020-07-27 09:23:48 +00:00
|
|
|
|
2020-08-07 11:37:49 +00:00
|
|
|
require.NoError(t, callback.Invoke(ic))
|
|
|
|
require.NoError(t, ic.VM.Run())
|
|
|
|
require.Equal(t, 2, ic.VM.Estack().Len())
|
|
|
|
require.Equal(t, big.NewInt(6), ic.VM.Estack().Pop().Item().Value())
|
|
|
|
require.Equal(t, big.NewInt(42), ic.VM.Estack().Pop().Item().Value())
|
2020-07-27 09:23:48 +00:00
|
|
|
})
|
|
|
|
}
|
2020-07-28 10:17:38 +00:00
|
|
|
func TestSyscallCallback(t *testing.T) {
|
|
|
|
_, ic, bc := createVM(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
ic.Functions = append(ic.Functions, []interop.Function{
|
|
|
|
{
|
|
|
|
ID: 0x42,
|
2020-08-07 11:37:49 +00:00
|
|
|
Func: func(ic *interop.Context) error {
|
|
|
|
a := ic.VM.Estack().Pop().BigInt()
|
|
|
|
b := ic.VM.Estack().Pop().BigInt()
|
|
|
|
ic.VM.Estack().PushVal(new(big.Int).Add(a, b))
|
2020-07-28 10:17:38 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
ParamCount: 2,
|
|
|
|
},
|
2020-07-28 14:15:23 +00:00
|
|
|
{
|
|
|
|
ID: 0x53,
|
2020-08-07 11:37:49 +00:00
|
|
|
Func: func(_ *interop.Context) error { return nil },
|
2020-07-28 14:15:23 +00:00
|
|
|
DisallowCallback: true,
|
|
|
|
},
|
2020-07-28 10:17:38 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Good", func(t *testing.T) {
|
|
|
|
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(12), stackitem.Make(30)})
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, []byte{byte(opcode.RET)}, args, 0x42)
|
|
|
|
require.NoError(t, callback.CreateFromSyscall(ic))
|
|
|
|
require.NoError(t, callback.Invoke(ic))
|
|
|
|
require.Equal(t, 1, ic.VM.Estack().Len())
|
|
|
|
require.Equal(t, big.NewInt(42), ic.VM.Estack().Pop().Item().Value())
|
2020-07-28 10:17:38 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Invalid", func(t *testing.T) {
|
|
|
|
t.Run("InvalidParameterCount", func(t *testing.T) {
|
|
|
|
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(12)})
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, []byte{byte(opcode.RET)}, args, 0x42)
|
|
|
|
require.NoError(t, callback.CreateFromSyscall(ic))
|
|
|
|
require.Error(t, callback.Invoke(ic))
|
2020-07-28 10:17:38 +00:00
|
|
|
})
|
|
|
|
t.Run("MissingSyscall", func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, []byte{byte(opcode.RET)}, stackitem.NewArray(nil), 0x43)
|
|
|
|
require.Error(t, callback.CreateFromSyscall(ic))
|
2020-07-28 10:17:38 +00:00
|
|
|
})
|
2020-07-28 14:15:23 +00:00
|
|
|
t.Run("Disallowed", func(t *testing.T) {
|
2020-08-07 11:37:49 +00:00
|
|
|
loadScript(ic, []byte{byte(opcode.RET)}, stackitem.NewArray(nil), 0x53)
|
|
|
|
require.Error(t, callback.CreateFromSyscall(ic))
|
2020-07-28 14:15:23 +00:00
|
|
|
})
|
2020-07-28 10:17:38 +00:00
|
|
|
})
|
|
|
|
}
|
2020-10-22 14:11:41 +00:00
|
|
|
|
|
|
|
func TestRuntimeCheckWitness(t *testing.T) {
|
|
|
|
_, ic, bc := createVM(t)
|
|
|
|
defer bc.Close()
|
|
|
|
|
|
|
|
script := []byte{byte(opcode.RET)}
|
|
|
|
scriptHash := hash.Hash160(script)
|
|
|
|
check := func(t *testing.T, ic *interop.Context, arg interface{}, shouldFail bool, expected ...bool) {
|
|
|
|
ic.VM.Estack().PushVal(arg)
|
|
|
|
err := runtime.CheckWitness(ic)
|
|
|
|
if shouldFail {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, expected)
|
|
|
|
actual, ok := ic.VM.Estack().Pop().Value().(bool)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, expected[0], actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Run("error", func(t *testing.T) {
|
|
|
|
t.Run("not a hash or key", func(t *testing.T) {
|
|
|
|
check(t, ic, []byte{1, 2, 3}, true)
|
|
|
|
})
|
|
|
|
t.Run("script container is not a transaction", func(t *testing.T) {
|
|
|
|
loadScriptWithHashAndFlags(ic, script, scriptHash, smartcontract.AllowStates)
|
|
|
|
check(t, ic, random.Uint160().BytesBE(), true)
|
|
|
|
})
|
|
|
|
t.Run("check scope", func(t *testing.T) {
|
|
|
|
t.Run("CustomGroups, missing AllowStates flag", func(t *testing.T) {
|
|
|
|
hash := random.Uint160()
|
|
|
|
tx := &transaction.Transaction{
|
|
|
|
Signers: []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: hash,
|
|
|
|
Scopes: transaction.CustomGroups,
|
|
|
|
AllowedGroups: []*keys.PublicKey{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ic.Container = tx
|
|
|
|
callingScriptHash := scriptHash
|
|
|
|
loadScriptWithHashAndFlags(ic, script, callingScriptHash, smartcontract.All)
|
|
|
|
ic.VM.LoadScriptWithHash([]byte{0x1}, random.Uint160(), smartcontract.AllowCall)
|
|
|
|
check(t, ic, hash.BytesBE(), true)
|
|
|
|
})
|
|
|
|
t.Run("CustomGroups, unknown contract", func(t *testing.T) {
|
|
|
|
hash := random.Uint160()
|
|
|
|
tx := &transaction.Transaction{
|
|
|
|
Signers: []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: hash,
|
|
|
|
Scopes: transaction.CustomGroups,
|
|
|
|
AllowedGroups: []*keys.PublicKey{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ic.Container = tx
|
|
|
|
callingScriptHash := scriptHash
|
|
|
|
loadScriptWithHashAndFlags(ic, script, callingScriptHash, smartcontract.All)
|
|
|
|
ic.VM.LoadScriptWithHash([]byte{0x1}, random.Uint160(), smartcontract.AllowStates)
|
|
|
|
check(t, ic, hash.BytesBE(), true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
t.Run("calling scripthash", func(t *testing.T) {
|
|
|
|
t.Run("hashed witness", func(t *testing.T) {
|
|
|
|
callingScriptHash := scriptHash
|
|
|
|
loadScriptWithHashAndFlags(ic, script, callingScriptHash, smartcontract.All)
|
|
|
|
ic.VM.LoadScriptWithHash([]byte{0x1}, random.Uint160(), smartcontract.All)
|
|
|
|
check(t, ic, callingScriptHash.BytesBE(), false, true)
|
|
|
|
})
|
|
|
|
t.Run("keyed witness", func(t *testing.T) {
|
|
|
|
pk, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
callingScriptHash := pk.PublicKey().GetScriptHash()
|
|
|
|
loadScriptWithHashAndFlags(ic, script, callingScriptHash, smartcontract.All)
|
|
|
|
ic.VM.LoadScriptWithHash([]byte{0x1}, random.Uint160(), smartcontract.All)
|
|
|
|
check(t, ic, pk.PublicKey().Bytes(), false, true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("check scope", func(t *testing.T) {
|
|
|
|
t.Run("Global", func(t *testing.T) {
|
|
|
|
hash := random.Uint160()
|
|
|
|
tx := &transaction.Transaction{
|
|
|
|
Signers: []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: hash,
|
|
|
|
Scopes: transaction.Global,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
loadScriptWithHashAndFlags(ic, script, scriptHash, smartcontract.AllowStates)
|
|
|
|
ic.Container = tx
|
|
|
|
check(t, ic, hash.BytesBE(), false, true)
|
|
|
|
})
|
|
|
|
t.Run("CalledByEntry", func(t *testing.T) {
|
|
|
|
hash := random.Uint160()
|
|
|
|
tx := &transaction.Transaction{
|
|
|
|
Signers: []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: hash,
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
loadScriptWithHashAndFlags(ic, script, scriptHash, smartcontract.AllowStates)
|
|
|
|
ic.Container = tx
|
|
|
|
check(t, ic, hash.BytesBE(), false, true)
|
|
|
|
})
|
|
|
|
t.Run("CustomContracts", func(t *testing.T) {
|
|
|
|
hash := random.Uint160()
|
|
|
|
tx := &transaction.Transaction{
|
|
|
|
Signers: []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: hash,
|
|
|
|
Scopes: transaction.CustomContracts,
|
|
|
|
AllowedContracts: []util.Uint160{scriptHash},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
loadScriptWithHashAndFlags(ic, script, scriptHash, smartcontract.AllowStates)
|
|
|
|
ic.Container = tx
|
|
|
|
check(t, ic, hash.BytesBE(), false, true)
|
|
|
|
})
|
|
|
|
t.Run("CustomGroups", func(t *testing.T) {
|
|
|
|
t.Run("empty calling scripthash", func(t *testing.T) {
|
|
|
|
hash := random.Uint160()
|
|
|
|
tx := &transaction.Transaction{
|
|
|
|
Signers: []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: hash,
|
|
|
|
Scopes: transaction.CustomGroups,
|
|
|
|
AllowedGroups: []*keys.PublicKey{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
loadScriptWithHashAndFlags(ic, script, scriptHash, smartcontract.AllowStates)
|
|
|
|
ic.Container = tx
|
|
|
|
check(t, ic, hash.BytesBE(), false, false)
|
|
|
|
})
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
|
|
|
targetHash := random.Uint160()
|
|
|
|
pk, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
tx := &transaction.Transaction{
|
|
|
|
Signers: []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: targetHash,
|
|
|
|
Scopes: transaction.CustomGroups,
|
|
|
|
AllowedGroups: []*keys.PublicKey{pk.PublicKey()},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
contractScript := []byte{byte(opcode.PUSH1), byte(opcode.RET)}
|
|
|
|
contractScriptHash := hash.Hash160(contractScript)
|
|
|
|
contractState := &state.Contract{
|
|
|
|
ID: 15,
|
|
|
|
Script: contractScript,
|
|
|
|
Manifest: manifest.Manifest{
|
|
|
|
Groups: []manifest.Group{{PublicKey: pk.PublicKey()}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, ic.DAO.PutContractState(contractState))
|
|
|
|
loadScriptWithHashAndFlags(ic, contractScript, contractScriptHash, smartcontract.All)
|
|
|
|
ic.VM.LoadScriptWithHash([]byte{0x1}, random.Uint160(), smartcontract.AllowStates)
|
|
|
|
ic.Container = tx
|
|
|
|
check(t, ic, targetHash.BytesBE(), false, true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("bad scope", func(t *testing.T) {
|
|
|
|
hash := random.Uint160()
|
|
|
|
tx := &transaction.Transaction{
|
|
|
|
Signers: []transaction.Signer{
|
|
|
|
{
|
|
|
|
Account: hash,
|
|
|
|
Scopes: transaction.None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
loadScriptWithHashAndFlags(ic, script, scriptHash, smartcontract.AllowStates)
|
|
|
|
ic.Container = tx
|
|
|
|
check(t, ic, hash.BytesBE(), false, false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|