Merge pull request #660 from nspcc-dev/update-getblock-response-wrapper

[rpc] Fix wrong answer for getblock method
This commit is contained in:
Roman Khimov 2020-02-13 13:03:44 +03:00 committed by GitHub
commit cac233383f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 40 deletions

View file

@ -88,32 +88,9 @@ type StringResultResponse struct {
// GetBlockResponse struct for testing.
type GetBlockResponse struct {
Jsonrpc string `json:"jsonrpc"`
Result struct {
Version int `json:"version"`
Previousblockhash string `json:"previousblockhash"`
Merkleroot string `json:"merkleroot"`
Time int `json:"time"`
Height int `json:"height"`
Nonce int `json:"nonce"`
NextConsensus string `json:"next_consensus"`
Script struct {
Invocation string `json:"invocation"`
Verification string `json:"verification"`
} `json:"script"`
Tx []struct {
Type string `json:"type"`
Version int `json:"version"`
Attributes interface{} `json:"attributes"`
Vin interface{} `json:"vin"`
Vout interface{} `json:"vout"`
Scripts interface{} `json:"scripts"`
} `json:"tx"`
Confirmations int `json:"confirmations"`
Nextblockhash string `json:"nextblockhash"`
Hash string `json:"hash"`
} `json:"result"`
ID int `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result wrappers.Block `json:"result"`
ID int `json:"id"`
}
// GetAssetResponse struct for testing.

View file

@ -12,6 +12,7 @@ import (
"testing"
"github.com/CityOfZion/neo-go/pkg/core"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/rpc/wrappers"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
@ -123,8 +124,16 @@ var rpcTestCases = map[string][]rpcTestCase{
block, err := e.chain.GetBlock(e.chain.GetHeaderHash(1))
require.NoErrorf(t, err, "could not get block")
expectedHash := "0x" + block.Hash().StringLE()
assert.Equal(t, expectedHash, res.Result.Hash)
assert.Equal(t, block.Hash(), res.Result.Hash)
for i := range res.Result.Tx {
tx := res.Result.Tx[i]
require.Equal(t, transaction.MinerType, tx.Type)
miner, ok := block.Transactions[i].Data.(*transaction.MinerTX)
require.True(t, ok)
require.Equal(t, miner.Nonce, tx.Nonce)
require.Equal(t, block.Transactions[i].Hash(), tx.TxID)
}
},
},
{

View file

@ -1,34 +1,107 @@
package wrappers
import (
"strconv"
"github.com/CityOfZion/neo-go/pkg/core"
"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"
)
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.Block / block.Base on the RPC Server.
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.
func NewBlock(block *block.Block, chain core.Blockchainer) Block {
blockWrapper := Block{
Block: block,
Hash: block.Hash(),
func NewBlock(b *block.Block, chain core.Blockchainer) Block {
res := Block{
Version: b.Version,
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{}) {
blockWrapper.NextBlockHash = hash
res.NextBlockHash = &hash
}
blockWrapper.Confirmations = chain.BlockHeight() - block.Index - 1
return blockWrapper
for i := range b.Transactions {
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
}