Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snapshot): provide snapshot structure acceleration #324

Open
wants to merge 89 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
9485284
refactor: extract the zktrie logic into a new package, and prepare th…
mortal123 Apr 21, 2023
87d01e6
fix: reset the inner object for zktrie.Trie and zktrie.Secure
mortal123 Apr 21, 2023
8f18c57
Added tests for secure_trie_test and trie_test, skipping iterator and…
KevinYum Apr 23, 2023
c107000
adapt tests to zk-trie hash and encode contraints, remove benchmarks
KevinYum Apr 23, 2023
cbda0e0
feat: prove and verify for trie and secure trie
mortal123 Apr 23, 2023
ff4b17b
feat: struct BinaryPath to facilitate path representation
mortal123 Apr 23, 2023
406cf81
fix: proofs in statedb
mortal123 Apr 24, 2023
a6c009a
fix: set impl in trie
mortal123 Apr 24, 2023
4695cfc
fix: add zk trie magic bytes back
mortal123 Apr 25, 2023
fecd2f7
chore: secure trie add sanity checks for storage and account update
mortal123 Apr 25, 2023
9030b56
feat: add stack trie implementation
mortal123 Apr 25, 2023
85d7085
add test cases of proof_test for Trie
KevinYum Apr 26, 2023
ceeff53
fix: separate the use case of trie.Update and trie.UpdateAccount
mortal123 Apr 26, 2023
d9620b9
add test cases of proof_test for SecureTrie
KevinYum Apr 26, 2023
d77935d
fix: assign flag for leaf node in stack trie
mortal123 Apr 26, 2023
31ad213
chore: remove comments
mortal123 Apr 27, 2023
8c6e7dd
fix: fix bit order when update trie
mortal123 Apr 27, 2023
cf1d46b
chore: add key check in stacktrie
mortal123 Apr 27, 2023
095a0c0
add test cases of stacktrie_test
KevinYum Apr 27, 2023
dc98589
chore: replace zktrie with trie
mortal123 Apr 28, 2023
9df7803
chore: add binary path
mortal123 Apr 28, 2023
3c80d49
feat: trie iterator
mortal123 Apr 28, 2023
0cdb57f
feat: add proof range verify
mortal123 Apr 30, 2023
b16a518
fix test discrepancy introduced by commit e5af6e6
KevinYum May 2, 2023
c5df8ef
fix: remove debug log
mortal123 May 2, 2023
b857a85
feat: add iterator for secure trie
mortal123 May 2, 2023
2b5c2ac
chore: add key length checker in trie and stack_trie
mortal123 May 2, 2023
bec5e8e
fix: assign root hash for node iterator
mortal123 May 2, 2023
092de1e
fix: secure trie iterator bug
mortal123 May 2, 2023
065e203
Enforce trie_test and stacktrie_test cases to have key length of 32 b…
KevinYum May 2, 2023
1f07ad1
fix: make secure trie get key transform valid
mortal123 May 3, 2023
ef113e0
add test cases for iterator, align usage of key->secureKey conversion
KevinYum May 3, 2023
17e9f23
fix: save the unset internal node into cache
mortal123 May 4, 2023
826c66a
fix: range proof verification
mortal123 May 4, 2023
0eedd3a
fix: add trie update with kind method
mortal123 May 4, 2023
f217e05
chore: delete the snapshot disable
mortal123 May 4, 2023
43db248
fix: correcting account marshalling
mortal123 May 4, 2023
df91a1e
fix: check keys range in range proof verify
mortal123 May 4, 2023
945789f
add test cases for rangeVerify
KevinYum May 4, 2023
4c6e214
chore: fix minor issues and move poseidon hash into crypto packages
mortal123 May 5, 2023
48d8eaf
add basic benchmarks for geth and snap feature for comparison
KevinYum May 6, 2023
4de19cc
add snap sync
mortal123 May 8, 2023
895ac7f
fix iterator value decode bug
mortal123 May 8, 2023
fd8382d
enable cleans cache for trie database
mortal123 May 8, 2023
1816186
adopt proof tracer with secure trie
mortal123 May 9, 2023
6110df9
fix empty root hash
mortal123 May 9, 2023
c1339d8
allow snapshot diff layer do the accumulation when snapshot is genera…
mortal123 May 11, 2023
0e21b17
fix file import
mortal123 May 11, 2023
c67e3e4
make tests compilable
mortal123 May 11, 2023
b5850c2
dump log if zktrie is disabled
mortal123 May 11, 2023
af4ffc7
fix bugs for testing
mortal123 May 11, 2023
d596e08
add range proof test for account trie
KevinYum May 12, 2023
53e58c1
fix the checking method for trie node
mortal123 May 12, 2023
d7fb7c2
chore: go fmt with imports
mortal123 May 12, 2023
0120513
add node hash method
mortal123 May 12, 2023
a34513d
bug fix
mortal123 May 12, 2023
d61cef6
bug fix for zktrie proof
mortal123 May 12, 2023
ace8416
add test cases to sync(healer)
KevinYum May 12, 2023
0290fab
provide toString function for trie
mortal123 May 13, 2023
c5f6453
fix bug for hashing node proof key
mortal123 May 13, 2023
4ffc934
fix test code generate_test.go and genesis_test.go, handle correspond…
mortal123 May 13, 2023
6ca5353
fix testcases in statedb_test.go
mortal123 May 13, 2023
5bb7436
change compact encoding
mortal123 May 14, 2023
ee908e3
fix bugs and testcase (state sync)
mortal123 May 14, 2023
811e93a
disable DESTRUCT opcode testcase
mortal123 May 14, 2023
b3dce78
correcting the judgement of empty zktrie
mortal123 May 14, 2023
50640b3
add test cases for snap/sync_test (not passed yet)
KevinYum May 14, 2023
1a7417d
fix bugs for testcase and stacktrie
mortal123 May 14, 2023
7ada128
shrink test size for snap/sync_test to adapt poor trie performance
KevinYum May 15, 2023
239c07a
fix bugs for testcase
mortal123 May 15, 2023
77c4db5
fix tracer testcase
mortal123 May 15, 2023
fc90758
fix api testcase
mortal123 May 15, 2023
fee15c7
fix testcases in downloader and enable fast sync
mortal123 May 15, 2023
9dc5d2d
change the tree cap strategy
mortal123 May 16, 2023
71c9917
skip testcase related to zktrie database
mortal123 May 16, 2023
29b2943
Merge branch 'develop' of https://github.com/scroll-tech/go-ethereum …
mortal123 May 16, 2023
838aaaa
chore for pr check
mortal123 May 16, 2023
0fc49c5
makefile tests include zktrie
mortal123 May 16, 2023
868258f
fix bugs related account decode
mortal123 May 17, 2023
39b9d99
fix bugs of sync depth
mortal123 May 20, 2023
35cb810
add comment
mortal123 May 21, 2023
5deab05
enable zktrie snap sync
mortal123 May 23, 2023
ed09860
add benchmark for stacktrie
mortal123 May 23, 2023
987d7ad
eliminate inappropriate fmt printf
mortal123 May 23, 2023
1009e84
fix comment style in core/blockchain_test.go
mortal123 May 23, 2023
0059a2a
fix comment style
mortal123 May 23, 2023
ec5c3a9
merge from develop
mortal123 May 23, 2023
339e03f
fix account encoding in cmd snapshot
mortal123 May 24, 2023
8c6618f
Merge tag 'scroll-v3.3.1' of https://github.com/scroll-tech/go-ethere…
mortal123 May 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test: all
# genesis test
cd ${PWD}/cmd/geth; go test -test.run TestCustomGenesis
# module test
$(GORUN) build/ci.go test ./consensus ./core ./eth ./miner ./node ./trie
$(GORUN) build/ci.go test ./consensus ./core ./eth ./miner ./node ./trie ./zktrie

