From 593a477e6c086459cf7a25133dbeaa14d2253dfb Mon Sep 17 00:00:00 2001 From: arnaucube Date: Fri, 11 Dec 2020 18:58:22 +0100 Subject: [PATCH] ZKInputs generation ISFinalAccFee update - Update IntermediateState FinalAccFee values to fit in the Circom circuit expected inputs - Add ZKInputs generation test with transactions generated by Til (TestZKInputs6) - BatchBuilder: remove redundant MakeCheckpoint call --- batchbuilder/batchbuilder.go | 4 -- common/zk.go | 4 +- db/statedb/txprocessors.go | 97 +++++++++++++++------------------- db/statedb/zkinputsgen_test.go | 70 +++++++++++++++++++++++- 4 files changed, 113 insertions(+), 62 deletions(-) diff --git a/batchbuilder/batchbuilder.go b/batchbuilder/batchbuilder.go index 58968c6..5d39209 100644 --- a/batchbuilder/batchbuilder.go +++ b/batchbuilder/batchbuilder.go @@ -61,9 +61,5 @@ func (bb *BatchBuilder) BuildBatch(coordIdxs []common.Idx, configBatch *ConfigBa MaxL1Tx: 64, } ptOut, err := bb.localStateDB.ProcessTxs(ptc, coordIdxs, l1usertxs, l1coordinatortxs, pooll2txs) - if err != nil { - return nil, tracerr.Wrap(err) - } - err = bb.localStateDB.MakeCheckpoint() return ptOut.ZKInputs, tracerr.Wrap(err) } diff --git a/common/zk.go b/common/zk.go index 5073811..53c3d6f 100644 --- a/common/zk.go +++ b/common/zk.go @@ -221,7 +221,7 @@ type ZKInputs struct { ISInitStateRootFee *big.Int `json:"imInitStateRootFee"` // Hash // ISFinalAccFee final accumulated fees (before computing the fees-tx). // Contains the final values of the ISAccFeeOut parameter - ISFinalAccFee []*big.Int `json:"imFinalAccFee"` // big.Int, len: [maxFeeIdxs - 1] + ISFinalAccFee []*big.Int `json:"imFinalAccFee"` // big.Int, len: [maxFeeIdxs] } func bigIntsToStrings(v interface{}) interface{} { @@ -384,7 +384,7 @@ func NewZKInputs(nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32, currentNumBatc } zki.ISStateRootFee = newSlice(maxFeeIdxs - 1) zki.ISInitStateRootFee = big.NewInt(0) - zki.ISFinalAccFee = newSlice(maxFeeIdxs - 1) + zki.ISFinalAccFee = newSlice(maxFeeIdxs) return zki } diff --git a/db/statedb/txprocessors.go b/db/statedb/txprocessors.go index 941961c..06f1c81 100644 --- a/db/statedb/txprocessors.go +++ b/db/statedb/txprocessors.go @@ -191,6 +191,11 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use return nil, tracerr.Wrap(err) } s.zki.Metadata.L1TxsData = append(s.zki.Metadata.L1TxsData, l1TxData) + l1TxDataAvailability, err := l1coordinatortxs[i].BytesDataAvailability(s.zki.Metadata.NLevels) + if err != nil { + return nil, tracerr.Wrap(err) + } + s.zki.Metadata.L1TxsDataAvailability = append(s.zki.Metadata.L1TxsDataAvailability, l1TxDataAvailability) s.zki.ISOutIdx[s.i] = s.idx.BigInt() s.zki.ISStateRoot[s.i] = s.mt.Root().BigInt() @@ -222,7 +227,7 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use if s.zki != nil { // get the feePlanTokens - feePlanTokens, err := s.getFeePlanTokens(coordIdxs, l2txs) + feePlanTokens, err := s.getFeePlanTokens(coordIdxs) if err != nil { log.Error(err) return nil, tracerr.Wrap(err) @@ -252,9 +257,6 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use s.zki.ISExitRoot[s.i] = exitTree.Root().BigInt() } } - if s.i == nTx-1 { - s.zki.ISFinalAccFee = formatAccumulatedFees(collectedFees, s.zki.FeePlanTokens) - } } if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder { if exitIdx != nil && exitTree != nil { @@ -281,6 +283,8 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use s.zki.TxCompressedData[i] = new(big.Int).SetBytes(common.SignatureConstantBytes) } } + isFinalAccFee := formatAccumulatedFees(collectedFees, s.zki.FeePlanTokens) + copy(s.zki.ISFinalAccFee, isFinalAccFee) // before computing the Fees txs, set the ISInitStateRootFee s.zki.ISInitStateRootFee = s.mt.Root().BigInt() } @@ -289,34 +293,34 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use // Coordinator Idxs iFee := 0 for idx, accumulatedFee := range s.accumulatedFees { - // send the fee to the Idx of the Coordinator for the TokenID - accCoord, err := s.GetAccount(idx) - if err != nil { - log.Errorw("Can not distribute accumulated fees to coordinator account: No coord Idx to receive fee", "idx", idx) - return nil, tracerr.Wrap(err) - } - if s.zki != nil { - s.zki.TokenID3[iFee] = accCoord.TokenID.BigInt() - s.zki.Nonce3[iFee] = accCoord.Nonce.BigInt() - if babyjub.PointCoordSign(accCoord.PublicKey.X) { - s.zki.Sign3[iFee] = big.NewInt(1) + cmp := accumulatedFee.Cmp(big.NewInt(0)) + if cmp == 1 { // accumulatedFee>0 + // send the fee to the Idx of the Coordinator for the TokenID + accCoord, err := s.GetAccount(idx) + if err != nil { + log.Errorw("Can not distribute accumulated fees to coordinator account: No coord Idx to receive fee", "idx", idx) + return nil, tracerr.Wrap(err) + } + if s.zki != nil { + s.zki.TokenID3[iFee] = accCoord.TokenID.BigInt() + s.zki.Nonce3[iFee] = accCoord.Nonce.BigInt() + if babyjub.PointCoordSign(accCoord.PublicKey.X) { + s.zki.Sign3[iFee] = big.NewInt(1) + } + s.zki.Ay3[iFee] = accCoord.PublicKey.Y + s.zki.Balance3[iFee] = accCoord.Balance + s.zki.EthAddr3[iFee] = common.EthAddrToBigInt(accCoord.EthAddr) + } + accCoord.Balance = new(big.Int).Add(accCoord.Balance, accumulatedFee) + pFee, err := s.UpdateAccount(idx, accCoord) + if err != nil { + log.Error(err) + return nil, tracerr.Wrap(err) + } + if s.zki != nil { + s.zki.Siblings3[iFee] = siblingsToZKInputFormat(pFee.Siblings) + s.zki.ISStateRootFee[iFee] = s.mt.Root().BigInt() } - s.zki.Ay3[iFee] = accCoord.PublicKey.Y - s.zki.Balance3[iFee] = accCoord.Balance - s.zki.EthAddr3[iFee] = common.EthAddrToBigInt(accCoord.EthAddr) - - // add Coord Idx to ZKInputs.FeeTxsData - s.zki.FeeIdxs[iFee] = idx.BigInt() - } - accCoord.Balance = new(big.Int).Add(accCoord.Balance, accumulatedFee) - pFee, err := s.UpdateAccount(idx, accCoord) - if err != nil { - log.Error(err) - return nil, tracerr.Wrap(err) - } - if s.zki != nil { - s.zki.Siblings3[iFee] = siblingsToZKInputFormat(pFee.Siblings) - s.zki.ISStateRootFee[iFee] = s.mt.Root().BigInt() } iFee++ } @@ -324,6 +328,10 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use for i := len(s.accumulatedFees); i < int(ptc.MaxFeeTx)-1; i++ { s.zki.ISStateRootFee[i] = s.mt.Root().BigInt() } + // add Coord Idx to ZKInputs.FeeTxsData + for i := 0; i < len(coordIdxs); i++ { + s.zki.FeeIdxs[i] = coordIdxs[i].BigInt() + } } if s.typ == TypeTxSelector { @@ -368,7 +376,6 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use // compute last ZKInputs parameters s.zki.GlobalChainID = big.NewInt(0) // TODO, 0: ethereum, this will be get from config file - // zki.FeeIdxs = ? // TODO, this will be get from the config file s.zki.Metadata.NewStateRootRaw = s.mt.Root() s.zki.Metadata.NewExitRootRaw = exitTree.Root() @@ -384,35 +391,15 @@ func (s *StateDB) ProcessTxs(ptc ProcessTxsConfig, coordIdxs []common.Idx, l1use // getFeePlanTokens returns an array of *big.Int containing a list of tokenIDs // corresponding to the given CoordIdxs and the processed L2Txs -func (s *StateDB) getFeePlanTokens(coordIdxs []common.Idx, l2txs []common.PoolL2Tx) ([]*big.Int, error) { - // get Coordinator TokenIDs corresponding to the Idxs where the Fees - // will be sent - coordTokenIDs := make(map[common.TokenID]bool) +func (s *StateDB) getFeePlanTokens(coordIdxs []common.Idx) ([]*big.Int, error) { + var tBI []*big.Int for i := 0; i < len(coordIdxs); i++ { acc, err := s.GetAccount(coordIdxs[i]) if err != nil { log.Errorf("could not get account to determine TokenID of CoordIdx %d not found: %s", coordIdxs[i], err.Error()) return nil, tracerr.Wrap(err) } - coordTokenIDs[acc.TokenID] = true - } - - tokenIDs := make(map[common.TokenID]bool) - for i := 0; i < len(l2txs); i++ { - // as L2Tx does not have parameter TokenID, get it from the - // AccountsDB (in the StateDB) - acc, err := s.GetAccount(l2txs[i].FromIdx) - if err != nil { - log.Errorf("could not get account to determine TokenID of L2Tx: FromIdx %d not found: %s", l2txs[i].FromIdx, err.Error()) - return nil, tracerr.Wrap(err) - } - if _, ok := coordTokenIDs[acc.TokenID]; ok { - tokenIDs[acc.TokenID] = true - } - } - var tBI []*big.Int - for t := range tokenIDs { - tBI = append(tBI, t.BigInt()) + tBI = append(tBI, acc.TokenID.BigInt()) } return tBI, nil } diff --git a/db/statedb/zkinputsgen_test.go b/db/statedb/zkinputsgen_test.go index c412c19..aa3bfe1 100644 --- a/db/statedb/zkinputsgen_test.go +++ b/db/statedb/zkinputsgen_test.go @@ -12,6 +12,7 @@ import ( ethCommon "github.com/ethereum/go-ethereum/common" "github.com/hermeznetwork/hermez-node/common" + "github.com/hermeznetwork/hermez-node/log" "github.com/hermeznetwork/hermez-node/test/til" "github.com/iden3/go-iden3-crypto/babyjub" "github.com/stretchr/testify/assert" @@ -26,7 +27,7 @@ func printZKInputs(t *testing.T, zki *common.ZKInputs) { require.Nil(t, err) fmt.Println("\nCopy&Paste into js circom test:\n let zkInput = JSON.parse(`" + string(s) + "`);") - // fmt.Println("\nZKInputs json:\n echo '" + string(s) + "' | jq") + // fmt.Println("\nZKInputs json:\n echo '" + string(s) + "' | jq > ~/go.txt") fmt.Printf(` const output={ @@ -1001,3 +1002,70 @@ func TestZKInputs5(t *testing.T) { expected := `{"auxFromIdx":["256","257","0","0","0","0","0","0","0","0"],"auxToIdx":["0","0","0","0","0","0","0","0","0","0"],"ay1":["15238403086306505038849621710779816852318505119327426213168494964113886299863","4172448640254579435434214421479401747968866348490029667576411173067925161293","4172448640254579435434214421479401747968866348490029667576411173067925161293","15238403086306505038849621710779816852318505119327426213168494964113886299863","15238403086306505038849621710779816852318505119327426213168494964113886299863","0","0","0","0","0"],"ay2":["0","0","4172448640254579435434214421479401747968866348490029667576411173067925161293","4172448640254579435434214421479401747968866348490029667576411173067925161293","15238403086306505038849621710779816852318505119327426213168494964113886299863","0","0","0","0","0"],"ay3":["4172448640254579435434214421479401747968866348490029667576411173067925161293","0"],"balance1":["16000000","16000000","16000000","16000000","15998899","0","0","0","0","0"],"balance2":["0","0","1000","15999000","1000","0","0","0","0","0"],"balance3":["16000000","0"],"currentNumBatch":"1","ethAddr1":["721457446580647751014191829380889690493307935711","247512291986854564435551364600938690683113101007","247512291986854564435551364600938690683113101007","721457446580647751014191829380889690493307935711","721457446580647751014191829380889690493307935711","0","0","0","0","0"],"ethAddr2":["0","0","247512291986854564435551364600938690683113101007","247512291986854564435551364600938690683113101007","721457446580647751014191829380889690493307935711","0","0","0","0","0"],"ethAddr3":["247512291986854564435551364600938690683113101007","0"],"feeIdxs":["257","0"],"feePlanTokens":["1","0"],"fromBjjCompressed":[["1","1","1","0","1","0","1","1","0","1","1","0","0","0","1","0","0","1","0","0","0","0","0","1","1","1","1","1","0","0","1","0","1","0","1","1","1","1","1","0","0","1","0","1","0","0","0","0","1","0","1","0","0","0","1","1","0","0","1","0","0","0","0","0","0","1","0","1","0","0","1","0","1","1","1","0","1","0","1","0","1","1","1","1","1","1","0","0","1","0","0","0","1","0","1","0","0","1","0","0","1","1","0","1","0","0","0","1","1","1","1","0","0","0","1","1","0","1","0","1","1","0","1","0","1","1","0","1","1","0","1","1","0","1","1","0","0","1","0","0","0","0","0","1","1","1","0","1","1","1","0","0","0","0","1","1","0","1","1","1","1","0","0","1","1","1","0","0","1","0","1","0","1","0","1","0","1","0","1","1","1","0","0","0","1","0","1","1","1","0","0","0","1","1","0","1","1","1","1","0","1","1","1","0","1","1","1","1","1","1","1","0","1","1","0","0","1","1","0","1","0","0","0","1","0","0","0","1","0","1","1","0","1","0","0","0","0","1","0","1","0","0","0","0","1","1","0","1","1","0","0","0","0","1","0","0"],["1","0","1","1","0","0","1","0","1","0","1","0","0","0","0","0","1","1","0","0","0","0","1","1","1","1","1","0","0","0","0","0","0","0","0","0","0","0","1","0","0","0","1","1","0","0","0","0","1","0","1","0","0","1","1","0","1","0","0","1","1","1","1","0","1","1","1","1","1","0","1","0","0","1","0","0","0","0","0","0","1","1","0","1","1","0","1","1","0","1","1","0","1","0","0","1","1","0","0","0","1","1","0","1","0","0","0","1","1","1","0","1","0","0","1","1","1","0","0","0","0","0","0","0","0","1","1","0","0","0","0","1","1","1","0","0","1","1","1","1","0","1","1","0","1","0","1","0","1","0","1","1","0","1","1","1","1","1","0","0","0","0","1","0","1","0","0","1","1","0","1","1","1","1","1","0","1","0","1","1","1","0","0","1","1","1","1","1","1","1","0","0","0","0","1","0","1","1","1","0","1","1","1","1","1","0","0","1","1","0","1","1","1","1","0","0","1","0","0","1","1","0","0","1","1","0","0","0","1","1","0","1","1","0","1","0","0","0","0","1","1","0","0","1","1","1","0","0","1","0","0","1","0","0","0","0"],["1","0","1","1","0","0","1","0","1","0","1","0","0","0","0","0","1","1","0","0","0","0","1","1","1","1","1","0","0","0","0","0","0","0","0","0","0","0","1","0","0","0","1","1","0","0","0","0","1","0","1","0","0","1","1","0","1","0","0","1","1","1","1","0","1","1","1","1","1","0","1","0","0","1","0","0","0","0","0","0","1","1","0","1","1","0","1","1","0","1","1","0","1","0","0","1","1","0","0","0","1","1","0","1","0","0","0","1","1","1","0","1","0","0","1","1","1","0","0","0","0","0","0","0","0","1","1","0","0","0","0","1","1","1","0","0","1","1","1","1","0","1","1","0","1","0","1","0","1","0","1","1","0","1","1","1","1","1","0","0","0","0","1","0","1","0","0","1","1","0","1","1","1","1","1","0","1","0","1","1","1","0","0","1","1","1","1","1","1","1","0","0","0","0","1","0","1","1","1","0","1","1","1","1","1","0","0","1","1","0","1","1","1","1","0","0","1","0","0","1","1","0","0","1","1","0","0","0","1","1","0","1","1","0","1","0","0","0","0","1","1","0","0","1","1","1","0","0","1","0","0","1","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"fromEthAddr":["721457446580647751014191829380889690493307935711","247512291986854564435551364600938690683113101007","247512291986854564435551364600938690683113101007","0","0","0","0","0","0","0"],"fromIdx":["0","0","257","256","256","0","0","0","0","0"],"globalChainID":"0","imAccFeeOut":[["0","0"],["0","0"],["0","0"],["101","0"],["202","0"],["202","0"],["202","0"],["202","0"],["202","0"]],"imExitRoot":["0","0","15113551174181131002458155929083094803636342713620925500154902553188452967723","15113551174181131002458155929083094803636342713620925500154902553188452967723","13150113024143718339322897969767386088676980172693068463217180231955278216962","13150113024143718339322897969767386088676980172693068463217180231955278216962","13150113024143718339322897969767386088676980172693068463217180231955278216962","13150113024143718339322897969767386088676980172693068463217180231955278216962","13150113024143718339322897969767386088676980172693068463217180231955278216962"],"imFinalAccFee":["202","0"],"imInitStateRootFee":"9613727628923763144036402071154968334137401166500556370159010076062685768496","imOnChain":["1","1","1","0","0","0","0","0","0"],"imOutIdx":["256","257","257","257","257","257","257","257","257"],"imStateRoot":["2999178063326948609414231200730958862089790119006655219527433501846141543551","13160175861809095962915811919507877524206523306071085047160493107056995190544","18329209406553022761679227986529674784516469819376621817750821323774238711751","3690178825271720012663633183291856847981843295851769874179245020746738552351","9613727628923763144036402071154968334137401166500556370159010076062685768496","9613727628923763144036402071154968334137401166500556370159010076062685768496","9613727628923763144036402071154968334137401166500556370159010076062685768496","9613727628923763144036402071154968334137401166500556370159010076062685768496","9613727628923763144036402071154968334137401166500556370159010076062685768496"],"imStateRootFee":["9004936174112171168716185012590576631374182232656264130522697453639057968430"],"isOld0_1":["1","0","0","0","0","0","0","0","0","0"],"isOld0_2":["0","0","1","0","0","0","0","0","0","0"],"loadAmountF":["10400","10400","0","0","0","0","0","0","0","0"],"maxNumBatch":["0","0","0","0","0","0","0","0","0","0"],"newAccount":["1","1","0","0","0","0","0","0","0","0"],"newExit":["0","0","1","0","1","0","0","0","0","0"],"nonce1":["0","0","0","0","1","0","0","0","0","0"],"nonce2":["0","0","0","0","0","0","0","0","0","0"],"nonce3":["0","0"],"oldKey1":["0","256","0","0","0","0","0","0","0","0"],"oldKey2":["0","0","0","0","257","0","0","0","0","0"],"oldLastIdx":"255","oldStateRoot":"0","oldValue1":["0","9733782510199048326382833205201407219982604211594942097825192094127807440165","0","0","0","0","0","0","0","0"],"oldValue2":["0","0","0","0","1321905978366742801526079282634745616644967252474273161740186384129043490004","0","0","0","0","0"],"onChain":["1","1","1","0","0","0","0","0","0","0"],"r8x":["0","0","0","21866069362303334754705967992284812681414720313777842941818990564691665974706","10385899866949013738446141130588224389448419060831450943717283733270769541673","0","0","0","0","0"],"r8y":["0","0","0","8161164862399339301953140132511699534761447837662845009028853083383916740840","13656260309192947110726378285601635254714377815750000021119696180600460145845","0","0","0","0","0"],"rqOffset":["0","0","0","0","0","0","0","0","0","0"],"rqToBjjAy":["0","0","0","0","0","0","0","0","0","0"],"rqToEthAddr":["0","0","0","0","0","0","0","0","0","0"],"rqTxCompressedDataV2":["0","0","0","0","0","0","0","0","0","0"],"s":["0","0","0","917203591853393674451581842743986504926217885723848085172949612934849937782","1578713319885150969153996052434970232464211993679257852379270056501708806302","0","0","0","0","0"],"siblings1":[["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["2999178063326948609414231200730958862089790119006655219527433501846141543551","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["11279990388494833286198556001199775823962972503915172009203378468347881508894","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["6975841694765113541634698345295957238501610055097872059913911260522365532165","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"siblings2":[["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["18206494837933532633746175540777421367331523385545468990813257189787137445167","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"siblings3":[["18217919661438562246423754072112686562705548970555156767842923363070658026697","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"sign1":["0","0","0","0","0","0","0","0","0","0"],"sign2":["0","0","0","0","0","0","0","0","0","0"],"sign3":["0","0"],"toBjjAy":["0","0","0","0","0","0","0","0","0","0"],"toEthAddr":["0","0","0","0","0","0","0","0","0","0"],"toIdx":["0","0","1","257","1","0","0","0","0","0"],"tokenID1":["1","1","1","1","1","0","0","0","0","0"],"tokenID2":["0","0","1","1","1","0","0","0","0","0"],"tokenID3":["1","0"],"txCompressedData":["1461501637330902918203684832716283019659255211535","1461501637330902918203684832716283019659255211535","1483802382529433541424448713503268057827794216463","869620039695611037216780722449287736442401437351785860888678352267503119","869620039695617314318516109130051572231804362608598311573698869050729999","3322668559","3322668559","3322668559","3322668559","3322668559"],"txCompressedDataV2":["0","0","0","3089511010385631938450432878260044363267416533431436181760","3089511010385654239195631408883185898985617124198904234240","0","0","0","0","0"]}` assert.Equal(t, expected, string(s)) } + +// TestZKInputs6: +// Tests ZKInputs generated by the batches generated by +// til.SetBlockchainMinimumFlow0 +func TestZKInputs6(t *testing.T) { + dir, err := ioutil.TempDir("", "tmpdb") + require.Nil(t, err) + defer assert.Nil(t, os.RemoveAll(dir)) + + nLevels := 16 + + sdb, err := NewStateDB(dir, TypeBatchBuilder, nLevels) + assert.Nil(t, err) + + // Coordinator Idx where to send the fees + // coordIdxs := []common.Idx{256, 257} + + ptc := ProcessTxsConfig{ + NLevels: uint32(nLevels), + MaxTx: 10, + MaxL1Tx: 4, + MaxFeeTx: 4, + } + + tc := til.NewContext(common.RollupConstMaxL1UserTx) + blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0) + require.Nil(t, err) + + log.Debug("block:0 batch:0, only L1CoordinatorTxs") + ptOut, err := sdb.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil) + require.Nil(t, err) + + assert.Equal(t, "9039235803989265562752459273677612535578150724983094202749787856042851287937", sdb.mt.Root().BigInt().String()) + assert.Equal(t, "0", ptOut.ZKInputs.Metadata.NewExitRootRaw.BigInt().String()) + h, err := ptOut.ZKInputs.HashGlobalData() + require.Nil(t, err) + assert.Equal(t, "16379429180374022967705349031545993941940235797391087559198349725707777217313", h.String()) + + // printZKInputs(t, ptOut.ZKInputs) + + log.Debug("block:0 batch:1") + l1UserTxs := []common.L1Tx{} + l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs) + ptOut, err = sdb.ProcessTxs(ptc, nil, l1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs) + require.Nil(t, err) + + assert.Equal(t, "11268490488303545450371226436237399651863451560820293060171443690124510027423", sdb.mt.Root().BigInt().String()) + assert.Equal(t, "0", ptOut.ZKInputs.Metadata.NewExitRootRaw.BigInt().String()) + h, err = ptOut.ZKInputs.HashGlobalData() + require.Nil(t, err) + assert.Equal(t, "7929589021941867224637424679829482351183189155476180469293857163025959492111", h.String()) + + // printZKInputs(t, ptOut.ZKInputs) + + // log.Debug("block:0 batch:2") + l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[2].Batch.ForgeL1TxsNum]) + l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs) + fmt.Println(l1UserTxs) + ptOut, err = sdb.ProcessTxs(ptc, nil, l1UserTxs, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs) + require.Nil(t, err) + assert.Equal(t, "4506051426679555819811005692198685182747763336038770877076710632305611650930", sdb.mt.Root().BigInt().String()) + h, err = ptOut.ZKInputs.HashGlobalData() + require.Nil(t, err) + assert.Equal(t, "212658139389894676556504584698627643826403691212271531527535357720589015657", h.String()) + + // printZKInputs(t, ptOut.ZKInputs) +}