2020-02-21 14:56:28 +00:00
|
|
|
package result
|
|
|
|
|
|
|
|
import (
|
2020-08-13 10:16:48 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"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"
|
2020-02-21 14:56:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ApplicationLog wrapper used for the representation of the
|
|
|
|
// state.AppExecResult based on the specific tx on the RPC Server.
|
|
|
|
type ApplicationLog struct {
|
2020-08-13 10:16:48 +00:00
|
|
|
TxHash util.Uint256
|
|
|
|
Trigger string
|
|
|
|
VMState string
|
|
|
|
GasConsumed int64
|
|
|
|
Stack []stackitem.Item
|
|
|
|
Events []NotificationEvent
|
2020-02-21 14:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//NotificationEvent response wrapper
|
|
|
|
type NotificationEvent struct {
|
|
|
|
Contract util.Uint160 `json:"contract"`
|
2020-07-30 11:57:02 +00:00
|
|
|
Name string `json:"eventname"`
|
2020-02-21 14:56:28 +00:00
|
|
|
Item smartcontract.Parameter `json:"state"`
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:16:48 +00:00
|
|
|
type applicationLogAux struct {
|
|
|
|
TxHash util.Uint256 `json:"txid"`
|
|
|
|
Trigger string `json:"trigger"`
|
|
|
|
VMState string `json:"vmstate"`
|
|
|
|
GasConsumed int64 `json:"gasconsumed,string"`
|
|
|
|
Stack []json.RawMessage `json:"stack"`
|
|
|
|
Events []NotificationEvent `json:"notifications"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (l ApplicationLog) MarshalJSON() ([]byte, error) {
|
|
|
|
arr := make([]json.RawMessage, len(l.Stack))
|
|
|
|
for i := range arr {
|
|
|
|
data, err := stackitem.ToJSONWithTypes(l.Stack[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arr[i] = data
|
|
|
|
}
|
|
|
|
return json.Marshal(&applicationLogAux{
|
|
|
|
TxHash: l.TxHash,
|
|
|
|
Trigger: l.Trigger,
|
|
|
|
VMState: l.VMState,
|
|
|
|
GasConsumed: l.GasConsumed,
|
|
|
|
Stack: arr,
|
|
|
|
Events: l.Events,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
func (l *ApplicationLog) UnmarshalJSON(data []byte) error {
|
|
|
|
aux := new(applicationLogAux)
|
|
|
|
if err := json.Unmarshal(data, aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
st := make([]stackitem.Item, len(aux.Stack))
|
|
|
|
var err error
|
|
|
|
for i := range st {
|
|
|
|
st[i], err = stackitem.FromJSONWithTypes(aux.Stack[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Stack = st
|
|
|
|
l.Trigger = aux.Trigger
|
|
|
|
l.TxHash = aux.TxHash
|
|
|
|
l.VMState = aux.VMState
|
|
|
|
l.Events = aux.Events
|
|
|
|
l.GasConsumed = aux.GasConsumed
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-10 22:00:19 +00:00
|
|
|
// StateEventToResultNotification converts state.NotificationEvent to
|
|
|
|
// result.NotificationEvent.
|
|
|
|
func StateEventToResultNotification(event state.NotificationEvent) NotificationEvent {
|
2020-06-03 12:55:06 +00:00
|
|
|
seen := make(map[stackitem.Item]bool)
|
2020-07-30 11:57:02 +00:00
|
|
|
item := smartcontract.ParameterFromStackItem(event.Item, seen)
|
2020-05-10 22:00:19 +00:00
|
|
|
return NotificationEvent{
|
|
|
|
Contract: event.ScriptHash,
|
2020-07-30 11:57:02 +00:00
|
|
|
Name: event.Name,
|
2020-05-10 22:00:19 +00:00
|
|
|
Item: item,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:56:28 +00:00
|
|
|
// NewApplicationLog creates a new ApplicationLog wrapper.
|
2020-06-18 19:56:42 +00:00
|
|
|
func NewApplicationLog(appExecRes *state.AppExecResult) ApplicationLog {
|
2020-02-21 14:56:28 +00:00
|
|
|
events := make([]NotificationEvent, 0, len(appExecRes.Events))
|
|
|
|
for _, e := range appExecRes.Events {
|
2020-05-10 22:00:19 +00:00
|
|
|
events = append(events, StateEventToResultNotification(e))
|
2020-02-21 14:56:28 +00:00
|
|
|
}
|
2020-06-18 19:56:42 +00:00
|
|
|
return ApplicationLog{
|
|
|
|
TxHash: appExecRes.TxHash,
|
|
|
|
Trigger: appExecRes.Trigger.String(),
|
2020-07-27 14:57:53 +00:00
|
|
|
VMState: appExecRes.VMState.String(),
|
2020-06-23 14:15:35 +00:00
|
|
|
GasConsumed: appExecRes.GasConsumed,
|
2020-08-13 10:16:48 +00:00
|
|
|
Stack: appExecRes.Stack,
|
2020-02-21 14:56:28 +00:00
|
|
|
Events: events,
|
|
|
|
}
|
|
|
|
}
|