From 258f397b9a0750dc9342e4e7e98b913859358d7c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 10 Oct 2019 20:02:09 +0300 Subject: [PATCH] core: append transactions to the block in GetBlock() We want to get a full block, so it has to have transactions inside. Unfortunately our tests were used to this wrong behavior and utilized completely bogus transactions without data that couldn't be persisted, so fix that also. --- pkg/core/block_test.go | 4 ++-- pkg/core/blockchain.go | 7 +++++++ pkg/core/blockchain_test.go | 9 ++++----- pkg/core/helper_test.go | 14 +++++++++++--- pkg/rpc/server_helper_test.go | 7 ++++--- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/pkg/core/block_test.go b/pkg/core/block_test.go index 164a0ab88..b8e390c71 100644 --- a/pkg/core/block_test.go +++ b/pkg/core/block_test.go @@ -84,8 +84,8 @@ func TestHashBlockEqualsHashHeader(t *testing.T) { func TestBlockVerify(t *testing.T) { block := newBlock( 0, - newTX(transaction.MinerType), - newTX(transaction.IssueType), + newMinerTX(), + newIssueTX(), ) assert.True(t, block.Verify(false)) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 57da76ff2..7013e3719 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -523,6 +523,13 @@ func (bc *Blockchain) GetBlock(hash util.Uint256) (*Block, error) { if len(block.Transactions) == 0 { return nil, fmt.Errorf("only header is available") } + for _, tx := range block.Transactions { + stx, _, err := bc.GetTransaction(tx.Hash()) + if err != nil { + return nil, err + } + *tx = *stx + } return block, nil } diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index 8dab93802..9938ac34a 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -6,7 +6,6 @@ import ( "github.com/CityOfZion/neo-go/config" "github.com/CityOfZion/neo-go/pkg/core/storage" - "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/io" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -39,9 +38,9 @@ func TestAddHeaders(t *testing.T) { func TestAddBlock(t *testing.T) { bc := newTestChain(t) blocks := []*Block{ - newBlock(1, newTX(transaction.MinerType)), - newBlock(2, newTX(transaction.MinerType)), - newBlock(3, newTX(transaction.MinerType)), + newBlock(1, newMinerTX()), + newBlock(2, newMinerTX()), + newBlock(3, newMinerTX()), } for i := 0; i < len(blocks); i++ { @@ -70,7 +69,7 @@ func TestAddBlock(t *testing.T) { func TestGetHeader(t *testing.T) { bc := newTestChain(t) - block := newBlock(1, newTX(transaction.MinerType)) + block := newBlock(1, newMinerTX()) err := bc.AddBlock(block) assert.Nil(t, err) diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 65c44a8c0..205ae8bbc 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -40,14 +40,22 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *Block { func makeBlocks(n int) []*Block { blocks := make([]*Block, n) for i := 0; i < n; i++ { - blocks[i] = newBlock(uint32(i+1), newTX(transaction.MinerType)) + blocks[i] = newBlock(uint32(i+1), newMinerTX()) } return blocks } -func newTX(t transaction.TXType) *transaction.Transaction { +func newMinerTX() *transaction.Transaction { return &transaction.Transaction{ - Type: t, + Type: transaction.MinerType, + Data: &transaction.MinerTX{}, + } +} + +func newIssueTX() *transaction.Transaction { + return &transaction.Transaction{ + Type: transaction.IssueType, + Data: &transaction.IssueTX{}, } } diff --git a/pkg/rpc/server_helper_test.go b/pkg/rpc/server_helper_test.go index fd818750f..bbb7f8b34 100644 --- a/pkg/rpc/server_helper_test.go +++ b/pkg/rpc/server_helper_test.go @@ -172,14 +172,15 @@ func initBlocks(t *testing.T, chain *core.Blockchain) { func makeBlocks(n int) []*core.Block { blocks := make([]*core.Block, n) for i := 0; i < n; i++ { - blocks[i] = newBlock(uint32(i+1), newTX(transaction.MinerType)) + blocks[i] = newBlock(uint32(i+1), newMinerTX()) } return blocks } -func newTX(t transaction.TXType) *transaction.Transaction { +func newMinerTX() *transaction.Transaction { return &transaction.Transaction{ - Type: t, + Type: transaction.MinerType, + Data: &transaction.MinerTX{}, } }