diff --git a/sequencer/common/zk.go b/sequencer/common/zk.go index 92d38e1..5c89760 100644 --- a/sequencer/common/zk.go +++ b/sequencer/common/zk.go @@ -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 @@ -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"` @@ -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 diff --git a/sequencer/coordinator/pipeline.go b/sequencer/coordinator/pipeline.go index cdc92b2..adc4913 100644 --- a/sequencer/coordinator/pipeline.go +++ b/sequencer/coordinator/pipeline.go @@ -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( @@ -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 ð.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 diff --git a/sequencer/database/statedb/state_db.go b/sequencer/database/statedb/state_db.go index 402cdf4..3ef4328 100644 --- a/sequencer/database/statedb/state_db.go +++ b/sequencer/database/statedb/state_db.go @@ -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 +} diff --git a/sequencer/txprocessor/txprocessor.go b/sequencer/txprocessor/txprocessor.go index 043477c..3c3a3ae 100644 --- a/sequencer/txprocessor/txprocessor.go +++ b/sequencer/txprocessor/txprocessor.go @@ -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, ¤tBatchValueNum) + txProcessor.zki = common.NewZKInputs( + txProcessor.config.ChainID, + txProcessor.config.MaxTx, + txProcessor.config.MaxL1Tx, + txProcessor.config.NLevels, + ¤tBatchValueNum, + ) //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() @@ -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 @@ -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{