2019-11-28 16:06:09 +00:00
|
|
|
package state
|
2019-11-13 13:55:20 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-03 16:58:50 +00:00
|
|
|
"encoding/json"
|
2020-06-29 08:25:32 +00:00
|
|
|
"errors"
|
2020-09-03 16:58:50 +00:00
|
|
|
"fmt"
|
2020-06-29 08:25:32 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-08 14:28:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2019-11-13 13:55:20 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NotificationEvent is a tuple of the scripthash that has emitted the Item as a
|
|
|
|
// notification and the item itself.
|
2019-11-13 13:55:20 +00:00
|
|
|
type NotificationEvent struct {
|
2020-09-03 16:58:50 +00:00
|
|
|
ScriptHash util.Uint160 `json:"contract"`
|
|
|
|
Name string `json:"eventname"`
|
|
|
|
Item *stackitem.Array `json:"state"`
|
2019-11-13 13:55:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// AppExecResult represents the result of the script execution, gathering together
|
2019-11-13 13:55:20 +00:00
|
|
|
// all resulting notifications, state, stack and other metadata.
|
|
|
|
type AppExecResult struct {
|
2020-11-11 15:43:28 +00:00
|
|
|
Container util.Uint256
|
|
|
|
Execution
|
2019-11-13 13:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Serializable interface.
|
2019-12-09 15:25:15 +00:00
|
|
|
func (ne *NotificationEvent) EncodeBinary(w *io.BinWriter) {
|
2022-05-31 17:10:20 +00:00
|
|
|
ne.EncodeBinaryWithContext(w, stackitem.NewSerializationContext())
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinaryWithContext is the same as EncodeBinary, but allows to efficiently reuse
|
|
|
|
// stack item serialization context.
|
|
|
|
func (ne *NotificationEvent) EncodeBinaryWithContext(w *io.BinWriter, sc *stackitem.SerializationContext) {
|
2020-05-04 07:35:56 +00:00
|
|
|
ne.ScriptHash.EncodeBinary(w)
|
2020-06-29 08:25:32 +00:00
|
|
|
w.WriteString(ne.Name)
|
2022-05-31 17:10:20 +00:00
|
|
|
b, err := sc.Serialize(ne.Item, false)
|
|
|
|
if err != nil {
|
|
|
|
w.Err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteBytes(b)
|
2019-11-13 13:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the Serializable interface.
|
|
|
|
func (ne *NotificationEvent) DecodeBinary(r *io.BinReader) {
|
2020-05-04 07:35:56 +00:00
|
|
|
ne.ScriptHash.DecodeBinary(r)
|
2020-06-29 08:25:32 +00:00
|
|
|
ne.Name = r.ReadString()
|
2021-07-06 16:56:23 +00:00
|
|
|
item := stackitem.DecodeBinary(r)
|
2020-06-29 08:25:32 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
arr, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
r.Err = errors.New("Array or Struct expected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ne.Item = stackitem.NewArray(arr)
|
2019-11-13 13:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements the Serializable interface.
|
|
|
|
func (aer *AppExecResult) EncodeBinary(w *io.BinWriter) {
|
2022-05-31 17:10:20 +00:00
|
|
|
aer.EncodeBinaryWithContext(w, stackitem.NewSerializationContext())
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinaryWithContext is the same as EncodeBinary, but allows to efficiently reuse
|
|
|
|
// stack item serialization context.
|
|
|
|
func (aer *AppExecResult) EncodeBinaryWithContext(w *io.BinWriter, sc *stackitem.SerializationContext) {
|
2020-11-11 15:43:28 +00:00
|
|
|
w.WriteBytes(aer.Container[:])
|
2020-02-26 13:44:54 +00:00
|
|
|
w.WriteB(byte(aer.Trigger))
|
2020-07-27 14:57:53 +00:00
|
|
|
w.WriteB(byte(aer.VMState))
|
2020-06-23 14:15:35 +00:00
|
|
|
w.WriteU64LE(uint64(aer.GasConsumed))
|
2020-12-18 09:28:01 +00:00
|
|
|
// Stack items are expected to be marshaled one by one.
|
|
|
|
w.WriteVarUint(uint64(len(aer.Stack)))
|
|
|
|
for _, it := range aer.Stack {
|
2022-05-31 17:10:20 +00:00
|
|
|
b, err := sc.Serialize(it, true)
|
|
|
|
if err != nil {
|
|
|
|
w.Err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteBytes(b)
|
2020-12-18 09:28:01 +00:00
|
|
|
}
|
2021-08-06 08:55:43 +00:00
|
|
|
w.WriteVarUint(uint64(len(aer.Events)))
|
|
|
|
for i := range aer.Events {
|
2022-05-31 17:10:20 +00:00
|
|
|
aer.Events[i].EncodeBinaryWithContext(w, sc)
|
2021-08-06 08:55:43 +00:00
|
|
|
}
|
2020-10-05 14:04:17 +00:00
|
|
|
w.WriteVarBytes([]byte(aer.FaultException))
|
2019-11-13 13:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the Serializable interface.
|
|
|
|
func (aer *AppExecResult) DecodeBinary(r *io.BinReader) {
|
2020-11-11 15:43:28 +00:00
|
|
|
r.ReadBytes(aer.Container[:])
|
2020-02-26 13:44:54 +00:00
|
|
|
aer.Trigger = trigger.Type(r.ReadB())
|
2022-07-08 14:28:29 +00:00
|
|
|
aer.VMState = vmstate.State(r.ReadB())
|
2020-06-23 14:15:35 +00:00
|
|
|
aer.GasConsumed = int64(r.ReadU64LE())
|
2020-12-18 09:28:01 +00:00
|
|
|
sz := r.ReadVarUint()
|
2021-07-19 10:35:48 +00:00
|
|
|
if stackitem.MaxDeserialized < sz && r.Err == nil {
|
2020-12-18 09:28:01 +00:00
|
|
|
r.Err = errors.New("invalid format")
|
|
|
|
}
|
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
arr := make([]stackitem.Item, sz)
|
|
|
|
for i := 0; i < int(sz); i++ {
|
2021-07-06 16:56:23 +00:00
|
|
|
arr[i] = stackitem.DecodeBinaryProtected(r)
|
2020-12-18 09:28:01 +00:00
|
|
|
if r.Err != nil {
|
2020-07-31 12:48:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 09:28:01 +00:00
|
|
|
aer.Stack = arr
|
2019-11-13 13:55:20 +00:00
|
|
|
r.ReadArray(&aer.Events)
|
2020-10-05 14:04:17 +00:00
|
|
|
aer.FaultException = r.ReadString()
|
2019-11-13 13:55:20 +00:00
|
|
|
}
|
2020-09-03 16:58:50 +00:00
|
|
|
|
|
|
|
// notificationEventAux is an auxiliary struct for NotificationEvent JSON marshalling.
|
|
|
|
type notificationEventAux struct {
|
|
|
|
ScriptHash util.Uint160 `json:"contract"`
|
|
|
|
Name string `json:"eventname"`
|
|
|
|
Item json.RawMessage `json:"state"`
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-10-19 10:44:20 +00:00
|
|
|
func (ne NotificationEvent) MarshalJSON() ([]byte, error) {
|
2020-09-03 16:58:50 +00:00
|
|
|
item, err := stackitem.ToJSONWithTypes(ne.Item)
|
|
|
|
if err != nil {
|
2021-07-06 21:42:36 +00:00
|
|
|
item = []byte(fmt.Sprintf(`"error: %v"`, err))
|
2020-09-03 16:58:50 +00:00
|
|
|
}
|
|
|
|
return json.Marshal(¬ificationEventAux{
|
|
|
|
ScriptHash: ne.ScriptHash,
|
|
|
|
Name: ne.Name,
|
|
|
|
Item: item,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-09-03 16:58:50 +00:00
|
|
|
func (ne *NotificationEvent) UnmarshalJSON(data []byte) error {
|
|
|
|
aux := new(notificationEventAux)
|
|
|
|
if err := json.Unmarshal(data, aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
item, err := stackitem.FromJSONWithTypes(aux.Item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if t := item.Type(); t != stackitem.ArrayT {
|
|
|
|
return fmt.Errorf("failed to convert notification event state of type %s to array", t.String())
|
|
|
|
}
|
|
|
|
ne.Item = item.(*stackitem.Array)
|
|
|
|
ne.Name = aux.Name
|
|
|
|
ne.ScriptHash = aux.ScriptHash
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// appExecResultAux is an auxiliary struct for JSON marshalling.
|
2020-09-03 16:58:50 +00:00
|
|
|
type appExecResultAux struct {
|
2020-11-12 10:06:11 +00:00
|
|
|
Container util.Uint256 `json:"container"`
|
2020-11-11 15:43:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-11-11 15:43:28 +00:00
|
|
|
func (aer *AppExecResult) MarshalJSON() ([]byte, error) {
|
|
|
|
h, err := json.Marshal(&appExecResultAux{
|
2020-11-12 10:06:11 +00:00
|
|
|
Container: aer.Container,
|
2020-11-11 15:43:28 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal hash: %w", err)
|
|
|
|
}
|
|
|
|
exec, err := json.Marshal(aer.Execution)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal execution: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if h[len(h)-1] != '}' || exec[0] != '{' {
|
|
|
|
return nil, errors.New("can't merge internal jsons")
|
|
|
|
}
|
|
|
|
h[len(h)-1] = ','
|
|
|
|
h = append(h, exec[1:]...)
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-11-11 15:43:28 +00:00
|
|
|
func (aer *AppExecResult) UnmarshalJSON(data []byte) error {
|
|
|
|
aux := new(appExecResultAux)
|
|
|
|
if err := json.Unmarshal(data, aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &aer.Execution); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-12 10:06:11 +00:00
|
|
|
aer.Container = aux.Container
|
2020-11-11 15:43:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execution represents the result of a single script execution, gathering together
|
|
|
|
// all resulting notifications, state, stack and other metadata.
|
|
|
|
type Execution struct {
|
|
|
|
Trigger trigger.Type
|
2022-07-08 14:28:29 +00:00
|
|
|
VMState vmstate.State
|
2020-11-11 15:43:28 +00:00
|
|
|
GasConsumed int64
|
|
|
|
Stack []stackitem.Item
|
|
|
|
Events []NotificationEvent
|
|
|
|
FaultException string
|
|
|
|
}
|
|
|
|
|
|
|
|
// executionAux represents an auxiliary struct for Execution JSON marshalling.
|
|
|
|
type executionAux struct {
|
2020-10-05 14:04:17 +00:00
|
|
|
Trigger string `json:"trigger"`
|
|
|
|
VMState string `json:"vmstate"`
|
2021-02-09 08:16:18 +00:00
|
|
|
GasConsumed int64 `json:"gasconsumed,string"`
|
2020-10-05 14:04:17 +00:00
|
|
|
Stack json.RawMessage `json:"stack"`
|
|
|
|
Events []NotificationEvent `json:"notifications"`
|
|
|
|
FaultException string `json:"exception,omitempty"`
|
2020-09-03 16:58:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-11-11 15:43:28 +00:00
|
|
|
func (e Execution) MarshalJSON() ([]byte, error) {
|
|
|
|
arr := make([]json.RawMessage, len(e.Stack))
|
2020-09-03 16:58:50 +00:00
|
|
|
for i := range arr {
|
2020-11-11 15:43:28 +00:00
|
|
|
data, err := stackitem.ToJSONWithTypes(e.Stack[i])
|
2020-09-03 16:58:50 +00:00
|
|
|
if err != nil {
|
2021-07-06 21:42:36 +00:00
|
|
|
data = []byte(fmt.Sprintf(`"error: %v"`, err))
|
2020-09-03 16:58:50 +00:00
|
|
|
}
|
|
|
|
arr[i] = data
|
|
|
|
}
|
2020-12-18 09:28:01 +00:00
|
|
|
st, err := json.Marshal(arr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-21 11:08:15 +00:00
|
|
|
}
|
2020-11-11 15:43:28 +00:00
|
|
|
return json.Marshal(&executionAux{
|
|
|
|
Trigger: e.Trigger.String(),
|
|
|
|
VMState: e.VMState.String(),
|
2021-02-09 08:16:18 +00:00
|
|
|
GasConsumed: e.GasConsumed,
|
2020-10-05 14:04:17 +00:00
|
|
|
Stack: st,
|
2020-11-11 15:43:28 +00:00
|
|
|
Events: e.Events,
|
|
|
|
FaultException: e.FaultException,
|
2020-09-03 16:58:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-11-11 15:43:28 +00:00
|
|
|
func (e *Execution) UnmarshalJSON(data []byte) error {
|
|
|
|
aux := new(executionAux)
|
2020-09-03 16:58:50 +00:00
|
|
|
if err := json.Unmarshal(data, aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var arr []json.RawMessage
|
|
|
|
if err := json.Unmarshal(aux.Stack, &arr); err == nil {
|
|
|
|
st := make([]stackitem.Item, len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
st[i], err = stackitem.FromJSONWithTypes(arr[i])
|
|
|
|
if err != nil {
|
2020-12-18 09:28:01 +00:00
|
|
|
var s string
|
|
|
|
if json.Unmarshal(arr[i], &s) != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = nil
|
2020-09-03 16:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == nil {
|
2020-11-11 15:43:28 +00:00
|
|
|
e.Stack = st
|
2020-09-03 16:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
trigger, err := trigger.FromString(aux.Trigger)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-11 15:43:28 +00:00
|
|
|
e.Trigger = trigger
|
2022-07-08 14:28:29 +00:00
|
|
|
state, err := vmstate.FromString(aux.VMState)
|
2020-09-03 16:58:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-11 15:43:28 +00:00
|
|
|
e.VMState = state
|
|
|
|
e.Events = aux.Events
|
2021-02-09 08:16:18 +00:00
|
|
|
e.GasConsumed = aux.GasConsumed
|
2020-11-11 15:43:28 +00:00
|
|
|
e.FaultException = aux.FaultException
|
2020-09-03 16:58:50 +00:00
|
|
|
return nil
|
|
|
|
}
|