Skip to content

Commit c72b16c

Browse files
meowsbitsholiman
andauthored
core: use block difficulty for genesis (ethereum#23793)
* 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 <[email protected]> * 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 90e3ffd Date: 2021-10-22 08:28:00-07:00 Signed-off-by: meows <[email protected]> * 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 <[email protected]> * les: fix test with genesis change Co-authored-by: Martin Holst Swende <[email protected]>
1 parent 48dc34b commit c72b16c

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

core/blockchain_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func TestReorgLongHeaders(t *testing.T) { testReorgLong(t, false) }
360360
func TestReorgLongBlocks(t *testing.T) { testReorgLong(t, true) }
361361

362362
func testReorgLong(t *testing.T, full bool) {
363-
testReorg(t, []int64{0, 0, -9}, []int64{0, 0, 0, -9}, 393280, full)
363+
testReorg(t, []int64{0, 0, -9}, []int64{0, 0, 0, -9}, 393280+params.GenesisDifficulty.Int64(), full)
364364
}
365365

366366
// Tests that reorganising a short difficult chain after a long easy one
@@ -380,7 +380,7 @@ func testReorgShort(t *testing.T, full bool) {
380380
for i := 0; i < len(diff); i++ {
381381
diff[i] = -9
382382
}
383-
testReorg(t, easy, diff, 12615120, full)
383+
testReorg(t, easy, diff, 12615120+params.GenesisDifficulty.Int64(), full)
384384
}
385385

386386
func testReorg(t *testing.T, first, second []int64, td int64, full bool) {

core/genesis.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) {
322322
if config.Clique != nil && len(block.Extra()) == 0 {
323323
return nil, errors.New("can't start clique chain without signers")
324324
}
325-
rawdb.WriteTd(db, block.Hash(), block.NumberU64(), g.Difficulty)
325+
rawdb.WriteTd(db, block.Hash(), block.NumberU64(), block.Difficulty())
326326
rawdb.WriteBlock(db, block)
327327
rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), nil)
328328
rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())

core/genesis_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,33 @@ func TestGenesisHashes(t *testing.T) {
209209
}
210210
}
211211
}
212+
213+
func TestGenesis_Commit(t *testing.T) {
214+
genesis := &Genesis{
215+
BaseFee: big.NewInt(params.InitialBaseFee),
216+
Config: params.TestChainConfig,
217+
// difficulty is nil
218+
}
219+
220+
db := rawdb.NewMemoryDatabase()
221+
genesisBlock, err := genesis.Commit(db)
222+
if err != nil {
223+
t.Fatal(err)
224+
}
225+
226+
if genesis.Difficulty != nil {
227+
t.Fatalf("assumption wrong")
228+
}
229+
230+
// This value should have been set as default in the ToBlock method.
231+
if genesisBlock.Difficulty().Cmp(params.GenesisDifficulty) != 0 {
232+
t.Errorf("assumption wrong: want: %d, got: %v", params.GenesisDifficulty, genesisBlock.Difficulty())
233+
}
234+
235+
// Expect the stored total difficulty to be the difficulty of the genesis block.
236+
stored := rawdb.ReadTd(db, genesisBlock.Hash(), genesisBlock.NumberU64())
237+
238+
if stored.Cmp(genesisBlock.Difficulty()) != 0 {
239+
t.Errorf("inequal difficulty; stored: %v, genesisBlock: %v", stored, genesisBlock.Difficulty())
240+
}
241+
}

les/fetcher_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/core/rawdb"
2727
"github.com/ethereum/go-ethereum/core/types"
2828
"github.com/ethereum/go-ethereum/p2p/enode"
29+
"github.com/ethereum/go-ethereum/params"
2930
)
3031

3132
// verifyImportEvent verifies that one single event arrive on an import channel.
@@ -247,7 +248,7 @@ func testInvalidAnnounces(t *testing.T, protocol int) {
247248
// Prepare announcement by latest header.
248249
headerOne := s.backend.Blockchain().GetHeaderByNumber(1)
249250
hash, number := headerOne.Hash(), headerOne.Number.Uint64()
250-
td := big.NewInt(200) // bad td
251+
td := big.NewInt(params.GenesisDifficulty.Int64() + 200) // bad td
251252

252253
// Sign the announcement if necessary.
253254
announce := announceData{hash, number, td, 0, nil}

0 commit comments

Comments
 (0)