transaction: drop Network from Transaction

We only need it when signing/verifying.
This commit is contained in:
Roman Khimov 2021-03-25 19:18:01 +03:00
parent df12adaa9e
commit d314f82db3
54 changed files with 305 additions and 373 deletions

View file

@ -2,25 +2,20 @@ package result
import (
"encoding/json"
"errors"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// Invoke represents code invocation result and is used by several RPC calls
// that invoke functions, scripts and generic bytecode. Transaction is
// represented in raw serialized format, use transaction.NewTransactionFromBytes
// or GetTransaction method to deserialize it.
// that invoke functions, scripts and generic bytecode.
type Invoke struct {
State string
GasConsumed int64
Script []byte
Stack []stackitem.Item
FaultException string
// Transaction represents transaction bytes. Use GetTransaction method to decode it.
Transaction []byte
Transaction *transaction.Transaction
}
type invokeAux struct {
@ -52,25 +47,29 @@ func (r Invoke) MarshalJSON() ([]byte, error) {
return nil, err
}
}
var txbytes []byte
if r.Transaction != nil {
txbytes = r.Transaction.Bytes()
}
return json.Marshal(&invokeAux{
GasConsumed: r.GasConsumed,
Script: r.Script,
State: r.State,
Stack: st,
FaultException: r.FaultException,
Transaction: r.Transaction,
Transaction: txbytes,
})
}
// UnmarshalJSON implements json.Unmarshaler.
func (r *Invoke) UnmarshalJSON(data []byte) error {
var err error
aux := new(invokeAux)
if err := json.Unmarshal(data, aux); err != nil {
if err = json.Unmarshal(data, aux); err != nil {
return err
}
var arr []json.RawMessage
if err := json.Unmarshal(aux.Stack, &arr); err == nil {
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])
@ -82,18 +81,17 @@ func (r *Invoke) UnmarshalJSON(data []byte) error {
r.Stack = st
}
}
var tx *transaction.Transaction
if len(aux.Transaction) != 0 {
tx, err = transaction.NewTransactionFromBytes(aux.Transaction)
if err != nil {
return err
}
}
r.GasConsumed = aux.GasConsumed
r.Script = aux.Script
r.State = aux.State
r.FaultException = aux.FaultException
r.Transaction = aux.Transaction
r.Transaction = tx
return nil
}
// GetTransaction returns decoded transaction from Invoke.Transaction bytes.
func (r *Invoke) GetTransaction(magic netmode.Magic) (*transaction.Transaction, error) {
if r.Transaction == nil {
return nil, errors.New("empty transaction")
}
return transaction.NewTransactionFromBytes(magic, r.Transaction)
}

View file

@ -6,19 +6,26 @@ import (
"math/big"
"testing"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
func TestInvoke_MarshalJSON(t *testing.T) {
tx := transaction.New([]byte{1, 2, 3, 4}, 0)
tx.Signers = []transaction.Signer{{Account: util.Uint160{1, 2, 3}}}
tx.Scripts = []transaction.Witness{transaction.Witness{InvocationScript: []byte{}, VerificationScript: []byte{}}}
_ = tx.Size()
tx.Hash()
result := &Invoke{
State: "HALT",
GasConsumed: 237626000,
Script: []byte{10},
Stack: []stackitem.Item{stackitem.NewBigInteger(big.NewInt(1))},
FaultException: "",
// Transaction represents transaction bytes. Use GetTransaction method to decode it.
Transaction: []byte{1, 2, 3, 4},
Transaction: tx,
}
data, err := json.Marshal(result)
@ -30,7 +37,7 @@ func TestInvoke_MarshalJSON(t *testing.T) {
"stack":[
{"type":"Integer","value":"1"}
],
"tx":"` + base64.StdEncoding.EncodeToString(result.Transaction) + `"
"tx":"` + base64.StdEncoding.EncodeToString(tx.Bytes()) + `"
}`
require.JSONEq(t, expected, string(data))