Browse Source

eth/downloader: fix negative balance issue in tests

The test chain generated by makeChainFork included invalid uncle
headers, crashing the generator during the state commit.

The headers were invalid because they used the iteration counter as the
block number, even though makeChainFork uses a block with number > 0 as
the parent. Fix this by introducing BlockGen.Number, which allows
accessing the actual number of the block being generated.
Felix Lange 10 years ago
parent
commit
9be5d5cd90
2 changed files with 10 additions and 2 deletions
  1. 5 0
      core/chain_makers.go
  2. 5 2
      eth/downloader/downloader_test.go

+ 5 - 0
core/chain_makers.go

@@ -104,6 +104,11 @@ func (b *BlockGen) AddTx(tx *types.Transaction) {
 	b.receipts = append(b.receipts, receipt)
 	b.receipts = append(b.receipts, receipt)
 }
 }
 
 
+// Number returns the block number of the block being generated.
+func (b *BlockGen) Number() *big.Int {
+	return new(big.Int).Set(b.header.Number)
+}
+
 // AddUncheckedReceipts forcefully adds a receipts to the block without a
 // AddUncheckedReceipts forcefully adds a receipts to the block without a
 // backing transaction.
 // backing transaction.
 //
 //

+ 5 - 2
eth/downloader/downloader_test.go

@@ -61,8 +61,11 @@ func makeChain(n int, seed byte, parent *types.Block, parentReceipts types.Recei
 			block.AddTx(tx)
 			block.AddTx(tx)
 		}
 		}
 		// If the block number is a multiple of 5, add a bonus uncle to the block
 		// If the block number is a multiple of 5, add a bonus uncle to the block
-		if i%5 == 0 {
-			block.AddUncle(&types.Header{ParentHash: block.PrevBlock(i - 1).Hash(), Number: big.NewInt(int64(i - 1))})
+		if i > 0 && i%5 == 0 {
+			block.AddUncle(&types.Header{
+				ParentHash: block.PrevBlock(i - 1).Hash(),
+				Number:     big.NewInt(block.Number().Int64() - 1),
+			})
 		}
 		}
 	})
 	})
 	// Convert the block-chain into a hash-chain and header/block maps
 	// Convert the block-chain into a hash-chain and header/block maps