2020-01-13 10:21:44 +00:00
|
|
|
package result
|
2018-03-30 06:15:03 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-23 14:31:28 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-02-11 15:33:23 +00:00
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-03-30 06:15:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-02-11 15:33:23 +00:00
|
|
|
// Tx wrapper used for the representation of
|
|
|
|
// transaction on the RPC Server.
|
|
|
|
Tx struct {
|
2020-03-23 14:31:28 +00:00
|
|
|
*transaction.Transaction
|
|
|
|
Fees
|
|
|
|
}
|
2020-02-11 15:33:23 +00:00
|
|
|
|
2020-03-23 14:31:28 +00:00
|
|
|
// Fees is an auxilliary struct for proper Tx marshaling.
|
|
|
|
Fees struct {
|
2020-02-11 15:33:23 +00:00
|
|
|
SysFee util.Fixed8 `json:"sys_fee"`
|
|
|
|
NetFee util.Fixed8 `json:"net_fee"`
|
|
|
|
}
|
|
|
|
|
2018-03-30 06:15:03 +00:00
|
|
|
// Block wrapper used for the representation of
|
2020-01-15 08:29:50 +00:00
|
|
|
// block.Block / block.Base on the RPC Server.
|
2018-03-30 06:15:03 +00:00
|
|
|
Block struct {
|
2020-05-13 18:27:08 +00:00
|
|
|
*block.Base
|
|
|
|
BlockMetadataAndTx
|
|
|
|
}
|
2020-02-11 15:33:23 +00:00
|
|
|
|
2020-05-13 18:27:08 +00:00
|
|
|
// BlockMetadataAndTx is an additional metadata added to standard
|
|
|
|
// block.Base plus specially encoded transactions.
|
|
|
|
BlockMetadataAndTx struct {
|
|
|
|
Size int `json:"size"`
|
|
|
|
NextBlockHash *util.Uint256 `json:"nextblockhash,omitempty"`
|
|
|
|
Confirmations uint32 `json:"confirmations"`
|
|
|
|
Tx []Tx `json:"tx"`
|
2018-03-30 06:15:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewBlock creates a new Block wrapper.
|
2020-02-11 15:33:23 +00:00
|
|
|
func NewBlock(b *block.Block, chain core.Blockchainer) Block {
|
|
|
|
res := Block{
|
2020-05-13 18:27:08 +00:00
|
|
|
Base: &b.Base,
|
|
|
|
BlockMetadataAndTx: BlockMetadataAndTx{
|
|
|
|
Size: io.GetVarSize(b),
|
|
|
|
Confirmations: chain.BlockHeight() - b.Index - 1,
|
|
|
|
Tx: make([]Tx, 0, len(b.Transactions)),
|
|
|
|
},
|
2018-03-30 06:15:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 15:33:23 +00:00
|
|
|
hash := chain.GetHeaderHash(int(b.Index) + 1)
|
2018-03-30 06:15:03 +00:00
|
|
|
if !hash.Equals(util.Uint256{}) {
|
2020-02-11 15:33:23 +00:00
|
|
|
res.NextBlockHash = &hash
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range b.Transactions {
|
2020-03-23 14:31:28 +00:00
|
|
|
res.Tx = append(res.Tx, Tx{
|
|
|
|
Transaction: b.Transactions[i],
|
|
|
|
Fees: Fees{
|
|
|
|
SysFee: chain.SystemFee(b.Transactions[i]),
|
|
|
|
NetFee: chain.NetworkFee(b.Transactions[i]),
|
|
|
|
},
|
|
|
|
})
|
2018-03-30 06:15:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 15:33:23 +00:00
|
|
|
return res
|
2018-03-30 06:15:03 +00:00
|
|
|
}
|
2020-03-23 14:31:28 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (t Tx) MarshalJSON() ([]byte, error) {
|
|
|
|
output, err := json.Marshal(&Fees{
|
|
|
|
SysFee: t.SysFee,
|
|
|
|
NetFee: t.NetFee,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txBytes, err := json.Marshal(t.Transaction)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to keep both transaction.Transaction and tx at the same level in json in order to match C# API,
|
|
|
|
// so there's no way to marshall Tx correctly with standard json.Marshaller tool.
|
|
|
|
if output[len(output)-1] != '}' || txBytes[0] != '{' {
|
|
|
|
return nil, errors.New("can't merge internal jsons")
|
|
|
|
}
|
|
|
|
output[len(output)-1] = ','
|
|
|
|
output = append(output, txBytes[1:]...)
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Marshaler interface.
|
|
|
|
func (t *Tx) UnmarshalJSON(data []byte) error {
|
|
|
|
// As transaction.Transaction and tx are at the same level in json, do unmarshalling
|
|
|
|
// separately for both structs.
|
|
|
|
output := new(Fees)
|
|
|
|
err := json.Unmarshal(data, output)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.SysFee = output.SysFee
|
|
|
|
t.NetFee = output.NetFee
|
|
|
|
|
|
|
|
transaction := new(transaction.Transaction)
|
|
|
|
err = json.Unmarshal(data, transaction)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Transaction = transaction
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-13 18:27:08 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (b Block) MarshalJSON() ([]byte, error) {
|
|
|
|
output, err := json.Marshal(b.BlockMetadataAndTx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
baseBytes, err := json.Marshal(b.Base)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to keep both "fields" at the same level in json in order to
|
|
|
|
// match C# API, so there's no way to marshall Block correctly with
|
|
|
|
// standard json.Marshaller tool.
|
|
|
|
if output[len(output)-1] != '}' || baseBytes[0] != '{' {
|
|
|
|
return nil, errors.New("can't merge internal jsons")
|
|
|
|
}
|
|
|
|
output[len(output)-1] = ','
|
|
|
|
output = append(output, baseBytes[1:]...)
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler interface.
|
|
|
|
func (b *Block) UnmarshalJSON(data []byte) error {
|
|
|
|
// As block.Base and BlockMetadataAndTx are at the same level in json,
|
|
|
|
// do unmarshalling separately for both structs.
|
|
|
|
metaTx := new(BlockMetadataAndTx)
|
|
|
|
base := new(block.Base)
|
|
|
|
err := json.Unmarshal(data, metaTx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(data, base)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.Base = base
|
|
|
|
b.BlockMetadataAndTx = *metaTx
|
|
|
|
return nil
|
|
|
|
}
|