2022-08-15 15:44:12 +00:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testAct struct {
|
|
|
|
err error
|
|
|
|
res *result.Invoke
|
|
|
|
tx *transaction.Transaction
|
|
|
|
txh util.Uint256
|
|
|
|
vub uint32
|
|
|
|
}
|
|
|
|
|
2023-04-03 10:34:24 +00:00
|
|
|
func (t *testAct) Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) {
|
2022-08-15 15:44:12 +00:00
|
|
|
return t.res, t.err
|
|
|
|
}
|
2023-04-03 10:34:24 +00:00
|
|
|
func (t *testAct) MakeCall(contract util.Uint160, method string, params ...any) (*transaction.Transaction, error) {
|
2022-08-15 15:44:12 +00:00
|
|
|
return t.tx, t.err
|
|
|
|
}
|
2023-04-03 10:34:24 +00:00
|
|
|
func (t *testAct) MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...any) (*transaction.Transaction, error) {
|
2022-08-15 15:44:12 +00:00
|
|
|
return t.tx, t.err
|
|
|
|
}
|
2023-04-03 10:34:24 +00:00
|
|
|
func (t *testAct) SendCall(contract util.Uint160, method string, params ...any) (util.Uint256, uint32, error) {
|
2022-08-15 15:44:12 +00:00
|
|
|
return t.txh, t.vub, t.err
|
|
|
|
}
|
|
|
|
func (t *testAct) MakeRun(script []byte) (*transaction.Transaction, error) {
|
|
|
|
return t.tx, t.err
|
|
|
|
}
|
|
|
|
func (t *testAct) MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error) {
|
|
|
|
return t.tx, t.err
|
|
|
|
}
|
|
|
|
func (t *testAct) SendRun(script []byte) (util.Uint256, uint32, error) {
|
|
|
|
return t.txh, t.vub, t.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReader(t *testing.T) {
|
|
|
|
ta := new(testAct)
|
|
|
|
pc := NewReader(ta)
|
|
|
|
|
|
|
|
meth := []func() (int64, error){
|
|
|
|
pc.GetExecFeeFactor,
|
|
|
|
pc.GetFeePerByte,
|
|
|
|
pc.GetStoragePrice,
|
|
|
|
}
|
|
|
|
|
|
|
|
ta.err = errors.New("")
|
|
|
|
for _, m := range meth {
|
|
|
|
_, err := m()
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
_, err := pc.IsBlocked(util.Uint160{1, 2, 3})
|
|
|
|
require.Error(t, err)
|
2023-09-20 13:33:04 +00:00
|
|
|
_, err = pc.GetAttributeFee(transaction.ConflictsT)
|
|
|
|
require.Error(t, err)
|
2022-08-15 15:44:12 +00:00
|
|
|
|
|
|
|
ta.err = nil
|
|
|
|
ta.res = &result.Invoke{
|
|
|
|
State: "HALT",
|
|
|
|
Stack: []stackitem.Item{
|
|
|
|
stackitem.Make(42),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, m := range meth {
|
|
|
|
val, err := m()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(42), val)
|
|
|
|
}
|
2023-09-20 13:33:04 +00:00
|
|
|
v, err := pc.GetAttributeFee(transaction.ConflictsT)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(42), v)
|
2022-08-15 15:44:12 +00:00
|
|
|
ta.res = &result.Invoke{
|
|
|
|
State: "HALT",
|
|
|
|
Stack: []stackitem.Item{
|
|
|
|
stackitem.Make(true),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
val, err := pc.IsBlocked(util.Uint160{1, 2, 3})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntSetters(t *testing.T) {
|
|
|
|
ta := new(testAct)
|
|
|
|
pc := New(ta)
|
|
|
|
|
|
|
|
meth := []func(int64) (util.Uint256, uint32, error){
|
|
|
|
pc.SetExecFeeFactor,
|
|
|
|
pc.SetFeePerByte,
|
|
|
|
pc.SetStoragePrice,
|
|
|
|
}
|
|
|
|
|
|
|
|
ta.err = errors.New("")
|
|
|
|
for _, m := range meth {
|
|
|
|
_, _, err := m(42)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2023-09-20 13:33:04 +00:00
|
|
|
_, _, err := pc.SetAttributeFee(transaction.OracleResponseT, 123)
|
|
|
|
require.Error(t, err)
|
2022-08-15 15:44:12 +00:00
|
|
|
|
|
|
|
ta.err = nil
|
|
|
|
ta.txh = util.Uint256{1, 2, 3}
|
|
|
|
ta.vub = 42
|
|
|
|
for _, m := range meth {
|
|
|
|
h, vub, err := m(100)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ta.txh, h)
|
|
|
|
require.Equal(t, ta.vub, vub)
|
|
|
|
}
|
2023-09-20 13:33:04 +00:00
|
|
|
h, vub, err := pc.SetAttributeFee(transaction.OracleResponseT, 123)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ta.txh, h)
|
|
|
|
require.Equal(t, ta.vub, vub)
|
2022-08-15 15:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint160Setters(t *testing.T) {
|
|
|
|
ta := new(testAct)
|
|
|
|
pc := New(ta)
|
|
|
|
|
|
|
|
meth := []func(util.Uint160) (util.Uint256, uint32, error){
|
|
|
|
pc.BlockAccount,
|
|
|
|
pc.UnblockAccount,
|
|
|
|
}
|
|
|
|
|
|
|
|
ta.err = errors.New("")
|
|
|
|
for _, m := range meth {
|
|
|
|
_, _, err := m(util.Uint160{})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ta.err = nil
|
|
|
|
ta.txh = util.Uint256{1, 2, 3}
|
|
|
|
ta.vub = 42
|
|
|
|
for _, m := range meth {
|
|
|
|
h, vub, err := m(util.Uint160{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ta.txh, h)
|
|
|
|
require.Equal(t, ta.vub, vub)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntTransactions(t *testing.T) {
|
|
|
|
ta := new(testAct)
|
|
|
|
pc := New(ta)
|
|
|
|
|
|
|
|
for _, fun := range []func(int64) (*transaction.Transaction, error){
|
|
|
|
pc.SetExecFeeFactorTransaction,
|
|
|
|
pc.SetExecFeeFactorUnsigned,
|
|
|
|
pc.SetFeePerByteTransaction,
|
|
|
|
pc.SetFeePerByteUnsigned,
|
|
|
|
pc.SetStoragePriceTransaction,
|
|
|
|
pc.SetStoragePriceUnsigned,
|
|
|
|
} {
|
|
|
|
ta.err = errors.New("")
|
|
|
|
_, err := fun(1)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
ta.err = nil
|
|
|
|
ta.tx = &transaction.Transaction{Nonce: 100500, ValidUntilBlock: 42}
|
|
|
|
tx, err := fun(1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ta.tx, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint160Transactions(t *testing.T) {
|
|
|
|
ta := new(testAct)
|
|
|
|
pc := New(ta)
|
|
|
|
|
|
|
|
for _, fun := range []func(util.Uint160) (*transaction.Transaction, error){
|
|
|
|
pc.BlockAccountTransaction,
|
|
|
|
pc.BlockAccountUnsigned,
|
|
|
|
pc.UnblockAccountTransaction,
|
|
|
|
pc.UnblockAccountUnsigned,
|
|
|
|
} {
|
|
|
|
ta.err = errors.New("")
|
|
|
|
_, err := fun(util.Uint160{1})
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
ta.err = nil
|
|
|
|
ta.tx = &transaction.Transaction{Nonce: 100500, ValidUntilBlock: 42}
|
|
|
|
tx, err := fun(util.Uint160{1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ta.tx, tx)
|
|
|
|
}
|
|
|
|
}
|