Skip to content

Commit

Permalink
always use most recent info tree index for new blocks (#1429)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexoscott authored Nov 8, 2024
1 parent 48f7026 commit 41a3543
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 57 deletions.
14 changes: 7 additions & 7 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,32 +1255,32 @@ func (db *HermezDbReader) GetL1InfoTreeUpdate(idx uint64) (*types.L1InfoTreeUpda
return update, nil
}

func (db *HermezDbReader) GetLatestL1InfoTreeUpdate() (*types.L1InfoTreeUpdate, bool, error) {
func (db *HermezDbReader) GetLatestL1InfoTreeUpdate() (*types.L1InfoTreeUpdate, error) {
cursor, err := db.tx.Cursor(L1_INFO_TREE_UPDATES)
if err != nil {
return nil, false, err
return nil, err
}
defer cursor.Close()

count, err := cursor.Count()
if err != nil {
return nil, false, err
return nil, err
}
if count == 0 {
return nil, false, nil
return nil, nil
}

_, v, err := cursor.Last()
if err != nil {
return nil, false, err
return nil, err
}
if len(v) == 0 {
return nil, false, nil
return nil, nil
}

result := &types.L1InfoTreeUpdate{}
result.Unmarshall(v)
return result, true, nil
return result, nil
}

func (db *HermezDb) WriteBlockL1InfoTreeIndex(blockNumber uint64, l1Index uint64) error {
Expand Down
2 changes: 1 addition & 1 deletion zk/l1infotree/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (u *Updater) WarmUp(tx kv.RwTx) (err error) {

u.progress = progress

latestUpdate, _, err := hermezDb.GetLatestL1InfoTreeUpdate()
latestUpdate, err := hermezDb.GetLatestL1InfoTreeUpdate()
if err != nil {
return err
}
Expand Down
83 changes: 34 additions & 49 deletions zk/stages/stage_sequence_execute_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,66 +353,51 @@ func prepareTickers(cfg *SequenceBlockCfg) (*time.Ticker, *time.Ticker, *time.Ti
// will be called at the start of every new block created within a batch to figure out if there is a new GER
// we can use or not. In the special case that this is the first block we just return 0 as we need to use the
// 0 index first before we can use 1+
func calculateNextL1TreeUpdateToUse(lastInfoIndex uint64, hermezDb *hermez_db.HermezDb, proposedTimestamp uint64) (uint64, *zktypes.L1InfoTreeUpdate, error) {
func calculateNextL1TreeUpdateToUse(recentlyUsed uint64, hermezDb *hermez_db.HermezDb, proposedTimestamp uint64) (uint64, *zktypes.L1InfoTreeUpdate, error) {
// always default to 0 and only update this if the next available index has reached finality
var (
nextL1Index uint64 = 0
l1Info *zktypes.L1InfoTreeUpdate
err error
)
var nextL1Index uint64 = 0

if lastInfoIndex == 0 {
// potentially at the start of the chain so get the latest info tree index in the DB and work
// backwards until we find a valid one to use
l1Info, err = getNetworkStartInfoTreeIndex(hermezDb, proposedTimestamp)
if err != nil || l1Info == nil {
return 0, nil, err
}
nextL1Index = l1Info.Index
} else {
// check if the next index is there and if it has reached finality or not
l1Info, err = hermezDb.GetL1InfoTreeUpdate(lastInfoIndex + 1)
if err != nil {
return 0, nil, err
}
/*
get the progress of the chain so far, then get the latest available data for the next index.
If these values are the same info tree update we return 0 as no-change. If the next index is
higher we return that one so long as it is valid.
*/

// ensure that we are above the min timestamp for this index to use it
if l1Info != nil && l1Info.Timestamp <= proposedTimestamp {
nextL1Index = l1Info.Index
}
latestIndex, err := hermezDb.GetLatestL1InfoTreeUpdate()
if err != nil {
return 0, nil, err
}

return nextL1Index, l1Info, nil
}

func getNetworkStartInfoTreeIndex(hermezDb *hermez_db.HermezDb, proposedTimestamp uint64) (*zktypes.L1InfoTreeUpdate, error) {
l1Info, found, err := hermezDb.GetLatestL1InfoTreeUpdate()
if err != nil || !found || l1Info == nil {
return nil, err
if latestIndex == nil || latestIndex.Index <= recentlyUsed {
// no change
return 0, nil, nil
}

if l1Info.Timestamp > proposedTimestamp {
// not valid so move back one index - we need one less than or equal to the proposed timestamp
lastIndex := l1Info.Index
for lastIndex > 0 {
lastIndex = lastIndex - 1
l1Info, err = hermezDb.GetL1InfoTreeUpdate(lastIndex)
if err != nil {
return nil, err
}
if l1Info != nil && l1Info.Timestamp <= proposedTimestamp {
break
}
// now verify that the latest known index is valid and work backwards until we find one that is
// or, we reach the most recently used index or 0
for {
if latestIndex.Timestamp <= proposedTimestamp {
nextL1Index = latestIndex.Index
break
}

if latestIndex.Index == 0 || latestIndex.Index <= recentlyUsed {
// end of the line
return 0, nil, nil
}

latestIndex, err = hermezDb.GetL1InfoTreeUpdate(latestIndex.Index - 1)
if err != nil {
return 0, nil, err
}

if latestIndex == nil {
return 0, nil, nil
}
}

// final check that the l1Info is actually valid before returning, index 0 or 1 might be invalid for
// some strange reason so just use index 0 in this case - it is always safer to use a 0 index
if l1Info == nil || l1Info.Timestamp > proposedTimestamp {
return nil, nil
}

return l1Info, nil
return nextL1Index, latestIndex, nil
}

func updateSequencerProgress(tx kv.RwTx, newHeight uint64, newBatch uint64, unwinding bool) error {
Expand Down

0 comments on commit 41a3543

Please sign in to comment.