Skip to content

Commit

Permalink
Merge both fixes for l1infotree duplicates (#829)
Browse files Browse the repository at this point in the history
* merge both fixes for l1infotree-duplicates

* 1-time init of info tree
  • Loading branch information
kstoykov authored Jul 19, 2024
1 parent 4438e12 commit a2c8d8b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 72 deletions.
23 changes: 0 additions & 23 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hermez_db

import (
"bytes"
"errors"
"fmt"
"math"
Expand Down Expand Up @@ -1448,28 +1447,6 @@ func (db *HermezDb) WriteL1InfoTreeLeaf(l1Index uint64, leaf common.Hash) error
return db.tx.Put(L1_INFO_LEAVES, Uint64ToBytes(l1Index), leaf.Bytes())
}

func (db *HermezDbReader) IsL1InfoTreeLeafSaves(leaf common.Hash) (bool, error) {
c, err := db.tx.Cursor(L1_INFO_LEAVES)
if err != nil {
return false, err
}
defer c.Close()

leafBytes := leaf.Bytes()

for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return false, err
}

if bytes.Equal(v, leafBytes) {
return true, nil
}
}

return false, nil
}

func (db *HermezDbReader) GetAllL1InfoTreeLeaves() ([]common.Hash, error) {
c, err := db.tx.Cursor(L1_INFO_LEAVES)
if err != nil {
Expand Down
73 changes: 24 additions & 49 deletions zk/stages/stage_l1_info_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func SpawnL1InfoTreeStage(
progress = cfg.zkCfg.L1FirstBlock - 1
}

latestUpdate, found, err := hermezDb.GetLatestL1InfoTreeUpdate()
latestUpdate, _, err := hermezDb.GetLatestL1InfoTreeUpdate()
if err != nil {
return err
}
Expand Down Expand Up @@ -112,8 +112,10 @@ LOOP:
defer ticker.Stop()
processed := 0

var tree *l1infotree.L1InfoTree
treeInitialised := false
tree, err := initialiseL1InfoTree(hermezDb)
if err != nil {
return err
}

// process the logs in chunks
for _, chunk := range chunks {
Expand All @@ -129,17 +131,9 @@ LOOP:
}

for _, l := range chunk {
header := headersMap[l.BlockNumber]
switch l.Topics[0] {
case contracts.UpdateL1InfoTreeTopic:
if !treeInitialised {
tree, err = initialiseL1InfoTree(hermezDb)
if err != nil {
return err
}
treeInitialised = true
}

header := headersMap[l.BlockNumber]
if header == nil {
header, err = cfg.syncer.GetHeader(l.BlockNumber)
if err != nil {
Expand All @@ -148,61 +142,42 @@ LOOP:
}

tmpUpdate, err := CreateL1InfoTreeUpdate(l, header)

if err != nil {
return err
}

leafHash := l1infotree.HashLeafData(tmpUpdate.GER, tmpUpdate.ParentHash, tmpUpdate.Timestamp)

if tree.LeafExists(leafHash) {
log.Warn("Skipping log as L1 Info Tree leaf already exists", "hash", leafHash)
continue
}

if found {
if latestUpdate != nil {
tmpUpdate.Index = latestUpdate.Index + 1
} else {
tmpUpdate.Index = 0
}

found = true
} // if latestUpdate is nil then Index = 0 which is the default value so no need to set it
latestUpdate = tmpUpdate

err = HandleL1InfoTreeUpdate(hermezDb, latestUpdate)
newRoot, err := tree.AddLeaf(uint32(latestUpdate.Index), leafHash)
if err != nil {
return err
}

leafFoundInDb, err := hermezDb.IsL1InfoTreeLeafSaves(leafHash)
if err != nil {
log.Debug("New L1 Index",
"index", latestUpdate.Index,
"root", newRoot.String(),
"mainnet", latestUpdate.MainnetExitRoot.String(),
"rollup", latestUpdate.RollupExitRoot.String(),
"ger", latestUpdate.GER.String(),
"parent", latestUpdate.ParentHash.String(),
)

if err = HandleL1InfoTreeUpdate(hermezDb, latestUpdate); err != nil {
return err
}

if leafFoundInDb {
log.Warn("Leaf already saved in db", "index", latestUpdate.Index, "hash", leafHash)
} else {
if err = hermezDb.WriteL1InfoTreeLeaf(latestUpdate.Index, leafHash); err != nil {
return err
}

newRoot, err := tree.AddLeaf(uint32(latestUpdate.Index), leafHash)
if err != nil {
return err
}
log.Debug("New L1 Index",
"index", latestUpdate.Index,
"root", newRoot.String(),
"mainnet", latestUpdate.MainnetExitRoot.String(),
"rollup", latestUpdate.RollupExitRoot.String(),
"ger", latestUpdate.GER.String(),
"parent", latestUpdate.ParentHash.String(),
)

err = hermezDb.WriteL1InfoTreeRoot(common.BytesToHash(newRoot[:]), latestUpdate.Index)
if err != nil {
return err
}
if err = hermezDb.WriteL1InfoTreeLeaf(latestUpdate.Index, leafHash); err != nil {
return err
}
if err = hermezDb.WriteL1InfoTreeRoot(common.BytesToHash(newRoot[:]), latestUpdate.Index); err != nil {
return err
}

processed++
Expand Down

0 comments on commit a2c8d8b

Please sign in to comment.