Skip to content

Commit

Permalink
fix: update some logs
Browse files Browse the repository at this point in the history
  • Loading branch information
VM committed Apr 2, 2024
1 parent 4a62838 commit 20de6e1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 53 deletions.
46 changes: 23 additions & 23 deletions cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func dbTrieGet(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, false, false)
db := utils.MakeChainDatabase(ctx, stack, false)
defer db.Close()

scheme := ctx.String(utils.StateSchemeFlag.Name)
Expand All @@ -492,7 +492,7 @@ func dbTrieGet(ctx *cli.Context) error {
return err
}
nodeVal, hash := rawdb.ReadAccountTrieNode(db, pathKey)
log.Info("TrieGet result ", "PathKey", common.Bytes2Hex(pathKey), "Hash: ", hash, "node: ", trie.NodeString(hash.Bytes(), nodeVal))
log.Info("TrieGet result", "pathKey", common.Bytes2Hex(pathKey), "hash", hash, "node", trie.NodeString(hash.Bytes(), nodeVal))
} else if ctx.NArg() == 2 {
owner, err = hexutil.Decode(ctx.Args().Get(0))
if err != nil {
Expand All @@ -506,7 +506,7 @@ func dbTrieGet(ctx *cli.Context) error {
}

nodeVal, hash := rawdb.ReadStorageTrieNode(db, common.BytesToHash(owner), pathKey)
log.Info("TrieGet result ", "PathKey: ", common.Bytes2Hex(pathKey), "Owner: ", common.BytesToHash(owner), "Hash: ", hash, "node: ", trie.NodeString(hash.Bytes(), nodeVal))
log.Info("TrieGet result", "pathKey", common.Bytes2Hex(pathKey), "owner", common.BytesToHash(owner), "Hash: ", hash, "node: ", trie.NodeString(hash.Bytes(), nodeVal))
}
} else if scheme == rawdb.HashScheme {
if ctx.NArg() == 1 {
Expand All @@ -517,12 +517,12 @@ func dbTrieGet(ctx *cli.Context) error {
}
val, err := db.Get(hashKey)
if err != nil {
log.Error("db get failed, ", "error: ", err)
log.Error("Failed to get value from db, ", "error", err)
return err
}
log.Info("TrieGet result ", "HashKey: ", common.BytesToHash(hashKey), "node: ", trie.NodeString(hashKey, val))
log.Info("TrieGet result", "hash key", common.BytesToHash(hashKey), "node", trie.NodeString(hashKey, val))
} else {
log.Error("args too much")
log.Error("Too many args")
}
}

Expand All @@ -537,7 +537,7 @@ func dbTrieDelete(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, false, false)
db := utils.MakeChainDatabase(ctx, stack, false)
defer db.Close()

scheme := ctx.String(utils.StateSchemeFlag.Name)
Expand Down Expand Up @@ -580,11 +580,11 @@ func dbTrieDelete(ctx *cli.Context) error {
}
err = db.Delete(hashKey)
if err != nil {
log.Error("db delete failed", "err", err)
log.Error("Failed to delete data in db", "err", err)
return err
}
} else {
log.Error("args too much")
log.Error("Too many args")
}
}
return nil
Expand Down Expand Up @@ -910,22 +910,22 @@ func hbss2pbss(ctx *cli.Context) error {
return fmt.Errorf("required arguments: %v", ctx.Command.ArgsUsage)
}

var jobnum uint64
var jobNum uint64
var err error
if ctx.NArg() == 1 {
jobnum, err = strconv.ParseUint(ctx.Args().Get(0), 10, 64)
jobNum, err = strconv.ParseUint(ctx.Args().Get(0), 10, 64)
if err != nil {
return fmt.Errorf("failed to Parse jobnum, Args[1]: %v, err: %v", ctx.Args().Get(1), err)
return fmt.Errorf("failed to parse job num, Args[1]: %v, err: %v", ctx.Args().Get(1), err)
}
} else {
// by default
jobnum = 1000
jobNum = 1000
}

stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, false, false)
db := utils.MakeChainDatabase(ctx, stack, false)
db.Sync()
defer db.Close()

Expand All @@ -938,11 +938,11 @@ func hbss2pbss(ctx *cli.Context) error {
headerHash := rawdb.ReadHeadHeaderHash(db)
blockNumber := rawdb.ReadHeaderNumber(db, headerHash)
if blockNumber == nil {
log.Error("read header number failed.")
return fmt.Errorf("read header number failed")
log.Error("Failed to read header number")
return fmt.Errorf("failed to read header number")
}

log.Info("hbss2pbss converting", "HeaderHash: ", headerHash.String(), ", blockNumber: ", *blockNumber)
log.Info("hbss2pbss converting", "HeaderHash", headerHash.String(), "blockNumber", *blockNumber)

var headerBlockHash common.Hash
var trieRootHash common.Hash
Expand All @@ -958,19 +958,19 @@ func hbss2pbss(ctx *cli.Context) error {
}
if (trieRootHash == common.Hash{}) {
log.Error("Empty root hash")
return fmt.Errorf("Empty root hash.")
return fmt.Errorf("empty root hash")
}

id := trie.StateTrieID(trieRootHash)
theTrie, err := trie.New(id, triedb)
if err != nil {
log.Error("fail to new trie tree", "err", err, "rootHash", err, trieRootHash.String())
log.Error("Failed to new trie tree", "err", err, "root hash", trieRootHash.String())
return err
}

h2p, err := trie.NewHbss2Pbss(theTrie, triedb, trieRootHash, *blockNumber, jobnum)
h2p, err := trie.NewHbss2Pbss(theTrie, triedb, trieRootHash, *blockNumber, jobNum)
if err != nil {
log.Error("fail to new hash2pbss", "err", err, "rootHash", err, trieRootHash.String())
log.Error("Failed to new hash2pbss", "err", err, "root hash", trieRootHash.String())
return err
}
h2p.Run()
Expand All @@ -986,8 +986,8 @@ func pruneHashTrie(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, false, false)
db := utils.MakeChainDatabase(ctx, stack, false)
defer db.Close()

return rawdb.PruneHashTrieNodeInDataBase(db)
return rawdb.PruneHashTrieNodeInDatabase(db)
}
6 changes: 3 additions & 3 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func (s *stat) Count() string {
return s.count.String()
}

func PruneHashTrieNodeInDataBase(db ethdb.Database) error {
func PruneHashTrieNodeInDatabase(db ethdb.Database) error {
it := db.NewIterator([]byte{}, []byte{})
defer it.Release()

Expand All @@ -461,13 +461,13 @@ func PruneHashTrieNodeInDataBase(db ethdb.Database) error {
db.Delete(key)
total_num++
if total_num%100000 == 0 {
log.Info("Pruning ", "Complete progress: ", total_num, "hash-base trie nodes")
log.Info("Pruning hash-base trie nodes", "complete progress", total_num)
}
default:
continue
}
}
log.Info("Pruning ", "Complete progress", total_num, "hash-base trie nodes")
log.Info("Pruning hash-base trie nodes", "complete progress", total_num)
return nil
}

