|
@ -37,6 +37,7 @@ type ProcessTxOutput struct { |
|
|
ExitInfos []common.ExitInfo |
|
|
ExitInfos []common.ExitInfo |
|
|
CreatedAccounts []common.Account |
|
|
CreatedAccounts []common.Account |
|
|
CoordinatorIdxsMap map[common.TokenID]common.Idx |
|
|
CoordinatorIdxsMap map[common.TokenID]common.Idx |
|
|
|
|
|
CollectedFees map[common.TokenID]*big.Int |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// ProcessTxs process the given L1Txs & L2Txs applying the needed updates to
|
|
|
// ProcessTxs process the given L1Txs & L2Txs applying the needed updates to
|
|
@ -66,6 +67,7 @@ func (s *StateDB) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinatortxs |
|
|
ExitInfos: nil, |
|
|
ExitInfos: nil, |
|
|
CreatedAccounts: nil, |
|
|
CreatedAccounts: nil, |
|
|
CoordinatorIdxsMap: nil, |
|
|
CoordinatorIdxsMap: nil, |
|
|
|
|
|
CollectedFees: nil, |
|
|
}, nil |
|
|
}, nil |
|
|
} |
|
|
} |
|
|
exits := make([]processedExit, nTx) |
|
|
exits := make([]processedExit, nTx) |
|
@ -76,6 +78,14 @@ func (s *StateDB) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinatortxs |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var collectedFees map[common.TokenID]*big.Int |
|
|
|
|
|
if s.typ == TypeSynchronizer { |
|
|
|
|
|
collectedFees = make(map[common.TokenID]*big.Int) |
|
|
|
|
|
for tokenID := range coordIdxsMap { |
|
|
|
|
|
collectedFees[tokenID] = big.NewInt(0) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if s.typ == TypeBatchBuilder { |
|
|
if s.typ == TypeBatchBuilder { |
|
|
s.zki = common.NewZKInputs(nTx, 24, 32) // TODO this values will be parameters of the function, taken from config file/coordinator call
|
|
|
s.zki = common.NewZKInputs(nTx, 24, 32) // TODO this values will be parameters of the function, taken from config file/coordinator call
|
|
|
s.zki.OldLastIdx = (s.idx - 1).BigInt() |
|
|
s.zki.OldLastIdx = (s.idx - 1).BigInt() |
|
@ -138,7 +148,7 @@ func (s *StateDB) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinatortxs |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
for i := 0; i < len(l2txs); i++ { |
|
|
for i := 0; i < len(l2txs); i++ { |
|
|
exitIdx, exitAccount, newExit, err := s.processL2Tx(coordIdxsMap, exitTree, &l2txs[i]) |
|
|
|
|
|
|
|
|
exitIdx, exitAccount, newExit, err := s.processL2Tx(coordIdxsMap, collectedFees, exitTree, &l2txs[i]) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
@ -211,6 +221,7 @@ func (s *StateDB) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinatortxs |
|
|
ExitInfos: exitInfos, |
|
|
ExitInfos: exitInfos, |
|
|
CreatedAccounts: createdAccounts, |
|
|
CreatedAccounts: createdAccounts, |
|
|
CoordinatorIdxsMap: coordIdxsMap, |
|
|
CoordinatorIdxsMap: coordIdxsMap, |
|
|
|
|
|
CollectedFees: collectedFees, |
|
|
}, nil |
|
|
}, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -234,6 +245,7 @@ func (s *StateDB) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinatortxs |
|
|
ExitInfos: nil, |
|
|
ExitInfos: nil, |
|
|
CreatedAccounts: nil, |
|
|
CreatedAccounts: nil, |
|
|
CoordinatorIdxsMap: coordIdxsMap, |
|
|
CoordinatorIdxsMap: coordIdxsMap, |
|
|
|
|
|
CollectedFees: nil, |
|
|
}, nil |
|
|
}, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -296,7 +308,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx) |
|
|
|
|
|
|
|
|
// coordIdxsMap is 'nil', as at L1Txs there is no L2 fees
|
|
|
// coordIdxsMap is 'nil', as at L1Txs there is no L2 fees
|
|
|
// 0 for the parameter toIdx, as at L1Tx ToIdx can only be 0 in the Deposit type case.
|
|
|
// 0 for the parameter toIdx, as at L1Tx ToIdx can only be 0 in the Deposit type case.
|
|
|
err := s.applyTransfer(nil, tx.Tx(), 0) |
|
|
|
|
|
|
|
|
err := s.applyTransfer(nil, nil, tx.Tx(), 0) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Error(err) |
|
|
log.Error(err) |
|
|
return nil, nil, false, nil, err |
|
|
return nil, nil, false, nil, err |
|
@ -347,7 +359,7 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx) |
|
|
case common.TxTypeForceExit: |
|
|
case common.TxTypeForceExit: |
|
|
// execute exit flow
|
|
|
// execute exit flow
|
|
|
// coordIdxsMap is 'nil', as at L1Txs there is no L2 fees
|
|
|
// coordIdxsMap is 'nil', as at L1Txs there is no L2 fees
|
|
|
exitAccount, newExit, err := s.applyExit(nil, exitTree, tx.Tx()) |
|
|
|
|
|
|
|
|
exitAccount, newExit, err := s.applyExit(nil, nil, exitTree, tx.Tx()) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Error(err) |
|
|
log.Error(err) |
|
|
return nil, nil, false, nil, err |
|
|
return nil, nil, false, nil, err |
|
@ -373,7 +385,8 @@ func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx) |
|
|
// StateDB depending on the transaction Type. It returns the 3 parameters
|
|
|
// StateDB depending on the transaction Type. It returns the 3 parameters
|
|
|
// related to the Exit (in case of): Idx, ExitAccount, boolean determining if
|
|
|
// related to the Exit (in case of): Idx, ExitAccount, boolean determining if
|
|
|
// the Exit created a new Leaf in the ExitTree.
|
|
|
// the Exit created a new Leaf in the ExitTree.
|
|
|
func (s *StateDB) processL2Tx(coordIdxsMap map[common.TokenID]common.Idx, exitTree *merkletree.MerkleTree, tx *common.PoolL2Tx) (*common.Idx, *common.Account, bool, error) { |
|
|
|
|
|
|
|
|
func (s *StateDB) processL2Tx(coordIdxsMap map[common.TokenID]common.Idx, collectedFees map[common.TokenID]*big.Int, |
|
|
|
|
|
exitTree *merkletree.MerkleTree, tx *common.PoolL2Tx) (*common.Idx, *common.Account, bool, error) { |
|
|
var err error |
|
|
var err error |
|
|
// if tx.ToIdx==0, get toIdx by ToEthAddr or ToBJJ
|
|
|
// if tx.ToIdx==0, get toIdx by ToEthAddr or ToBJJ
|
|
|
if tx.ToIdx == common.Idx(0) && tx.AuxToIdx == common.Idx(0) { |
|
|
if tx.ToIdx == common.Idx(0) && tx.AuxToIdx == common.Idx(0) { |
|
@ -440,14 +453,14 @@ func (s *StateDB) processL2Tx(coordIdxsMap map[common.TokenID]common.Idx, exitTr |
|
|
case common.TxTypeTransfer, common.TxTypeTransferToEthAddr, common.TxTypeTransferToBJJ: |
|
|
case common.TxTypeTransfer, common.TxTypeTransferToEthAddr, common.TxTypeTransferToBJJ: |
|
|
// go to the MT account of sender and receiver, and update
|
|
|
// go to the MT account of sender and receiver, and update
|
|
|
// balance & nonce
|
|
|
// balance & nonce
|
|
|
err = s.applyTransfer(coordIdxsMap, tx.Tx(), tx.AuxToIdx) |
|
|
|
|
|
|
|
|
err = s.applyTransfer(coordIdxsMap, collectedFees, tx.Tx(), tx.AuxToIdx) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Error(err) |
|
|
log.Error(err) |
|
|
return nil, nil, false, err |
|
|
return nil, nil, false, err |
|
|
} |
|
|
} |
|
|
case common.TxTypeExit: |
|
|
case common.TxTypeExit: |
|
|
// execute exit flow
|
|
|
// execute exit flow
|
|
|
exitAccount, newExit, err := s.applyExit(coordIdxsMap, exitTree, tx.Tx()) |
|
|
|
|
|
|
|
|
exitAccount, newExit, err := s.applyExit(coordIdxsMap, collectedFees, exitTree, tx.Tx()) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Error(err) |
|
|
log.Error(err) |
|
|
return nil, nil, false, err |
|
|
return nil, nil, false, err |
|
@ -565,7 +578,8 @@ func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error { |
|
|
// tx.ToIdx==0, then toIdx!=0, and will be used the toIdx parameter as Idx of
|
|
|
// tx.ToIdx==0, then toIdx!=0, and will be used the toIdx parameter as Idx of
|
|
|
// the receiver. This parameter is used when the tx.ToIdx is not specified and
|
|
|
// the receiver. This parameter is used when the tx.ToIdx is not specified and
|
|
|
// the real ToIdx is found trhrough the ToEthAddr or ToBJJ.
|
|
|
// the real ToIdx is found trhrough the ToEthAddr or ToBJJ.
|
|
|
func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx, tx common.Tx, auxToIdx common.Idx) error { |
|
|
|
|
|
|
|
|
func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx, collectedFees map[common.TokenID]*big.Int, |
|
|
|
|
|
tx common.Tx, auxToIdx common.Idx) error { |
|
|
if auxToIdx == common.Idx(0) { |
|
|
if auxToIdx == common.Idx(0) { |
|
|
auxToIdx = tx.ToIdx |
|
|
auxToIdx = tx.ToIdx |
|
|
} |
|
|
} |
|
@ -600,6 +614,10 @@ func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx, tx c |
|
|
log.Error(err) |
|
|
log.Error(err) |
|
|
return err |
|
|
return err |
|
|
} |
|
|
} |
|
|
|
|
|
if s.typ == TypeSynchronizer { |
|
|
|
|
|
collected := collectedFees[tx.TokenID] |
|
|
|
|
|
collected.Add(collected, fee) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -709,7 +727,8 @@ func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error { |
|
|
|
|
|
|
|
|
// It returns the ExitAccount and a boolean determining if the Exit created a
|
|
|
// It returns the ExitAccount and a boolean determining if the Exit created a
|
|
|
// new Leaf in the ExitTree.
|
|
|
// new Leaf in the ExitTree.
|
|
|
func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx, exitTree *merkletree.MerkleTree, tx common.Tx) (*common.Account, bool, error) { |
|
|
|
|
|
|
|
|
func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx, collectedFees map[common.TokenID]*big.Int, |
|
|
|
|
|
exitTree *merkletree.MerkleTree, tx common.Tx) (*common.Account, bool, error) { |
|
|
// 0. subtract tx.Amount from current Account in StateMT
|
|
|
// 0. subtract tx.Amount from current Account in StateMT
|
|
|
// add the tx.Amount into the Account (tx.FromIdx) in the ExitMT
|
|
|
// add the tx.Amount into the Account (tx.FromIdx) in the ExitMT
|
|
|
acc, err := s.GetAccount(tx.FromIdx) |
|
|
acc, err := s.GetAccount(tx.FromIdx) |
|
@ -734,6 +753,10 @@ func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx, exitTree |
|
|
log.Error(err) |
|
|
log.Error(err) |
|
|
return nil, false, err |
|
|
return nil, false, err |
|
|
} |
|
|
} |
|
|
|
|
|
if s.typ == TypeSynchronizer { |
|
|
|
|
|
collected := collectedFees[tx.TokenID] |
|
|
|
|
|
collected.Add(collected, fee) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|