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:
Roman Khimov 2020-06-18 11:47:54 +03:00
parent a7cce3f894
commit 5fe8287fbb
4 changed files with 13 additions and 21 deletions

View file

@ -64,7 +64,7 @@ func getResultBlock1() *result.Block {
panic(err) panic(err)
} }
return &result.Block{ return &result.Block{
Block: b, Block: *b,
BlockMetadata: result.BlockMetadata{ BlockMetadata: result.BlockMetadata{
Size: 1681, Size: 1681,
NextBlockHash: &b2Hash, NextBlockHash: &b2Hash,
@ -85,7 +85,7 @@ func getTxMoveNeo() *result.TransactionOutputRaw {
panic(err) panic(err)
} }
return &result.TransactionOutputRaw{ return &result.TransactionOutputRaw{
Transaction: tx, Transaction: *tx,
TransactionMetadata: result.TransactionMetadata{ TransactionMetadata: result.TransactionMetadata{
Timestamp: b1.Timestamp, Timestamp: b1.Timestamp,
Blockhash: b1.Block.Hash(), Blockhash: b1.Block.Hash(),
@ -155,7 +155,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
serverResponse: `{"id":1,"jsonrpc":"2.0","result":"` + hexB1 + `"}`, serverResponse: `{"id":1,"jsonrpc":"2.0","result":"` + hexB1 + `"}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
b := getResultBlock1() 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 + `"}`, serverResponse: `{"id":1,"jsonrpc":"2.0","result":"` + hexB1 + `"}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
b := getResultBlock1() 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 + `"}`, serverResponse: `{"id":1,"jsonrpc":"2.0","result":"` + hexTxMoveNeo + `"}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
tx := getTxMoveNeo() tx := getTxMoveNeo()
return tx.Transaction return &tx.Transaction
}, },
}, },
{ {

View file

@ -14,7 +14,7 @@ type (
// 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 block.Block
BlockMetadata BlockMetadata
} }
@ -30,7 +30,7 @@ type (
// NewBlock creates a new Block wrapper. // NewBlock creates a new Block wrapper.
func NewBlock(b *block.Block, chain blockchainer.Blockchainer) Block { func NewBlock(b *block.Block, chain blockchainer.Blockchainer) Block {
res := Block{ res := Block{
Block: b, Block: *b,
BlockMetadata: BlockMetadata{ BlockMetadata: BlockMetadata{
Size: io.GetVarSize(b), Size: io.GetVarSize(b),
Confirmations: chain.BlockHeight() - b.Index + 1, 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, // As block.Block and BlockMetadata are at the same level in json,
// do unmarshalling separately for both structs. // do unmarshalling separately for both structs.
meta := new(BlockMetadata) meta := new(BlockMetadata)
base := new(block.Block)
err := json.Unmarshal(data, meta) err := json.Unmarshal(data, meta)
if err != nil { if err != nil {
return err return err
} }
err = json.Unmarshal(data, base) err = json.Unmarshal(data, &b.Block)
if err != nil { if err != nil {
return err return err
} }
b.Block = base
b.BlockMetadata = *meta b.BlockMetadata = *meta
return nil return nil
} }

View file

@ -13,7 +13,7 @@ import (
// TransactionOutputRaw is used as a wrapper to represents // TransactionOutputRaw is used as a wrapper to represents
// a Transaction. // a Transaction.
type TransactionOutputRaw struct { type TransactionOutputRaw struct {
*transaction.Transaction transaction.Transaction
TransactionMetadata TransactionMetadata
} }
@ -29,7 +29,7 @@ func NewTransactionOutputRaw(tx *transaction.Transaction, header *block.Header,
// confirmations formula // confirmations formula
confirmations := int(chain.BlockHeight() - header.Base.Index + 1) confirmations := int(chain.BlockHeight() - header.Base.Index + 1)
return TransactionOutputRaw{ return TransactionOutputRaw{
Transaction: tx, Transaction: *tx,
TransactionMetadata: TransactionMetadata{ TransactionMetadata: TransactionMetadata{
Blockhash: header.Hash(), Blockhash: header.Hash(),
Confirmations: confirmations, Confirmations: confirmations,
@ -48,7 +48,7 @@ func (t TransactionOutputRaw) MarshalJSON() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
txBytes, err := json.Marshal(t.Transaction) txBytes, err := json.Marshal(&t.Transaction)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -76,11 +76,5 @@ func (t *TransactionOutputRaw) UnmarshalJSON(data []byte) error {
t.Confirmations = output.Confirmations t.Confirmations = output.Confirmations
t.Timestamp = output.Timestamp t.Timestamp = output.Timestamp
transaction := new(transaction.Transaction) return json.Unmarshal(data, &t.Transaction)
err = json.Unmarshal(data, transaction)
if err != nil {
return err
}
t.Transaction = transaction
return nil
} }

View file

@ -890,7 +890,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
err := json.Unmarshal(txOut, &actual) err := json.Unmarshal(txOut, &actual)
require.NoErrorf(t, err, "could not parse response: %s", txOut) 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, 8, actual.Confirmations)
assert.Equal(t, TXHash, actual.Transaction.Hash()) assert.Equal(t, TXHash, actual.Transaction.Hash())
}) })