Skip to content

Commit

Permalink
triedb/pathdb: enlarge maxDiffLayers to 28800
Browse files Browse the repository at this point in the history
* triedb/pathdb/database.go
  * Export maxDiffLayersto make this setting available for use in other packages
* triedb/pathdb/database_test.go
  * Adjust test cases to reflect the increased `maxDiffLayers` value
  * Update test logic to simulate and verify behavior with the increased `MaxDiffLayers` value.
* core/blockchain_test.go
  * Introduce `TestMaxDiffLayers` and `TestAdditionalLayers` constants to align with the updated `MaxDiffLayers`.
  * Adjust chain generation and pruning logic to reflect the enlarged `MaxDiffLayers`.
  • Loading branch information
yysung1123 authored and vicky-sunshine committed Feb 15, 2025
1 parent ea7ce54 commit d4b561f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
27 changes: 16 additions & 11 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ import (
"github.com/ethereum/go-ethereum/ethdb/pebble"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/triedb/pathdb"
"github.com/holiman/uint256"
)

// So we can deterministically seed different blockchains
var (
canonicalSeed = 1
forkSeed = 2

TestTriesInMemory = 128
TestMaxDiffLayers = pathdb.MaxDiffLayers
TestAdditionalLayers = 128
)

// newCanonical creates a chain database, and injects a deterministic canonical
Expand Down Expand Up @@ -1693,8 +1698,8 @@ func testLargeReorgTrieGC(t *testing.T, scheme string) {
BaseFee: big.NewInt(params.InitialBaseFee),
}
genDb, shared, _ := GenerateChainWithGenesis(genesis, engine, 64, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) })
original, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, 2*state.TriesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) })
competitor, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, 2*state.TriesInMemory+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) })
original, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, TestMaxDiffLayers+TestAdditionalLayers, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) })
competitor, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, TestMaxDiffLayers+TestAdditionalLayers+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) })

