2023-08-01 07:17:32 -05:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package pathdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2024-03-18 21:50:08 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/internal/testrand"
|
2023-08-01 07:17:32 -05:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
|
|
|
)
|
|
|
|
|
|
|
|
func emptyLayer() *diskLayer {
|
|
|
|
return &diskLayer{
|
2024-03-26 15:25:41 -05:00
|
|
|
db: New(rawdb.NewMemoryDatabase(), nil, false),
|
2024-11-29 05:30:45 -06:00
|
|
|
buffer: newBuffer(defaultBufferSize, nil, nil, 0),
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// goos: darwin
|
|
|
|
// goarch: arm64
|
|
|
|
// pkg: github.com/ethereum/go-ethereum/trie
|
|
|
|
// BenchmarkSearch128Layers
|
|
|
|
// BenchmarkSearch128Layers-8 243826 4755 ns/op
|
|
|
|
func BenchmarkSearch128Layers(b *testing.B) { benchmarkSearch(b, 0, 128) }
|
|
|
|
|
|
|
|
// goos: darwin
|
|
|
|
// goarch: arm64
|
|
|
|
// pkg: github.com/ethereum/go-ethereum/trie
|
|
|
|
// BenchmarkSearch512Layers
|
|
|
|
// BenchmarkSearch512Layers-8 49686 24256 ns/op
|
|
|
|
func BenchmarkSearch512Layers(b *testing.B) { benchmarkSearch(b, 0, 512) }
|
|
|
|
|
|
|
|
// goos: darwin
|
|
|
|
// goarch: arm64
|
|
|
|
// pkg: github.com/ethereum/go-ethereum/trie
|
|
|
|
// BenchmarkSearch1Layer
|
|
|
|
// BenchmarkSearch1Layer-8 14062725 88.40 ns/op
|
|
|
|
func BenchmarkSearch1Layer(b *testing.B) { benchmarkSearch(b, 127, 128) }
|
|
|
|
|
|
|
|
func benchmarkSearch(b *testing.B, depth int, total int) {
|
|
|
|
var (
|
|
|
|
npath []byte
|
|
|
|
nblob []byte
|
|
|
|
)
|
|
|
|
// First, we set up 128 diff layers, with 3K items each
|
|
|
|
fill := func(parent layer, index int) *diffLayer {
|
|
|
|
nodes := make(map[common.Hash]map[string]*trienode.Node)
|
|
|
|
nodes[common.Hash{}] = make(map[string]*trienode.Node)
|
|
|
|
for i := 0; i < 3000; i++ {
|
|
|
|
var (
|
2024-03-18 21:50:08 -05:00
|
|
|
path = testrand.Bytes(32)
|
|
|
|
blob = testrand.Bytes(100)
|
|
|
|
node = trienode.New(crypto.Keccak256Hash(blob), blob)
|
2023-08-01 07:17:32 -05:00
|
|
|
)
|
2024-04-30 09:25:35 -05:00
|
|
|
nodes[common.Hash{}][string(path)] = node
|
2023-08-01 07:17:32 -05:00
|
|
|
if npath == nil && depth == index {
|
|
|
|
npath = common.CopyBytes(path)
|
2024-04-30 09:25:35 -05:00
|
|
|
nblob = common.CopyBytes(blob)
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
}
|
all: implement state history v2 (#30107)
This pull request delivers the new version of the state history, where
the raw storage key is used instead of the hash.
Before the cancun fork, it's supported by protocol to destruct a
specific account and therefore, all the storage slot owned by it should
be wiped in the same transition.
Technically, storage wiping should be performed through storage
iteration, and only the storage key hash will be available for traversal
if the state snapshot is not available. Therefore, the storage key hash
is chosen as the identifier in the old version state history.
Fortunately, account self-destruction has been deprecated by the
protocol since the Cancun fork, and there are no empty accounts eligible
for deletion under EIP-158. Therefore, we can conclude that no storage
wiping should occur after the Cancun fork. In this case, it makes no
sense to keep using hash.
Besides, another big reason for making this change is the current format
state history is unusable if verkle is activated. Verkle tree has a
different key derivation scheme (merkle uses keccak256), the preimage of
key hash must be provided in order to make verkle rollback functional.
This pull request is a prerequisite for landing verkle.
Additionally, the raw storage key is more human-friendly for those who
want to manually check the history, even though Solidity already
performs some hashing to derive the storage location.
---
This pull request doesn't bump the database version, as I believe the
database should still be compatible if users degrade from the new geth
version to old one, the only side effect is the persistent new version
state history will be unusable.
---------
Co-authored-by: Zsolt Felfoldi <zsfelfoldi@gmail.com>
2025-01-16 19:59:02 -06:00
|
|
|
return newDiffLayer(parent, common.Hash{}, 0, 0, newNodeSet(nodes), NewStateSetWithOrigin(nil, nil, nil, nil, false))
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
var layer layer
|
|
|
|
layer = emptyLayer()
|
|
|
|
for i := 0; i < total; i++ {
|
|
|
|
layer = fill(layer, i)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
var (
|
|
|
|
have []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
for i := 0; i < b.N; i++ {
|
2024-03-26 15:25:41 -05:00
|
|
|
have, _, _, err = layer.node(common.Hash{}, npath, 0)
|
2023-08-01 07:17:32 -05:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !bytes.Equal(have, nblob) {
|
|
|
|
b.Fatalf("have %x want %x", have, nblob)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// goos: darwin
|
|
|
|
// goarch: arm64
|
|
|
|
// pkg: github.com/ethereum/go-ethereum/trie
|
|
|
|
// BenchmarkPersist
|
|
|
|
// BenchmarkPersist-8 10 111252975 ns/op
|
|
|
|
func BenchmarkPersist(b *testing.B) {
|
|
|
|
// First, we set up 128 diff layers, with 3K items each
|
|
|
|
fill := func(parent layer) *diffLayer {
|
|
|
|
nodes := make(map[common.Hash]map[string]*trienode.Node)
|
|
|
|
nodes[common.Hash{}] = make(map[string]*trienode.Node)
|
|
|
|
for i := 0; i < 3000; i++ {
|
|
|
|
var (
|
2024-03-18 21:50:08 -05:00
|
|
|
path = testrand.Bytes(32)
|
|
|
|
blob = testrand.Bytes(100)
|
|
|
|
node = trienode.New(crypto.Keccak256Hash(blob), blob)
|
2023-08-01 07:17:32 -05:00
|
|
|
)
|
2024-04-30 09:25:35 -05:00
|
|
|
nodes[common.Hash{}][string(path)] = node
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
all: implement state history v2 (#30107)
This pull request delivers the new version of the state history, where
the raw storage key is used instead of the hash.
Before the cancun fork, it's supported by protocol to destruct a
specific account and therefore, all the storage slot owned by it should
be wiped in the same transition.
Technically, storage wiping should be performed through storage
iteration, and only the storage key hash will be available for traversal
if the state snapshot is not available. Therefore, the storage key hash
is chosen as the identifier in the old version state history.
Fortunately, account self-destruction has been deprecated by the
protocol since the Cancun fork, and there are no empty accounts eligible
for deletion under EIP-158. Therefore, we can conclude that no storage
wiping should occur after the Cancun fork. In this case, it makes no
sense to keep using hash.
Besides, another big reason for making this change is the current format
state history is unusable if verkle is activated. Verkle tree has a
different key derivation scheme (merkle uses keccak256), the preimage of
key hash must be provided in order to make verkle rollback functional.
This pull request is a prerequisite for landing verkle.
Additionally, the raw storage key is more human-friendly for those who
want to manually check the history, even though Solidity already
performs some hashing to derive the storage location.
---
This pull request doesn't bump the database version, as I believe the
database should still be compatible if users degrade from the new geth
version to old one, the only side effect is the persistent new version
state history will be unusable.
---------
Co-authored-by: Zsolt Felfoldi <zsfelfoldi@gmail.com>
2025-01-16 19:59:02 -06:00
|
|
|
return newDiffLayer(parent, common.Hash{}, 0, 0, newNodeSet(nodes), NewStateSetWithOrigin(nil, nil, nil, nil, false))
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
var layer layer
|
|
|
|
layer = emptyLayer()
|
|
|
|
for i := 1; i < 128; i++ {
|
|
|
|
layer = fill(layer)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
dl, ok := layer.(*diffLayer)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
dl.persist(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkJournal benchmarks the performance for journaling the layers.
|
|
|
|
//
|
|
|
|
// BenchmarkJournal
|
|
|
|
// BenchmarkJournal-8 10 110969279 ns/op
|
|
|
|
func BenchmarkJournal(b *testing.B) {
|
|
|
|
b.SkipNow()
|
|
|
|
|
|
|
|
// First, we set up 128 diff layers, with 3K items each
|
|
|
|
fill := func(parent layer) *diffLayer {
|
|
|
|
nodes := make(map[common.Hash]map[string]*trienode.Node)
|
|
|
|
nodes[common.Hash{}] = make(map[string]*trienode.Node)
|
|
|
|
for i := 0; i < 3000; i++ {
|
|
|
|
var (
|
2024-03-18 21:50:08 -05:00
|
|
|
path = testrand.Bytes(32)
|
|
|
|
blob = testrand.Bytes(100)
|
|
|
|
node = trienode.New(crypto.Keccak256Hash(blob), blob)
|
2023-08-01 07:17:32 -05:00
|
|
|
)
|
2024-04-30 09:25:35 -05:00
|
|
|
nodes[common.Hash{}][string(path)] = node
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
all: implement state history v2 (#30107)
This pull request delivers the new version of the state history, where
the raw storage key is used instead of the hash.
Before the cancun fork, it's supported by protocol to destruct a
specific account and therefore, all the storage slot owned by it should
be wiped in the same transition.
Technically, storage wiping should be performed through storage
iteration, and only the storage key hash will be available for traversal
if the state snapshot is not available. Therefore, the storage key hash
is chosen as the identifier in the old version state history.
Fortunately, account self-destruction has been deprecated by the
protocol since the Cancun fork, and there are no empty accounts eligible
for deletion under EIP-158. Therefore, we can conclude that no storage
wiping should occur after the Cancun fork. In this case, it makes no
sense to keep using hash.
Besides, another big reason for making this change is the current format
state history is unusable if verkle is activated. Verkle tree has a
different key derivation scheme (merkle uses keccak256), the preimage of
key hash must be provided in order to make verkle rollback functional.
This pull request is a prerequisite for landing verkle.
Additionally, the raw storage key is more human-friendly for those who
want to manually check the history, even though Solidity already
performs some hashing to derive the storage location.
---
This pull request doesn't bump the database version, as I believe the
database should still be compatible if users degrade from the new geth
version to old one, the only side effect is the persistent new version
state history will be unusable.
---------
Co-authored-by: Zsolt Felfoldi <zsfelfoldi@gmail.com>
2025-01-16 19:59:02 -06:00
|
|
|
return newDiffLayer(parent, common.Hash{}, 0, 0, newNodeSet(nodes), NewStateSetWithOrigin(nil, nil, nil, nil, false))
|
2023-08-01 07:17:32 -05:00
|
|
|
}
|
|
|
|
var layer layer
|
|
|
|
layer = emptyLayer()
|
|
|
|
for i := 0; i < 128; i++ {
|
|
|
|
layer = fill(layer)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
layer.journal(new(bytes.Buffer))
|
|
|
|
}
|
|
|
|
}
|