From 90e3ffd393ad394ecfe0acb83af7a7cfb0158863 Mon Sep 17 00:00:00 2001 From: meows Date: Fri, 22 Oct 2021 08:25:32 -0700 Subject: [PATCH] core: write test showing that TD is not stored properly at genesis The ToBlock method applies a default value for an empty difficulty value. This default is not carried over through the Commit method because the TotalDifficulty database write writes the original difficulty value (nil) instead of the defaulty value present on the genesis Block. Date: 2021-10-22 08:25:32-07:00 Signed-off-by: meows --- core/genesis_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/core/genesis_test.go b/core/genesis_test.go index 055be2796c..056c1c195f 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -209,3 +209,33 @@ func TestGenesisHashes(t *testing.T) { } } } + +func TestGenesis_Commit(t *testing.T) { + genesis := &Genesis{ + BaseFee: big.NewInt(params.InitialBaseFee), + Config: params.TestChainConfig, + // difficulty is nil + } + + db := rawdb.NewMemoryDatabase() + genesisBlock, err := genesis.Commit(db) + if err != nil { + t.Fatal(err) + } + + if genesis.Difficulty != nil { + t.Fatalf("assumption wrong") + } + + // This value should have been set as default in the ToBlock method. + if genesisBlock.Difficulty().Cmp(params.GenesisDifficulty) != 0 { + t.Errorf("assumption wrong: want: %d, got: %v", params.GenesisDifficulty, genesisBlock.Difficulty()) + } + + // Expect the stored total difficulty to be the difficulty of the genesis block. + stored := rawdb.ReadTd(db, genesisBlock.Hash(), genesisBlock.NumberU64()) + + if stored.Cmp(genesisBlock.Difficulty()) != 0 { + t.Errorf("inequal difficulty; stored: %v, genesisBlock: %v", stored, genesisBlock.Difficulty()) + } +}