Skip to content

Commit

Permalink
triedb/pathdb: enlarge maxDiffLayers to 57600
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 committed Dec 12, 2024
1 parent 9e46b4d commit bfc0824
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ jobs:
os: linux
arch: amd64
dist: bionic
vm:
size: large
go: 1.21.x
env:
- GO111MODULE=on
script:
- travis_wait 120 go run build/ci.go test $TEST_PACKAGES
- travis_wait 120 go run build/ci.go test -timeout 60m $TEST_PACKAGES

- stage: Docker Image Release
script:
Expand Down
23 changes: 13 additions & 10 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/triedb/pathdb"
"github.com/holiman/uint256"
)

Expand All @@ -54,7 +55,9 @@ var (
forkSeed1 = 2
forkSeed2 = 3

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

// newCanonical creates a chain database, and injects a deterministic canonical
Expand Down Expand Up @@ -1953,8 +1956,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*TriesInMemory, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{2}) })
competitor, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, 2*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(), t.TempDir(), "", false, false, false, false, false)
Expand Down Expand Up @@ -1991,11 +1994,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.
// 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 @@ -2936,7 +2939,7 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) {
BaseFee: big.NewInt(params.InitialBaseFee),
}
// Generate and import the canonical chain
_, blocks, _ := GenerateChainWithGenesis(genesis, engine, 2*TriesInMemory, nil)
_, blocks, _ := GenerateChainWithGenesis(genesis, engine, TestMaxDiffLayers+TestAdditionalLayers, nil)

chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil)
if err != nil {
Expand All @@ -2947,11 +2950,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.
// 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 = TestTriesInMemory + 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 @@ -37,8 +37,8 @@ import (
)

const (
// 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 = 57600

// defaultCleanSize is the default memory allowance of clean cache.
defaultCleanSize = 16 * 1024 * 1024
Expand Down Expand Up @@ -262,7 +262,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
21 changes: 20 additions & 1 deletion triedb/pathdb/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import (
"github.com/holiman/uint256"
)

const (
layersForTest = 128
)

func updateTrie(addrHash common.Hash, root common.Hash, dirties, cleans map[common.Hash][]byte) (common.Hash, *trienode.NodeSet) {
h, err := newTestHasher(addrHash, root, cleans)
if err != nil {
Expand Down Expand Up @@ -113,7 +117,22 @@ func newTester(t *testing.T, historyLimit uint64) *tester {
snapStorages: make(map[common.Hash]map[common.Hash]map[common.Hash][]byte),
}
)
for i := 0; i < 2*128; i++ {

for i := 0; i < layersForTest; i++ {
var parent = types.EmptyRootHash
if len(obj.roots) != 0 {
parent = obj.roots[len(obj.roots)-1]
}
root, nodes, states := obj.generate(parent)
if err := db.Update(root, parent, uint64(i), nodes, states); err != nil {
panic(fmt.Errorf("failed to update state changes, err: %w", err))
}
obj.roots = append(obj.roots, root)
}

for i := layersForTest; i < layersForTest*2; i++ {
// Commit the state changes to simulate a tree with max diff layer before proceeding to the next layer
db.Commit(obj.roots[len(obj.roots)-1], false)
var parent = types.EmptyRootHash
if len(obj.roots) != 0 {
parent = obj.roots[len(obj.roots)-1]
Expand Down

0 comments on commit bfc0824

Please sign in to comment.