From 90e3ffd393ad394ecfe0acb83af7a7cfb0158863 Mon Sep 17 00:00:00 2001 From: meows Date: Fri, 22 Oct 2021 08:25:32 -0700 Subject: [PATCH 1/4] 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 055be2796c39..056c1c195f6b 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()) + } +} From 7d917a68091d0e32a9bb80f7b2d27447ea7e055b Mon Sep 17 00:00:00 2001 From: meows Date: Fri, 22 Oct 2021 08:28:00 -0700 Subject: [PATCH 2/4] core: write TD value from Block, not original genesis value This an issue where a default TD value was not written to the database, resulting in a 0 value TD at genesis. A test for this issue was provided at 90e3ffd393 Date: 2021-10-22 08:28:00-07:00 Signed-off-by: meows --- core/genesis.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/genesis.go b/core/genesis.go index 38ace4920bda..af36247471e9 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -322,7 +322,7 @@ func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) { if config.Clique != nil && len(block.Extra()) == 0 { return nil, errors.New("can't start clique chain without signers") } - rawdb.WriteTd(db, block.Hash(), block.NumberU64(), g.Difficulty) + rawdb.WriteTd(db, block.Hash(), block.NumberU64(), block.Difficulty()) rawdb.WriteBlock(db, block) rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), nil) rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64()) From 45013ca331e0e62799fa36268749ea88fa4d6355 Mon Sep 17 00:00:00 2001 From: meows Date: Fri, 22 Oct 2021 09:16:01 -0700 Subject: [PATCH 3/4] core: fix tests by adding GenesisDifficulty to expected result See prior two commits. Date: 2021-10-22 09:16:01-07:00 Signed-off-by: meows --- core/blockchain_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 62036ae75765..3f649403899a 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -360,7 +360,7 @@ func TestReorgLongHeaders(t *testing.T) { testReorgLong(t, false) } func TestReorgLongBlocks(t *testing.T) { testReorgLong(t, true) } func testReorgLong(t *testing.T, full bool) { - testReorg(t, []int64{0, 0, -9}, []int64{0, 0, 0, -9}, 393280, full) + testReorg(t, []int64{0, 0, -9}, []int64{0, 0, 0, -9}, 393280+params.GenesisDifficulty.Int64(), full) } // Tests that reorganising a short difficult chain after a long easy one @@ -380,7 +380,7 @@ func testReorgShort(t *testing.T, full bool) { for i := 0; i < len(diff); i++ { diff[i] = -9 } - testReorg(t, easy, diff, 12615120, full) + testReorg(t, easy, diff, 12615120+params.GenesisDifficulty.Int64(), full) } func testReorg(t *testing.T, first, second []int64, td int64, full bool) { From 916da01694bb17ecf4a8df1a933080897ecb3421 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Mon, 25 Oct 2021 22:13:35 +0200 Subject: [PATCH 4/4] les: fix test with genesis change --- les/fetcher_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/les/fetcher_test.go b/les/fetcher_test.go index ef700651e3bf..a922ab0f839c 100644 --- a/les/fetcher_test.go +++ b/les/fetcher_test.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/p2p/enode" + "github.com/ethereum/go-ethereum/params" ) // verifyImportEvent verifies that one single event arrive on an import channel. @@ -247,7 +248,7 @@ func testInvalidAnnounces(t *testing.T, protocol int) { // Prepare announcement by latest header. headerOne := s.backend.Blockchain().GetHeaderByNumber(1) hash, number := headerOne.Hash(), headerOne.Number.Uint64() - td := big.NewInt(200) // bad td + td := big.NewInt(params.GenesisDifficulty.Int64() + 200) // bad td // Sign the announcement if necessary. announce := announceData{hash, number, td, 0, nil}