Skip to content

Commit

Permalink
triedb/pathdb: support v0 journal format
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed Aug 21, 2024
1 parent 8ce3ddd commit 063b150
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions triedb/pathdb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
// - Version 0: initial version
// - Version 1: storage.Incomplete field is removed
const journalVersion uint64 = 1
const journalVersionV0 uint64 = 0

// journalNode represents a trie node persisted in the journal.
type journalNode struct {
Expand Down Expand Up @@ -75,6 +76,13 @@ type journalStorage struct {
Slots [][]byte
}

type journalStorageV0 struct {
Incomplete bool
Account common.Address
Hashes []common.Hash
Slots [][]byte
}

// loadJournal tries to parse the layer journal from the disk.
func (db *Database) loadJournal(diskRoot common.Hash) (layer, error) {
journal := rawdb.ReadTrieJournal(db.diskdb)
Expand All @@ -88,7 +96,7 @@ func (db *Database) loadJournal(diskRoot common.Hash) (layer, error) {
if err != nil {
return nil, errMissVersion
}
if version != journalVersion {
if version != 0 && version != journalVersion {
return nil, fmt.Errorf("%w want %d got %d", errUnexpectedVersion, journalVersion, version)
}
// Secondly, resolve the disk layer root, ensure it's continuous
Expand All @@ -109,7 +117,7 @@ func (db *Database) loadJournal(diskRoot common.Hash) (layer, error) {
return nil, err
}
// Load all the diff layers from the journal
head, err := db.loadDiffLayer(base, r)
head, err := db.loadDiffLayer(base, r, version)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -182,7 +190,7 @@ func (db *Database) loadDiskLayer(r *rlp.Stream) (layer, error) {

// loadDiffLayer reads the next sections of a layer journal, reconstructing a new
// diff and verifying that it can be linked to the requested parent.
func (db *Database) loadDiffLayer(parent layer, r *rlp.Stream) (layer, error) {
func (db *Database) loadDiffLayer(parent layer, r *rlp.Stream, layerJournalVersion uint64) (layer, error) {
// Read the next diff journal entry
var root common.Hash
if err := r.Decode(&root); err != nil {
Expand Down Expand Up @@ -226,8 +234,26 @@ func (db *Database) loadDiffLayer(parent layer, r *rlp.Stream) (layer, error) {
for i, addr := range jaccounts.Addresses {
accounts[addr] = jaccounts.Accounts[i]
}
if err := r.Decode(&jstorages); err != nil {
return nil, fmt.Errorf("load diff storages: %v", err)
if layerJournalVersion == journalVersion {
if err := r.Decode(&jstorages); err != nil {
return nil, fmt.Errorf("load diff storages: %v", err)
}
} else if layerJournalVersion == journalVersionV0 {
var jstoragesV0 []journalStorageV0
if err := r.Decode(&jstoragesV0); err != nil {
return nil, fmt.Errorf("load diff storages: %v", err)
}
for _, st := range jstoragesV0 {
if st.Incomplete { // Storage diff entries that are complete are compatible with the journal v1 type.
log.Warn("legacy v0 diff layer shows incomplete storage-diff write, cannot recover this, have to drop journal")
return nil, fmt.Errorf("legacy v0 diff layer with incomplete storage-diff: %w", errUnexpectedVersion)
}
jstorages = append(jstorages, journalStorage{
Account: st.Account,
Hashes: st.Hashes,
Slots: st.Slots,
})
}
}
for _, entry := range jstorages {
set := make(map[common.Hash][]byte)
Expand All @@ -240,7 +266,7 @@ func (db *Database) loadDiffLayer(parent layer, r *rlp.Stream) (layer, error) {
}
storages[entry.Account] = set
}
return db.loadDiffLayer(newDiffLayer(parent, root, parent.stateID()+1, block, nodes, triestate.New(accounts, storages)), r)
return db.loadDiffLayer(newDiffLayer(parent, root, parent.stateID()+1, block, nodes, triestate.New(accounts, storages)), r, layerJournalVersion)
}

// journal implements the layer interface, marshaling the un-flushed trie nodes
Expand Down

0 comments on commit 063b150

Please sign in to comment.