lint: ## Run linters.
$(GORUN) build/ci.go lint
Expand Down
3 changes: 2 additions & 1 deletion cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/scroll-tech/go-ethereum/params"
"github.com/scroll-tech/go-ethereum/rlp"
"github.com/scroll-tech/go-ethereum/trie"
"github.com/scroll-tech/go-ethereum/zktrie"
)

type Prestate struct {
Expand Down Expand Up @@ -263,7 +264,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
}

func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB {
sdb := state.NewDatabaseWithConfig(db, &trie.Config{Preimages: true})
sdb := state.NewDatabaseWithConfig(db, &zktrie.Config{Preimages: true})
statedb, _ := state.New(common.Hash{}, sdb, nil)
for addr, a := range accounts {
statedb.SetCode(addr, a.Code)
Expand Down
6 changes: 3 additions & 3 deletions cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/scroll-tech/go-ethereum/core/rawdb"
"github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/trie"
"github.com/scroll-tech/go-ethereum/zktrie"
)

var (
Expand Down Expand Up @@ -501,12 +501,12 @@ func dbDumpTrie(ctx *cli.Context) error {
return err
}
}
theTrie, err := trie.New(stRoot, trie.NewDatabase(db))
theTrie, err := zktrie.New(stRoot, zktrie.NewDatabase(db))
if err != nil {
return err
}
var count int64
it := trie.NewIterator(theTrie.NodeIterator(start))
it := zktrie.NewIterator(theTrie.NodeIterator(start))
for it.Next() {
if max > 0 && count == max {
fmt.Printf("Exiting after %d values\n", count)
Expand Down
37 changes: 18 additions & 19 deletions cmd/geth/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ import (
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/crypto/codehash"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rlp"
"github.com/scroll-tech/go-ethereum/trie"
"github.com/scroll-tech/go-ethereum/zktrie"
)

var (
// emptyRoot is the known root hash of an empty trie.
emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
emptyRoot = common.Hash{}

// emptyKeccakCodeHash is the known hash of the empty EVM bytecode.
emptyKeccakCodeHash = codehash.EmptyKeccakCodeHash.Bytes()
Expand Down Expand Up @@ -226,7 +225,7 @@ func verifyState(ctx *cli.Context) error {
log.Error("Failed to load head block")
return errors.New("no head block")
}
snaptree, err := snapshot.New(chaindb, trie.NewDatabase(chaindb), 256, headBlock.Root(), false, false, false)
snaptree, err := snapshot.New(chaindb, zktrie.NewDatabase(chaindb), 256, headBlock.Root(), false, false, false)
if err != nil {
log.Error("Failed to open snapshot tree", "err", err)
return err
Expand Down Expand Up @@ -283,8 +282,8 @@ func traverseState(ctx *cli.Context) error {
root = headBlock.Root()
log.Info("Start traversing the state", "root", root, "number", headBlock.NumberU64())
}
triedb := trie.NewDatabase(chaindb)
t, err := trie.NewSecure(root, triedb)
triedb := zktrie.NewDatabase(chaindb)
t, err := zktrie.NewSecure(root, triedb)
if err != nil {
log.Error("Failed to open trie", "root", root, "err", err)
return err
Expand All @@ -296,21 +295,21 @@ func traverseState(ctx *cli.Context) error {
lastReport time.Time
start = time.Now()
)
accIter := trie.NewIterator(t.NodeIterator(nil))
accIter := zktrie.NewIterator(t.NodeIterator(nil))
for accIter.Next() {
accounts += 1
var acc types.StateAccount
if err := rlp.DecodeBytes(accIter.Value, &acc); err != nil {
acc, err := types.UnmarshalStateAccount(accIter.Value)
if err != nil {
log.Error("Invalid account encountered during traversal", "err", err)
return err
}
if acc.Root != emptyRoot {
storageTrie, err := trie.NewSecure(acc.Root, triedb)
storageTrie, err := zktrie.NewSecure(acc.Root, triedb)
if err != nil {
log.Error("Failed to open storage trie", "root", acc.Root, "err", err)
return err
}
storageIter := trie.NewIterator(storageTrie.NodeIterator(nil))
storageIter := zktrie.NewIterator(storageTrie.NodeIterator(nil))
for storageIter.Next() {
slots += 1
}
Expand Down Expand Up @@ -373,8 +372,8 @@ func traverseRawState(ctx *cli.Context) error {
root = headBlock.Root()
log.Info("Start traversing the state", "root", root, "number", headBlock.NumberU64())
}
triedb := trie.NewDatabase(chaindb)
t, err := trie.NewSecure(root, triedb)
triedb := zktrie.NewDatabase(chaindb)
t, err := zktrie.NewSecure(root, triedb)
if err != nil {
log.Error("Failed to open trie", "root", root, "err", err)
return err
Expand All @@ -395,7 +394,7 @@ func traverseRawState(ctx *cli.Context) error {
if node != (common.Hash{}) {
// Check the present for non-empty hash node(embedded node doesn't
// have their own hash).
blob := rawdb.ReadTrieNode(chaindb, node)
blob := rawdb.ReadZKTrieNode(chaindb, node)
if len(blob) == 0 {
log.Error("Missing trie node(account)", "hash", node)
return errors.New("missing account")
Expand All @@ -405,13 +404,13 @@ func traverseRawState(ctx *cli.Context) error {
// dig into the storage trie further.
if accIter.Leaf() {
accounts += 1
var acc types.StateAccount
if err := rlp.DecodeBytes(accIter.LeafBlob(), &acc); err != nil {
var acc *types.StateAccount
if acc, err = types.UnmarshalStateAccount(accIter.LeafBlob()); err != nil {
log.Error("Invalid account encountered during traversal", "err", err)
return errors.New("invalid account")
}
if acc.Root != emptyRoot {
storageTrie, err := trie.NewSecure(acc.Root, triedb)
storageTrie, err := zktrie.NewSecure(acc.Root, triedb)
if err != nil {
log.Error("Failed to open storage trie", "root", acc.Root, "err", err)
return errors.New("missing storage trie")
Expand All @@ -424,7 +423,7 @@ func traverseRawState(ctx *cli.Context) error {
// Check the present for non-empty hash node(embedded node doesn't
// have their own hash).
if node != (common.Hash{}) {
blob := rawdb.ReadTrieNode(chaindb, node)
blob := rawdb.ReadZKTrieNode(chaindb, node)
if len(blob) == 0 {
log.Error("Missing trie node(storage)", "hash", node)
return errors.New("missing storage")
Expand Down Expand Up @@ -478,7 +477,7 @@ func dumpState(ctx *cli.Context) error {
if err != nil {
return err
}
snaptree, err := snapshot.New(db, trie.NewDatabase(db), 256, root, false, false, false)
snaptree, err := snapshot.New(db, zktrie.NewDatabase(db), 256, root, false, false, false)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions common/big.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
Big3 = big.NewInt(3)
Big0 = big.NewInt(0)
Big32 = big.NewInt(32)
Big248 = big.NewInt(248)
Big256 = big.NewInt(256)
Big257 = big.NewInt(257)
)
23 changes: 11 additions & 12 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import (
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/metrics"
"github.com/scroll-tech/go-ethereum/params"
"github.com/scroll-tech/go-ethereum/trie"
"github.com/scroll-tech/go-ethereum/trie/zkproof"
"github.com/scroll-tech/go-ethereum/zktrie"
"github.com/scroll-tech/go-ethereum/zktrie/zkproof"
)

var (
Expand Down Expand Up @@ -230,26 +230,25 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
blockCache, _ := lru.New(blockCacheLimit)
txLookupCache, _ := lru.New(txLookupCacheLimit)
futureBlocks, _ := lru.New(maxFutureBlocks)
// override snapshot setting
if chainConfig.Scroll.ZktrieEnabled() && cacheConfig.SnapshotLimit > 0 {
log.Warn("Snapshot has been disabled by zktrie")
cacheConfig.SnapshotLimit = 0
}

if chainConfig.Scroll.FeeVaultEnabled() {
log.Warn("Using fee vault address", "FeeVaultAddress", *chainConfig.Scroll.FeeVaultAddress)
}

if !chainConfig.Scroll.ZktrieEnabled() {
log.Warn("zktrie should not be disabled, we will enable it")
chainConfig.Scroll.UseZktrie = true
}

bc := &BlockChain{
chainConfig: chainConfig,
cacheConfig: cacheConfig,
db: db,
triegc: prque.New(nil),
stateCache: state.NewDatabaseWithConfig(db, &trie.Config{
Cache: cacheConfig.TrieCleanLimit,
Journal: cacheConfig.TrieCleanJournal,
stateCache: state.NewDatabaseWithConfig(db, &zktrie.Config{
Cache: cacheConfig.TrieCleanLimit,
//Journal: cacheConfig.TrieCleanJournal,
Preimages: cacheConfig.Preimages,
Zktrie: chainConfig.Scroll.ZktrieEnabled(),
}),
quit: make(chan struct{}),
chainmu: syncx.NewClosableMutex(),
Expand Down Expand Up @@ -653,7 +652,7 @@ func (bc *BlockChain) FastSyncCommitHead(hash common.Hash) error {
if block == nil {
return fmt.Errorf("non existent block [%x..]", hash[:4])
}
if _, err := trie.NewSecure(block.Root(), bc.stateCache.TrieDB()); err != nil {
if _, err := zktrie.NewSecure(block.Root(), bc.stateCache.TrieDB()); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we totally drop any options for non-zktrie db within this PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I understand that there is no reason for us to use the original trie for related operations involving state? However, in some places where block, transaction and receipt trie are calculated, the original trie is still used

return err
}

Expand Down
5 changes: 5 additions & 0 deletions core/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,10 @@ func testRepair(t *testing.T, tt *rewindTest, snapshots bool) {
}
}

func skipForTrieDB(t *testing.T) {
t.Skip("skipping testing because zktrie database is not support now")
}

// TestIssue23496 tests scenario described in https://github.com/scroll-tech/go-ethereum/pull/23496#issuecomment-926393893
// Credits to @zzyalbert for finding the issue.
//
Expand All @@ -1879,6 +1883,7 @@ func testRepair(t *testing.T, tt *rewindTest, snapshots bool) {
// In this case the snapshot layer of B3 is not created because of existent
// state.
func TestIssue23496(t *testing.T) {
skipForTrieDB(t)
// It's hard to follow the test case, visualize the input
//log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))

Expand Down
1 change: 1 addition & 0 deletions core/blockchain_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ func TestLowCommitCrashWithNewSnapshot(t *testing.T) {
// committed point so the chain should be rewound to genesis and the disk layer
// should be left for recovery.
func TestHighCommitCrashWithNewSnapshot(t *testing.T) {
skipForTrieDB(t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do these tests have to be skipped explicitly now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because this test relies on the behavior of the GC in the trie.
For example, in this example, there would be a chain
G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
Among them, the snapshot is persisted at C4, and then the trie commit at C6. The test expects that the restart after the crash will start from G, because there is no trie information in the DB in the range of C1-4. The current trie db is currently written directly, so C4 also exists in the DB, so this test case cannot pass.

// Chain:
// G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
//
Expand Down
1 change: 1 addition & 0 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,7 @@ func TestBlockchainHeaderchainReorgConsistency(t *testing.T) {
// Tests that importing small side forks doesn't leave junk in the trie database
// cache (which would eventually cause memory issues).
func TestTrieForkGC(t *testing.T) {
skipForTrieDB(t)
// Generate a canonical chain to act as the main dataset
engine := ethash.NewFaker()

Expand Down
22 changes: 2 additions & 20 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
// We have the genesis block in database(perhaps in ancient database)
// but the corresponding state is missing.
header := rawdb.ReadHeader(db, stored, 0)

var trieCfg *trie.Config

if genesis == nil {
storedcfg := rawdb.ReadChainConfig(db, stored)
if storedcfg == nil {
log.Warn("Found genesis block without chain config")
} else {
trieCfg = &trie.Config{Zktrie: storedcfg.Scroll.ZktrieEnabled()}
}
} else {
trieCfg = &trie.Config{Zktrie: genesis.Config.Scroll.ZktrieEnabled()}
}

if _, err := state.New(header.Root, state.NewDatabaseWithConfig(db, trieCfg), nil); err != nil {
if _, err := state.New(header.Root, state.NewDatabaseWithConfig(db, nil), nil); err != nil {
if genesis == nil {
genesis = DefaultGenesisBlock()
}
Expand Down Expand Up @@ -275,11 +261,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
if db == nil {
db = rawdb.NewMemoryDatabase()
}
var trieCfg *trie.Config
if g.Config != nil {
trieCfg = &trie.Config{Zktrie: g.Config.Scroll.ZktrieEnabled()}
}
statedb, err := state.New(common.Hash{}, state.NewDatabaseWithConfig(db, trieCfg), nil)
statedb, err := state.New(common.Hash{}, state.NewDatabaseWithConfig(db, nil), nil)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestInvalidCliqueConfig(t *testing.T) {

func TestSetupGenesis(t *testing.T) {
var (
customghash = common.HexToHash("0x700380ab70d789c462c4e8f0db082842095321f390d0a3f25f400f0746db32bc")
customghash = common.HexToHash("0xede2c5eacff3e68fec4fa5042867bc12acad1fd44dd9e489e9eb83f625dc038a")
customg = Genesis{
Config: &params.ChainConfig{HomesteadBlock: big.NewInt(3)},
Alloc: GenesisAlloc{
Expand Down
20 changes: 20 additions & 0 deletions core/rawdb/accessors_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) {
}
}

// ReadTrieNode retrieves the trie node of the provided hash.
func ReadZKTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte {
data, _ := db.Get(trieNodeKey(hash))
return data
}

// WriteTrieNode writes the provided trie node database.
func WriteZKTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) {
if err := db.Put(trieNodeKey(hash), node); err != nil {
log.Crit("Failed to store trie node", "err", err)
}
}

// DeleteTrieNode deletes the specified trie node from the database.
func DeleteZKTrieNode(db ethdb.KeyValueWriter, hash common.Hash) {
if err := db.Delete(trieNodeKey(hash)); err != nil {
log.Crit("Failed to delete trie node", "err", err)
}
}

// ReadTrieNode retrieves the trie node of the provided hash.
func ReadTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte {
data, _ := db.Get(hash.Bytes())
Expand Down
8 changes: 8 additions & 0 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ func preimageKey(hash common.Hash) []byte {
return append(PreimagePrefix, hash.Bytes()...)
}

func trieNodeKey(hash common.Hash) []byte {
dst := append([]byte{}, hash[:]...)
for i, j := 0, len(dst)-1; i < j; i, j = i+1, j-1 {
dst[i], dst[j] = dst[j], dst[i]
}
return dst
}

// codeKey = CodePrefix + hash
func codeKey(hash common.Hash) []byte {
return append(CodePrefix, hash.Bytes()...)
Expand Down
Loading