From 6528148bfed371680b45767031d6c574b24b78d0 Mon Sep 17 00:00:00 2001 From: VM Date: Sun, 24 Nov 2024 15:26:57 +0800 Subject: [PATCH] feat: journal nodebufferlist snapshot data for journal file recovering --- core/rawdb/freezer.go | 2 +- triedb/pathdb/journal.go | 26 ++++---------------------- triedb/pathdb/nodebufferlist.go | 2 +- 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go index 54b911f1cb..ed0d582380 100644 --- a/core/rawdb/freezer.go +++ b/core/rawdb/freezer.go @@ -127,7 +127,7 @@ func NewFreezer(datadir string, namespace string, readonly, writeTrieNode bool, // Create the tables. for name, disableSnappy := range tables { if name == stateHistoryTrieNodesData && !writeTrieNode { - log.Info("Not create trie node data") + log.Info("Not create trie node data in freezer db") continue } table, err := newTable(datadir, name, readMeter, writeMeter, sizeGauge, maxTableSize, disableSnappy, readonly) diff --git a/triedb/pathdb/journal.go b/triedb/pathdb/journal.go index 5422c0978b..65d8faa81b 100644 --- a/triedb/pathdb/journal.go +++ b/triedb/pathdb/journal.go @@ -339,6 +339,8 @@ func (db *Database) loadDiskLayer(r *rlp.Stream, journalTypeForReader JournalTyp return nil, fmt.Errorf("invalid state id: stored %d resolved %d", stored, id) } + log.Info("load disk layer decode info", "stored id", stored, "root", root) + var nodes map[common.Hash]map[string]*trienode.Node var nodesArray []nblJournalData @@ -357,19 +359,7 @@ func (db *Database) loadDiskLayer(r *rlp.Stream, journalTypeForReader JournalTyp if err := journalBuf.Decode(&encoded); err != nil { return nil, fmt.Errorf("failed to load disk nodes: %v", err) } - nodes = make(map[common.Hash]map[string]*trienode.Node) - // todo: use flatten function - for _, entry := range encoded { - subset := make(map[string]*trienode.Node) - for _, n := range entry.Nodes { - if len(n.Blob) > 0 { - subset[string(n.Path)] = trienode.New(crypto.Keccak256Hash(n.Blob), n.Blob) - } else { - subset[string(n.Path)] = trienode.NewDeleted() - } - } - nodes[entry.Owner] = subset - } + nodes = flattenTrieNodes(encoded) } if journalTypeForReader == JournalFileType { @@ -531,15 +521,7 @@ func (dl *diskLayer) journal(w io.Writer, journalType JournalType) error { } log.Info("Journal file and node buffer list", "multi layer nodes count", len(nodes)) } else { - bufferNodes := dl.buffer.getAllNodes() - nodes := make([]journalNodes, 0, len(bufferNodes)) - for owner, subset := range bufferNodes { - entry := journalNodes{Owner: owner} - for path, node := range subset { - entry.Nodes = append(entry.Nodes, journalNode{Path: []byte(path), Blob: node.Blob}) - } - nodes = append(nodes, entry) - } + nodes := compressTrieNodes(dl.buffer.getAllNodes()) if err := rlp.Encode(journalBuf, nodes); err != nil { return err } diff --git a/triedb/pathdb/nodebufferlist.go b/triedb/pathdb/nodebufferlist.go index 7920662bcd..d37bdcd303 100644 --- a/triedb/pathdb/nodebufferlist.go +++ b/triedb/pathdb/nodebufferlist.go @@ -561,7 +561,7 @@ func (nf *nodebufferlist) recoverJournalData(nodesArray []nblJournalData) { mdl := newMultiDifflayer(nf.limit, nodesArray[i].size, nodesArray[i].root, flattenTrieNodes(nodesArray[i].nodes), nodesArray[i].layers) nf.pushFront(mdl) } - nf.count = uint64(length) + nf.count = uint64(length) - 1 log.Info("recover journal data", "nf count", nf.count, "node array", len(nodesArray)) }