Browse Source

core, core/types, eth: add and use Block.Body

This fixes a few uses of unkeyed Body literals which go vet was
complaining about.
Felix Lange 9 years ago
parent
commit
24cdac41f3
5 changed files with 10 additions and 7 deletions
  1. 1 1
      core/blockchain.go
  2. 1 1
      core/database_util.go
  3. 3 3
      core/database_util_test.go
  4. 3 0
      core/types/block.go
  5. 2 2
      eth/backend.go

+ 1 - 1
core/blockchain.go

@@ -678,7 +678,7 @@ func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain
 				}
 				}
 			}
 			}
 			// Write all the data out into the database
 			// 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)
 				errs[index] = fmt.Errorf("failed to write block body: %v", err)
 				atomic.AddInt32(&failed, 1)
 				atomic.AddInt32(&failed, 1)
 				glog.Fatal(errs[index])
 				glog.Fatal(errs[index])

+ 1 - 1
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.
 // WriteBlock serializes a block into the database, header and body separately.
 func WriteBlock(db ethdb.Database, block *types.Block) error {
 func WriteBlock(db ethdb.Database, block *types.Block) error {
 	// Store the body first to retain database consistency
 	// 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
 		return err
 	}
 	}
 	// Store the header too, signaling full block ownership
 	// Store the header too, signaling full block ownership

+ 3 - 3
core/database_util_test.go

@@ -196,7 +196,7 @@ func TestBlockStorage(t *testing.T) {
 	if entry := GetBody(db, block.Hash()); entry == nil {
 	if entry := GetBody(db, block.Hash()); entry == nil {
 		t.Fatalf("Stored body not found")
 		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()) {
 	} 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
 	// Delete the block and verify the execution
 	DeleteBlock(db, block.Hash())
 	DeleteBlock(db, block.Hash())
@@ -230,7 +230,7 @@ func TestPartialBlockStorage(t *testing.T) {
 	DeleteHeader(db, block.Hash())
 	DeleteHeader(db, block.Hash())
 
 
 	// Store a body and check that it's not recognized as a block
 	// 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)
 		t.Fatalf("Failed to write body into database: %v", err)
 	}
 	}
 	if entry := GetBlock(db, block.Hash()); entry != nil {
 	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 {
 	if err := WriteHeader(db, block.Header()); err != nil {
 		t.Fatalf("Failed to write header into database: %v", err)
 		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)
 		t.Fatalf("Failed to write body into database: %v", err)
 	}
 	}
 	if entry := GetBlock(db, block.Hash()); entry == nil {
 	if entry := GetBlock(db, block.Hash()); entry == nil {

+ 3 - 0
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) }
 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 {
 func (b *Block) HashNoNonce() common.Hash {
 	return b.header.HashNoNonce()
 	return b.header.HashNoNonce()
 }
 }

+ 2 - 2
eth/backend.go

@@ -557,7 +557,7 @@ func upgradeChainDatabase(db ethdb.Database) error {
 			if err := core.WriteTd(db, block.Hash(), block.DeprecatedTd()); err != nil {
 			if err := core.WriteTd(db, block.Hash(), block.DeprecatedTd()); err != nil {
 				return err
 				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
 				return err
 			}
 			}
 			if err := core.WriteHeader(db, block.Header()); err != nil {
 			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 {
 		if err := core.WriteTd(db, current.Hash(), current.DeprecatedTd()); err != nil {
 			return err
 			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
 			return err
 		}
 		}
 		if err := core.WriteHeader(db, current.Header()); err != nil {
 		if err := core.WriteHeader(db, current.Header()); err != nil {