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.

1224 lines
38 KiB

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