2020-01-13 10:21:44 +00:00
|
|
|
package result
|
2018-03-30 06:15:03 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-13 18:27:08 +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/block"
|
|
|
|
"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 (
|
2022-01-13 01:06:06 +00:00
|
|
|
// LedgerAux is a set of methods needed to construct some outputs.
|
|
|
|
LedgerAux interface {
|
|
|
|
BlockHeight() uint32
|
|
|
|
GetHeaderHash(int) util.Uint256
|
|
|
|
}
|
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-06-18 08:47:54 +00:00
|
|
|
block.Block
|
2020-05-13 18:27:08 +00:00
|
|
|
BlockMetadata
|
|
|
|
}
|
2020-02-11 15:33:23 +00:00
|
|
|
|
2020-05-13 18:27:08 +00:00
|
|
|
// BlockMetadata is an additional metadata added to standard
|
|
|
|
// block.Block.
|
|
|
|
BlockMetadata struct {
|
|
|
|
Size int `json:"size"`
|
|
|
|
NextBlockHash *util.Uint256 `json:"nextblockhash,omitempty"`
|
|
|
|
Confirmations uint32 `json:"confirmations"`
|
2018-03-30 06:15:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewBlock creates a new Block wrapper.
|
2022-01-13 01:06:06 +00:00
|
|
|
func NewBlock(b *block.Block, chain LedgerAux) Block {
|
2020-02-11 15:33:23 +00:00
|
|
|
res := Block{
|
2020-06-18 08:47:54 +00:00
|
|
|
Block: *b,
|
2020-05-13 18:27:08 +00:00
|
|
|
BlockMetadata: BlockMetadata{
|
|
|
|
Size: io.GetVarSize(b),
|
2020-06-05 16:27:39 +00:00
|
|
|
Confirmations: chain.BlockHeight() - b.Index + 1,
|
2020-04-22 05:57:55 +00:00
|
|
|
},
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
2018-03-30 06:15:03 +00:00
|
|
|
}
|
2020-05-13 18:27:08 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler interface.
|
|
|
|
func (b Block) MarshalJSON() ([]byte, error) {
|
|
|
|
output, err := json.Marshal(b.BlockMetadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
baseBytes, err := json.Marshal(b.Block)
|
|
|
|
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.Block and BlockMetadata are at the same level in json,
|
|
|
|
// do unmarshalling separately for both structs.
|
|
|
|
meta := new(BlockMetadata)
|
|
|
|
err := json.Unmarshal(data, meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-18 08:47:54 +00:00
|
|
|
err = json.Unmarshal(data, &b.Block)
|
2020-05-13 18:27:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.BlockMetadata = *meta
|
|
|
|
return nil
|
|
|
|
}
|