Update getblock RPC action response

This commit is contained in:
Evgeniy Kulikov 2020-02-11 18:33:23 +03:00
parent bcff9faac4
commit 41ebf12eb3
No known key found for this signature in database
GPG key ID: BF6AEE0A2A699BF2

View file

@ -1,34 +1,107 @@
package wrappers package wrappers
import ( import (
"strconv"
"github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/block" "github.com/CityOfZion/neo-go/pkg/core/block"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
) )
type ( type (
// Tx wrapper used for the representation of
// transaction on the RPC Server.
Tx struct {
TxID util.Uint256 `json:"txid"`
Size int `json:"size"`
Type transaction.TXType `json:"type"`
Version uint8 `json:"version"`
Attributes []transaction.Attribute `json:"attributes"`
VIn []transaction.Input `json:"vin"`
VOut []transaction.Output `json:"vout"`
Scripts []transaction.Witness `json:"scripts"`
SysFee util.Fixed8 `json:"sys_fee"`
NetFee util.Fixed8 `json:"net_fee"`
Nonce uint32 `json:"nonce,omitempty"`
}
// Block wrapper used for the representation of // Block wrapper used for the representation of
// block.Block / block.Base on the RPC Server. // block.Block / block.Base on the RPC Server.
Block struct { Block struct {
*block.Block
Confirmations uint32 `json:"confirmations"`
NextBlockHash util.Uint256 `json:"nextblockhash,omitempty"`
Hash util.Uint256 `json:"hash"` 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"`
Time uint32 `json:"time"`
Index uint32 `json:"index"`
Nonce string `json:"nonce"`
NextConsensus util.Uint160 `json:"nextconsensus"`
Confirmations uint32 `json:"confirmations"`
Script transaction.Witness `json:"script"`
Tx []Tx `json:"tx"`
} }
) )
// NewBlock creates a new Block wrapper. // NewBlock creates a new Block wrapper.
func NewBlock(block *block.Block, chain core.Blockchainer) Block { func NewBlock(b *block.Block, chain core.Blockchainer) Block {
blockWrapper := Block{ res := Block{
Block: block, Version: b.Version,
Hash: block.Hash(), Hash: b.Hash(),
Size: io.GetVarSize(b),
PreviousBlockHash: b.PrevHash,
MerkleRoot: b.MerkleRoot,
Time: b.Timestamp,
Index: b.Index,
Nonce: strconv.FormatUint(b.ConsensusData, 16),
NextConsensus: b.NextConsensus,
Confirmations: chain.BlockHeight() - b.Index - 1,
Script: b.Script,
Tx: make([]Tx, 0, len(b.Transactions)),
} }
hash := chain.GetHeaderHash(int(block.Index) + 1) hash := chain.GetHeaderHash(int(b.Index) + 1)
if !hash.Equals(util.Uint256{}) { if !hash.Equals(util.Uint256{}) {
blockWrapper.NextBlockHash = hash res.NextBlockHash = &hash
} }
blockWrapper.Confirmations = chain.BlockHeight() - block.Index - 1 for i := range b.Transactions {
return blockWrapper tx := Tx{
TxID: b.Transactions[i].Hash(),
Size: io.GetVarSize(b.Transactions[i]),
Type: b.Transactions[i].Type,
Version: b.Transactions[i].Version,
Attributes: make([]transaction.Attribute, 0, len(b.Transactions[i].Attributes)),
VIn: make([]transaction.Input, 0, len(b.Transactions[i].Inputs)),
VOut: make([]transaction.Output, 0, len(b.Transactions[i].Outputs)),
Scripts: make([]transaction.Witness, 0, len(b.Transactions[i].Scripts)),
}
copy(tx.Attributes, b.Transactions[i].Attributes)
copy(tx.VIn, b.Transactions[i].Inputs)
copy(tx.VOut, b.Transactions[i].Outputs)
copy(tx.Scripts, b.Transactions[i].Scripts)
tx.SysFee = chain.SystemFee(b.Transactions[i])
tx.NetFee = chain.NetworkFee(b.Transactions[i])
// set nonce only for MinerTransaction
if miner, ok := b.Transactions[i].Data.(*transaction.MinerTX); ok {
tx.Nonce = miner.Nonce
}
res.Tx = append(res.Tx, tx)
}
return res
} }