2020-01-13 09:27:34 +00:00
|
|
|
package result
|
2019-10-29 15:31:39 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-31 12:26:28 +00:00
|
|
|
"encoding/json"
|
2021-04-28 14:46:34 +00:00
|
|
|
"fmt"
|
2020-07-31 12:26:28 +00:00
|
|
|
|
2022-01-18 19:35:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2021-04-29 14:19:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/iterator"
|
2022-01-18 19:35:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-01-18 21:02:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2020-09-22 09:03:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-04-28 14:46:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-07-31 12:26:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2019-10-29 15:31:39 +00:00
|
|
|
)
|
|
|
|
|
2020-01-13 09:27:34 +00:00
|
|
|
// Invoke represents code invocation result and is used by several RPC calls
|
2021-03-25 16:18:01 +00:00
|
|
|
// that invoke functions, scripts and generic bytecode.
|
2020-01-13 09:27:34 +00:00
|
|
|
type Invoke struct {
|
2021-04-28 14:46:34 +00:00
|
|
|
State string
|
|
|
|
GasConsumed int64
|
|
|
|
Script []byte
|
|
|
|
Stack []stackitem.Item
|
|
|
|
FaultException string
|
2022-01-18 19:35:44 +00:00
|
|
|
Notifications []state.NotificationEvent
|
2021-04-28 14:46:34 +00:00
|
|
|
Transaction *transaction.Transaction
|
2021-11-20 18:55:55 +00:00
|
|
|
Diagnostics *InvokeDiag
|
2021-04-28 14:46:34 +00:00
|
|
|
maxIteratorResultItems int
|
2021-10-07 11:27:55 +00:00
|
|
|
finalize func()
|
2021-04-28 14:46:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 18:55:55 +00:00
|
|
|
// InvokeDiag is an additional diagnostic data for invocation.
|
|
|
|
type InvokeDiag struct {
|
2022-01-18 21:02:19 +00:00
|
|
|
Changes []storage.Operation `json:"storagechanges"`
|
2021-11-20 18:55:55 +00:00
|
|
|
Invocations []*vm.InvocationTree `json:"invokedcontracts"`
|
|
|
|
}
|
|
|
|
|
2021-04-28 14:46:34 +00:00
|
|
|
// NewInvoke returns new Invoke structure with the given fields set.
|
2022-01-18 19:35:44 +00:00
|
|
|
func NewInvoke(ic *interop.Context, script []byte, faultException string, maxIteratorResultItems int) *Invoke {
|
2021-11-20 18:55:55 +00:00
|
|
|
var diag *InvokeDiag
|
2022-01-18 19:35:44 +00:00
|
|
|
tree := ic.VM.GetInvocationTree()
|
2021-11-20 18:55:55 +00:00
|
|
|
if tree != nil {
|
2022-01-18 21:02:19 +00:00
|
|
|
diag = &InvokeDiag{
|
|
|
|
Invocations: tree.Calls,
|
|
|
|
Changes: storage.BatchToOperations(ic.DAO.GetBatch()),
|
|
|
|
}
|
2021-11-20 18:55:55 +00:00
|
|
|
}
|
2022-01-18 19:35:44 +00:00
|
|
|
notifications := ic.Notifications
|
|
|
|
if notifications == nil {
|
|
|
|
notifications = make([]state.NotificationEvent, 0)
|
|
|
|
}
|
2021-04-28 14:46:34 +00:00
|
|
|
return &Invoke{
|
2022-01-18 19:35:44 +00:00
|
|
|
State: ic.VM.State().String(),
|
|
|
|
GasConsumed: ic.VM.GasConsumed(),
|
2021-04-28 14:46:34 +00:00
|
|
|
Script: script,
|
2022-01-18 19:35:44 +00:00
|
|
|
Stack: ic.VM.Estack().ToArray(),
|
2021-04-28 14:46:34 +00:00
|
|
|
FaultException: faultException,
|
2022-01-18 19:35:44 +00:00
|
|
|
Notifications: notifications,
|
2021-11-20 18:55:55 +00:00
|
|
|
Diagnostics: diag,
|
2021-04-28 14:46:34 +00:00
|
|
|
maxIteratorResultItems: maxIteratorResultItems,
|
2022-01-18 19:35:44 +00:00
|
|
|
finalize: ic.Finalize,
|
2021-04-28 14:46:34 +00:00
|
|
|
}
|
2020-07-31 12:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type invokeAux struct {
|
2022-01-18 19:35:44 +00:00
|
|
|
State string `json:"state"`
|
|
|
|
GasConsumed int64 `json:"gasconsumed,string"`
|
|
|
|
Script []byte `json:"script"`
|
|
|
|
Stack json.RawMessage `json:"stack"`
|
|
|
|
FaultException string `json:"exception,omitempty"`
|
|
|
|
Notifications []state.NotificationEvent `json:"notifications"`
|
|
|
|
Transaction []byte `json:"tx,omitempty"`
|
|
|
|
Diagnostics *InvokeDiag `json:"diagnostics,omitempty"`
|
2020-07-31 12:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:46:34 +00:00
|
|
|
type iteratorAux struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Value []json.RawMessage `json:"iterator"`
|
|
|
|
Truncated bool `json:"truncated"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator represents deserialized VM iterator values with truncated flag.
|
|
|
|
type Iterator struct {
|
|
|
|
Values []stackitem.Item
|
|
|
|
Truncated bool
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:27:55 +00:00
|
|
|
// Finalize releases resources occupied by Iterators created at the script invocation.
|
|
|
|
// This method will be called automatically on Invoke marshalling.
|
|
|
|
func (r *Invoke) Finalize() {
|
|
|
|
if r.finalize != nil {
|
|
|
|
r.finalize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:26:28 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (r Invoke) MarshalJSON() ([]byte, error) {
|
2021-10-07 11:27:55 +00:00
|
|
|
defer r.Finalize()
|
2020-07-31 12:26:28 +00:00
|
|
|
var st json.RawMessage
|
|
|
|
arr := make([]json.RawMessage, len(r.Stack))
|
|
|
|
for i := range arr {
|
2021-04-28 14:46:34 +00:00
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
err error
|
|
|
|
)
|
2021-04-29 14:19:30 +00:00
|
|
|
if (r.Stack[i].Type() == stackitem.InteropT) && iterator.IsIterator(r.Stack[i]) {
|
|
|
|
iteratorValues, truncated := iterator.Values(r.Stack[i], r.maxIteratorResultItems)
|
2021-04-28 14:46:34 +00:00
|
|
|
value := make([]json.RawMessage, len(iteratorValues))
|
|
|
|
for j := range iteratorValues {
|
|
|
|
value[j], err = stackitem.ToJSONWithTypes(iteratorValues[j])
|
|
|
|
if err != nil {
|
2021-07-06 21:42:36 +00:00
|
|
|
st = []byte(fmt.Sprintf(`"error: %v"`, err))
|
2021-04-28 14:46:34 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data, err = json.Marshal(iteratorAux{
|
|
|
|
Type: stackitem.InteropT.String(),
|
|
|
|
Value: value,
|
|
|
|
Truncated: truncated,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal iterator: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data, err = stackitem.ToJSONWithTypes(r.Stack[i])
|
|
|
|
if err != nil {
|
2021-07-06 21:42:36 +00:00
|
|
|
st = []byte(fmt.Sprintf(`"error: %v"`, err))
|
2021-04-28 14:46:34 +00:00
|
|
|
break
|
|
|
|
}
|
2020-07-31 12:26:28 +00:00
|
|
|
}
|
|
|
|
arr[i] = data
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if st == nil {
|
|
|
|
st, err = json.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 16:18:01 +00:00
|
|
|
var txbytes []byte
|
|
|
|
if r.Transaction != nil {
|
|
|
|
txbytes = r.Transaction.Bytes()
|
|
|
|
}
|
2020-07-31 12:26:28 +00:00
|
|
|
return json.Marshal(&invokeAux{
|
2021-02-09 08:16:18 +00:00
|
|
|
GasConsumed: r.GasConsumed,
|
2020-10-05 13:33:20 +00:00
|
|
|
Script: r.Script,
|
|
|
|
State: r.State,
|
|
|
|
Stack: st,
|
|
|
|
FaultException: r.FaultException,
|
2022-01-18 19:35:44 +00:00
|
|
|
Notifications: r.Notifications,
|
2021-03-25 16:18:01 +00:00
|
|
|
Transaction: txbytes,
|
2021-11-20 18:55:55 +00:00
|
|
|
Diagnostics: r.Diagnostics,
|
2020-07-31 12:26:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
func (r *Invoke) UnmarshalJSON(data []byte) error {
|
2021-03-25 16:18:01 +00:00
|
|
|
var err error
|
2020-07-31 12:26:28 +00:00
|
|
|
aux := new(invokeAux)
|
2021-03-25 16:18:01 +00:00
|
|
|
if err = json.Unmarshal(data, aux); err != nil {
|
2020-07-31 12:26:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
var arr []json.RawMessage
|
2021-03-25 16:18:01 +00:00
|
|
|
if err = json.Unmarshal(aux.Stack, &arr); err == nil {
|
2020-07-31 12:26:28 +00:00
|
|
|
st := make([]stackitem.Item, len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
st[i], err = stackitem.FromJSONWithTypes(arr[i])
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2021-04-28 14:46:34 +00:00
|
|
|
if st[i].Type() == stackitem.InteropT {
|
|
|
|
iteratorAux := new(iteratorAux)
|
|
|
|
if json.Unmarshal(arr[i], iteratorAux) == nil {
|
|
|
|
iteratorValues := make([]stackitem.Item, len(iteratorAux.Value))
|
|
|
|
for j := range iteratorValues {
|
|
|
|
iteratorValues[j], err = stackitem.FromJSONWithTypes(iteratorAux.Value[j])
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("failed to unmarshal iterator values: %w", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// it's impossible to restore initial iterator type; also iterator is almost
|
|
|
|
// useless outside of the VM, thus let's replace it with a special structure.
|
|
|
|
st[i] = stackitem.NewInterop(Iterator{
|
|
|
|
Values: iteratorValues,
|
|
|
|
Truncated: iteratorAux.Truncated,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 12:26:28 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
r.Stack = st
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 16:18:01 +00:00
|
|
|
var tx *transaction.Transaction
|
|
|
|
if len(aux.Transaction) != 0 {
|
|
|
|
tx, err = transaction.NewTransactionFromBytes(aux.Transaction)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 08:16:18 +00:00
|
|
|
r.GasConsumed = aux.GasConsumed
|
2020-07-31 12:26:28 +00:00
|
|
|
r.Script = aux.Script
|
|
|
|
r.State = aux.State
|
2020-10-05 13:33:20 +00:00
|
|
|
r.FaultException = aux.FaultException
|
2022-01-18 19:35:44 +00:00
|
|
|
r.Notifications = aux.Notifications
|
2021-03-25 16:18:01 +00:00
|
|
|
r.Transaction = tx
|
2021-11-20 18:55:55 +00:00
|
|
|
r.Diagnostics = aux.Diagnostics
|
2020-07-31 12:26:28 +00:00
|
|
|
return nil
|
2019-10-29 15:31:39 +00:00
|
|
|
}
|