2020-11-11 15:43:28 +00:00
|
|
|
package result
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ApplicationLog represent the results of the script executions for a block or a transaction.
|
2020-11-11 15:43:28 +00:00
|
|
|
type ApplicationLog struct {
|
2020-11-10 11:30:00 +00:00
|
|
|
Container util.Uint256
|
|
|
|
IsTransaction bool
|
|
|
|
Executions []state.Execution
|
2020-11-11 15:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// applicationLogAux is an auxiliary struct for ApplicationLog JSON marshalling.
|
|
|
|
type applicationLogAux struct {
|
|
|
|
TxHash *util.Uint256 `json:"txid,omitempty"`
|
|
|
|
BlockHash *util.Uint256 `json:"blockhash,omitempty"`
|
|
|
|
Executions []json.RawMessage `json:"executions"`
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2020-11-11 15:43:28 +00:00
|
|
|
func (l ApplicationLog) MarshalJSON() ([]byte, error) {
|
|
|
|
result := &applicationLogAux{
|
|
|
|
Executions: make([]json.RawMessage, len(l.Executions)),
|
|
|
|
}
|
2020-11-10 11:30:00 +00:00
|
|
|
if l.IsTransaction {
|
2020-11-11 15:43:28 +00:00
|
|
|
result.TxHash = &l.Container
|
|
|
|
} else {
|
|
|
|
result.BlockHash = &l.Container
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
for i := range result.Executions {
|
|
|
|
result.Executions[i], err = json.Marshal(l.Executions[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal execution #%d: %w", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json.Marshal(result)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2020-11-11 15:43:28 +00:00
|
|
|
func (l *ApplicationLog) UnmarshalJSON(data []byte) error {
|
|
|
|
aux := new(applicationLogAux)
|
|
|
|
if err := json.Unmarshal(data, aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if aux.TxHash != nil {
|
|
|
|
l.Container = *aux.TxHash
|
|
|
|
} else if aux.BlockHash != nil {
|
|
|
|
l.Container = *aux.BlockHash
|
|
|
|
} else {
|
|
|
|
return errors.New("no block or transaction hash")
|
|
|
|
}
|
|
|
|
l.Executions = make([]state.Execution, len(aux.Executions))
|
|
|
|
for i := range l.Executions {
|
|
|
|
err := json.Unmarshal(aux.Executions[i], &l.Executions[i])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal execution #%d: %w", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewApplicationLog creates an ApplicationLog from a set of several application execution results
|
2020-11-10 11:30:00 +00:00
|
|
|
// including only the results with the specified trigger.
|
|
|
|
func NewApplicationLog(hash util.Uint256, aers []state.AppExecResult, trig trigger.Type) ApplicationLog {
|
2020-11-11 15:43:28 +00:00
|
|
|
result := ApplicationLog{
|
2020-11-10 11:30:00 +00:00
|
|
|
Container: hash,
|
|
|
|
IsTransaction: aers[0].Trigger == trigger.Application,
|
2020-11-11 15:43:28 +00:00
|
|
|
}
|
|
|
|
for _, aer := range aers {
|
2020-11-10 11:30:00 +00:00
|
|
|
if aer.Trigger&trig != 0 {
|
|
|
|
result.Executions = append(result.Executions, aer.Execution)
|
|
|
|
}
|
2020-11-11 15:43:28 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|