From 24cdac41f34b37145c89572712db0854dd411110 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 15 Apr 2016 10:57:37 +0200 Subject: [PATCH] core, core/types, eth: add and use Block.Body This fixes a few uses of unkeyed Body literals which go vet was complaining about. --- core/blockchain.go | 2 +- core/database_util.go | 2 +- core/database_util_test.go | 6 +++--- core/types/block.go | 3 +++ eth/backend.go | 4 ++-- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 177a3bbce..e740eb861 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -678,7 +678,7 @@ func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain } } // Write all the data out into the database - if err := WriteBody(self.chainDb, block.Hash(), &types.Body{block.Transactions(), block.Uncles()}); err != nil { + if err := WriteBody(self.chainDb, block.Hash(), block.Body()); err != nil { errs[index] = fmt.Errorf("failed to write block body: %v", err) atomic.AddInt32(&failed, 1) glog.Fatal(errs[index]) diff --git a/core/database_util.go b/core/database_util.go index e1e8136d1..3ba80062c 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -318,7 +318,7 @@ func WriteTd(db ethdb.Database, hash common.Hash, td *big.Int) error { // WriteBlock serializes a block into the database, header and body separately. func WriteBlock(db ethdb.Database, block *types.Block) error { // Store the body first to retain database consistency - if err := WriteBody(db, block.Hash(), &types.Body{block.Transactions(), block.Uncles()}); err != nil { + if err := WriteBody(db, block.Hash(), block.Body()); err != nil { return err } // Store the header too, signaling full block ownership diff --git a/core/database_util_test.go b/core/database_util_test.go index ce1ffea8a..9ef787624 100644 --- a/core/database_util_test.go +++ b/core/database_util_test.go @@ -196,7 +196,7 @@ func TestBlockStorage(t *testing.T) { if entry := GetBody(db, block.Hash()); entry == nil { t.Fatalf("Stored body not found") } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) { - t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, &types.Body{block.Transactions(), block.Uncles()}) + t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body()) } // Delete the block and verify the execution DeleteBlock(db, block.Hash()) @@ -230,7 +230,7 @@ func TestPartialBlockStorage(t *testing.T) { DeleteHeader(db, block.Hash()) // Store a body and check that it's not recognized as a block - if err := WriteBody(db, block.Hash(), &types.Body{block.Transactions(), block.Uncles()}); err != nil { + if err := WriteBody(db, block.Hash(), block.Body()); err != nil { t.Fatalf("Failed to write body into database: %v", err) } if entry := GetBlock(db, block.Hash()); entry != nil { @@ -242,7 +242,7 @@ func TestPartialBlockStorage(t *testing.T) { if err := WriteHeader(db, block.Header()); err != nil { t.Fatalf("Failed to write header into database: %v", err) } - if err := WriteBody(db, block.Hash(), &types.Body{block.Transactions(), block.Uncles()}); err != nil { + if err := WriteBody(db, block.Hash(), block.Body()); err != nil { t.Fatalf("Failed to write body into database: %v", err) } if entry := GetBlock(db, block.Hash()); entry == nil { diff --git a/core/types/block.go b/core/types/block.go index 5e6a9019d..387a063ae 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -330,6 +330,9 @@ func (b *Block) Extra() []byte { return common.CopyBytes(b.header.Ext func (b *Block) Header() *Header { return CopyHeader(b.header) } +// Body returns the non-header content of the block. +func (b *Block) Body() *Body { return &Body{b.transactions, b.uncles} } + func (b *Block) HashNoNonce() common.Hash { return b.header.HashNoNonce() } diff --git a/eth/backend.go b/eth/backend.go index ea12e4215..76cf8783b 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -557,7 +557,7 @@ func upgradeChainDatabase(db ethdb.Database) error { if err := core.WriteTd(db, block.Hash(), block.DeprecatedTd()); err != nil { return err } - if err := core.WriteBody(db, block.Hash(), &types.Body{block.Transactions(), block.Uncles()}); err != nil { + if err := core.WriteBody(db, block.Hash(), block.Body()); err != nil { return err } if err := core.WriteHeader(db, block.Header()); err != nil { @@ -573,7 +573,7 @@ func upgradeChainDatabase(db ethdb.Database) error { if err := core.WriteTd(db, current.Hash(), current.DeprecatedTd()); err != nil { return err } - if err := core.WriteBody(db, current.Hash(), &types.Body{current.Transactions(), current.Uncles()}); err != nil { + if err := core.WriteBody(db, current.Hash(), current.Body()); err != nil { return err } if err := core.WriteHeader(db, current.Header()); err != nil {