Closed cantincy closed 6 months ago
package main
import (
"strconv"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/triedb"
)
func f() {
dir := "./data"
db, err := rawdb.NewLevelDBDatabase(dir, 16, 16, "leveldb", false)
if err != nil {
panic(err)
}
defer db.Close()
tdb := triedb.NewDatabase(db, triedb.HashDefaults)
mpt := trie.NewEmpty(tdb)
for i := 0; i < 100_0000; i++ {
mpt.MustUpdate([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
}
root, nodes, err = mpt.Commit(true)
if err != nil {
panic(err)
}
tdb.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes))
tdb.Commit(root)
}
func main() {
f()
}
You need to explicitly flush the trie changes to underlying database with these additional codes.
@rjl493456442 It does work! Thank you very much for your help :)
I am studying trie in Ethereum (merkle patricia tree) and when I run the following code, the leveldb data directory is empty with no data in it. I would like to ask how to persist trie data to leveldb? I would appreciate it if there's any help.
go ethereum: 1.14.0 go: 1.22.0