// Import the shared chain and the original canonical one
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false, false, false, false, false)
Expand Down Expand Up @@ -1731,11 +1736,11 @@ func testLargeReorgTrieGC(t *testing.T, scheme string) {
if _, err := chain.InsertChain(competitor[len(competitor)-2:]); err != nil {
t.Fatalf("failed to finalize competitor chain: %v", err)
}
// In path-based trie database implementation, it will keep 128 diff + 1 disk
// layers, totally 129 latest states available. In hash-based it's 128.
states := state.TriesInMemory
// In path-based trie database implementation, it will keep maxDiffLayers diff + 1 disk
// layers, totally maxDiffLayers+1 latest states available. In hash-based it's 128.
states := TestTriesInMemory
if scheme == rawdb.PathScheme {
states = states + 1
states = TestMaxDiffLayers + 1
}
for i, block := range competitor[:len(competitor)-states] {
if chain.HasState(block.Root()) {
Expand Down Expand Up @@ -2664,7 +2669,7 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) {
BaseFee: big.NewInt(params.InitialBaseFee),
}
// Generate and import the canonical chain
_, blocks, _ := GenerateChainWithGenesis(genesis, engine, 2*state.TriesInMemory, nil)
_, blocks, _ := GenerateChainWithGenesis(genesis, engine, TestMaxDiffLayers+TestAdditionalLayers, nil)

// Construct a database with freezer enabled
datadir := t.TempDir()
Expand All @@ -2689,11 +2694,11 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) {
if n, err := chain.InsertChain(blocks); err != nil {
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
}
// In path-based trie database implementation, it will keep 128 diff + 1 disk
// layers, totally 129 latest states available. In hash-based it's 128.
states := state.TriesInMemory
// In path-based trie database implementation, it will keep maxDiffLayers diff + 1 disk
// layers, totally maxDiffLayers+1 latest states available. In hash-based it's 128.
states := TestTriesInMemory
if scheme == rawdb.PathScheme {
states = state.TriesInMemory + 1
states = TestMaxDiffLayers + 1
}
lastPrunedIndex := len(blocks) - states - 1
lastPrunedBlock := blocks[lastPrunedIndex]
Expand Down
6 changes: 3 additions & 3 deletions triedb/pathdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ const (
)

var (
// maxDiffLayers is the maximum diff layers allowed in the layer tree.
maxDiffLayers = 128
// MaxDiffLayers is the maximum diff layers allowed in the layer tree.
MaxDiffLayers = 28800
)

// layer is the interface implemented by all state layers which includes some
Expand Down Expand Up @@ -344,7 +344,7 @@ func (db *Database) Update(root common.Hash, parentRoot common.Hash, block uint6
// - head-1 layer is paired with HEAD-1 state
// - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state
// - head-128 layer(disk layer) is paired with HEAD-128 state
return db.tree.cap(root, maxDiffLayers)
return db.tree.cap(root, MaxDiffLayers)
}

// Commit traverses downwards the layer tree from a specified layer with the
Expand Down
35 changes: 21 additions & 14 deletions triedb/pathdb/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,10 @@ func (t *tester) bottomIndex() int {

func TestDatabaseRollback(t *testing.T) {
// Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4
backupMaxDiffLayers := MaxDiffLayers
MaxDiffLayers = 4
defer func() {
maxDiffLayers = 128
MaxDiffLayers = backupMaxDiffLayers
}()

// Verify state histories
Expand Down Expand Up @@ -481,9 +482,10 @@ func TestDatabaseRollback(t *testing.T) {

func TestDatabaseRecoverable(t *testing.T) {
// Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4
backupMaxDiffLayers := MaxDiffLayers
MaxDiffLayers = 4
defer func() {
maxDiffLayers = 128
MaxDiffLayers = backupMaxDiffLayers
}()

var (
Expand Down Expand Up @@ -526,9 +528,10 @@ func TestDatabaseRecoverable(t *testing.T) {

func TestDisable(t *testing.T) {
// Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4
backupMaxDiffLayers := MaxDiffLayers
MaxDiffLayers = 4
defer func() {
maxDiffLayers = 128
MaxDiffLayers = backupMaxDiffLayers
}()

tester := newTester(t, 0, false, 32)
Expand Down Expand Up @@ -572,9 +575,10 @@ func TestDisable(t *testing.T) {

func TestCommit(t *testing.T) {
// Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4
backupMaxDiffLayers := MaxDiffLayers
MaxDiffLayers = 4
defer func() {
maxDiffLayers = 128
MaxDiffLayers = backupMaxDiffLayers
}()

tester := newTester(t, 0, false, 12)
Expand Down Expand Up @@ -602,9 +606,10 @@ func TestCommit(t *testing.T) {

func TestJournal(t *testing.T) {
// Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4
backupMaxDiffLayers := MaxDiffLayers
MaxDiffLayers = 4
defer func() {
maxDiffLayers = 128
MaxDiffLayers = backupMaxDiffLayers
}()

tester := newTester(t, 0, false, 12)
Expand Down Expand Up @@ -632,9 +637,10 @@ func TestJournal(t *testing.T) {

func TestCorruptedJournal(t *testing.T) {
// Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4
backupMaxDiffLayers := MaxDiffLayers
MaxDiffLayers = 4
defer func() {
maxDiffLayers = 128
MaxDiffLayers = backupMaxDiffLayers
}()

tester := newTester(t, 0, false, 12)
Expand Down Expand Up @@ -680,9 +686,10 @@ func TestCorruptedJournal(t *testing.T) {
// always falls within the range of [oldest-history-id, latest-history-id].
func TestTailTruncateHistory(t *testing.T) {
// Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4
backupMaxDiffLayers := MaxDiffLayers
MaxDiffLayers = 4
defer func() {
maxDiffLayers = 128
MaxDiffLayers = backupMaxDiffLayers
}()

tester := newTester(t, 10, false, 12)
Expand Down

0 comments on commit d4b561f

Please sign in to comment.