diff --git a/common/l2tx.go b/common/l2tx.go index b228640..bb83f3d 100644 --- a/common/l2tx.go +++ b/common/l2tx.go @@ -124,6 +124,15 @@ func L2TxsToPoolL2Txs(txs []L2Tx) []PoolL2Tx { return r } +// TxIDsFromL2Txs returns an array of TxID from the []L2Tx +func TxIDsFromL2Txs(txs []L2Tx) []TxID { + txIDs := make([]TxID, len(txs)) + for i, tx := range txs { + txIDs[i] = tx.TxID + } + return txIDs +} + // BytesDataAvailability encodes a L2Tx into []byte for the Data Availability func (tx L2Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) { idxLen := nLevels / 8 //nolint:gomnd diff --git a/common/pooll2tx.go b/common/pooll2tx.go index 7c7312f..a3f794b 100644 --- a/common/pooll2tx.go +++ b/common/pooll2tx.go @@ -345,6 +345,15 @@ func PoolL2TxsToL2Txs(txs []PoolL2Tx) ([]L2Tx, error) { return l2Txs, nil } +// TxIDsFromPoolL2Txs returns an array of TxID from the []PoolL2Tx +func TxIDsFromPoolL2Txs(txs []PoolL2Tx) []TxID { + txIDs := make([]TxID, len(txs)) + for i, tx := range txs { + txIDs[i] = tx.TxID + } + return txIDs +} + // PoolL2TxState is a string that represents the status of a L2 transaction type PoolL2TxState string diff --git a/coordinator/coordinator.go b/coordinator/coordinator.go index cbbbd6c..69b2888 100644 --- a/coordinator/coordinator.go +++ b/coordinator/coordinator.go @@ -467,7 +467,7 @@ func (t *TxManager) rollupForgeBatch(ctx context.Context, batchInfo *BatchInfo) batchInfo.EthTx = ethTx log.Infow("TxManager ethClient.RollupForgeBatch", "batch", batchInfo.BatchNum, "tx", ethTx.Hash().Hex()) t.cfg.debugBatchStore(batchInfo) - if err := t.l2DB.DoneForging(l2TxsIDs(batchInfo.L2Txs), batchInfo.BatchNum); err != nil { + if err := t.l2DB.DoneForging(common.TxIDsFromL2Txs(batchInfo.L2Txs), batchInfo.BatchNum); err != nil { return tracerr.Wrap(err) } return nil @@ -776,22 +776,6 @@ func (p *Pipeline) Stop(ctx context.Context) { } } -func poolL2TxsIDs(txs []common.PoolL2Tx) []common.TxID { - txIDs := make([]common.TxID, len(txs)) - for i, tx := range txs { - txIDs[i] = tx.TxID - } - return txIDs -} - -func l2TxsIDs(txs []common.L2Tx) []common.TxID { - txIDs := make([]common.TxID, len(txs)) - for i, tx := range txs { - txIDs[i] = tx.TxID - } - return txIDs -} - // sendServerProof sends the circuit inputs to the proof server func (p *Pipeline) sendServerProof(ctx context.Context, batchInfo *BatchInfo) error { p.cfg.debugBatchStore(batchInfo) @@ -861,7 +845,7 @@ func (p *Pipeline) forgeBatch(batchNum common.BatchNum) (*BatchInfo, error) { batchInfo.CoordIdxs = coordIdxs batchInfo.VerifierIdx = p.cfg.VerifierIdx - if err := p.l2DB.StartForging(poolL2TxsIDs(poolL2Txs), batchInfo.BatchNum); err != nil { + if err := p.l2DB.StartForging(common.TxIDsFromPoolL2Txs(poolL2Txs), batchInfo.BatchNum); err != nil { return nil, tracerr.Wrap(err) } diff --git a/test/til/sets.go b/test/til/sets.go index e977206..310cd89 100644 --- a/test/til/sets.go +++ b/test/til/sets.go @@ -215,19 +215,14 @@ Type: Blockchain AddToken(1) -// Coordinator accounts, Idxs: 256, 257 -CreateAccountCoordinator(0) Coord -CreateAccountCoordinator(1) Coord - // close Block:0, Batch:0 -> batch // forge L1Coord{2} +> batch CreateAccountDeposit(0) A: 500 CreateAccountDeposit(1) C: 0 -CreateAccountCoordinator(0) C // close Block:0, Batch:1 -> batchL1 // freeze L1User{2}, forge L1Coord{1} +> batchL1 // freeze L1User{2}, forge L1Coord{0} // Expected balances: // Coord(0): 0, Coord(1): 0 // C(0): 0 @@ -263,13 +258,19 @@ CreateAccountDeposit(0) D: 800 // B(0): 400 // C(0): 0 +// Coordinator creates needed accounts to receive Fees +CreateAccountCoordinator(1) Coord +CreateAccountCoordinator(0) Coord +// Coordinator creates needed 'To' accounts for the L2Txs CreateAccountCoordinator(1) B +CreateAccountCoordinator(0) C + Transfer(1) A-B: 200 (126) Transfer(0) B-C: 100 (126) // close Block:0, Batch:6 -> batchL1 // forge L1User{1}, forge L1Coord{2}, forge L2{2} +> batchL1 // forge L1User{1}, forge L1Coord{4}, forge L2{2} // Expected balances: // Coord(0): 10, Coord(1): 20 // A(0): 600, A(1): 280 diff --git a/test/til/txs.go b/test/til/txs.go index afb65fc..3d63ee7 100644 --- a/test/til/txs.go +++ b/test/til/txs.go @@ -123,6 +123,7 @@ type Account struct { type User struct { Name string BJJ *babyjub.PrivateKey + EthSk *ecdsa.PrivateKey Addr ethCommon.Address Accounts map[common.TokenID]*Account } @@ -696,6 +697,7 @@ func (tc *Context) generateKeys(userNames []string) { u := User{ Name: userNames[i-1], BJJ: &sk, + EthSk: &key, Addr: addr, Accounts: make(map[common.TokenID]*Account), } diff --git a/txprocessor/txprocessor.go b/txprocessor/txprocessor.go index c18a771..9a37cf0 100644 --- a/txprocessor/txprocessor.go +++ b/txprocessor/txprocessor.go @@ -65,6 +65,11 @@ func NewTxProcessor(sdb *statedb.StateDB, config Config) *TxProcessor { } } +// StateDB returns a pointer to the StateDB of the TxProcessor +func (tp *TxProcessor) StateDB() *statedb.StateDB { + return tp.s +} + func (tp *TxProcessor) resetZKInputs() { tp.zki = nil tp.i = 0 // initialize current transaction index in the ZKInputs generation @@ -566,7 +571,12 @@ func (tp *TxProcessor) ProcessL2Tx(coordIdxsMap map[common.TokenID]common.Idx, return nil, nil, false, tracerr.Wrap(fmt.Errorf("In StateDB with Synchronizer mode L2.ToIdx can't be 0")) } // case when tx.Type== common.TxTypeTransferToEthAddr or common.TxTypeTransferToBJJ - tx.AuxToIdx, err = tp.s.GetIdxByEthAddrBJJ(tx.ToEthAddr, tx.ToBJJ, tx.TokenID) + + accSender, err := tp.s.GetAccount(tx.FromIdx) + if err != nil { + return nil, nil, false, tracerr.Wrap(err) + } + tx.AuxToIdx, err = tp.s.GetIdxByEthAddrBJJ(tx.ToEthAddr, tx.ToBJJ, accSender.TokenID) if err != nil { return nil, nil, false, tracerr.Wrap(err) } @@ -782,8 +792,7 @@ func (tp *TxProcessor) applyDeposit(tx *common.L1Tx, transfer bool) error { // the receiver. This parameter is used when the tx.ToIdx is not specified and // the real ToIdx is found trhrough the ToEthAddr or ToBJJ. func (tp *TxProcessor) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx, - collectedFees map[common.TokenID]*big.Int, - tx common.Tx, auxToIdx common.Idx) error { + collectedFees map[common.TokenID]*big.Int, tx common.Tx, auxToIdx common.Idx) error { if auxToIdx == common.Idx(0) { auxToIdx = tx.ToIdx } @@ -858,7 +867,7 @@ func (tp *TxProcessor) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx, } else { accReceiver, err = tp.s.GetAccount(auxToIdx) if err != nil { - log.Error(err) + log.Error(err, auxToIdx) return tracerr.Wrap(err) } } @@ -1020,6 +1029,7 @@ func (tp *TxProcessor) applyExit(coordIdxsMap map[common.TokenID]common.Idx, if err != nil { return nil, false, tracerr.Wrap(fmt.Errorf("Can not use CoordIdx that does not exist in the tree. TokenID: %d, CoordIdx: %d", acc.TokenID, coordIdxsMap[acc.TokenID])) } + // accumulate the fee for the Coord account accumulated := tp.AccumulatedFees[accCoord.Idx] accumulated.Add(accumulated, fee) diff --git a/txprocessor/txprocessor_test.go b/txprocessor/txprocessor_test.go index 0abd7d8..b4b0420 100644 --- a/txprocessor/txprocessor_test.go +++ b/txprocessor/txprocessor_test.go @@ -214,8 +214,6 @@ func TestProcessTxsBalances(t *testing.T) { blocks, err := tc.GenerateBlocks(til.SetBlockchainMinimumFlow0) require.NoError(t, err) - // Coordinator Idx where to send the fees - coordIdxs := []common.Idx{256, 257} config := Config{ NLevels: 32, MaxFeeTx: 64, @@ -232,20 +230,20 @@ func TestProcessTxsBalances(t *testing.T) { log.Debug("block:0 batch:1") l1UserTxs := []common.L1Tx{} l2Txs := common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[1].L2Txs) - _, err = tp.ProcessTxs(coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs) + _, err = tp.ProcessTxs(nil, l1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs) require.NoError(t, err) 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) - _, err = tp.ProcessTxs(coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs) + _, err = tp.ProcessTxs(nil, l1UserTxs, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs) require.NoError(t, err) checkBalance(t, tc, sdb, "A", 0, "500") log.Debug("block:0 batch:3") l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[3].Batch.ForgeL1TxsNum]) l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[3].L2Txs) - _, err = tp.ProcessTxs(coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[3].L1CoordinatorTxs, l2Txs) + _, err = tp.ProcessTxs(nil, l1UserTxs, blocks[0].Rollup.Batches[3].L1CoordinatorTxs, l2Txs) require.NoError(t, err) checkBalance(t, tc, sdb, "A", 0, "500") checkBalance(t, tc, sdb, "A", 1, "500") @@ -253,7 +251,7 @@ func TestProcessTxsBalances(t *testing.T) { log.Debug("block:0 batch:4") l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[4].Batch.ForgeL1TxsNum]) l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[4].L2Txs) - _, err = tp.ProcessTxs(coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[4].L1CoordinatorTxs, l2Txs) + _, err = tp.ProcessTxs(nil, l1UserTxs, blocks[0].Rollup.Batches[4].L1CoordinatorTxs, l2Txs) require.NoError(t, err) checkBalance(t, tc, sdb, "A", 0, "500") checkBalance(t, tc, sdb, "A", 1, "500") @@ -261,12 +259,13 @@ func TestProcessTxsBalances(t *testing.T) { log.Debug("block:0 batch:5") l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[5].Batch.ForgeL1TxsNum]) l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[5].L2Txs) - _, err = tp.ProcessTxs(coordIdxs, l1UserTxs, blocks[0].Rollup.Batches[5].L1CoordinatorTxs, l2Txs) + _, err = tp.ProcessTxs(nil, l1UserTxs, blocks[0].Rollup.Batches[5].L1CoordinatorTxs, l2Txs) require.NoError(t, err) checkBalance(t, tc, sdb, "A", 0, "600") checkBalance(t, tc, sdb, "A", 1, "500") checkBalance(t, tc, sdb, "B", 0, "400") + coordIdxs := []common.Idx{261, 262} log.Debug("block:0 batch:6") l1UserTxs = til.L1TxsToCommonL1Txs(tc.Queues[*blocks[0].Rollup.Batches[6].Batch.ForgeL1TxsNum]) l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[6].L2Txs) diff --git a/txprocessor/zkinputsgen_test.go b/txprocessor/zkinputsgen_test.go index 604e61d..56c87a6 100644 --- a/txprocessor/zkinputsgen_test.go +++ b/txprocessor/zkinputsgen_test.go @@ -1212,11 +1212,11 @@ func TestZKInputs6(t *testing.T) { ptOut, err := tp.ProcessTxs(nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil) require.NoError(t, err) - assert.Equal(t, "9039235803989265562752459273677612535578150724983094202749787856042851287937", sdb.MT.Root().BigInt().String()) + assert.Equal(t, "0", sdb.MT.Root().BigInt().String()) assert.Equal(t, "0", ptOut.ZKInputs.Metadata.NewExitRootRaw.BigInt().String()) h, err := ptOut.ZKInputs.HashGlobalData() require.NoError(t, err) - assert.Equal(t, "16379429180374022967705349031545993941940235797391087559198349725707777217313", h.String()) + assert.Equal(t, "11185464138041166840819960504404439577014916009324100031008662249284619863031", h.String()) // printZKInputs(t, ptOut.ZKInputs) @@ -1226,11 +1226,11 @@ func TestZKInputs6(t *testing.T) { ptOut, err = tp.ProcessTxs(nil, l1UserTxs, blocks[0].Rollup.Batches[1].L1CoordinatorTxs, l2Txs) require.NoError(t, err) - assert.Equal(t, "11268490488303545450371226436237399651863451560820293060171443690124510027423", sdb.MT.Root().BigInt().String()) + assert.Equal(t, "0", sdb.MT.Root().BigInt().String()) assert.Equal(t, "0", ptOut.ZKInputs.Metadata.NewExitRootRaw.BigInt().String()) h, err = ptOut.ZKInputs.HashGlobalData() require.NoError(t, err) - assert.Equal(t, "7929589021941867224637424679829482351183189155476180469293857163025959492111", h.String()) + assert.Equal(t, "12631863710217571237457816324742333499903148838621785764212585803181094983889", h.String()) // printZKInputs(t, ptOut.ZKInputs) @@ -1239,10 +1239,10 @@ func TestZKInputs6(t *testing.T) { l2Txs = common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[2].L2Txs) ptOut, err = tp.ProcessTxs(nil, l1UserTxs, blocks[0].Rollup.Batches[2].L1CoordinatorTxs, l2Txs) require.NoError(t, err) - assert.Equal(t, "4506051426679555819811005692198685182747763336038770877076710632305611650930", sdb.MT.Root().BigInt().String()) + assert.Equal(t, "1226521246017973425160735051912281623711495425744154152193517863144350256876", sdb.MT.Root().BigInt().String()) h, err = ptOut.ZKInputs.HashGlobalData() require.NoError(t, err) - assert.Equal(t, "4701632846207201125105176884973241543664109364248244712634276477520091620527", h.String()) + assert.Equal(t, "10907825458127261621699288732778996369331396845273565886224483543414801610880", h.String()) // printZKInputs(t, ptOut.ZKInputs) }