Skip to content

Commit

Permalink
trie: polish code for hash2path tool
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnss committed Sep 20, 2023
1 parent 58a9c66 commit 7d1ca36
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions trie/hash2path.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie/trienode"
Expand All @@ -26,7 +27,6 @@ type Hash2Path struct {
concurrentQueue chan struct{}
totalNum uint64
wg sync.WaitGroup
// batch ethdb.Batch
}

const (
Expand All @@ -51,7 +51,6 @@ func NewHash2Path(tr *Trie, db *Database, stateRootHash common.Hash, blocknum ui
root: tr.root,
concurrentQueue: make(chan struct{}, jobnum),
wg: sync.WaitGroup{},
// batch: db.diskdb.NewBatch(),
}

return ins, nil
Expand All @@ -71,39 +70,23 @@ func (t *Trie) resloveWithoutTrack(n node, prefix []byte) (node, error) {
func (h2p *Hash2Path) writeNode(pathKey []byte, n *trienode.Node, owner common.Hash) {
if owner == (common.Hash{}) {
rawdb.WriteAccountTrieNode(h2p.db.DiskDB(), pathKey, n.Blob)
// fmt.Println("WriteNodes account node, ", "path: ", common.Bytes2Hex(pathKey), "Hash: ", n.Hash, "BlobHash: ", crypto.Keccak256Hash(n.Blob))
log.Debug("WriteNodes account node, ", "path: ", common.Bytes2Hex(pathKey), "Hash: ", n.Hash, "BlobHash: ", crypto.Keccak256Hash(n.Blob))
} else {
rawdb.WriteStorageTrieNode(h2p.db.DiskDB(), owner, pathKey, n.Blob)
// fmt.Println("WriteNodes storage node, ", "path: ", common.Bytes2Hex(pathKey), "owner: ", owner.String(), "Hash: ", n.Hash, "BlobHash: ", crypto.Keccak256Hash(n.Blob))
log.Debug("WriteNodes storage node, ", "path: ", common.Bytes2Hex(pathKey), "owner: ", owner.String(), "Hash: ", n.Hash, "BlobHash: ", crypto.Keccak256Hash(n.Blob))
}
// if delete the nodes of the account trie here, the error will occur when open storage trie.
// rawdb.DeleteTrieNode(h2p.db.DiskDB(), owner, nil, n.Hash, rawdb.HashScheme)

// if h2p.batch.ValueSize() > 100000 {
// err := h2p.batch.Write()
// if err != nil {
// fmt.Println("batch write error: ", err)
// }
// // reset for next batch write
// h2p.batch.Reset()
// }
}

// Run statistics, external call
func (h2p *Hash2Path) Run() {
log.Info("Find Account Trie Tree, rootHash: ", h2p.trie.Hash().String(), "BlockNum: ", h2p.blocknum)
log.Debug("Find Account Trie Tree, rootHash: ", h2p.trie.Hash().String(), "BlockNum: ", h2p.blocknum)

h2p.ConcurrentTraversal(h2p.trie, h2p.root, []byte{})
h2p.wg.Wait()

fmt.Println("hash2Path run finished.")
rawdb.WritePersistentStateID(h2p.db.DiskDB(), h2p.blocknum)
rawdb.WriteStateID(h2p.db.DiskDB(), h2p.stateRootHash, h2p.blocknum)
// err := h2p.batch.Write()
// if err != nil {
// fmt.Println("batch write error: ", err)
// }
// h2p.batch.Reset()
}

func (h2p *Hash2Path) SubConcurrentTraversal(theTrie *Trie, theNode node, path []byte) {
Expand Down Expand Up @@ -184,7 +167,7 @@ func (h2p *Hash2Path) ConcurrentTraversal(theTrie *Trie, theNode node, path []by
log.Error("New Storage trie error", "err", err, "root", account.Root.String(), "owner", ownerAddress.String())
break
}
// log.Info("Find Contract Trie Tree, rootHash: ", tr.Hash().String(), "")
log.Debug("Find Contract Trie Tree, rootHash: ", tr.Hash().String(), "")
h2p.wg.Add(1)
go h2p.SubConcurrentTraversal(tr, tr.root, []byte{})
default:
Expand Down

0 comments on commit 7d1ca36

Please sign in to comment.