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.

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