Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check index for non increasing #1052

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions zk/datastream/server/data_stream_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer"
zktypes "github.com/ledgerwatch/erigon/zk/types"
"github.com/ledgerwatch/erigon/zk/utils"
"github.com/ledgerwatch/log/v3"

libcommon "github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/gateway-fm/cdk-erigon-lib/kv"
Expand All @@ -30,6 +31,7 @@ type DbReader interface {
GetInvalidBatch(batchNumber uint64) (bool, error)
GetBatchNoByL2Block(blockNumber uint64) (uint64, error)
CheckBatchNoByL2Block(l2BlockNo uint64) (uint64, bool, error)
GetPreviousIndexBlock(blockNumber uint64) (uint64, uint64, bool, error)
}

type BookmarkType byte
Expand Down Expand Up @@ -208,21 +210,12 @@ func createBlockWithBatchCheckStreamEntriesProto(
}
}

blockNum := block.NumberU64()

l1InfoTreeMinTimestamps := make(map[uint64]uint64)
deltaTimestamp := block.Time() - lastBlock.Time()
if blockNum == 1 {
deltaTimestamp = block.Time()
l1InfoTreeMinTimestamps[0] = 0
}

if blockEntries, err = createFullBlockStreamEntriesProto(reader, tx, block, block.Transactions(), forkId, deltaTimestamp, batchNumber, l1InfoTreeMinTimestamps); err != nil {
if blockEntries, err = createFullBlockStreamEntriesProto(reader, tx, block, lastBlock, block.Transactions(), forkId, batchNumber, make(map[uint64]uint64)); err != nil {
return nil, err
}

if blockEntries.Size() == 0 {
return nil, fmt.Errorf("didn't create any entries for block %d", blockNum)
return nil, fmt.Errorf("didn't create any entries for block %d", block.NumberU64())
}

entries := NewDataStreamEntries(len(endEntriesProto) + startEntriesProto.Size() + blockEntries.Size())
Expand All @@ -236,15 +229,21 @@ func createBlockWithBatchCheckStreamEntriesProto(
func createFullBlockStreamEntriesProto(
reader DbReader,
tx kv.Tx,
block *eritypes.Block,
block,
lastBlock *eritypes.Block,
filteredTransactions eritypes.Transactions,
forkId,
deltaTimestamp,
batchNumber uint64,
l1InfoTreeMinTimestamps map[uint64]uint64,
) (*DataStreamEntries, error) {
entries := NewDataStreamEntries(len(filteredTransactions) + 3) // block bookmark + block + block end
blockNum := block.NumberU64()
deltaTimestamp := block.Time() - lastBlock.Time()
if blockNum == 1 {
deltaTimestamp = block.Time()
l1InfoTreeMinTimestamps[0] = 0
}

entries := NewDataStreamEntries(len(filteredTransactions) + 3) // block bookmark + block + block end
// L2 BLOCK BOOKMARK
entries.Add(newL2BlockBookmarkEntryProto(blockNum))

Expand All @@ -263,6 +262,14 @@ func createFullBlockStreamEntriesProto(
}

if l1InfoIndex > 0 {
prevIndexBlock, prevIndex, found, err := reader.GetPreviousIndexBlock(blockNum)
if err != nil {
return nil, err
}
if found && prevIndex >= l1InfoIndex {
log.Warn("1 info index not bigger than previous index", "prevIndex", prevIndex, "prevBlock", prevIndexBlock, "l1InfoIndex", l1InfoIndex, "currentBlock", blockNum)
mandrigin marked this conversation as resolved.
Show resolved Hide resolved
}

// get the l1 info data, so we can add the min timestamp to the map
l1Info, err := reader.GetL1InfoTreeUpdate(l1InfoIndex)
if err != nil {
Expand Down Expand Up @@ -361,18 +368,12 @@ func BuildWholeBatchStreamEntriesProto(
for _, block := range blocks {
blockNum := block.NumberU64()

deltaTimestamp := block.Time() - lastBlock.Time()
if blockNum == 1 {
deltaTimestamp = block.Time()
l1InfoTreeMinTimestamps[0] = 0
}

txForBlock, found := txsPerBlock[blockNum]
if !found {
return nil, fmt.Errorf("no transactions array found for block %d", blockNum)
}

blockEntries, err := createFullBlockStreamEntriesProto(reader, tx, &block, txForBlock, forkId, deltaTimestamp, batchNumber, l1InfoTreeMinTimestamps)
blockEntries, err := createFullBlockStreamEntriesProto(reader, tx, &block, &lastBlock, txForBlock, forkId, batchNumber, l1InfoTreeMinTimestamps)
if err != nil {
return nil, err
}
Expand Down
9 changes: 1 addition & 8 deletions zk/datastream/server/datastream_populate.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,7 @@ func (srv *DataStreamServer) WriteBlockWithBatchStartToStream(
}
}

l1InfoTreeMinTimestamps := make(map[uint64]uint64)
deltaTimestamp := block.Time() - prevBlock.Time()
if blockNum == 1 {
deltaTimestamp = block.Time()
l1InfoTreeMinTimestamps[0] = 0
}

blockEntries, err := createFullBlockStreamEntriesProto(reader, tx, &block, block.Transactions(), forkId, deltaTimestamp, batchNum, make(map[uint64]uint64))
blockEntries, err := createFullBlockStreamEntriesProto(reader, tx, &block, &prevBlock, block.Transactions(), forkId, batchNum, make(map[uint64]uint64))
if err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,32 @@ func (db *HermezDbReader) GetBlockL1InfoTreeIndex(blockNumber uint64) (uint64, e
return BytesToUint64(v), nil
}

// gets the previous saved index and block for that index
// uses current inex block as parameter
func (db *HermezDbReader) GetPreviousIndexBlock(currentIndexBlockNumber uint64) (blockNum uint64, index uint64, found bool, err error) {
c, err := db.tx.Cursor(BLOCK_L1_INFO_TREE_INDEX)
if err != nil {
return
}
defer c.Close()

k, _, err := c.SeekExact(Uint64ToBytes(currentIndexBlockNumber))
if err != nil || k == nil {
return
}

k, v, err := c.Prev()
if err != nil || k == nil {
return
}

blockNum = BytesToUint64(k)
index = BytesToUint64(v)
found = true

return
}

func (db *HermezDb) WriteBlockL1InfoTreeIndexProgress(blockNumber uint64, l1Index uint64) error {
latestBlockNumber, latestL1Index, err := db.GetLatestBlockL1InfoTreeIndexProgress()
if err != nil {
Expand Down
Loading