You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1393 lines
45 KiB

  1. package txprocessor
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "math/big"
  8. "os"
  9. "github.com/hermeznetwork/hermez-node/common"
  10. "github.com/hermeznetwork/hermez-node/db/statedb"
  11. "github.com/hermeznetwork/hermez-node/log"
  12. "github.com/hermeznetwork/tracerr"
  13. "github.com/iden3/go-iden3-crypto/babyjub"
  14. "github.com/iden3/go-merkletree"
  15. "github.com/iden3/go-merkletree/db"
  16. "github.com/iden3/go-merkletree/db/pebble"
  17. )
  18. // TxProcessor represents the TxProcessor object
  19. type TxProcessor struct {
  20. s *statedb.StateDB
  21. zki *common.ZKInputs
  22. // i is the current transaction index in the ZKInputs generation (zki)
  23. i int
  24. // AccumulatedFees contains the accumulated fees for each token (Coord
  25. // Idx) in the processed batch
  26. AccumulatedFees map[common.Idx]*big.Int
  27. // updatedAccounts stores the last version of the account when it has
  28. // been created/updated by any of the processed transactions.
  29. updatedAccounts map[common.Idx]*common.Account
  30. config Config
  31. }
  32. // Config contains the TxProcessor configuration parameters
  33. type Config struct {
  34. NLevels uint32
  35. // MaxFeeTx is the maximum number of coordinator accounts that can receive fees
  36. MaxFeeTx uint32
  37. MaxTx uint32
  38. MaxL1Tx uint32
  39. // ChainID of the blockchain
  40. ChainID uint16
  41. }
  42. type processedExit struct {
  43. exit bool
  44. newExit bool
  45. idx common.Idx
  46. acc common.Account
  47. }
  48. // ProcessTxOutput contains the output of the ProcessTxs method
  49. type ProcessTxOutput struct {
  50. ZKInputs *common.ZKInputs
  51. ExitInfos []common.ExitInfo
  52. CreatedAccounts []common.Account
  53. CoordinatorIdxsMap map[common.TokenID]common.Idx
  54. CollectedFees map[common.TokenID]*big.Int
  55. // UpdatedAccounts returns the current state of each account
  56. // created/updated by any of the processed transactions.
  57. UpdatedAccounts map[common.Idx]*common.Account
  58. }
  59. func newErrorNotEnoughBalance(tx common.Tx) error {
  60. var msg error
  61. if tx.IsL1 {
  62. msg = fmt.Errorf("Invalid transaction, not enough balance on sender account. TxID: %s, TxType: %s, FromIdx: %d, ToIdx: %d, Amount: %d",
  63. tx.TxID, tx.Type, tx.FromIdx, tx.ToIdx, tx.Amount)
  64. } else {
  65. msg = fmt.Errorf("Invalid transaction, not enough balance on sender account. TxID: %s, TxType: %s, FromIdx: %d, ToIdx: %d, Amount: %d, Fee: %d",
  66. tx.TxID, tx.Type, tx.FromIdx, tx.ToIdx, tx.Amount, tx.Fee)
  67. }
  68. return tracerr.Wrap(msg)
  69. }
  70. // NewTxProcessor returns a new TxProcessor with the given *StateDB & Config
  71. func NewTxProcessor(sdb *statedb.StateDB, config Config) *TxProcessor {
  72. return &TxProcessor{
  73. s: sdb,
  74. zki: nil,
  75. i: 0,
  76. config: config,
  77. }
  78. }
  79. // StateDB returns a pointer to the StateDB of the TxProcessor
  80. func (tp *TxProcessor) StateDB() *statedb.StateDB {
  81. return tp.s
  82. }
  83. func (tp *TxProcessor) resetZKInputs() {
  84. tp.zki = nil
  85. tp.i = 0 // initialize current transaction index in the ZKInputs generation
  86. }
  87. // ProcessTxs process the given L1Txs & L2Txs applying the needed updates to
  88. // the StateDB depending on the transaction Type. If StateDB
  89. // type==TypeBatchBuilder, returns the common.ZKInputs to generate the
  90. // SnarkProof later used by the BatchBuilder. If StateDB
  91. // type==TypeSynchronizer, assumes that the call is done from the Synchronizer,
  92. // returns common.ExitTreeLeaf that is later used by the Synchronizer to update
  93. // the HistoryDB, and adds Nonce & TokenID to the L2Txs.
  94. // And if TypeSynchronizer returns an array of common.Account with all the
  95. // created accounts.
  96. func (tp *TxProcessor) ProcessTxs(coordIdxs []common.Idx, l1usertxs, l1coordinatortxs []common.L1Tx,
  97. l2txs []common.PoolL2Tx) (ptOut *ProcessTxOutput, err error) {
  98. defer func() {
  99. if err == nil {
  100. err = tp.s.MakeCheckpoint()
  101. }
  102. }()
  103. var exitTree *merkletree.MerkleTree
  104. var createdAccounts []common.Account
  105. if tp.zki != nil {
  106. return nil, tracerr.Wrap(errors.New("Expected StateDB.zki==nil, something went wrong and it's not empty"))
  107. }
  108. defer tp.resetZKInputs()
  109. if len(coordIdxs) >= int(tp.config.MaxFeeTx) {
  110. return nil, tracerr.Wrap(fmt.Errorf("CoordIdxs (%d) length must be smaller than MaxFeeTx (%d)", len(coordIdxs), tp.config.MaxFeeTx))
  111. }
  112. nTx := len(l1usertxs) + len(l1coordinatortxs) + len(l2txs)
  113. if nTx > int(tp.config.MaxTx) {
  114. return nil, tracerr.Wrap(fmt.Errorf("L1UserTx + L1CoordinatorTx + L2Tx (%d) can not be bigger than MaxTx (%d)", nTx, tp.config.MaxTx))
  115. }
  116. if len(l1usertxs)+len(l1coordinatortxs) > int(tp.config.MaxL1Tx) {
  117. return nil, tracerr.Wrap(fmt.Errorf("L1UserTx + L1CoordinatorTx (%d) can not be bigger than MaxL1Tx (%d)", len(l1usertxs)+len(l1coordinatortxs), tp.config.MaxTx))
  118. }
  119. if tp.s.Type() == statedb.TypeSynchronizer {
  120. tp.updatedAccounts = make(map[common.Idx]*common.Account)
  121. }
  122. exits := make([]processedExit, nTx)
  123. if tp.s.Type() == statedb.TypeBatchBuilder {
  124. tp.zki = common.NewZKInputs(tp.config.ChainID, tp.config.MaxTx, tp.config.MaxL1Tx,
  125. tp.config.MaxFeeTx, tp.config.NLevels, (tp.s.CurrentBatch() + 1).BigInt())
  126. tp.zki.OldLastIdx = tp.s.CurrentIdx().BigInt()
  127. tp.zki.OldStateRoot = tp.s.MT.Root().BigInt()
  128. tp.zki.Metadata.NewLastIdxRaw = tp.s.CurrentIdx()
  129. }
  130. // TBD if ExitTree is only in memory or stored in disk, for the moment
  131. // is only needed in memory
  132. if tp.s.Type() == statedb.TypeSynchronizer || tp.s.Type() == statedb.TypeBatchBuilder {
  133. tmpDir, err := ioutil.TempDir("", "hermez-statedb-exittree")
  134. if err != nil {
  135. return nil, tracerr.Wrap(err)
  136. }
  137. defer func() {
  138. if err := os.RemoveAll(tmpDir); err != nil {
  139. log.Errorw("Deleting statedb temp exit tree", "err", err)
  140. }
  141. }()
  142. sto, err := pebble.NewPebbleStorage(tmpDir, false)
  143. if err != nil {
  144. return nil, tracerr.Wrap(err)
  145. }
  146. defer sto.Close()
  147. exitTree, err = merkletree.NewMerkleTree(sto, tp.s.MT.MaxLevels())
  148. if err != nil {
  149. return nil, tracerr.Wrap(err)
  150. }
  151. }
  152. // Process L1UserTxs
  153. for i := 0; i < len(l1usertxs); i++ {
  154. // assumption: l1usertx are sorted by L1Tx.Position
  155. exitIdx, exitAccount, newExit, createdAccount, err := tp.ProcessL1Tx(exitTree,
  156. &l1usertxs[i])
  157. if err != nil {
  158. return nil, tracerr.Wrap(err)
  159. }
  160. if tp.s.Type() == statedb.TypeSynchronizer {
  161. if createdAccount != nil {
  162. createdAccounts = append(createdAccounts, *createdAccount)
  163. l1usertxs[i].EffectiveFromIdx = createdAccount.Idx
  164. } else {
  165. l1usertxs[i].EffectiveFromIdx = l1usertxs[i].FromIdx
  166. }
  167. }
  168. if tp.zki != nil {
  169. l1TxData, err := l1usertxs[i].BytesGeneric()
  170. if err != nil {
  171. return nil, tracerr.Wrap(err)
  172. }
  173. tp.zki.Metadata.L1TxsData = append(tp.zki.Metadata.L1TxsData, l1TxData)
  174. l1TxDataAvailability, err :=
  175. l1usertxs[i].BytesDataAvailability(tp.zki.Metadata.NLevels)
  176. if err != nil {
  177. return nil, tracerr.Wrap(err)
  178. }
  179. tp.zki.Metadata.L1TxsDataAvailability =
  180. append(tp.zki.Metadata.L1TxsDataAvailability, l1TxDataAvailability)
  181. tp.zki.ISOutIdx[tp.i] = tp.s.CurrentIdx().BigInt()
  182. tp.zki.ISStateRoot[tp.i] = tp.s.MT.Root().BigInt()
  183. if exitIdx == nil {
  184. tp.zki.ISExitRoot[tp.i] = exitTree.Root().BigInt()
  185. }
  186. }
  187. if tp.s.Type() == statedb.TypeSynchronizer || tp.s.Type() == statedb.TypeBatchBuilder {
  188. if exitIdx != nil && exitTree != nil && exitAccount != nil {
  189. exits[tp.i] = processedExit{
  190. exit: true,
  191. newExit: newExit,
  192. idx: *exitIdx,
  193. acc: *exitAccount,
  194. }
  195. }
  196. tp.i++
  197. }
  198. }
  199. // Process L1CoordinatorTxs
  200. for i := 0; i < len(l1coordinatortxs); i++ {
  201. exitIdx, _, _, createdAccount, err := tp.ProcessL1Tx(exitTree, &l1coordinatortxs[i])
  202. if err != nil {
  203. return nil, tracerr.Wrap(err)
  204. }
  205. if exitIdx != nil {
  206. log.Error("Unexpected Exit in L1CoordinatorTx")
  207. }
  208. if tp.s.Type() == statedb.TypeSynchronizer {
  209. if createdAccount != nil {
  210. createdAccounts = append(createdAccounts, *createdAccount)
  211. l1coordinatortxs[i].EffectiveFromIdx = createdAccount.Idx
  212. } else {
  213. l1coordinatortxs[i].EffectiveFromIdx = l1coordinatortxs[i].FromIdx
  214. }
  215. }
  216. if tp.zki != nil {
  217. l1TxData, err := l1coordinatortxs[i].BytesGeneric()
  218. if err != nil {
  219. return nil, tracerr.Wrap(err)
  220. }
  221. tp.zki.Metadata.L1TxsData = append(tp.zki.Metadata.L1TxsData, l1TxData)
  222. l1TxDataAvailability, err :=
  223. l1coordinatortxs[i].BytesDataAvailability(tp.zki.Metadata.NLevels)
  224. if err != nil {
  225. return nil, tracerr.Wrap(err)
  226. }
  227. tp.zki.Metadata.L1TxsDataAvailability =
  228. append(tp.zki.Metadata.L1TxsDataAvailability, l1TxDataAvailability)
  229. tp.zki.ISOutIdx[tp.i] = tp.s.CurrentIdx().BigInt()
  230. tp.zki.ISStateRoot[tp.i] = tp.s.MT.Root().BigInt()
  231. tp.i++
  232. }
  233. }
  234. // remove repeated CoordIdxs that are for the same TokenID (use the
  235. // first occurrence)
  236. usedCoordTokenIDs := make(map[common.TokenID]bool)
  237. var filteredCoordIdxs []common.Idx
  238. for i := 0; i < len(coordIdxs); i++ {
  239. accCoord, err := tp.s.GetAccount(coordIdxs[i])
  240. if err != nil {
  241. return nil, tracerr.Wrap(err)
  242. }
  243. if !usedCoordTokenIDs[accCoord.TokenID] {
  244. usedCoordTokenIDs[accCoord.TokenID] = true
  245. filteredCoordIdxs = append(filteredCoordIdxs, coordIdxs[i])
  246. }
  247. }
  248. coordIdxs = filteredCoordIdxs
  249. tp.AccumulatedFees = make(map[common.Idx]*big.Int)
  250. for _, idx := range coordIdxs {
  251. tp.AccumulatedFees[idx] = big.NewInt(0)
  252. }
  253. // once L1UserTxs & L1CoordinatorTxs are processed, get TokenIDs of
  254. // coordIdxs. In this way, if a coordIdx uses an Idx that is being
  255. // created in the current batch, at this point the Idx will be created
  256. coordIdxsMap, err := tp.s.GetTokenIDsFromIdxs(coordIdxs)
  257. if err != nil {
  258. return nil, tracerr.Wrap(err)
  259. }
  260. // collectedFees will contain the amount of fee collected for each
  261. // TokenID
  262. var collectedFees map[common.TokenID]*big.Int
  263. if tp.s.Type() == statedb.TypeSynchronizer || tp.s.Type() == statedb.TypeBatchBuilder {
  264. collectedFees = make(map[common.TokenID]*big.Int)
  265. for tokenID := range coordIdxsMap {
  266. collectedFees[tokenID] = big.NewInt(0)
  267. }
  268. }
  269. if tp.zki != nil {
  270. // get the feePlanTokens
  271. feePlanTokens, err := tp.getFeePlanTokens(coordIdxs)
  272. if err != nil {
  273. log.Error(err)
  274. return nil, tracerr.Wrap(err)
  275. }
  276. copy(tp.zki.FeePlanTokens, feePlanTokens)
  277. }
  278. // Process L2Txs
  279. for i := 0; i < len(l2txs); i++ {
  280. exitIdx, exitAccount, newExit, err := tp.ProcessL2Tx(coordIdxsMap, collectedFees,
  281. exitTree, &l2txs[i])
  282. if err != nil {
  283. return nil, tracerr.Wrap(err)
  284. }
  285. if tp.zki != nil {
  286. l2TxData, err := l2txs[i].L2Tx().BytesDataAvailability(tp.zki.Metadata.NLevels)
  287. if err != nil {
  288. return nil, tracerr.Wrap(err)
  289. }
  290. tp.zki.Metadata.L2TxsData = append(tp.zki.Metadata.L2TxsData, l2TxData)
  291. // Intermediate States
  292. if tp.i < nTx-1 {
  293. tp.zki.ISOutIdx[tp.i] = tp.s.CurrentIdx().BigInt()
  294. tp.zki.ISStateRoot[tp.i] = tp.s.MT.Root().BigInt()
  295. tp.zki.ISAccFeeOut[tp.i] = formatAccumulatedFees(collectedFees, tp.zki.FeePlanTokens, coordIdxs)
  296. if exitIdx == nil {
  297. tp.zki.ISExitRoot[tp.i] = exitTree.Root().BigInt()
  298. }
  299. }
  300. }
  301. if tp.s.Type() == statedb.TypeSynchronizer || tp.s.Type() == statedb.TypeBatchBuilder {
  302. if exitIdx != nil && exitTree != nil {
  303. exits[tp.i] = processedExit{
  304. exit: true,
  305. newExit: newExit,
  306. idx: *exitIdx,
  307. acc: *exitAccount,
  308. }
  309. }
  310. tp.i++
  311. }
  312. }
  313. if tp.zki != nil {
  314. txCompressedDataEmpty := common.TxCompressedDataEmpty(tp.config.ChainID)
  315. last := tp.i - 1
  316. if tp.i == 0 {
  317. last = 0
  318. }
  319. for i := last; i < int(tp.config.MaxTx); i++ {
  320. if i < int(tp.config.MaxTx)-1 {
  321. tp.zki.ISOutIdx[i] = tp.s.CurrentIdx().BigInt()
  322. tp.zki.ISStateRoot[i] = tp.s.MT.Root().BigInt()
  323. tp.zki.ISAccFeeOut[i] = formatAccumulatedFees(collectedFees,
  324. tp.zki.FeePlanTokens, coordIdxs)
  325. tp.zki.ISExitRoot[i] = exitTree.Root().BigInt()
  326. }
  327. if i >= tp.i {
  328. tp.zki.TxCompressedData[i] = txCompressedDataEmpty
  329. }
  330. }
  331. isFinalAccFee := formatAccumulatedFees(collectedFees, tp.zki.FeePlanTokens, coordIdxs)
  332. copy(tp.zki.ISFinalAccFee, isFinalAccFee)
  333. // before computing the Fees txs, set the ISInitStateRootFee
  334. tp.zki.ISInitStateRootFee = tp.s.MT.Root().BigInt()
  335. }
  336. // distribute the AccumulatedFees from the processed L2Txs into the
  337. // Coordinator Idxs
  338. iFee := 0
  339. for _, idx := range coordIdxs {
  340. accumulatedFee := tp.AccumulatedFees[idx]
  341. // send the fee to the Idx of the Coordinator for the TokenID
  342. // (even if the AccumulatedFee==0, as is how the zk circuit
  343. // works)
  344. accCoord, err := tp.s.GetAccount(idx)
  345. if err != nil {
  346. log.Errorw("Can not distribute accumulated fees to coordinator account: No coord Idx to receive fee", "idx", idx)
  347. return nil, tracerr.Wrap(err)
  348. }
  349. if tp.zki != nil {
  350. tp.zki.TokenID3[iFee] = accCoord.TokenID.BigInt()
  351. tp.zki.Nonce3[iFee] = accCoord.Nonce.BigInt()
  352. coordBJJSign, coordBJJY := babyjub.UnpackSignY(accCoord.BJJ)
  353. if coordBJJSign {
  354. tp.zki.Sign3[iFee] = big.NewInt(1)
  355. }
  356. tp.zki.Ay3[iFee] = coordBJJY
  357. tp.zki.Balance3[iFee] = accCoord.Balance
  358. tp.zki.EthAddr3[iFee] = common.EthAddrToBigInt(accCoord.EthAddr)
  359. }
  360. accCoord.Balance = new(big.Int).Add(accCoord.Balance, accumulatedFee)
  361. pFee, err := tp.updateAccount(idx, accCoord)
  362. if err != nil {
  363. log.Error(err)
  364. return nil, tracerr.Wrap(err)
  365. }
  366. if tp.zki != nil {
  367. tp.zki.Siblings3[iFee] = siblingsToZKInputFormat(pFee.Siblings)
  368. tp.zki.ISStateRootFee[iFee] = tp.s.MT.Root().BigInt()
  369. }
  370. iFee++
  371. }
  372. if tp.zki != nil {
  373. for i := len(tp.AccumulatedFees); i < int(tp.config.MaxFeeTx)-1; i++ {
  374. tp.zki.ISStateRootFee[i] = tp.s.MT.Root().BigInt()
  375. }
  376. // add Coord Idx to ZKInputs.FeeTxsData
  377. for i := 0; i < len(coordIdxs); i++ {
  378. tp.zki.FeeIdxs[i] = coordIdxs[i].BigInt()
  379. }
  380. }
  381. if tp.s.Type() == statedb.TypeTxSelector {
  382. return nil, nil
  383. }
  384. if tp.s.Type() == statedb.TypeSynchronizer {
  385. // once all txs processed (exitTree root frozen), for each Exit,
  386. // generate common.ExitInfo data
  387. var exitInfos []common.ExitInfo
  388. exitInfosByIdx := make(map[common.Idx]*common.ExitInfo)
  389. for i := 0; i < nTx; i++ {
  390. if !exits[i].exit {
  391. continue
  392. }
  393. exitIdx := exits[i].idx
  394. exitAccount := exits[i].acc
  395. // 0. generate MerkleProof
  396. p, err := exitTree.GenerateSCVerifierProof(exitIdx.BigInt(), nil)
  397. if err != nil {
  398. return nil, tracerr.Wrap(err)
  399. }
  400. // 1. generate common.ExitInfo
  401. ei := common.ExitInfo{
  402. AccountIdx: exitIdx,
  403. MerkleProof: p,
  404. Balance: exitAccount.Balance,
  405. }
  406. if prevExit, ok := exitInfosByIdx[exitIdx]; !ok {
  407. exitInfos = append(exitInfos, ei)
  408. exitInfosByIdx[exitIdx] = &exitInfos[len(exitInfos)-1]
  409. } else {
  410. *prevExit = ei
  411. }
  412. }
  413. // retun exitInfos, createdAccounts and collectedFees, so Synchronizer will
  414. // be able to store it into HistoryDB for the concrete BatchNum
  415. return &ProcessTxOutput{
  416. ZKInputs: nil,
  417. ExitInfos: exitInfos,
  418. CreatedAccounts: createdAccounts,
  419. CoordinatorIdxsMap: coordIdxsMap,
  420. CollectedFees: collectedFees,
  421. UpdatedAccounts: tp.updatedAccounts,
  422. }, nil
  423. }
  424. // compute last ZKInputs parameters
  425. tp.zki.GlobalChainID = big.NewInt(int64(tp.config.ChainID))
  426. tp.zki.Metadata.NewStateRootRaw = tp.s.MT.Root()
  427. tp.zki.Metadata.NewExitRootRaw = exitTree.Root()
  428. // return ZKInputs as the BatchBuilder will return it to forge the Batch
  429. return &ProcessTxOutput{
  430. ZKInputs: tp.zki,
  431. ExitInfos: nil,
  432. CreatedAccounts: nil,
  433. CoordinatorIdxsMap: coordIdxsMap,
  434. CollectedFees: nil,
  435. }, nil
  436. }
  437. // getFeePlanTokens returns an array of *big.Int containing a list of tokenIDs
  438. // corresponding to the given CoordIdxs and the processed L2Txs
  439. func (tp *TxProcessor) getFeePlanTokens(coordIdxs []common.Idx) ([]*big.Int, error) {
  440. var tBI []*big.Int
  441. for i := 0; i < len(coordIdxs); i++ {
  442. acc, err := tp.s.GetAccount(coordIdxs[i])
  443. if err != nil {
  444. log.Errorf("could not get account to determine TokenID of CoordIdx %d not found: %s", coordIdxs[i], err.Error())
  445. return nil, tracerr.Wrap(err)
  446. }
  447. tBI = append(tBI, acc.TokenID.BigInt())
  448. }
  449. return tBI, nil
  450. }
  451. // ProcessL1Tx process the given L1Tx applying the needed updates to the
  452. // StateDB depending on the transaction Type. It returns the 3 parameters
  453. // related to the Exit (in case of): Idx, ExitAccount, boolean determining if
  454. // the Exit created a new Leaf in the ExitTree.
  455. // And another *common.Account parameter which contains the created account in
  456. // case that has been a new created account and that the StateDB is of type
  457. // TypeSynchronizer.
  458. func (tp *TxProcessor) ProcessL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx) (*common.Idx,
  459. *common.Account, bool, *common.Account, error) {
  460. // ZKInputs
  461. if tp.zki != nil {
  462. // Txs
  463. var err error
  464. tp.zki.TxCompressedData[tp.i], err = tx.TxCompressedData(tp.config.ChainID)
  465. if err != nil {
  466. log.Error(err)
  467. return nil, nil, false, nil, tracerr.Wrap(err)
  468. }
  469. tp.zki.FromIdx[tp.i] = tx.FromIdx.BigInt()
  470. tp.zki.ToIdx[tp.i] = tx.ToIdx.BigInt()
  471. tp.zki.OnChain[tp.i] = big.NewInt(1)
  472. // L1Txs
  473. depositAmountF40, err := common.NewFloat40(tx.DepositAmount)
  474. if err != nil {
  475. return nil, nil, false, nil, tracerr.Wrap(err)
  476. }
  477. tp.zki.DepositAmountF[tp.i] = big.NewInt(int64(depositAmountF40))
  478. tp.zki.FromEthAddr[tp.i] = common.EthAddrToBigInt(tx.FromEthAddr)
  479. if tx.FromBJJ != common.EmptyBJJComp {
  480. tp.zki.FromBJJCompressed[tp.i] = BJJCompressedTo256BigInts(tx.FromBJJ)
  481. }
  482. // Intermediate States, for all the transactions except for the last one
  483. if tp.i < len(tp.zki.ISOnChain) { // len(tp.zki.ISOnChain) == nTx
  484. tp.zki.ISOnChain[tp.i] = big.NewInt(1)
  485. }
  486. if tx.Type == common.TxTypeForceTransfer ||
  487. tx.Type == common.TxTypeDepositTransfer ||
  488. tx.Type == common.TxTypeCreateAccountDepositTransfer ||
  489. tx.Type == common.TxTypeForceExit {
  490. // in the cases where at L1Tx there is usage of the
  491. // Amount parameter, add it at the ZKInputs.AmountF
  492. // slot
  493. amountF40, err := common.NewFloat40(tx.Amount)
  494. if err != nil {
  495. return nil, nil, false, nil, tracerr.Wrap(err)
  496. }
  497. tp.zki.AmountF[tp.i] = big.NewInt(int64(amountF40))
  498. }
  499. }
  500. switch tx.Type {
  501. case common.TxTypeForceTransfer:
  502. tp.computeEffectiveAmounts(tx)
  503. // go to the MT account of sender and receiver, and update balance
  504. // & nonce
  505. // coordIdxsMap is 'nil', as at L1Txs there is no L2 fees.
  506. // 0 for the parameter toIdx, as at L1Tx ToIdx can only be 0 in
  507. // the Deposit type case.
  508. err := tp.applyTransfer(nil, nil, tx.Tx(), 0)
  509. if err != nil {
  510. log.Error(err)
  511. return nil, nil, false, nil, tracerr.Wrap(err)
  512. }
  513. case common.TxTypeCreateAccountDeposit:
  514. tp.computeEffectiveAmounts(tx)
  515. // add new account to the MT, update balance of the MT account
  516. err := tp.applyCreateAccount(tx)
  517. if err != nil {
  518. log.Error(err)
  519. return nil, nil, false, nil, tracerr.Wrap(err)
  520. }
  521. // TODO applyCreateAccount will return the created account,
  522. // which in the case type==TypeSynchronizer will be added to an
  523. // array of created accounts that will be returned
  524. case common.TxTypeDeposit:
  525. tp.computeEffectiveAmounts(tx)
  526. // update balance of the MT account
  527. err := tp.applyDeposit(tx, false)
  528. if err != nil {
  529. log.Error(err)
  530. return nil, nil, false, nil, tracerr.Wrap(err)
  531. }
  532. case common.TxTypeDepositTransfer:
  533. tp.computeEffectiveAmounts(tx)
  534. // update balance in MT account, update balance & nonce of sender
  535. // & receiver
  536. err := tp.applyDeposit(tx, true)
  537. if err != nil {
  538. log.Error(err)
  539. return nil, nil, false, nil, tracerr.Wrap(err)
  540. }
  541. case common.TxTypeCreateAccountDepositTransfer:
  542. tp.computeEffectiveAmounts(tx)
  543. // add new account to the merkletree, update balance in MT account,
  544. // update balance & nonce of sender & receiver
  545. err := tp.applyCreateAccountDepositTransfer(tx)
  546. if err != nil {
  547. log.Error(err)
  548. return nil, nil, false, nil, tracerr.Wrap(err)
  549. }
  550. case common.TxTypeForceExit:
  551. tp.computeEffectiveAmounts(tx)
  552. // execute exit flow
  553. // coordIdxsMap is 'nil', as at L1Txs there is no L2 fees
  554. exitAccount, newExit, err := tp.applyExit(nil, nil, exitTree, tx.Tx(), tx.Amount)
  555. if err != nil {
  556. log.Error(err)
  557. return nil, nil, false, nil, tracerr.Wrap(err)
  558. }
  559. return &tx.FromIdx, exitAccount, newExit, nil, nil
  560. default:
  561. }
  562. var createdAccount *common.Account
  563. if tp.s.Type() == statedb.TypeSynchronizer &&
  564. (tx.Type == common.TxTypeCreateAccountDeposit ||
  565. tx.Type == common.TxTypeCreateAccountDepositTransfer) {
  566. var err error
  567. createdAccount, err = tp.s.GetAccount(tp.s.CurrentIdx())
  568. if err != nil {
  569. log.Error(err)
  570. return nil, nil, false, nil, tracerr.Wrap(err)
  571. }
  572. }
  573. return nil, nil, false, createdAccount, nil
  574. }
  575. // ProcessL2Tx process the given L2Tx applying the needed updates to the
  576. // StateDB depending on the transaction Type. It returns the 3 parameters
  577. // related to the Exit (in case of): Idx, ExitAccount, boolean determining if
  578. // the Exit created a new Leaf in the ExitTree.
  579. func (tp *TxProcessor) ProcessL2Tx(coordIdxsMap map[common.TokenID]common.Idx,
  580. collectedFees map[common.TokenID]*big.Int, exitTree *merkletree.MerkleTree,
  581. tx *common.PoolL2Tx) (*common.Idx, *common.Account, bool, error) {
  582. var err error
  583. // if tx.ToIdx==0, get toIdx by ToEthAddr or ToBJJ
  584. if tx.ToIdx == common.Idx(0) && tx.AuxToIdx == common.Idx(0) {
  585. if tp.s.Type() == statedb.TypeSynchronizer {
  586. // thisTypeould never be reached
  587. log.Error("WARNING: In StateDB with Synchronizer mode L2.ToIdx can't be 0")
  588. return nil, nil, false, tracerr.Wrap(fmt.Errorf("In StateDB with Synchronizer mode L2.ToIdx can't be 0"))
  589. }
  590. // case when tx.Type== common.TxTypeTransferToEthAddr or common.TxTypeTransferToBJJ
  591. accSender, err := tp.s.GetAccount(tx.FromIdx)
  592. if err != nil {
  593. return nil, nil, false, tracerr.Wrap(err)
  594. }
  595. tx.AuxToIdx, err = tp.s.GetIdxByEthAddrBJJ(tx.ToEthAddr, tx.ToBJJ, accSender.TokenID)
  596. if err != nil {
  597. return nil, nil, false, tracerr.Wrap(err)
  598. }
  599. }
  600. // ZKInputs
  601. if tp.zki != nil {
  602. // Txs
  603. tp.zki.TxCompressedData[tp.i], err = tx.TxCompressedData(tp.config.ChainID)
  604. if err != nil {
  605. return nil, nil, false, tracerr.Wrap(err)
  606. }
  607. tp.zki.TxCompressedDataV2[tp.i], err = tx.TxCompressedDataV2()
  608. if err != nil {
  609. return nil, nil, false, tracerr.Wrap(err)
  610. }
  611. tp.zki.FromIdx[tp.i] = tx.FromIdx.BigInt()
  612. tp.zki.ToIdx[tp.i] = tx.ToIdx.BigInt()
  613. // fill AuxToIdx if needed
  614. if tx.ToIdx == 0 {
  615. // use toIdx that can have been filled by tx.ToIdx or
  616. // if tx.Idx==0 (this case), toIdx is filled by the Idx
  617. // from db by ToEthAddr&ToBJJ
  618. tp.zki.AuxToIdx[tp.i] = tx.AuxToIdx.BigInt()
  619. }
  620. if tx.ToBJJ != common.EmptyBJJComp {
  621. _, tp.zki.ToBJJAy[tp.i] = babyjub.UnpackSignY(tx.ToBJJ)
  622. }
  623. tp.zki.ToEthAddr[tp.i] = common.EthAddrToBigInt(tx.ToEthAddr)
  624. tp.zki.OnChain[tp.i] = big.NewInt(0)
  625. amountF40, err := common.NewFloat40(tx.Amount)
  626. if err != nil {
  627. return nil, nil, false, tracerr.Wrap(err)
  628. }
  629. tp.zki.AmountF[tp.i] = big.NewInt(int64(amountF40))
  630. tp.zki.NewAccount[tp.i] = big.NewInt(0)
  631. // L2Txs
  632. // tp.zki.RqOffset[tp.i] = // TODO Rq once TxSelector is ready
  633. // tp.zki.RqTxCompressedDataV2[tp.i] = // TODO
  634. // tp.zki.RqToEthAddr[tp.i] = common.EthAddrToBigInt(tx.RqToEthAddr) // TODO
  635. // tp.zki.RqToBJJAy[tp.i] = tx.ToBJJ.Y // TODO
  636. signature, err := tx.Signature.Decompress()
  637. if err != nil {
  638. log.Error(err)
  639. return nil, nil, false, tracerr.Wrap(err)
  640. }
  641. tp.zki.S[tp.i] = signature.S
  642. tp.zki.R8x[tp.i] = signature.R8.X
  643. tp.zki.R8y[tp.i] = signature.R8.Y
  644. }
  645. // if StateDB type==TypeSynchronizer, will need to add Nonce
  646. if tp.s.Type() == statedb.TypeSynchronizer {
  647. // as tType==TypeSynchronizer, always tx.ToIdx!=0
  648. acc, err := tp.s.GetAccount(tx.FromIdx)
  649. if err != nil {
  650. log.Errorw("GetAccount", "fromIdx", tx.FromIdx, "err", err)
  651. return nil, nil, false, tracerr.Wrap(err)
  652. }
  653. tx.Nonce = acc.Nonce
  654. tx.TokenID = acc.TokenID
  655. }
  656. switch tx.Type {
  657. case common.TxTypeTransfer, common.TxTypeTransferToEthAddr, common.TxTypeTransferToBJJ:
  658. // go to the MT account of sender and receiver, and update
  659. // balance & nonce
  660. err = tp.applyTransfer(coordIdxsMap, collectedFees, tx.Tx(), tx.AuxToIdx)
  661. if err != nil {
  662. log.Error(err)
  663. return nil, nil, false, tracerr.Wrap(err)
  664. }
  665. case common.TxTypeExit:
  666. // execute exit flow
  667. exitAccount, newExit, err := tp.applyExit(coordIdxsMap, collectedFees, exitTree, tx.Tx(), tx.Amount)
  668. if err != nil {
  669. log.Error(err)
  670. return nil, nil, false, tracerr.Wrap(err)
  671. }
  672. return &tx.FromIdx, exitAccount, newExit, nil
  673. default:
  674. }
  675. return nil, nil, false, nil
  676. }
  677. // applyCreateAccount creates a new account in the account of the depositer, it
  678. // stores the deposit value
  679. func (tp *TxProcessor) applyCreateAccount(tx *common.L1Tx) error {
  680. account := &common.Account{
  681. TokenID: tx.TokenID,
  682. Nonce: 0,
  683. Balance: tx.EffectiveDepositAmount,
  684. BJJ: tx.FromBJJ,
  685. EthAddr: tx.FromEthAddr,
  686. }
  687. p, err := tp.createAccount(common.Idx(tp.s.CurrentIdx()+1), account)
  688. if err != nil {
  689. return tracerr.Wrap(err)
  690. }
  691. if tp.zki != nil {
  692. tp.zki.TokenID1[tp.i] = tx.TokenID.BigInt()
  693. tp.zki.Nonce1[tp.i] = big.NewInt(0)
  694. fromBJJSign, fromBJJY := babyjub.UnpackSignY(tx.FromBJJ)
  695. if fromBJJSign {
  696. tp.zki.Sign1[tp.i] = big.NewInt(1)
  697. }
  698. tp.zki.Ay1[tp.i] = fromBJJY
  699. tp.zki.Balance1[tp.i] = tx.EffectiveDepositAmount
  700. tp.zki.EthAddr1[tp.i] = common.EthAddrToBigInt(tx.FromEthAddr)
  701. tp.zki.Siblings1[tp.i] = siblingsToZKInputFormat(p.Siblings)
  702. if p.IsOld0 {
  703. tp.zki.IsOld0_1[tp.i] = big.NewInt(1)
  704. }
  705. tp.zki.OldKey1[tp.i] = p.OldKey.BigInt()
  706. tp.zki.OldValue1[tp.i] = p.OldValue.BigInt()
  707. tp.zki.Metadata.NewLastIdxRaw = tp.s.CurrentIdx() + 1
  708. tp.zki.AuxFromIdx[tp.i] = common.Idx(tp.s.CurrentIdx() + 1).BigInt()
  709. tp.zki.NewAccount[tp.i] = big.NewInt(1)
  710. if tp.i < len(tp.zki.ISOnChain) { // len(tp.zki.ISOnChain) == nTx
  711. // intermediate states
  712. tp.zki.ISOnChain[tp.i] = big.NewInt(1)
  713. }
  714. }
  715. return tp.s.SetCurrentIdx(tp.s.CurrentIdx() + 1)
  716. }
  717. // createAccount is a wrapper over the StateDB.CreateAccount method that also
  718. // stores the created account in the updatedAccounts map in case the StateDB is
  719. // of TypeSynchronizer
  720. func (tp *TxProcessor) createAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
  721. if tp.s.Type() == statedb.TypeSynchronizer {
  722. account.Idx = idx
  723. tp.updatedAccounts[idx] = account
  724. }
  725. return tp.s.CreateAccount(idx, account)
  726. }
  727. // updateAccount is a wrapper over the StateDB.UpdateAccount method that also
  728. // stores the updated account in the updatedAccounts map in case the StateDB is
  729. // of TypeSynchronizer
  730. func (tp *TxProcessor) updateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
  731. if tp.s.Type() == statedb.TypeSynchronizer {
  732. account.Idx = idx
  733. tp.updatedAccounts[idx] = account
  734. }
  735. return tp.s.UpdateAccount(idx, account)
  736. }
  737. // applyDeposit updates the balance in the account of the depositer, if
  738. // andTransfer parameter is set to true, the method will also apply the
  739. // Transfer of the L1Tx/DepositTransfer
  740. func (tp *TxProcessor) applyDeposit(tx *common.L1Tx, transfer bool) error {
  741. accSender, err := tp.s.GetAccount(tx.FromIdx)
  742. if err != nil {
  743. return tracerr.Wrap(err)
  744. }
  745. if tp.zki != nil {
  746. tp.zki.TokenID1[tp.i] = accSender.TokenID.BigInt()
  747. tp.zki.Nonce1[tp.i] = accSender.Nonce.BigInt()
  748. senderBJJSign, senderBJJY := babyjub.UnpackSignY(accSender.BJJ)
  749. if senderBJJSign {
  750. tp.zki.Sign1[tp.i] = big.NewInt(1)
  751. }
  752. tp.zki.Ay1[tp.i] = senderBJJY
  753. tp.zki.Balance1[tp.i] = accSender.Balance
  754. tp.zki.EthAddr1[tp.i] = common.EthAddrToBigInt(accSender.EthAddr)
  755. }
  756. // add the deposit to the sender
  757. accSender.Balance = new(big.Int).Add(accSender.Balance, tx.EffectiveDepositAmount)
  758. // subtract amount to the sender
  759. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.EffectiveAmount)
  760. if accSender.Balance.Cmp(big.NewInt(0)) == -1 { // balance<0
  761. return newErrorNotEnoughBalance(tx.Tx())
  762. }
  763. // update sender account in localStateDB
  764. p, err := tp.updateAccount(tx.FromIdx, accSender)
  765. if err != nil {
  766. return tracerr.Wrap(err)
  767. }
  768. if tp.zki != nil {
  769. tp.zki.Siblings1[tp.i] = siblingsToZKInputFormat(p.Siblings)
  770. // IsOld0_1, OldKey1, OldValue1 not needed as this is not an insert
  771. }
  772. // in case that the tx is a L1Tx>DepositTransfer
  773. var accReceiver *common.Account
  774. if transfer {
  775. if tx.ToIdx == tx.FromIdx {
  776. accReceiver = accSender
  777. } else {
  778. accReceiver, err = tp.s.GetAccount(tx.ToIdx)
  779. if err != nil {
  780. return tracerr.Wrap(err)
  781. }
  782. }
  783. if tp.zki != nil {
  784. tp.zki.TokenID2[tp.i] = accReceiver.TokenID.BigInt()
  785. tp.zki.Nonce2[tp.i] = accReceiver.Nonce.BigInt()
  786. receiverBJJSign, receiverBJJY := babyjub.UnpackSignY(accReceiver.BJJ)
  787. if receiverBJJSign {
  788. tp.zki.Sign2[tp.i] = big.NewInt(1)
  789. }
  790. tp.zki.Ay2[tp.i] = receiverBJJY
  791. tp.zki.Balance2[tp.i] = accReceiver.Balance
  792. tp.zki.EthAddr2[tp.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  793. }
  794. // add amount to the receiver
  795. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.EffectiveAmount)
  796. // update receiver account in localStateDB
  797. p, err := tp.updateAccount(tx.ToIdx, accReceiver)
  798. if err != nil {
  799. return tracerr.Wrap(err)
  800. }
  801. if tp.zki != nil {
  802. tp.zki.Siblings2[tp.i] = siblingsToZKInputFormat(p.Siblings)
  803. // IsOld0_2, OldKey2, OldValue2 not needed as this is not an insert
  804. }
  805. }
  806. return nil
  807. }
  808. // applyTransfer updates the balance & nonce in the account of the sender, and
  809. // the balance in the account of the receiver.
  810. // Parameter 'toIdx' should be at 0 if the tx already has tx.ToIdx!=0, if
  811. // tx.ToIdx==0, then toIdx!=0, and will be used the toIdx parameter as Idx of
  812. // the receiver. This parameter is used when the tx.ToIdx is not specified and
  813. // the real ToIdx is found trhrough the ToEthAddr or ToBJJ.
  814. func (tp *TxProcessor) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx,
  815. collectedFees map[common.TokenID]*big.Int, tx common.Tx, auxToIdx common.Idx) error {
  816. if auxToIdx == common.Idx(0) {
  817. auxToIdx = tx.ToIdx
  818. }
  819. // get sender and receiver accounts from localStateDB
  820. accSender, err := tp.s.GetAccount(tx.FromIdx)
  821. if err != nil {
  822. log.Error(err)
  823. return tracerr.Wrap(err)
  824. }
  825. if tp.zki != nil {
  826. // Set the State1 before updating the Sender leaf
  827. tp.zki.TokenID1[tp.i] = accSender.TokenID.BigInt()
  828. tp.zki.Nonce1[tp.i] = accSender.Nonce.BigInt()
  829. senderBJJSign, senderBJJY := babyjub.UnpackSignY(accSender.BJJ)
  830. if senderBJJSign {
  831. tp.zki.Sign1[tp.i] = big.NewInt(1)
  832. }
  833. tp.zki.Ay1[tp.i] = senderBJJY
  834. tp.zki.Balance1[tp.i] = accSender.Balance
  835. tp.zki.EthAddr1[tp.i] = common.EthAddrToBigInt(accSender.EthAddr)
  836. }
  837. if !tx.IsL1 { // L2
  838. // increment nonce
  839. accSender.Nonce++
  840. // compute fee and subtract it from the accSender
  841. fee, err := common.CalcFeeAmount(tx.Amount, *tx.Fee)
  842. if err != nil {
  843. return tracerr.Wrap(err)
  844. }
  845. feeAndAmount := new(big.Int).Add(tx.Amount, fee)
  846. accSender.Balance = new(big.Int).Sub(accSender.Balance, feeAndAmount)
  847. if accSender.Balance.Cmp(big.NewInt(0)) == -1 { // balance<0
  848. return newErrorNotEnoughBalance(tx)
  849. }
  850. if _, ok := coordIdxsMap[accSender.TokenID]; ok {
  851. accCoord, err := tp.s.GetAccount(coordIdxsMap[accSender.TokenID])
  852. if err != nil {
  853. return tracerr.Wrap(fmt.Errorf("Can not use CoordIdx that does not exist in the tree. TokenID: %d, CoordIdx: %d", accSender.TokenID, coordIdxsMap[accSender.TokenID]))
  854. }
  855. // accumulate the fee for the Coord account
  856. accumulated := tp.AccumulatedFees[accCoord.Idx]
  857. accumulated.Add(accumulated, fee)
  858. if tp.s.Type() == statedb.TypeSynchronizer ||
  859. tp.s.Type() == statedb.TypeBatchBuilder {
  860. collected := collectedFees[accCoord.TokenID]
  861. collected.Add(collected, fee)
  862. }
  863. } else {
  864. log.Debugw("No coord Idx to receive fee", "tx", tx)
  865. }
  866. } else {
  867. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  868. if accSender.Balance.Cmp(big.NewInt(0)) == -1 { // balance<0
  869. return newErrorNotEnoughBalance(tx)
  870. }
  871. }
  872. // update sender account in localStateDB
  873. pSender, err := tp.updateAccount(tx.FromIdx, accSender)
  874. if err != nil {
  875. log.Error(err)
  876. return tracerr.Wrap(err)
  877. }
  878. if tp.zki != nil {
  879. tp.zki.Siblings1[tp.i] = siblingsToZKInputFormat(pSender.Siblings)
  880. }
  881. var accReceiver *common.Account
  882. if auxToIdx == tx.FromIdx {
  883. // if Sender is the Receiver, reuse 'accSender' pointer,
  884. // because in the DB the account for 'auxToIdx' won't be
  885. // updated yet
  886. accReceiver = accSender
  887. } else {
  888. accReceiver, err = tp.s.GetAccount(auxToIdx)
  889. if err != nil {
  890. log.Error(err, auxToIdx)
  891. return tracerr.Wrap(err)
  892. }
  893. }
  894. if tp.zki != nil {
  895. // Set the State2 before updating the Receiver leaf
  896. tp.zki.TokenID2[tp.i] = accReceiver.TokenID.BigInt()
  897. tp.zki.Nonce2[tp.i] = accReceiver.Nonce.BigInt()
  898. receiverBJJSign, receiverBJJY := babyjub.UnpackSignY(accReceiver.BJJ)
  899. if receiverBJJSign {
  900. tp.zki.Sign2[tp.i] = big.NewInt(1)
  901. }
  902. tp.zki.Ay2[tp.i] = receiverBJJY
  903. tp.zki.Balance2[tp.i] = accReceiver.Balance
  904. tp.zki.EthAddr2[tp.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  905. }
  906. // add amount-feeAmount to the receiver
  907. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  908. // update receiver account in localStateDB
  909. pReceiver, err := tp.updateAccount(auxToIdx, accReceiver)
  910. if err != nil {
  911. return tracerr.Wrap(err)
  912. }
  913. if tp.zki != nil {
  914. tp.zki.Siblings2[tp.i] = siblingsToZKInputFormat(pReceiver.Siblings)
  915. }
  916. return nil
  917. }
  918. // applyCreateAccountDepositTransfer, in a single tx, creates a new account,
  919. // makes a deposit, and performs a transfer to another account
  920. func (tp *TxProcessor) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
  921. auxFromIdx := common.Idx(tp.s.CurrentIdx() + 1)
  922. accSender := &common.Account{
  923. TokenID: tx.TokenID,
  924. Nonce: 0,
  925. Balance: tx.EffectiveDepositAmount,
  926. BJJ: tx.FromBJJ,
  927. EthAddr: tx.FromEthAddr,
  928. }
  929. if tp.zki != nil {
  930. // Set the State1 before updating the Sender leaf
  931. tp.zki.TokenID1[tp.i] = tx.TokenID.BigInt()
  932. tp.zki.Nonce1[tp.i] = big.NewInt(0)
  933. fromBJJSign, fromBJJY := babyjub.UnpackSignY(tx.FromBJJ)
  934. if fromBJJSign {
  935. tp.zki.Sign1[tp.i] = big.NewInt(1)
  936. }
  937. tp.zki.Ay1[tp.i] = fromBJJY
  938. tp.zki.Balance1[tp.i] = tx.EffectiveDepositAmount
  939. tp.zki.EthAddr1[tp.i] = common.EthAddrToBigInt(tx.FromEthAddr)
  940. }
  941. // subtract amount to the sender
  942. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.EffectiveAmount)
  943. if accSender.Balance.Cmp(big.NewInt(0)) == -1 { // balance<0
  944. return newErrorNotEnoughBalance(tx.Tx())
  945. }
  946. // create Account of the Sender
  947. p, err := tp.createAccount(common.Idx(tp.s.CurrentIdx()+1), accSender)
  948. if err != nil {
  949. return tracerr.Wrap(err)
  950. }
  951. if tp.zki != nil {
  952. tp.zki.Siblings1[tp.i] = siblingsToZKInputFormat(p.Siblings)
  953. if p.IsOld0 {
  954. tp.zki.IsOld0_1[tp.i] = big.NewInt(1)
  955. }
  956. tp.zki.OldKey1[tp.i] = p.OldKey.BigInt()
  957. tp.zki.OldValue1[tp.i] = p.OldValue.BigInt()
  958. tp.zki.Metadata.NewLastIdxRaw = tp.s.CurrentIdx() + 1
  959. tp.zki.AuxFromIdx[tp.i] = auxFromIdx.BigInt()
  960. tp.zki.NewAccount[tp.i] = big.NewInt(1)
  961. // intermediate states
  962. tp.zki.ISOnChain[tp.i] = big.NewInt(1)
  963. }
  964. var accReceiver *common.Account
  965. if tx.ToIdx == auxFromIdx {
  966. accReceiver = accSender
  967. } else {
  968. accReceiver, err = tp.s.GetAccount(tx.ToIdx)
  969. if err != nil {
  970. log.Error(err)
  971. return tracerr.Wrap(err)
  972. }
  973. }
  974. if tp.zki != nil {
  975. // Set the State2 before updating the Receiver leaf
  976. tp.zki.TokenID2[tp.i] = accReceiver.TokenID.BigInt()
  977. tp.zki.Nonce2[tp.i] = accReceiver.Nonce.BigInt()
  978. receiverBJJSign, receiverBJJY := babyjub.UnpackSignY(accReceiver.BJJ)
  979. if receiverBJJSign {
  980. tp.zki.Sign2[tp.i] = big.NewInt(1)
  981. }
  982. tp.zki.Ay2[tp.i] = receiverBJJY
  983. tp.zki.Balance2[tp.i] = accReceiver.Balance
  984. tp.zki.EthAddr2[tp.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  985. }
  986. // add amount to the receiver
  987. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.EffectiveAmount)
  988. // update receiver account in localStateDB
  989. p, err = tp.updateAccount(tx.ToIdx, accReceiver)
  990. if err != nil {
  991. return tracerr.Wrap(err)
  992. }
  993. if tp.zki != nil {
  994. tp.zki.Siblings2[tp.i] = siblingsToZKInputFormat(p.Siblings)
  995. }
  996. return tp.s.SetCurrentIdx(tp.s.CurrentIdx() + 1)
  997. }
  998. // It returns the ExitAccount and a boolean determining if the Exit created a
  999. // new Leaf in the ExitTree.
  1000. func (tp *TxProcessor) applyExit(coordIdxsMap map[common.TokenID]common.Idx,
  1001. collectedFees map[common.TokenID]*big.Int, exitTree *merkletree.MerkleTree,
  1002. tx common.Tx, originalAmount *big.Int) (*common.Account, bool, error) {
  1003. // 0. subtract tx.Amount from current Account in StateMT
  1004. // add the tx.Amount into the Account (tx.FromIdx) in the ExitMT
  1005. acc, err := tp.s.GetAccount(tx.FromIdx)
  1006. if err != nil {
  1007. return nil, false, tracerr.Wrap(err)
  1008. }
  1009. if tp.zki != nil {
  1010. tp.zki.TokenID1[tp.i] = acc.TokenID.BigInt()
  1011. tp.zki.Nonce1[tp.i] = acc.Nonce.BigInt()
  1012. accBJJSign, accBJJY := babyjub.UnpackSignY(acc.BJJ)
  1013. if accBJJSign {
  1014. tp.zki.Sign1[tp.i] = big.NewInt(1)
  1015. }
  1016. tp.zki.Ay1[tp.i] = accBJJY
  1017. tp.zki.Balance1[tp.i] = acc.Balance
  1018. tp.zki.EthAddr1[tp.i] = common.EthAddrToBigInt(acc.EthAddr)
  1019. }
  1020. if !tx.IsL1 {
  1021. // increment nonce
  1022. acc.Nonce++
  1023. // compute fee and subtract it from the accSender
  1024. fee, err := common.CalcFeeAmount(tx.Amount, *tx.Fee)
  1025. if err != nil {
  1026. return nil, false, tracerr.Wrap(err)
  1027. }
  1028. feeAndAmount := new(big.Int).Add(tx.Amount, fee)
  1029. acc.Balance = new(big.Int).Sub(acc.Balance, feeAndAmount)
  1030. if acc.Balance.Cmp(big.NewInt(0)) == -1 { // balance<0
  1031. return nil, false, newErrorNotEnoughBalance(tx)
  1032. }
  1033. if _, ok := coordIdxsMap[acc.TokenID]; ok {
  1034. accCoord, err := tp.s.GetAccount(coordIdxsMap[acc.TokenID])
  1035. if err != nil {
  1036. 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]))
  1037. }
  1038. // accumulate the fee for the Coord account
  1039. accumulated := tp.AccumulatedFees[accCoord.Idx]
  1040. accumulated.Add(accumulated, fee)
  1041. if tp.s.Type() == statedb.TypeSynchronizer ||
  1042. tp.s.Type() == statedb.TypeBatchBuilder {
  1043. collected := collectedFees[accCoord.TokenID]
  1044. collected.Add(collected, fee)
  1045. }
  1046. } else {
  1047. log.Debugw("No coord Idx to receive fee", "tx", tx)
  1048. }
  1049. } else {
  1050. acc.Balance = new(big.Int).Sub(acc.Balance, tx.Amount)
  1051. if acc.Balance.Cmp(big.NewInt(0)) == -1 { // balance<0
  1052. return nil, false, newErrorNotEnoughBalance(tx)
  1053. }
  1054. }
  1055. p, err := tp.updateAccount(tx.FromIdx, acc)
  1056. if err != nil {
  1057. return nil, false, tracerr.Wrap(err)
  1058. }
  1059. if tp.zki != nil {
  1060. tp.zki.Siblings1[tp.i] = siblingsToZKInputFormat(p.Siblings)
  1061. }
  1062. if exitTree == nil {
  1063. return nil, false, nil
  1064. }
  1065. // Do not add the Exit when Amount=0, not EffectiveAmount=0. In
  1066. // txprocessor.applyExit function, the tx.Amount is in reality the
  1067. // EffectiveAmount, that's why is used here the originalAmount
  1068. // parameter, which contains the real value of the tx.Amount (not
  1069. // tx.EffectiveAmount). This is a particularity of the approach of the
  1070. // circuit, the idea will be in the future to update the circuit and
  1071. // when Amount>0 but EffectiveAmount=0, to not add the Exit in the
  1072. // Exits MerkleTree, but for the moment the Go code is adapted to the
  1073. // circuit.
  1074. if originalAmount.Cmp(big.NewInt(0)) == 0 { // Amount == 0
  1075. // if the Exit Amount==0, the Exit is not added to the ExitTree
  1076. return nil, false, nil
  1077. }
  1078. exitAccount, err := statedb.GetAccountInTreeDB(exitTree.DB(), tx.FromIdx)
  1079. if tracerr.Unwrap(err) == db.ErrNotFound {
  1080. // 1a. if idx does not exist in exitTree:
  1081. // add new leaf 'ExitTreeLeaf', where ExitTreeLeaf.Balance =
  1082. // exitAmount (exitAmount=tx.Amount)
  1083. exitAccount := &common.Account{
  1084. TokenID: acc.TokenID,
  1085. Nonce: common.Nonce(0),
  1086. // as is a common.Tx, the tx.Amount is already an
  1087. // EffectiveAmount
  1088. Balance: tx.Amount,
  1089. BJJ: acc.BJJ,
  1090. EthAddr: acc.EthAddr,
  1091. }
  1092. if tp.zki != nil {
  1093. // Set the State2 before creating the Exit leaf
  1094. tp.zki.TokenID2[tp.i] = acc.TokenID.BigInt()
  1095. tp.zki.Nonce2[tp.i] = big.NewInt(0)
  1096. accBJJSign, accBJJY := babyjub.UnpackSignY(acc.BJJ)
  1097. if accBJJSign {
  1098. tp.zki.Sign2[tp.i] = big.NewInt(1)
  1099. }
  1100. tp.zki.Ay2[tp.i] = accBJJY
  1101. // Balance2 contains the ExitLeaf Balance before the
  1102. // leaf update, which is 0
  1103. tp.zki.Balance2[tp.i] = big.NewInt(0)
  1104. tp.zki.EthAddr2[tp.i] = common.EthAddrToBigInt(acc.EthAddr)
  1105. // as Leaf didn't exist in the ExitTree, set NewExit[i]=1
  1106. tp.zki.NewExit[tp.i] = big.NewInt(1)
  1107. }
  1108. p, err = statedb.CreateAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  1109. if err != nil {
  1110. return nil, false, tracerr.Wrap(err)
  1111. }
  1112. if tp.zki != nil {
  1113. tp.zki.Siblings2[tp.i] = siblingsToZKInputFormat(p.Siblings)
  1114. if p.IsOld0 {
  1115. tp.zki.IsOld0_2[tp.i] = big.NewInt(1)
  1116. }
  1117. tp.zki.OldKey2[tp.i] = p.OldKey.BigInt()
  1118. tp.zki.OldValue2[tp.i] = p.OldValue.BigInt()
  1119. tp.zki.ISExitRoot[tp.i] = exitTree.Root().BigInt()
  1120. }
  1121. return exitAccount, true, nil
  1122. } else if err != nil {
  1123. return exitAccount, false, tracerr.Wrap(err)
  1124. }
  1125. // 1b. if idx already exist in exitTree:
  1126. if tp.zki != nil {
  1127. // Set the State2 before updating the Exit leaf
  1128. tp.zki.TokenID2[tp.i] = acc.TokenID.BigInt()
  1129. // increment nonce from existing ExitLeaf
  1130. tp.zki.Nonce2[tp.i] = exitAccount.Nonce.BigInt()
  1131. accBJJSign, accBJJY := babyjub.UnpackSignY(acc.BJJ)
  1132. if accBJJSign {
  1133. tp.zki.Sign2[tp.i] = big.NewInt(1)
  1134. }
  1135. tp.zki.Ay2[tp.i] = accBJJY
  1136. // Balance2 contains the ExitLeaf Balance before the leaf
  1137. // update
  1138. tp.zki.Balance2[tp.i] = exitAccount.Balance
  1139. tp.zki.EthAddr2[tp.i] = common.EthAddrToBigInt(acc.EthAddr)
  1140. }
  1141. // update account, where account.Balance += exitAmount
  1142. exitAccount.Balance = new(big.Int).Add(exitAccount.Balance, tx.Amount)
  1143. p, err = statedb.UpdateAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  1144. if err != nil {
  1145. return nil, false, tracerr.Wrap(err)
  1146. }
  1147. if tp.zki != nil {
  1148. tp.zki.Siblings2[tp.i] = siblingsToZKInputFormat(p.Siblings)
  1149. if p.IsOld0 {
  1150. tp.zki.IsOld0_2[tp.i] = big.NewInt(1)
  1151. }
  1152. tp.zki.OldKey2[tp.i] = p.OldKey.BigInt()
  1153. tp.zki.OldValue2[tp.i] = p.OldValue.BigInt()
  1154. tp.zki.ISExitRoot[tp.i] = exitTree.Root().BigInt()
  1155. }
  1156. return exitAccount, false, nil
  1157. }
  1158. // computeEffectiveAmounts checks that the L1Tx data is correct
  1159. func (tp *TxProcessor) computeEffectiveAmounts(tx *common.L1Tx) {
  1160. tx.EffectiveAmount = tx.Amount
  1161. tx.EffectiveDepositAmount = tx.DepositAmount
  1162. if !tx.UserOrigin {
  1163. // case where the L1Tx is generated by the Coordinator
  1164. tx.EffectiveAmount = big.NewInt(0)
  1165. tx.EffectiveDepositAmount = big.NewInt(0)
  1166. return
  1167. }
  1168. if tx.Type == common.TxTypeCreateAccountDeposit {
  1169. return
  1170. }
  1171. if tx.ToIdx >= common.UserThreshold && tx.FromIdx == common.Idx(0) {
  1172. // CreateAccountDepositTransfer case
  1173. cmp := tx.DepositAmount.Cmp(tx.Amount)
  1174. if cmp == -1 { // DepositAmount<Amount
  1175. tx.EffectiveAmount = big.NewInt(0)
  1176. return
  1177. }
  1178. // check if tx.TokenID==receiver.TokenID
  1179. accReceiver, err := tp.s.GetAccount(tx.ToIdx)
  1180. if err != nil {
  1181. log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: can not get account for tx.ToIdx: %d", tx.ToIdx)
  1182. tx.EffectiveDepositAmount = big.NewInt(0)
  1183. tx.EffectiveAmount = big.NewInt(0)
  1184. return
  1185. }
  1186. if tx.TokenID != accReceiver.TokenID {
  1187. log.Debugf("EffectiveAmount = 0: tx TokenID (%d) != receiver account TokenID (%d)", tx.TokenID, accReceiver.TokenID)
  1188. tx.EffectiveAmount = big.NewInt(0)
  1189. return
  1190. }
  1191. return
  1192. }
  1193. accSender, err := tp.s.GetAccount(tx.FromIdx)
  1194. if err != nil {
  1195. log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: can not get account for tx.FromIdx: %d", tx.FromIdx)
  1196. tx.EffectiveDepositAmount = big.NewInt(0)
  1197. tx.EffectiveAmount = big.NewInt(0)
  1198. return
  1199. }
  1200. // check that tx.TokenID corresponds to the Sender account TokenID
  1201. if tx.TokenID != accSender.TokenID {
  1202. log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: tx.TokenID (%d) !=sender account TokenID (%d)", tx.TokenID, accSender.TokenID)
  1203. tx.EffectiveDepositAmount = big.NewInt(0)
  1204. tx.EffectiveAmount = big.NewInt(0)
  1205. return
  1206. }
  1207. // check that Sender has enough balance
  1208. bal := accSender.Balance
  1209. if tx.DepositAmount != nil {
  1210. bal = new(big.Int).Add(bal, tx.EffectiveDepositAmount)
  1211. }
  1212. cmp := bal.Cmp(tx.Amount)
  1213. if cmp == -1 {
  1214. log.Debugf("EffectiveAmount = 0: Not enough funds (%s<%s)", bal.String(), tx.Amount.String())
  1215. tx.EffectiveAmount = big.NewInt(0)
  1216. return
  1217. }
  1218. // check that the tx.FromEthAddr is the same than the EthAddress of the
  1219. // Sender
  1220. if !bytes.Equal(tx.FromEthAddr.Bytes(), accSender.EthAddr.Bytes()) {
  1221. log.Debugf("EffectiveAmount = 0: tx.FromEthAddr (%s) must be the same EthAddr of the sender account by the Idx (%s)", tx.FromEthAddr.Hex(), accSender.EthAddr.Hex())
  1222. tx.EffectiveAmount = big.NewInt(0)
  1223. }
  1224. if tx.ToIdx == common.Idx(1) || tx.ToIdx == common.Idx(0) {
  1225. // if transfer is Exit type, there are no more checks
  1226. return
  1227. }
  1228. // check that TokenID is the same for Sender & Receiver account
  1229. accReceiver, err := tp.s.GetAccount(tx.ToIdx)
  1230. if err != nil {
  1231. log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: can not get account for tx.ToIdx: %d", tx.ToIdx)
  1232. tx.EffectiveDepositAmount = big.NewInt(0)
  1233. tx.EffectiveAmount = big.NewInt(0)
  1234. return
  1235. }
  1236. if accSender.TokenID != accReceiver.TokenID {
  1237. log.Debugf("EffectiveAmount = 0: sender account TokenID (%d) != receiver account TokenID (%d)", accSender.TokenID, accReceiver.TokenID)
  1238. tx.EffectiveAmount = big.NewInt(0)
  1239. return
  1240. }
  1241. if tx.TokenID != accReceiver.TokenID {
  1242. log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: tx TokenID (%d) != receiver account TokenID (%d)", tx.TokenID, accReceiver.TokenID)
  1243. tx.EffectiveAmount = big.NewInt(0)
  1244. return
  1245. }
  1246. }
  1247. // CheckEnoughBalance returns true if the sender of the transaction has enough
  1248. // balance in the account to send the Amount+Fee, and also returns the account
  1249. // Balance and the Fee+Amount (which is used to give information about why the
  1250. // transaction is not selected in case that this method returns false.
  1251. func (tp *TxProcessor) CheckEnoughBalance(tx common.PoolL2Tx) (bool, *big.Int, *big.Int) {
  1252. acc, err := tp.s.GetAccount(tx.FromIdx)
  1253. if err != nil {
  1254. return false, nil, nil
  1255. }
  1256. fee, err := common.CalcFeeAmount(tx.Amount, tx.Fee)
  1257. if err != nil {
  1258. return false, nil, nil
  1259. }
  1260. feeAndAmount := new(big.Int).Add(tx.Amount, fee)
  1261. return acc.Balance.Cmp(feeAndAmount) != -1, // !=-1 balance<amount
  1262. acc.Balance, feeAndAmount
  1263. }