2020-01-13 10:21:44 +00:00
|
|
|
package result
|
2018-03-30 06:15:03 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-06 16:53:11 +00:00
|
|
|
"fmt"
|
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"
|
2020-04-08 10:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-03-06 16:53:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-03-03 14:21:42 +00:00
|
|
|
"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-04-22 05:57:55 +00:00
|
|
|
// ConsensusData is a wrapper for block.ConsensusData
|
|
|
|
ConsensusData struct {
|
|
|
|
PrimaryIndex uint32 `json:"primary"`
|
|
|
|
Nonce string `json:"nonce"`
|
|
|
|
}
|
|
|
|
|
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-02-11 15:33:23 +00:00
|
|
|
Hash util.Uint256 `json:"hash"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
Version uint32 `json:"version"`
|
|
|
|
NextBlockHash *util.Uint256 `json:"nextblockhash,omitempty"`
|
|
|
|
PreviousBlockHash util.Uint256 `json:"previousblockhash"`
|
|
|
|
MerkleRoot util.Uint256 `json:"merkleroot"`
|
2020-04-21 11:26:57 +00:00
|
|
|
Time uint64 `json:"time"`
|
2020-02-11 15:33:23 +00:00
|
|
|
Index uint32 `json:"index"`
|
2020-04-22 05:57:55 +00:00
|
|
|
ConsensusData ConsensusData `json:"consensus_data"`
|
2020-03-06 16:53:11 +00:00
|
|
|
NextConsensus string `json:"nextconsensus"`
|
2020-02-11 15:33:23 +00:00
|
|
|
|
|
|
|
Confirmations uint32 `json:"confirmations"`
|
|
|
|
|
|
|
|
Script transaction.Witness `json:"script"`
|
|
|
|
|
2020-05-08 17:54:24 +00:00
|
|
|
Tx []*transaction.Transaction `json:"tx"`
|
2018-03-30 06:15:03 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewBlock creates a new Block wrapper.
|
2020-04-08 10:56:04 +00:00
|
|
|
func NewBlock(b *block.Block, chain blockchainer.Blockchainer) Block {
|
2020-02-11 15:33:23 +00:00
|
|
|
res := Block{
|
|
|
|
Version: b.Version,
|
|
|
|
Hash: b.Hash(),
|
|
|
|
Size: io.GetVarSize(b),
|
|
|
|
PreviousBlockHash: b.PrevHash,
|
|
|
|
MerkleRoot: b.MerkleRoot,
|
|
|
|
Time: b.Timestamp,
|
|
|
|
Index: b.Index,
|
2020-04-22 05:57:55 +00:00
|
|
|
ConsensusData: ConsensusData{
|
|
|
|
PrimaryIndex: b.ConsensusData.PrimaryIndex,
|
|
|
|
Nonce: fmt.Sprintf("%016x", b.ConsensusData.Nonce),
|
|
|
|
},
|
|
|
|
NextConsensus: address.Uint160ToString(b.NextConsensus),
|
|
|
|
Confirmations: chain.BlockHeight() - b.Index - 1,
|
2020-02-11 15:33:23 +00:00
|
|
|
|
|
|
|
Script: b.Script,
|
|
|
|
|
2020-05-08 17:54:24 +00:00
|
|
|
Tx: 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
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
2018-03-30 06:15:03 +00:00
|
|
|
}
|