Expand Down
59 changes: 32 additions & 27 deletions trie/hbss2pbss.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
type Hbss2Pbss struct {
trie *Trie // traverse trie
db *Database
blocknum uint64
blockNum uint64
root node // root of triedb
stateRootHash common.Hash
concurrentQueue chan struct{}
Expand All @@ -33,7 +33,7 @@ const (
)

// NewHbss2Pbss return a hash2Path obj
func NewHbss2Pbss(tr *Trie, db *Database, stateRootHash common.Hash, blocknum uint64, jobnum uint64) (*Hbss2Pbss, error) {
func NewHbss2Pbss(tr *Trie, db *Database, stateRootHash common.Hash, blockNum uint64, jobNum uint64) (*Hbss2Pbss, error) {
if tr == nil {
return nil, errors.New("trie is nil")
}
Expand All @@ -44,18 +44,18 @@ func NewHbss2Pbss(tr *Trie, db *Database, stateRootHash common.Hash, blocknum ui

ins := &Hbss2Pbss{
trie: tr,
blocknum: blocknum,
blockNum: blockNum,
db: db,
stateRootHash: stateRootHash,
root: tr.root,
concurrentQueue: make(chan struct{}, jobnum),
concurrentQueue: make(chan struct{}, jobNum),
wg: sync.WaitGroup{},
}

return ins, nil
}

func (t *Trie) resloveWithoutTrack(n node, prefix []byte) (node, error) {
func (t *Trie) resolveWithoutTrack(n node, prefix []byte) (node, error) {
if n, ok := n.(hashNode); ok {
blob, err := t.reader.node(prefix, common.BytesToHash(n))
if err != nil {
Expand All @@ -69,24 +69,27 @@ func (t *Trie) resloveWithoutTrack(n node, prefix []byte) (node, error) {
func (h2p *Hbss2Pbss) writeNode(pathKey []byte, n *trienode.Node, owner common.Hash) {
if owner == (common.Hash{}) {
rawdb.WriteAccountTrieNode(h2p.db.diskdb, pathKey, n.Blob)
log.Debug("WriteNodes account node, ", "path: ", common.Bytes2Hex(pathKey), "Hash: ", n.Hash, "BlobHash: ", crypto.Keccak256Hash(n.Blob))
log.Debug("Write account trie node", "path", common.Bytes2Hex(pathKey), "hash", n.Hash,
"blob hash", crypto.Keccak256Hash(n.Blob))
} else {
rawdb.WriteStorageTrieNode(h2p.db.diskdb, owner, pathKey, n.Blob)
log.Debug("WriteNodes storage node, ", "path: ", common.Bytes2Hex(pathKey), "owner: ", owner.String(), "Hash: ", n.Hash, "BlobHash: ", crypto.Keccak256Hash(n.Blob))
log.Debug("Write storage trie node", "path", common.Bytes2Hex(pathKey), "owner", owner.String(),
"hash", n.Hash, "blob hash", crypto.Keccak256Hash(n.Blob))
}
}

// Run statistics, external call
func (h2p *Hbss2Pbss) Run() {
log.Debug("Find Account Trie Tree, rootHash: ", h2p.trie.Hash().String(), "BlockNum: ", h2p.blocknum)
log.Debug("Find account trie tree", "rootHash", h2p.trie.Hash().String(), "block num", h2p.blockNum)

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

log.Info("Total complete: %v, go routines Num: %v, h2p concurrentQueue: %v\n", h2p.totalNum, runtime.NumGoroutine(), len(h2p.concurrentQueue))
log.Info("Hbss to pbss statistics", "total complete", h2p.totalNum, "go routines num", runtime.NumGoroutine(),
"h2p concurrent queue", len(h2p.concurrentQueue))

rawdb.WritePersistentStateID(h2p.db.diskdb, h2p.blocknum)
rawdb.WriteStateID(h2p.db.diskdb, h2p.stateRootHash, h2p.blocknum)
rawdb.WritePersistentStateID(h2p.db.diskdb, h2p.blockNum)
rawdb.WriteStateID(h2p.db.diskdb, h2p.stateRootHash, h2p.blockNum)
}

func (h2p *Hbss2Pbss) SubConcurrentTraversal(theTrie *Trie, theNode node, path []byte) {
Expand All @@ -97,7 +100,7 @@ func (h2p *Hbss2Pbss) SubConcurrentTraversal(theTrie *Trie, theNode node, path [
}

func (h2p *Hbss2Pbss) ConcurrentTraversal(theTrie *Trie, theNode node, path []byte) {
total_num := uint64(0)
totalNum := uint64(0)
// nil node
if theNode == nil {
return
Expand All @@ -118,10 +121,11 @@ func (h2p *Hbss2Pbss) ConcurrentTraversal(theTrie *Trie, theNode node, path []by
var hash, _ = collapsed.cache()
collapsed.Children = h2p.commitChildren(path, current)

nodebytes := nodeToBytes(collapsed)
if common.BytesToHash(hash) != common.BytesToHash(crypto.Keccak256(nodebytes)) {
log.Error("Hash is inconsistent, hash: ", common.BytesToHash(hash), "node hash: ", common.BytesToHash(crypto.Keccak256(nodebytes)), "node: ", collapsed.fstring(""))
panic("hash inconsistent.")
nodeBytes := nodeToBytes(collapsed)
if common.BytesToHash(hash) != common.BytesToHash(crypto.Keccak256(nodeBytes)) {
log.Error("Hash is inconsistent", "hash", common.BytesToHash(hash),
"node hash", common.BytesToHash(crypto.Keccak256(nodeBytes)), "node", collapsed.fstring(""))
panic("inconsistent hash")
}

h2p.writeNode(path, trienode.New(common.BytesToHash(hash), nodeToBytes(collapsed)), theTrie.owner)
Expand All @@ -141,43 +145,44 @@ func (h2p *Hbss2Pbss) ConcurrentTraversal(theTrie *Trie, theNode node, path []by
}
}
case hashNode:
n, err := theTrie.resloveWithoutTrack(current, path)
n, err := theTrie.resolveWithoutTrack(current, path)
if err != nil {
log.Error("Resolve HashNode", "error", err, "TrieRoot", theTrie.Hash(), "Path", path)
log.Error("Failed to resolve hash node", "error", err, "trie root", theTrie.Hash(), "path", path)
return
}
h2p.ConcurrentTraversal(theTrie, n, path)
total_num = atomic.AddUint64(&h2p.totalNum, 1)
if total_num%100000 == 0 {
log.Info("Converting ", "Complete progress", total_num, "go routines Num", runtime.NumGoroutine(), "h2p concurrentQueue", len(h2p.concurrentQueue))
totalNum = atomic.AddUint64(&h2p.totalNum, 1)
if totalNum%100000 == 0 {
log.Info("Converting", "complete progress", totalNum, "go routines num", runtime.NumGoroutine(),
"h2p concurrentQueue", len(h2p.concurrentQueue))
}
return
case valueNode:
if !hasTerm(path) {
log.Info("ValueNode miss path term", "path", common.Bytes2Hex(path))
log.Info("ValueNode misses path term", "path", common.Bytes2Hex(path))
break
}
var account types.StateAccount
if err := rlp.Decode(bytes.NewReader(current), &account); err != nil {
// log.Info("Rlp decode account failed.", "err", err)
// log.Error("Failed to decode rlp account", "err", err)
break
}
if account.Root == (common.Hash{}) || account.Root == types.EmptyRootHash {
// log.Info("Not a storage trie.", "account", common.BytesToHash(path).String())
// log.Info("Not a storage trie", "account", common.BytesToHash(path).String())
break
}

ownerAddress := common.BytesToHash(hexToCompact(path))
tr, err := New(StorageTrieID(h2p.stateRootHash, ownerAddress, account.Root), h2p.db)
if err != nil {
log.Error("New Storage trie error", "err", err, "root", account.Root.String(), "owner", ownerAddress.String())
log.Error("Failed to new Storage trie", "err", err, "root", account.Root.String(), "owner", ownerAddress.String())
break
}
log.Debug("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:
panic(errors.New("Invalid node type to traverse."))
panic(errors.New("invalid node type to traverse"))
}
}

Expand Down

0 comments on commit 20de6e1

Please sign in to comment.