2022-07-29 19:02:25 +00:00
|
|
|
package invoker
|
|
|
|
|
|
|
|
import (
|
2022-08-17 13:38:03 +00:00
|
|
|
"errors"
|
2022-07-29 19:02:25 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-08-17 13:38:03 +00:00
|
|
|
"github.com/google/uuid"
|
2022-07-29 19:02:25 +00:00
|
|
|
"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/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-08-17 13:38:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-29 19:02:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rpcInv struct {
|
|
|
|
resInv *result.Invoke
|
2022-08-17 13:38:03 +00:00
|
|
|
resTrm bool
|
|
|
|
resItm []stackitem.Item
|
2022-07-29 19:02:25 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcInv) InvokeContractVerify(contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
|
|
|
|
return r.resInv, r.err
|
|
|
|
}
|
|
|
|
func (r *rpcInv) InvokeFunction(contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
|
|
|
|
return r.resInv, r.err
|
|
|
|
}
|
|
|
|
func (r *rpcInv) InvokeScript(script []byte, signers []transaction.Signer) (*result.Invoke, error) {
|
|
|
|
return r.resInv, r.err
|
|
|
|
}
|
|
|
|
func (r *rpcInv) InvokeContractVerifyAtHeight(height uint32, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
|
|
|
|
return r.resInv, r.err
|
|
|
|
}
|
|
|
|
func (r *rpcInv) InvokeContractVerifyWithState(stateroot util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
|
|
|
|
return r.resInv, r.err
|
|
|
|
}
|
|
|
|
func (r *rpcInv) InvokeFunctionAtHeight(height uint32, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
|
|
|
|
return r.resInv, r.err
|
|
|
|
}
|
|
|
|
func (r *rpcInv) InvokeFunctionWithState(stateroot util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
|
|
|
|
return r.resInv, r.err
|
|
|
|
}
|
|
|
|
func (r *rpcInv) InvokeScriptAtHeight(height uint32, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
|
|
|
|
return r.resInv, r.err
|
|
|
|
}
|
|
|
|
func (r *rpcInv) InvokeScriptWithState(stateroot util.Uint256, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
|
|
|
|
return r.resInv, r.err
|
|
|
|
}
|
2022-08-17 13:38:03 +00:00
|
|
|
func (r *rpcInv) TerminateSession(sessionID uuid.UUID) (bool, error) {
|
|
|
|
return r.resTrm, r.err
|
|
|
|
}
|
|
|
|
func (r *rpcInv) TraverseIterator(sessionID, iteratorID uuid.UUID, maxItemsCount int) ([]stackitem.Item, error) {
|
|
|
|
return r.resItm, r.err
|
|
|
|
}
|
2022-07-29 19:02:25 +00:00
|
|
|
|
|
|
|
func TestInvoker(t *testing.T) {
|
|
|
|
resExp := &result.Invoke{State: "HALT"}
|
2022-08-17 13:38:03 +00:00
|
|
|
ri := &rpcInv{resExp, true, nil, nil}
|
2022-07-29 19:02:25 +00:00
|
|
|
|
|
|
|
testInv := func(t *testing.T, inv *Invoker) {
|
|
|
|
res, err := inv.Call(util.Uint160{}, "method")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, resExp, res)
|
|
|
|
|
|
|
|
res, err = inv.Verify(util.Uint160{}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, resExp, res)
|
|
|
|
|
|
|
|
res, err = inv.Run([]byte{1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, resExp, res)
|
|
|
|
|
|
|
|
res, err = inv.Call(util.Uint160{}, "method")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, resExp, res)
|
|
|
|
|
|
|
|
res, err = inv.Verify(util.Uint160{}, nil, "param")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, resExp, res)
|
|
|
|
|
|
|
|
res, err = inv.Call(util.Uint160{}, "method", 42)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, resExp, res)
|
|
|
|
|
|
|
|
_, err = inv.Verify(util.Uint160{}, nil, make(map[int]int))
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
_, err = inv.Call(util.Uint160{}, "method", make(map[int]int))
|
|
|
|
require.Error(t, err)
|
2022-08-01 12:27:36 +00:00
|
|
|
|
|
|
|
res, err = inv.CallAndExpandIterator(util.Uint160{}, "method", 10, 42)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, resExp, res)
|
|
|
|
|
|
|
|
_, err = inv.CallAndExpandIterator(util.Uint160{}, "method", 10, make(map[int]int))
|
|
|
|
require.Error(t, err)
|
2022-07-29 19:02:25 +00:00
|
|
|
}
|
|
|
|
t.Run("standard", func(t *testing.T) {
|
|
|
|
testInv(t, New(ri, nil))
|
|
|
|
})
|
|
|
|
t.Run("historic, height", func(t *testing.T) {
|
|
|
|
testInv(t, NewHistoricAtHeight(100500, ri, nil))
|
|
|
|
})
|
|
|
|
t.Run("historic, state", func(t *testing.T) {
|
|
|
|
testInv(t, NewHistoricWithState(util.Uint256{}, ri, nil))
|
|
|
|
})
|
|
|
|
t.Run("broken historic", func(t *testing.T) {
|
|
|
|
inv := New(&historicConverter{client: ri}, nil) // It's not possible to do this from outside.
|
|
|
|
require.Panics(t, func() { _, _ = inv.Call(util.Uint160{}, "method") })
|
|
|
|
require.Panics(t, func() { _, _ = inv.Verify(util.Uint160{}, nil, "param") })
|
|
|
|
require.Panics(t, func() { _, _ = inv.Run([]byte{1}) })
|
|
|
|
})
|
2022-08-17 13:38:03 +00:00
|
|
|
t.Run("terminate session", func(t *testing.T) {
|
2022-09-09 10:41:45 +00:00
|
|
|
for _, inv := range []*Invoker{New(ri, nil), NewHistoricWithState(util.Uint256{}, ri, nil)} {
|
2022-08-17 13:38:03 +00:00
|
|
|
ri.err = errors.New("")
|
|
|
|
require.Error(t, inv.TerminateSession(uuid.UUID{}))
|
|
|
|
ri.err = nil
|
|
|
|
ri.resTrm = false
|
|
|
|
require.Error(t, inv.TerminateSession(uuid.UUID{}))
|
|
|
|
ri.resTrm = true
|
|
|
|
require.NoError(t, inv.TerminateSession(uuid.UUID{}))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("traverse iterator", func(t *testing.T) {
|
2022-09-09 10:41:45 +00:00
|
|
|
for _, inv := range []*Invoker{New(ri, nil), NewHistoricWithState(util.Uint256{}, ri, nil)} {
|
2022-08-17 13:38:03 +00:00
|
|
|
res, err := inv.TraverseIterator(uuid.UUID{}, &result.Iterator{
|
|
|
|
Values: []stackitem.Item{stackitem.Make(42)},
|
|
|
|
}, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []stackitem.Item{stackitem.Make(42)}, res)
|
|
|
|
|
|
|
|
res, err = inv.TraverseIterator(uuid.UUID{}, &result.Iterator{
|
|
|
|
Values: []stackitem.Item{stackitem.Make(42)},
|
|
|
|
}, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []stackitem.Item{stackitem.Make(42)}, res)
|
|
|
|
|
|
|
|
res, err = inv.TraverseIterator(uuid.UUID{}, &result.Iterator{
|
|
|
|
Values: []stackitem.Item{stackitem.Make(42)},
|
|
|
|
}, 2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []stackitem.Item{stackitem.Make(42)}, res)
|
|
|
|
|
|
|
|
ri.err = errors.New("")
|
|
|
|
_, err = inv.TraverseIterator(uuid.UUID{}, &result.Iterator{
|
|
|
|
ID: &uuid.UUID{},
|
|
|
|
}, 2)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
ri.err = nil
|
|
|
|
ri.resItm = []stackitem.Item{stackitem.Make(42)}
|
|
|
|
res, err = inv.TraverseIterator(uuid.UUID{}, &result.Iterator{
|
|
|
|
ID: &uuid.UUID{},
|
|
|
|
}, 2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []stackitem.Item{stackitem.Make(42)}, res)
|
|
|
|
}
|
|
|
|
})
|
2022-07-29 19:02:25 +00:00
|
|
|
}
|