Skip to content

Commit

Permalink
include new state roots in zkinput
Browse files Browse the repository at this point in the history
  • Loading branch information
Intizar-T committed Dec 26, 2024
1 parent c02369a commit 71e8cb8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 45 deletions.
10 changes: 10 additions & 0 deletions sequencer/common/zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package common

import (
"math/big"

"github.com/iden3/go-merkletree"
)

// ZKInputs represents the inputs that will be used to generate the zkSNARK
Expand All @@ -14,6 +16,7 @@ type ZKInputs struct {
// inputs for final `hashGlobalInputs`
// OldLastIdx is the last index assigned to an account
OldLastIdx *uint32 `json:"oldLastIdx"` // uint32 (max nLevels bits)

// OldStateRoot is the current account merkle tree root
OldAccountRoot *big.Int `json:"oldAccountRoot"`

Expand All @@ -25,6 +28,13 @@ type ZKInputs struct {
// OldScoreRoot is the current score merkle tree root
OldScoreRoot *big.Int `json:"oldScoreRoot"`

// New raw values
NewLastIdxRaw uint32 `json:"newLastIdx"`
NewAccountRootRaw *merkletree.Hash
NewScoreRootRaw *merkletree.Hash
NewVouchRootRaw *merkletree.Hash
NewExitRootRaw *merkletree.Hash

// GlobalChainID is the blockchain ID (0 for Ethereum mainnet). This
// value can be get from the smart contract.
GlobalChainID *uint16 `json:"globalChainID"` // uint16
Expand Down
16 changes: 8 additions & 8 deletions sequencer/coordinator/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func (p *Pipeline) getErrAtBatchNum() common.BatchNum {
return p.errAtBatchNum
}

// handleForgeBatch waits for an available proof server, calls p.forgeBatch to
// forge the batch and get the zkInputs, and then sends the zkInputs to the
// handleForgeBatch calls p.forgeBatch to
// forge/create the batch and get the zkInputs, and then sends the zkInputs to the
// selected proof server so that the proof computation begins.

func (p *Pipeline) handleForgeBatch(
Expand Down Expand Up @@ -444,13 +444,13 @@ func (p *Pipeline) SetSyncStatsVars(
// TODO: discuss with the circuit team if new roots are necessary
func prepareForgeBatchArgs(batchInfo *BatchInfo) *eth.RollupForgeBatchArgs {
proof := batchInfo.Proof
// zki := batchInfo.ZKInputs
zki := batchInfo.ZKInputs
return &eth.RollupForgeBatchArgs{
// NewLastIdx: int64(zki.Metadata.NewLastIdxRaw),
// NewAccountRoot: zki.Metadata.NewStateRootRaw.BigInt(),
// NewVouchRoot: zki.Metadata.NewStateRootRaw.BigInt(),
// NewScoreRoot: zki.Metadata.NewStateRootRaw.BigInt(),
// NewExitRoot: zki.Metadata.NewExitRootRaw.BigInt(),
NewLastIdx: int64(zki.NewLastIdxRaw),
NewAccountRoot: zki.NewAccountRootRaw.BigInt(),
NewVouchRoot: zki.NewVouchRootRaw.BigInt(),
NewScoreRoot: zki.NewScoreRootRaw.BigInt(),
NewExitRoot: zki.NewExitRootRaw.BigInt(),
L1UserTxs: batchInfo.L1UserTxs,
L1CoordinatorTxsAuths: batchInfo.L1CoordinatorTxsAuths,
// Circuit selector
Expand Down
5 changes: 5 additions & 0 deletions sequencer/database/statedb/state_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,8 @@ func (l *LocalStateDB) Reset(batchNum common.BatchNum, fromSynchronizer bool) er
// use checkpoint from LocalStateDB
return l.StateDB.Reset(batchNum)
}

// CurrentIdx returns the current in-memory CurrentAccountIdx of the StateDB.db
func (s *StateDB) CurrentIdx() common.AccountIdx {
return s.db.CurrentAccountIdx
}
49 changes: 12 additions & 37 deletions sequencer/txprocessor/txprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,18 @@ func (txProcessor *TxProcessor) ProcessTxs(l1usertxs []common.L1Tx) (ptOut *Proc

if txProcessor.state.Type() == statedb.TypeBatchBuilder {
currentBatchValueNum := uint32(uint32(txProcessor.state.CurrentBatch()) + 1)
txProcessor.zki = common.NewZKInputs(txProcessor.config.ChainID, txProcessor.config.MaxTx, txProcessor.config.MaxL1Tx,
txProcessor.config.NLevels, &currentBatchValueNum)
txProcessor.zki = common.NewZKInputs(
txProcessor.config.ChainID,
txProcessor.config.MaxTx,
txProcessor.config.MaxL1Tx,
txProcessor.config.NLevels,
&currentBatchValueNum,
)
//For Accounts
oldLastIdxValue := uint32(txProcessor.state.CurrentAccountIdx())
txProcessor.zki.OldLastIdx = &(oldLastIdxValue)
txProcessor.zki.OldAccountRoot = txProcessor.state.GetMTRootAccount()
txProcessor.zki.NewLastIdxRaw = uint32(txProcessor.state.CurrentAccountIdx())

//For Vouches
txProcessor.zki.OldVouchRoot = txProcessor.state.GetMTRootVouch()
Expand Down Expand Up @@ -379,41 +385,6 @@ func (txProcessor *TxProcessor) ProcessTxs(l1usertxs []common.L1Tx) (ptOut *Proc
}
}

// Process L2Txs
// for i := 0; i < len(l2txs); i++ {
// exitIdx, exitAccount, newExit, err := txProcessor.ProcessL2Tx(exitTree, &l2txs[i])
// if err != nil {
// return nil, common.Wrap(err)
// }
// if txProcessor.zki != nil {
// if err != nil {
// return nil, common.Wrap(err)
// }

// // Intermediate States
// if txProcessor.txIndex < nTx-1 {
// //TODO: Fill out the OutIdx and StateRoot, need to check it's parameters

// // txProcessor.zki.ISOutIdx[txProcessor.txIndex] = txProcessor.state.CurrentIdx().BigInt()
// // txProcessor.zki.ISStateRoot[txProcessor.txIndex] = txProcessor.state.MT.Root().BigInt()
// if exitIdx == nil {
// txProcessor.zki.ISExitRoot[txProcessor.txIndex] = exitTree.Root().BigInt()
// }
// }
// }
// if txProcessor.state.Type() == statedb.TypeSynchronizer || txProcessor.state.Type() == statedb.TypeBatchBuilder {
// if exitIdx != nil && exitTree != nil && exitAccount != nil {
// exits[txProcessor.txIndex] = processedExit{
// exit: true,
// newExit: newExit,
// idx: *exitIdx,
// acc: *exitAccount,
// }
// }
// txProcessor.txIndex++
// }
// }

// if txProcessor.zki != nil {
// // Fill the empty slots in the ZKInputs remaining after
// // processing all L1 & L2 txs
Expand Down Expand Up @@ -490,6 +461,10 @@ func (txProcessor *TxProcessor) ProcessTxs(l1usertxs []common.L1Tx) (ptOut *Proc
// // compute last ZKInputs parameters
valueGlobalChain := uint16(txProcessor.config.ChainID)
txProcessor.zki.GlobalChainID = &(valueGlobalChain)
txProcessor.zki.NewAccountRootRaw = txProcessor.state.AccountTree.Root()
txProcessor.zki.NewVouchRootRaw = txProcessor.state.VouchTree.Root()
txProcessor.zki.NewScoreRootRaw = txProcessor.state.ScoreTree.Root()
txProcessor.zki.NewExitRootRaw = exitTree.Root()

// return ZKInputs as the BatchBuilder will return it to forge the Batch
return &ProcessTxOutput{
Expand Down

0 comments on commit 71e8cb8

Please sign in to comment.