rpc: use non-pointer Block and Transaction in results
Pointers can be nil and in some cases it's important to always have access to Block or Transaction fields.
This commit is contained in:
parent
a7cce3f894
commit
5fe8287fbb
4 changed files with 13 additions and 21 deletions
|
@ -64,7 +64,7 @@ func getResultBlock1() *result.Block {
|
|||
panic(err)
|
||||
}
|
||||
return &result.Block{
|
||||
Block: b,
|
||||
Block: *b,
|
||||
BlockMetadata: result.BlockMetadata{
|
||||
Size: 1681,
|
||||
NextBlockHash: &b2Hash,
|
||||
|
@ -85,7 +85,7 @@ func getTxMoveNeo() *result.TransactionOutputRaw {
|
|||
panic(err)
|
||||
}
|
||||
return &result.TransactionOutputRaw{
|
||||
Transaction: tx,
|
||||
Transaction: *tx,
|
||||
TransactionMetadata: result.TransactionMetadata{
|
||||
Timestamp: b1.Timestamp,
|
||||
Blockhash: b1.Block.Hash(),
|
||||
|
@ -155,7 +155,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
|
|||
serverResponse: `{"id":1,"jsonrpc":"2.0","result":"` + hexB1 + `"}`,
|
||||
result: func(c *Client) interface{} {
|
||||
b := getResultBlock1()
|
||||
return b.Block
|
||||
return &b.Block
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -180,7 +180,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
|
|||
serverResponse: `{"id":1,"jsonrpc":"2.0","result":"` + hexB1 + `"}`,
|
||||
result: func(c *Client) interface{} {
|
||||
b := getResultBlock1()
|
||||
return b.Block
|
||||
return &b.Block
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -449,7 +449,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
|
|||
serverResponse: `{"id":1,"jsonrpc":"2.0","result":"` + hexTxMoveNeo + `"}`,
|
||||
result: func(c *Client) interface{} {
|
||||
tx := getTxMoveNeo()
|
||||
return tx.Transaction
|
||||
return &tx.Transaction
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
@ -14,7 +14,7 @@ type (
|
|||
// Block wrapper used for the representation of
|
||||
// block.Block / block.Base on the RPC Server.
|
||||
Block struct {
|
||||
*block.Block
|
||||
block.Block
|
||||
BlockMetadata
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ type (
|
|||
// NewBlock creates a new Block wrapper.
|
||||
func NewBlock(b *block.Block, chain blockchainer.Blockchainer) Block {
|
||||
res := Block{
|
||||
Block: b,
|
||||
Block: *b,
|
||||
BlockMetadata: BlockMetadata{
|
||||
Size: io.GetVarSize(b),
|
||||
Confirmations: chain.BlockHeight() - b.Index + 1,
|
||||
|
@ -72,16 +72,14 @@ 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)
|
||||
base := new(block.Block)
|
||||
err := json.Unmarshal(data, meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal(data, base)
|
||||
err = json.Unmarshal(data, &b.Block)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.Block = base
|
||||
b.BlockMetadata = *meta
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
// TransactionOutputRaw is used as a wrapper to represents
|
||||
// a Transaction.
|
||||
type TransactionOutputRaw struct {
|
||||
*transaction.Transaction
|
||||
transaction.Transaction
|
||||
TransactionMetadata
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header,
|
|||
// confirmations formula
|
||||
confirmations := int(chain.BlockHeight() - header.Base.Index + 1)
|
||||
return TransactionOutputRaw{
|
||||
Transaction: tx,
|
||||
Transaction: *tx,
|
||||
TransactionMetadata: TransactionMetadata{
|
||||
Blockhash: header.Hash(),
|
||||
Confirmations: confirmations,
|
||||
|
@ -48,7 +48,7 @@ func (t TransactionOutputRaw) MarshalJSON() ([]byte, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
txBytes, err := json.Marshal(t.Transaction)
|
||||
txBytes, err := json.Marshal(&t.Transaction)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -76,11 +76,5 @@ func (t *TransactionOutputRaw) UnmarshalJSON(data []byte) error {
|
|||
t.Confirmations = output.Confirmations
|
||||
t.Timestamp = output.Timestamp
|
||||
|
||||
transaction := new(transaction.Transaction)
|
||||
err = json.Unmarshal(data, transaction)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.Transaction = transaction
|
||||
return nil
|
||||
return json.Unmarshal(data, &t.Transaction)
|
||||
}
|
||||
|
|
|
@ -890,7 +890,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
|
|||
err := json.Unmarshal(txOut, &actual)
|
||||
require.NoErrorf(t, err, "could not parse response: %s", txOut)
|
||||
|
||||
assert.Equal(t, block.Transactions[0], actual.Transaction)
|
||||
assert.Equal(t, *block.Transactions[0], actual.Transaction)
|
||||
assert.Equal(t, 8, actual.Confirmations)
|
||||
assert.Equal(t, TXHash, actual.Transaction.Hash())
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue