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.

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