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.

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