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

implement wait for prover funcs #35

Merged
merged 2 commits into from
Jan 2, 2025
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
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
53 changes: 39 additions & 14 deletions sequencer/coordinator/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"database/sql"
"fmt"
"math/big"
"sync"
"time"
"tokamak-sybil-resistance/batchbuilder"
"tokamak-sybil-resistance/common"
"tokamak-sybil-resistance/coordinator/prover"
"tokamak-sybil-resistance/database/historydb"
"tokamak-sybil-resistance/eth"
"tokamak-sybil-resistance/log"
"tokamak-sybil-resistance/synchronizer"
"tokamak-sybil-resistance/txselector"
Expand Down Expand Up @@ -144,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 @@ -301,24 +303,23 @@ func (p *Pipeline) setErrAtBatchNum(batchNum common.BatchNum) {
p.errAtBatchNum = batchNum
}

// TODO: implement
// waitServerProof gets the generated zkProof & sends it to the SmartContract
func (p *Pipeline) waitServerProof(ctx context.Context, batchInfo *BatchInfo) error {
// TODO: implement prometheus metrics
// defer metric.MeasureDuration(metric.WaitServerProof, batchInfo.ProofStart,
// batchInfo.BatchNum.BigInt().String(), strconv.Itoa(batchInfo.PipelineNum))

// proof, pubInputs, err := batchInfo.ServerProof.GetProof(ctx) // blocking call,
// // until not resolved don't continue. Returns when the proof server has calculated the proof
// if err != nil {
// return common.Wrap(err)
// }
// batchInfo.Proof = proof
// batchInfo.PublicInputs = pubInputs
// batchInfo.ForgeBatchArgs = prepareForgeBatchArgs(batchInfo)
// batchInfo.Debug.Status = StatusProof
// p.cfg.debugBatchStore(batchInfo)
// log.Infow("Pipeline: batch proof calculated", "batch", batchInfo.BatchNum)
proof, pubInputs, err := batchInfo.ServerProof.GetProof(ctx) // blocking call,
// until not resolved don't continue. Returns when the proof server has calculated the proof
if err != nil {
return common.Wrap(err)
}
batchInfo.Proof = proof
batchInfo.PublicInputs = pubInputs
batchInfo.ForgeBatchArgs = prepareForgeBatchArgs(batchInfo)
batchInfo.Debug.Status = StatusProof
p.cfg.debugBatchStore(batchInfo)
log.Infow("Pipeline: batch proof calculated", "batch", batchInfo.BatchNum)
return nil
}

Expand Down Expand Up @@ -439,3 +440,27 @@ func (p *Pipeline) SetSyncStatsVars(
case <-ctx.Done():
}
}

// TODO: discuss with the circuit team if new roots are necessary
func prepareForgeBatchArgs(batchInfo *BatchInfo) *eth.RollupForgeBatchArgs {
proof := batchInfo.Proof
zki := batchInfo.ZKInputs
return &eth.RollupForgeBatchArgs{
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
VerifierIdx: batchInfo.VerifierIdx,
ProofA: [2]*big.Int{proof.PiA[0], proof.PiA[1]},
// Implementation of the verifier need a swap on the proofB vector
ProofB: [2][2]*big.Int{
{proof.PiB[0][1], proof.PiB[0][0]},
{proof.PiB[1][1], proof.PiB[1][0]},
},
ProofC: [2]*big.Int{proof.PiC[0], proof.PiC[1]},
}
}
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
Loading