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.

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