Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
revitteth committed Dec 4, 2024
1 parent d461203 commit f50d3e2
Show file tree
Hide file tree
Showing 13 changed files with 1,900 additions and 1,061 deletions.
8 changes: 4 additions & 4 deletions core/state/intra_block_state_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ type ReadOnlyHermezDb interface {
GetBlockL1BlockHash(l2BlockNo uint64) (libcommon.Hash, error)
GetIntermediateTxStateRoot(blockNum uint64, txhash libcommon.Hash) (libcommon.Hash, error)
GetReusedL1InfoTreeIndex(blockNum uint64) (bool, error)
GetSequenceByBatchNo(batchNo uint64) (*zktypes.L1BatchInfo, error)
GetSequenceByBatchNo(batchNo uint64) (*zktypes.BatchSequenceInfo, error)
GetHighestBlockInBatch(batchNo uint64) (uint64, bool, error)
GetSequenceByBatchNoOrHighest(batchNo uint64) (*zktypes.L1BatchInfo, error)
GetSequenceByBatchNoOrHighest(batchNo uint64) (*zktypes.BatchSequenceInfo, error)
GetLowestBlockInBatch(batchNo uint64) (uint64, bool, error)
GetL2BlockNosByBatch(batchNo uint64) ([]uint64, error)
GetBatchGlobalExitRoot(batchNum uint64) (*dstypes.GerUpdate, error)
GetVerificationByBatchNo(batchNo uint64) (*zktypes.L1BatchInfo, error)
GetVerificationByBatchNoOrHighest(batchNo uint64) (*zktypes.L1BatchInfo, error)
GetVerificationByBatchNo(batchNo uint64) (*zktypes.BatchVerificationInfo, error)
GetVerificationByBatchNoOrHighest(batchNo uint64) (*zktypes.BatchVerificationInfo, error)
GetL1BatchData(batchNumber uint64) ([]byte, error)
GetL1InfoTreeUpdateByGer(ger libcommon.Hash) (*zktypes.L1InfoTreeUpdate, error)
GetBlockL1InfoTreeIndex(blockNumber uint64) (uint64, error)
Expand Down
1 change: 0 additions & 1 deletion turbo/stages/zk_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func NewSequencerZkStages(ctx context.Context,

return zkStages.SequencerZkStages(ctx,
zkStages.StageL1SyncerCfg(db, l1Syncer, cfg.Zk),
zkStages.StageL1SequencerSyncCfg(db, cfg.Zk, sequencerStageSyncer),
zkStages.StageL1InfoTreeCfg(db, cfg.Zk, infoTreeUpdater),
zkStages.StageSequencerL1BlockSyncCfg(db, cfg.Zk, l1BlockSyncer),
zkStages.StageDataStreamCatchupCfg(dataStreamServer, db, cfg.Genesis.Config.ChainID.Uint64(), cfg.DatastreamVersion, cfg.HasExecutors()),
Expand Down
75 changes: 59 additions & 16 deletions zk/acc_input_hash/acc_input_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ import (
"github.com/ledgerwatch/erigon/core/systemcontracts"
eritypes "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/zk/utils"
"github.com/ledgerwatch/erigon/zkevm/log"
)

/*
NOTES:
We read the inforoot from the s/c in state.
Etrog (forkid 7):
- limitTs the timestamp of the l1 block when the batch was sequenced.
Elderberry (forkid 8):
- limitTs is the timestamp of the last block in the sequenced batch.
*/

const SpecialZeroHash = "0x27AE5BA08D7291C96C8CBDDCC148BF48A6D68C7974B94356F53754EF6171D757"

// BlockReader is a rawdb block reader abstraction for easier testing
Expand All @@ -31,6 +43,7 @@ type BatchDataReader interface {
type L1DataReader interface {
GetL1InfoTreeIndexToRoots() (map[uint64]common.Hash, error)
GetBlockL1InfoTreeIndex(blockNo uint64) (uint64, error)
GetHighestL1BlockTimestamp() (batchNo uint64, timestamp uint64, found bool, err error)
}

// AccInputHashReader combines the necessary reader interfaces
Expand All @@ -46,6 +59,7 @@ type AccInputHashCalculator interface {
}

type BaseCalc struct {
LogPrefix string
Ctx context.Context
Reader AccInputHashReader
Tx kv.Tx
Expand All @@ -55,12 +69,12 @@ type BaseCalc struct {
var Calculators = map[uint64]func(*BaseCalc) AccInputHashCalculator{
// pre 7 not supported
7: NewFork7Calculator, // etrog
8: NewFork7Calculator,
9: NewFork7Calculator,
10: NewFork7Calculator,
11: NewFork7Calculator,
12: NewFork7Calculator,
13: NewFork7Calculator,
8: NewFork8Calculator,
9: NewFork8Calculator,
10: NewFork8Calculator,
11: NewFork8Calculator,
12: NewFork8Calculator,
13: NewFork8Calculator,
}

func NewCalculator(ctx context.Context, tx kv.Tx, reader AccInputHashReader, forkID uint64) (AccInputHashCalculator, error) {
Expand All @@ -70,6 +84,7 @@ func NewCalculator(ctx context.Context, tx kv.Tx, reader AccInputHashReader, for
}

baseCalc := &BaseCalc{
LogPrefix: fmt.Sprintf("acc_input_hash fork: %d)", forkID),
Ctx: ctx,
Reader: reader,
Tx: tx,
Expand Down Expand Up @@ -147,18 +162,18 @@ func (f Fork7Calculator) Calculate(batchNum uint64) (common.Hash, error) {
}

// otherwise calculate it
accInputHash, err = LocalAccInputHashCalc(f.Ctx, f.Reader, f.BlockReader, f.Tx, batchNum, returnedBatchNo, accInputHash)
accInputHash, err = f.localAccInputHashCalc(batchNum, returnedBatchNo, accInputHash)
if err != nil {
return common.Hash{}, err
}

return accInputHash, nil
}

func LocalAccInputHashCalc(ctx context.Context, reader AccInputHashReader, blockReader BlockReader, tx kv.Tx, batchNum, startBatchNo uint64, prevAccInputHash common.Hash) (common.Hash, error) {
func (f Fork7Calculator) localAccInputHashCalc(batchNum, startBatchNo uint64, prevAccInputHash common.Hash) (common.Hash, error) {
var accInputHash common.Hash

infoTreeIndexes, err := reader.GetL1InfoTreeIndexToRoots()
infoTreeIndexes, err := f.Reader.GetL1InfoTreeIndexToRoots()
if err != nil {
return common.Hash{}, fmt.Errorf("failed to get l1 info tree indexes: %w", err)
}
Expand All @@ -174,19 +189,19 @@ func LocalAccInputHashCalc(ctx context.Context, reader AccInputHashReader, block
// TODO: handle batch 1 case where we should get check the aggregator code: https://github.com/0xPolygon/cdk/blob/develop/aggregator/aggregator.go#L1167

for i := startBatchNo; i <= batchNum; i++ {
currentForkId, err := reader.GetForkId(i)
currentForkId, err := f.Reader.GetForkId(i)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to get fork id for batch %d: %w", i, err)
}

batchBlockNos, err := reader.GetL2BlockNosByBatch(i)
batchBlockNos, err := f.Reader.GetL2BlockNosByBatch(i)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to get batch blocks for batch %d: %w", i, err)
}
batchBlocks := []*eritypes.Block{}
var coinbase common.Address
for in, blockNo := range batchBlockNos {
block, err := blockReader.ReadBlockByNumber(blockNo)
block, err := f.BlockReader.ReadBlockByNumber(blockNo)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to get block %d: %w", blockNo, err)
}
Expand All @@ -202,19 +217,19 @@ func LocalAccInputHashCalc(ctx context.Context, reader AccInputHashReader, block
lastBlockNoInPreviousBatch = firstBlockInBatch.NumberU64() - 1
}

lastBlockInPreviousBatch, err := blockReader.ReadBlockByNumber(lastBlockNoInPreviousBatch)
lastBlockInPreviousBatch, err := f.BlockReader.ReadBlockByNumber(lastBlockNoInPreviousBatch)
if err != nil {
return common.Hash{}, err
}

batchL2Data, err := utils.GenerateBatchDataFromDb(tx, reader, batchBlocks, lastBlockInPreviousBatch, currentForkId)
batchL2Data, err := utils.GenerateBatchDataFromDb(f.Tx, f.Reader, batchBlocks, lastBlockInPreviousBatch, currentForkId)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to generate batch data for batch %d: %w", i, err)
}

highestBlock := batchBlocks[len(batchBlocks)-1]

sr := state.NewPlainState(tx, highestBlock.NumberU64(), systemcontracts.SystemContractCodeLookup["hermez"])
sr := state.NewPlainState(f.Tx, highestBlock.NumberU64(), systemcontracts.SystemContractCodeLookup["hermez"])
if err != nil {
return common.Hash{}, fmt.Errorf("failed to get psr: %w", err)
}
Expand All @@ -225,7 +240,19 @@ func LocalAccInputHashCalc(ctx context.Context, reader AccInputHashReader, block
sr.Close()
l1InfoRoot := common.BytesToHash(l1InfoRootBytes)

limitTs := highestBlock.Time()
batchNo, limitTs, found, err := f.Reader.GetHighestL1BlockTimestamp()
if err != nil {
return common.Hash{}, fmt.Errorf("failed to get l1 block timestamp for batch %d: %w", i, err)
}
if !found {
// TODO: downrate this?
log.Warn(fmt.Sprintf("[%s] No l1 block timestamp found for batch %d", f.LogPrefix, i))

}

if batchNo != i {
log.Warn(fmt.Sprintf("[%s] L1 block timestamp batch no mismatch for batch %d", f.LogPrefix, i), "batchNo", batchNo, "i", i)
}

fmt.Println("[l1InfoRoot]", l1InfoRoot.Hex())
fmt.Println("[limitTs]", limitTs)
Expand All @@ -246,3 +273,19 @@ func LocalAccInputHashCalc(ctx context.Context, reader AccInputHashReader, block
}
return accInputHash, nil
}

type Fork8Calculator struct {
*BaseCalc
}

func NewFork8Calculator(bc *BaseCalc) AccInputHashCalculator {
return Fork8Calculator{BaseCalc: bc}
}

func (f Fork8Calculator) Calculate(batchNum uint64) (common.Hash, error) {
return common.Hash{}, nil
}

func (f Fork8Calculator) localAccInputHashCalc(batchNum, startBatchNo uint64, prevAccInputHash common.Hash) (common.Hash, error) {
return common.Hash{}, nil
}
Loading

0 comments on commit f50d3e2

Please sign in to comment.