Skip to content

Commit 8df11dc

Browse files
committed
triedb/pathdb: enlarge maxDiffLayers to 86400
* 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`.
1 parent 9e46b4d commit 8df11dc

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ jobs:
2424
os: linux
2525
arch: amd64
2626
dist: bionic
27+
vm:
28+
size: large
2729
go: 1.21.x
2830
env:
2931
- GO111MODULE=on
3032
script:
31-
- travis_wait 120 go run build/ci.go test $TEST_PACKAGES
33+
- travis_wait 120 go run build/ci.go test -timeout 60m $TEST_PACKAGES
3234

3335
- stage: Docker Image Release
3436
script:

core/blockchain_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"github.com/ethereum/go-ethereum/ethdb"
4646
"github.com/ethereum/go-ethereum/params"
4747
"github.com/ethereum/go-ethereum/trie"
48+
"github.com/ethereum/go-ethereum/triedb/pathdb"
4849
"github.com/holiman/uint256"
4950
)
5051

@@ -54,7 +55,9 @@ var (
5455
forkSeed1 = 2
5556
forkSeed2 = 3
5657

57-
TestTriesInMemory = 128
58+
TestTriesInMemory = 128
59+
TestMaxDiffLayers = pathdb.MaxDiffLayers
60+
TestAdditionalLayers = 128
5861
)
5962

6063
// newCanonical creates a chain database, and injects a deterministic canonical
@@ -1953,8 +1956,8 @@ func testLargeReorgTrieGC(t *testing.T, scheme string) {
19531956
BaseFee: big.NewInt(params.InitialBaseFee),
19541957
}
19551958
genDb, shared, _ := GenerateChainWithGenesis(genesis, engine, 64, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) })
1956-
original, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, 2*TriesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) })
1957-
competitor, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, 2*TriesInMemory+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) })
1959+
original, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, TestMaxDiffLayers+TestAdditionalLayers, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) })
1960+
competitor, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, TestMaxDiffLayers+TestAdditionalLayers+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) })
19581961

19591962
// Import the shared chain and the original canonical one
19601963
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false, false, false, false, false)
@@ -1991,11 +1994,11 @@ func testLargeReorgTrieGC(t *testing.T, scheme string) {
19911994
if _, err := chain.InsertChain(competitor[len(competitor)-2:]); err != nil {
19921995
t.Fatalf("failed to finalize competitor chain: %v", err)
19931996
}
1994-
// In path-based trie database implementation, it will keep 128 diff + 1 disk
1995-
// layers, totally 129 latest states available. In hash-based it's 128.
1997+
// In path-based trie database implementation, it will keep maxDiffLayers diff + 1 disk
1998+
// layers, totally maxDiffLayers+1 latest states available. In hash-based it's 128.
19961999
states := TestTriesInMemory
19972000
if scheme == rawdb.PathScheme {
1998-
states = states + 1
2001+
states = TestMaxDiffLayers + 1
19992002
}
20002003
for i, block := range competitor[:len(competitor)-states] {
20012004
if chain.HasState(block.Root()) {
@@ -2936,7 +2939,7 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) {
29362939
BaseFee: big.NewInt(params.InitialBaseFee),
29372940
}
29382941
// Generate and import the canonical chain
2939-
_, blocks, _ := GenerateChainWithGenesis(genesis, engine, 2*TriesInMemory, nil)
2942+
_, blocks, _ := GenerateChainWithGenesis(genesis, engine, TestMaxDiffLayers+TestAdditionalLayers, nil)
29402943

29412944
chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil)
29422945
if err != nil {
@@ -2947,11 +2950,11 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) {
29472950
if n, err := chain.InsertChain(blocks); err != nil {
29482951
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
29492952
}
2950-
// In path-based trie database implementation, it will keep 128 diff + 1 disk
2951-
// layers, totally 129 latest states available. In hash-based it's 128.
2953+
// In path-based trie database implementation, it will keep maxDiffLayers diff + 1 disk
2954+
// layers, totally maxDiffLayers+1 latest states available. In hash-based it's 128.
29522955
states := TestTriesInMemory
29532956
if scheme == rawdb.PathScheme {
2954-
states = TestTriesInMemory + 1
2957+
states = TestMaxDiffLayers + 1
29552958
}
29562959
lastPrunedIndex := len(blocks) - states - 1
29572960
lastPrunedBlock := blocks[lastPrunedIndex]

triedb/pathdb/database.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import (
3737
)
3838

3939
const (
40-
// maxDiffLayers is the maximum diff layers allowed in the layer tree.
41-
maxDiffLayers = 128
40+
// MaxDiffLayers is the maximum diff layers allowed in the layer tree.
41+
MaxDiffLayers = 86400
4242

4343
// defaultCleanSize is the default memory allowance of clean cache.
4444
defaultCleanSize = 16 * 1024 * 1024
@@ -262,7 +262,7 @@ func (db *Database) Update(root common.Hash, parentRoot common.Hash, block uint6
262262
// - head-1 layer is paired with HEAD-1 state
263263
// - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state
264264
// - head-128 layer(disk layer) is paired with HEAD-128 state
265-
return db.tree.cap(root, maxDiffLayers)
265+
return db.tree.cap(root, MaxDiffLayers)
266266
}
267267

268268
// Commit traverses downwards the layer tree from a specified layer with the

triedb/pathdb/database_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import (
3434
"github.com/holiman/uint256"
3535
)
3636

37+
const (
38+
layersForTest = 128
39+
)
40+
3741
func updateTrie(addrHash common.Hash, root common.Hash, dirties, cleans map[common.Hash][]byte) (common.Hash, *trienode.NodeSet) {
3842
h, err := newTestHasher(addrHash, root, cleans)
3943
if err != nil {
@@ -113,7 +117,22 @@ func newTester(t *testing.T, historyLimit uint64) *tester {
113117
snapStorages: make(map[common.Hash]map[common.Hash]map[common.Hash][]byte),
114118
}
115119
)
116-
for i := 0; i < 2*128; i++ {
120+
121+
for i := 0; i < layersForTest; i++ {
122+
var parent = types.EmptyRootHash
123+
if len(obj.roots) != 0 {
124+
parent = obj.roots[len(obj.roots)-1]
125+
}
126+
root, nodes, states := obj.generate(parent)
127+
if err := db.Update(root, parent, uint64(i), nodes, states); err != nil {
128+
panic(fmt.Errorf("failed to update state changes, err: %w", err))
129+
}
130+
obj.roots = append(obj.roots, root)
131+
}
132+
133+
for i := layersForTest; i < layersForTest*2; i++ {
134+
// Commit the state changes to simulate a tree with max diff layer before proceeding to the next layer
135+
db.Commit(obj.roots[len(obj.roots)-1], false)
117136
var parent = types.EmptyRootHash
118137
if len(obj.roots) != 0 {
119138
parent = obj.roots[len(obj.roots)-1]

0 commit comments

Comments
 (0)