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.

731 lines
22 KiB

  1. package statedb
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "math/big"
  6. "os"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/log"
  9. "github.com/iden3/go-iden3-crypto/babyjub"
  10. "github.com/iden3/go-merkletree"
  11. "github.com/iden3/go-merkletree/db"
  12. "github.com/iden3/go-merkletree/db/pebble"
  13. )
  14. var (
  15. // keyidx is used as key in the db to store the current Idx
  16. keyidx = []byte("k:idx")
  17. )
  18. func (s *StateDB) resetZKInputs() {
  19. s.zki = nil
  20. s.i = 0
  21. }
  22. type processedExit struct {
  23. exit bool
  24. newExit bool
  25. idx common.Idx
  26. acc common.Account
  27. }
  28. // ProcessTxs process the given L1Txs & L2Txs applying the needed updates to
  29. // the StateDB depending on the transaction Type. If StateDB
  30. // type==TypeBatchBuilder, returns the common.ZKInputs to generate the
  31. // SnarkProof later used by the BatchBuilder. If StateDB
  32. // type==TypeSynchronizer, assumes that the call is done from the Synchronizer,
  33. // returns common.ExitTreeLeaf that is later used by the Synchronizer to update
  34. // the HistoryDB, and adds Nonce & TokenID to the L2Txs.
  35. func (s *StateDB) ProcessTxs(l1usertxs, l1coordinatortxs []common.L1Tx, l2txs []common.PoolL2Tx) (*common.ZKInputs, []common.ExitInfo, error) {
  36. var err error
  37. var exitTree *merkletree.MerkleTree
  38. if s.zki != nil {
  39. return nil, nil, errors.New("Expected StateDB.zki==nil, something went wrong and it's not empty")
  40. }
  41. defer s.resetZKInputs()
  42. nTx := len(l1usertxs) + len(l1coordinatortxs) + len(l2txs)
  43. if nTx == 0 {
  44. // TODO return ZKInputs of batch without txs
  45. return nil, nil, nil
  46. }
  47. exits := make([]processedExit, nTx)
  48. if s.typ == TypeBatchBuilder {
  49. s.zki = common.NewZKInputs(nTx, 24, 32) // TODO this values will be parameters of the function, taken from config file/coordinator call
  50. s.zki.OldLastIdx = (s.idx - 1).BigInt()
  51. s.zki.OldStateRoot = s.mt.Root().BigInt()
  52. }
  53. // TBD if ExitTree is only in memory or stored in disk, for the moment
  54. // only needed in memory
  55. if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
  56. tmpDir, err := ioutil.TempDir("", "hermez-statedb-exittree")
  57. if err != nil {
  58. return nil, nil, err
  59. }
  60. defer func() {
  61. if err := os.RemoveAll(tmpDir); err != nil {
  62. log.Errorw("Deleting statedb temp exit tree", "err", err)
  63. }
  64. }()
  65. sto, err := pebble.NewPebbleStorage(tmpDir, false)
  66. if err != nil {
  67. return nil, nil, err
  68. }
  69. exitTree, err = merkletree.NewMerkleTree(sto, s.mt.MaxLevels())
  70. if err != nil {
  71. return nil, nil, err
  72. }
  73. }
  74. // assumption: l1usertx are sorted by L1Tx.Position
  75. for i := 0; i < len(l1usertxs); i++ {
  76. exitIdx, exitAccount, newExit, err := s.processL1Tx(exitTree, &l1usertxs[i])
  77. if err != nil {
  78. return nil, nil, err
  79. }
  80. if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
  81. if exitIdx != nil && exitTree != nil {
  82. exits[s.i] = processedExit{
  83. exit: true,
  84. newExit: newExit,
  85. idx: *exitIdx,
  86. acc: *exitAccount,
  87. }
  88. }
  89. s.i++
  90. }
  91. }
  92. for i := 0; i < len(l1coordinatortxs); i++ {
  93. exitIdx, exitAccount, newExit, err := s.processL1Tx(exitTree, &l1coordinatortxs[i])
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. if exitIdx != nil {
  98. log.Error("Unexpected Exit in L1CoordinatorTx")
  99. }
  100. if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
  101. if exitIdx != nil && exitTree != nil {
  102. exits[s.i] = processedExit{
  103. exit: true,
  104. newExit: newExit,
  105. idx: *exitIdx,
  106. acc: *exitAccount,
  107. }
  108. }
  109. s.i++
  110. }
  111. }
  112. for i := 0; i < len(l2txs); i++ {
  113. exitIdx, exitAccount, newExit, err := s.processL2Tx(exitTree, &l2txs[i])
  114. if err != nil {
  115. return nil, nil, err
  116. }
  117. if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
  118. if exitIdx != nil && exitTree != nil {
  119. exits[s.i] = processedExit{
  120. exit: true,
  121. newExit: newExit,
  122. idx: *exitIdx,
  123. acc: *exitAccount,
  124. }
  125. }
  126. s.i++
  127. }
  128. }
  129. if s.typ == TypeTxSelector {
  130. return nil, nil, nil
  131. }
  132. // once all txs processed (exitTree root frozen), for each Exit,
  133. // generate common.ExitInfo data
  134. var exitInfos []common.ExitInfo
  135. for i := 0; i < nTx; i++ {
  136. if !exits[i].exit {
  137. continue
  138. }
  139. exitIdx := exits[i].idx
  140. exitAccount := exits[i].acc
  141. // 0. generate MerkleProof
  142. p, err := exitTree.GenerateCircomVerifierProof(exitIdx.BigInt(), nil)
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. // 1. generate common.ExitInfo
  147. ei := common.ExitInfo{
  148. AccountIdx: exitIdx,
  149. MerkleProof: p,
  150. Balance: exitAccount.Balance,
  151. }
  152. exitInfos = append(exitInfos, ei)
  153. if s.zki != nil {
  154. s.zki.TokenID2[i] = exitAccount.TokenID.BigInt()
  155. s.zki.Nonce2[i] = exitAccount.Nonce.BigInt()
  156. if babyjub.PointCoordSign(exitAccount.PublicKey.X) {
  157. s.zki.Sign2[i] = big.NewInt(1)
  158. }
  159. s.zki.Ay2[i] = exitAccount.PublicKey.Y
  160. s.zki.Balance2[i] = exitAccount.Balance
  161. s.zki.EthAddr2[i] = common.EthAddrToBigInt(exitAccount.EthAddr)
  162. s.zki.Siblings2[i] = p.Siblings
  163. if exits[i].newExit {
  164. s.zki.NewExit[i] = big.NewInt(1)
  165. }
  166. if p.IsOld0 {
  167. s.zki.IsOld0_2[i] = big.NewInt(1)
  168. }
  169. s.zki.OldKey2[i] = p.OldKey.BigInt()
  170. s.zki.OldValue2[i] = p.OldValue.BigInt()
  171. }
  172. }
  173. if s.typ == TypeSynchronizer {
  174. return nil, exitInfos, nil
  175. }
  176. // compute last ZKInputs parameters
  177. s.zki.GlobalChainID = big.NewInt(0) // TODO, 0: ethereum, this will be get from config file
  178. // zki.FeeIdxs = ? // TODO, this will be get from the config file
  179. tokenIDs, err := s.getTokenIDsBigInt(l1usertxs, l1coordinatortxs, l2txs)
  180. if err != nil {
  181. return nil, nil, err
  182. }
  183. s.zki.FeePlanTokens = tokenIDs
  184. // s.zki.ISInitStateRootFee = s.mt.Root().BigInt()
  185. // TODO once the Node Config sets the Accounts where to send the Fees
  186. // compute fees & update ZKInputs
  187. // return exitInfos, so Synchronizer will be able to store it into
  188. // HistoryDB for the concrete BatchNum
  189. return s.zki, exitInfos, nil
  190. }
  191. // getTokenIDsBigInt returns the list of TokenIDs in *big.Int format
  192. func (s *StateDB) getTokenIDsBigInt(l1usertxs, l1coordinatortxs []common.L1Tx, l2txs []common.PoolL2Tx) ([]*big.Int, error) {
  193. tokenIDs := make(map[common.TokenID]bool)
  194. for i := 0; i < len(l1usertxs); i++ {
  195. tokenIDs[l1usertxs[i].TokenID] = true
  196. }
  197. for i := 0; i < len(l1coordinatortxs); i++ {
  198. tokenIDs[l1coordinatortxs[i].TokenID] = true
  199. }
  200. for i := 0; i < len(l2txs); i++ {
  201. // as L2Tx does not have parameter TokenID, get it from the
  202. // AccountsDB (in the StateDB)
  203. acc, err := s.GetAccount(l2txs[i].ToIdx)
  204. if err != nil {
  205. return nil, err
  206. }
  207. tokenIDs[acc.TokenID] = true
  208. }
  209. var tBI []*big.Int
  210. for t := range tokenIDs {
  211. tBI = append(tBI, t.BigInt())
  212. }
  213. return tBI, nil
  214. }
  215. // processL1Tx process the given L1Tx applying the needed updates to the
  216. // StateDB depending on the transaction Type. It returns the 3 parameters
  217. // related to the Exit (in case of): Idx, ExitAccount, boolean determining if
  218. // the Exit created a new Leaf in the ExitTree.
  219. func (s *StateDB) processL1Tx(exitTree *merkletree.MerkleTree, tx *common.L1Tx) (*common.Idx, *common.Account, bool, error) {
  220. // ZKInputs
  221. if s.zki != nil {
  222. // Txs
  223. // s.zki.TxCompressedData[s.i] = tx.TxCompressedData() // uncomment once L1Tx.TxCompressedData is ready
  224. s.zki.FromIdx[s.i] = tx.FromIdx.BigInt()
  225. s.zki.ToIdx[s.i] = tx.ToIdx.BigInt()
  226. s.zki.OnChain[s.i] = big.NewInt(1)
  227. // L1Txs
  228. s.zki.LoadAmountF[s.i] = tx.LoadAmount
  229. s.zki.FromEthAddr[s.i] = common.EthAddrToBigInt(tx.FromEthAddr)
  230. if tx.FromBJJ != nil {
  231. s.zki.FromBJJCompressed[s.i] = BJJCompressedTo256BigInts(tx.FromBJJ.Compress())
  232. }
  233. // Intermediate States
  234. s.zki.ISOnChain[s.i] = big.NewInt(1)
  235. }
  236. switch tx.Type {
  237. case common.TxTypeForceTransfer, common.TxTypeTransfer:
  238. // go to the MT account of sender and receiver, and update balance
  239. // & nonce
  240. err := s.applyTransfer(tx.Tx(), 0) // 0 for the parameter toIdx, as at L1Tx ToIdx can only be 0 in the Deposit type case.
  241. if err != nil {
  242. log.Error(err)
  243. return nil, nil, false, err
  244. }
  245. case common.TxTypeCreateAccountDeposit:
  246. // add new account to the MT, update balance of the MT account
  247. err := s.applyCreateAccount(tx)
  248. if err != nil {
  249. log.Error(err)
  250. return nil, nil, false, err
  251. }
  252. // TODO applyCreateAccount will return the created account,
  253. // which in the case type==TypeSynchronizer will be added to an
  254. // array of created accounts that will be returned
  255. if s.zki != nil {
  256. s.zki.AuxFromIdx[s.i] = s.idx.BigInt() // last s.idx is the one used for creating the new account
  257. s.zki.NewAccount[s.i] = big.NewInt(1)
  258. }
  259. case common.TxTypeDeposit:
  260. // update balance of the MT account
  261. err := s.applyDeposit(tx, false)
  262. if err != nil {
  263. log.Error(err)
  264. return nil, nil, false, err
  265. }
  266. case common.TxTypeDepositTransfer:
  267. // update balance in MT account, update balance & nonce of sender
  268. // & receiver
  269. err := s.applyDeposit(tx, true)
  270. if err != nil {
  271. log.Error(err)
  272. return nil, nil, false, err
  273. }
  274. case common.TxTypeCreateAccountDepositTransfer:
  275. // add new account to the merkletree, update balance in MT account,
  276. // update balance & nonce of sender & receiver
  277. err := s.applyCreateAccountDepositTransfer(tx)
  278. if err != nil {
  279. log.Error(err)
  280. return nil, nil, false, err
  281. }
  282. if s.zki != nil {
  283. s.zki.AuxFromIdx[s.i] = s.idx.BigInt() // last s.idx is the one used for creating the new account
  284. s.zki.NewAccount[s.i] = big.NewInt(1)
  285. }
  286. case common.TxTypeExit:
  287. // execute exit flow
  288. exitAccount, newExit, err := s.applyExit(exitTree, tx.Tx())
  289. if err != nil {
  290. log.Error(err)
  291. return nil, nil, false, err
  292. }
  293. return &tx.FromIdx, exitAccount, newExit, nil
  294. default:
  295. }
  296. return nil, nil, false, nil
  297. }
  298. // processL2Tx process the given L2Tx applying the needed updates to the
  299. // StateDB depending on the transaction Type. It returns the 3 parameters
  300. // related to the Exit (in case of): Idx, ExitAccount, boolean determining if
  301. // the Exit created a new Leaf in the ExitTree.
  302. func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2Tx) (*common.Idx, *common.Account, bool, error) {
  303. var err error
  304. // if tx.ToIdx==0, get toIdx by ToEthAddr or ToBJJ
  305. if tx.ToIdx == common.Idx(0) && tx.AuxToIdx == common.Idx(0) {
  306. // case when tx.Type== common.TxTypeTransferToEthAddr or common.TxTypeTransferToBJJ
  307. tx.AuxToIdx, err = s.GetIdxByEthAddrBJJ(tx.ToEthAddr, tx.ToBJJ)
  308. if err != nil {
  309. log.Error(err)
  310. return nil, nil, false, err
  311. }
  312. }
  313. // ZKInputs
  314. if s.zki != nil {
  315. // Txs
  316. // s.zki.TxCompressedData[s.i] = tx.TxCompressedData() // uncomment once L1Tx.TxCompressedData is ready
  317. // s.zki.TxCompressedDataV2[s.i] = tx.TxCompressedDataV2() // uncomment once L2Tx.TxCompressedDataV2 is ready
  318. s.zki.FromIdx[s.i] = tx.FromIdx.BigInt()
  319. s.zki.ToIdx[s.i] = tx.ToIdx.BigInt()
  320. // fill AuxToIdx if needed
  321. if tx.ToIdx == 0 {
  322. // use toIdx that can have been filled by tx.ToIdx or
  323. // if tx.Idx==0 (this case), toIdx is filled by the Idx
  324. // from db by ToEthAddr&ToBJJ
  325. s.zki.AuxToIdx[s.i] = tx.AuxToIdx.BigInt()
  326. }
  327. if tx.ToBJJ != nil {
  328. s.zki.ToBJJAy[s.i] = tx.ToBJJ.Y
  329. }
  330. s.zki.ToEthAddr[s.i] = common.EthAddrToBigInt(tx.ToEthAddr)
  331. s.zki.OnChain[s.i] = big.NewInt(0)
  332. s.zki.NewAccount[s.i] = big.NewInt(0)
  333. // L2Txs
  334. // s.zki.RqOffset[s.i] = // TODO Rq once TxSelector is ready
  335. // s.zki.RqTxCompressedDataV2[s.i] = // TODO
  336. // s.zki.RqToEthAddr[s.i] = common.EthAddrToBigInt(tx.RqToEthAddr) // TODO
  337. // s.zki.RqToBJJAy[s.i] = tx.ToBJJ.Y // TODO
  338. signature, err := tx.Signature.Decompress()
  339. if err != nil {
  340. log.Error(err)
  341. return nil, nil, false, err
  342. }
  343. s.zki.S[s.i] = signature.S
  344. s.zki.R8x[s.i] = signature.R8.X
  345. s.zki.R8y[s.i] = signature.R8.Y
  346. }
  347. // if StateDB type==TypeSynchronizer, will need to add Nonce
  348. if s.typ == TypeSynchronizer {
  349. // as type==TypeSynchronizer, always tx.ToIdx!=0
  350. acc, err := s.GetAccount(tx.FromIdx)
  351. if err != nil {
  352. log.Error(err)
  353. return nil, nil, false, err
  354. }
  355. tx.Nonce = acc.Nonce
  356. tx.TokenID = acc.TokenID
  357. }
  358. switch tx.Type {
  359. case common.TxTypeTransfer, common.TxTypeTransferToEthAddr, common.TxTypeTransferToBJJ:
  360. // go to the MT account of sender and receiver, and update
  361. // balance & nonce
  362. err = s.applyTransfer(tx.Tx(), tx.AuxToIdx)
  363. if err != nil {
  364. log.Error(err)
  365. return nil, nil, false, err
  366. }
  367. case common.TxTypeExit:
  368. // execute exit flow
  369. exitAccount, newExit, err := s.applyExit(exitTree, tx.Tx())
  370. if err != nil {
  371. log.Error(err)
  372. return nil, nil, false, err
  373. }
  374. return &tx.FromIdx, exitAccount, newExit, nil
  375. default:
  376. }
  377. return nil, nil, false, nil
  378. }
  379. // applyCreateAccount creates a new account in the account of the depositer, it
  380. // stores the deposit value
  381. func (s *StateDB) applyCreateAccount(tx *common.L1Tx) error {
  382. account := &common.Account{
  383. TokenID: tx.TokenID,
  384. Nonce: 0,
  385. Balance: tx.LoadAmount,
  386. PublicKey: tx.FromBJJ,
  387. EthAddr: tx.FromEthAddr,
  388. }
  389. p, err := s.CreateAccount(common.Idx(s.idx+1), account)
  390. if err != nil {
  391. return err
  392. }
  393. if s.zki != nil {
  394. s.zki.TokenID1[s.i] = tx.TokenID.BigInt()
  395. s.zki.Nonce1[s.i] = big.NewInt(0)
  396. if babyjub.PointCoordSign(tx.FromBJJ.X) {
  397. s.zki.Sign1[s.i] = big.NewInt(1)
  398. }
  399. s.zki.Ay1[s.i] = tx.FromBJJ.Y
  400. s.zki.Balance1[s.i] = tx.LoadAmount
  401. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(tx.FromEthAddr)
  402. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
  403. if p.IsOld0 {
  404. s.zki.IsOld0_1[s.i] = big.NewInt(1)
  405. }
  406. s.zki.OldKey1[s.i] = p.OldKey.BigInt()
  407. s.zki.OldValue1[s.i] = p.OldValue.BigInt()
  408. }
  409. s.idx = s.idx + 1
  410. return s.setIdx(s.idx)
  411. }
  412. // applyDeposit updates the balance in the account of the depositer, if
  413. // andTransfer parameter is set to true, the method will also apply the
  414. // Transfer of the L1Tx/DepositTransfer
  415. func (s *StateDB) applyDeposit(tx *common.L1Tx, transfer bool) error {
  416. // deposit the tx.LoadAmount into the sender account
  417. accSender, err := s.GetAccount(tx.FromIdx)
  418. if err != nil {
  419. return err
  420. }
  421. accSender.Balance = new(big.Int).Add(accSender.Balance, tx.LoadAmount)
  422. // in case that the tx is a L1Tx>DepositTransfer
  423. var accReceiver *common.Account
  424. if transfer {
  425. accReceiver, err = s.GetAccount(tx.ToIdx)
  426. if err != nil {
  427. return err
  428. }
  429. // subtract amount to the sender
  430. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  431. // add amount to the receiver
  432. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  433. }
  434. // update sender account in localStateDB
  435. p, err := s.UpdateAccount(tx.FromIdx, accSender)
  436. if err != nil {
  437. return err
  438. }
  439. if s.zki != nil {
  440. s.zki.TokenID1[s.i] = accSender.TokenID.BigInt()
  441. s.zki.Nonce1[s.i] = accSender.Nonce.BigInt()
  442. if babyjub.PointCoordSign(accSender.PublicKey.X) {
  443. s.zki.Sign1[s.i] = big.NewInt(1)
  444. }
  445. s.zki.Ay1[s.i] = accSender.PublicKey.Y
  446. s.zki.Balance1[s.i] = accSender.Balance
  447. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(accSender.EthAddr)
  448. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
  449. // IsOld0_1, OldKey1, OldValue1 not needed as this is not an insert
  450. }
  451. // this is done after updating Sender Account (depositer)
  452. if transfer {
  453. // update receiver account in localStateDB
  454. p, err := s.UpdateAccount(tx.ToIdx, accReceiver)
  455. if err != nil {
  456. return err
  457. }
  458. if s.zki != nil {
  459. s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
  460. s.zki.Nonce2[s.i] = accReceiver.Nonce.BigInt()
  461. if babyjub.PointCoordSign(accReceiver.PublicKey.X) {
  462. s.zki.Sign2[s.i] = big.NewInt(1)
  463. }
  464. s.zki.Ay2[s.i] = accReceiver.PublicKey.Y
  465. s.zki.Balance2[s.i] = accReceiver.Balance
  466. s.zki.EthAddr2[s.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  467. s.zki.Siblings2[s.i] = siblingsToZKInputFormat(p.Siblings)
  468. // IsOld0_2, OldKey2, OldValue2 not needed as this is not an insert
  469. }
  470. }
  471. return nil
  472. }
  473. // applyTransfer updates the balance & nonce in the account of the sender, and
  474. // the balance in the account of the receiver.
  475. // Parameter 'toIdx' should be at 0 if the tx already has tx.ToIdx!=0, if
  476. // tx.ToIdx==0, then toIdx!=0, and will be used the toIdx parameter as Idx of
  477. // the receiver. This parameter is used when the tx.ToIdx is not specified and
  478. // the real ToIdx is found trhrough the ToEthAddr or ToBJJ.
  479. func (s *StateDB) applyTransfer(tx common.Tx, auxToIdx common.Idx) error {
  480. if auxToIdx == common.Idx(0) {
  481. auxToIdx = tx.ToIdx
  482. }
  483. // get sender and receiver accounts from localStateDB
  484. accSender, err := s.GetAccount(tx.FromIdx)
  485. if err != nil {
  486. log.Error(err)
  487. return err
  488. }
  489. accReceiver, err := s.GetAccount(auxToIdx)
  490. if err != nil {
  491. log.Error(err)
  492. return err
  493. }
  494. // increment nonce
  495. accSender.Nonce++
  496. // subtract amount to the sender
  497. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  498. // add amount to the receiver
  499. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  500. // update sender account in localStateDB
  501. pSender, err := s.UpdateAccount(tx.FromIdx, accSender)
  502. if err != nil {
  503. log.Error(err)
  504. return err
  505. }
  506. if s.zki != nil {
  507. s.zki.TokenID1[s.i] = accSender.TokenID.BigInt()
  508. s.zki.Nonce1[s.i] = accSender.Nonce.BigInt()
  509. if babyjub.PointCoordSign(accSender.PublicKey.X) {
  510. s.zki.Sign1[s.i] = big.NewInt(1)
  511. }
  512. s.zki.Ay1[s.i] = accSender.PublicKey.Y
  513. s.zki.Balance1[s.i] = accSender.Balance
  514. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(accSender.EthAddr)
  515. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(pSender.Siblings)
  516. }
  517. // update receiver account in localStateDB
  518. pReceiver, err := s.UpdateAccount(auxToIdx, accReceiver)
  519. if err != nil {
  520. return err
  521. }
  522. if s.zki != nil {
  523. s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
  524. s.zki.Nonce2[s.i] = accReceiver.Nonce.BigInt()
  525. if babyjub.PointCoordSign(accReceiver.PublicKey.X) {
  526. s.zki.Sign2[s.i] = big.NewInt(1)
  527. }
  528. s.zki.Ay2[s.i] = accReceiver.PublicKey.Y
  529. s.zki.Balance2[s.i] = accReceiver.Balance
  530. s.zki.EthAddr2[s.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  531. s.zki.Siblings2[s.i] = siblingsToZKInputFormat(pReceiver.Siblings)
  532. }
  533. return nil
  534. }
  535. // applyCreateAccountDepositTransfer, in a single tx, creates a new account,
  536. // makes a deposit, and performs a transfer to another account
  537. func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
  538. accSender := &common.Account{
  539. TokenID: tx.TokenID,
  540. Nonce: 0,
  541. Balance: tx.LoadAmount,
  542. PublicKey: tx.FromBJJ,
  543. EthAddr: tx.FromEthAddr,
  544. }
  545. accSender.Balance = new(big.Int).Add(accSender.Balance, tx.LoadAmount)
  546. accReceiver, err := s.GetAccount(tx.ToIdx)
  547. if err != nil {
  548. return err
  549. }
  550. // subtract amount to the sender
  551. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  552. // add amount to the receiver
  553. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  554. // create Account of the Sender
  555. p, err := s.CreateAccount(common.Idx(s.idx+1), accSender)
  556. if err != nil {
  557. return err
  558. }
  559. if s.zki != nil {
  560. s.zki.TokenID1[s.i] = tx.TokenID.BigInt()
  561. s.zki.Nonce1[s.i] = big.NewInt(0)
  562. if babyjub.PointCoordSign(tx.FromBJJ.X) {
  563. s.zki.Sign1[s.i] = big.NewInt(1)
  564. }
  565. s.zki.Ay1[s.i] = tx.FromBJJ.Y
  566. s.zki.Balance1[s.i] = tx.LoadAmount
  567. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(tx.FromEthAddr)
  568. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
  569. if p.IsOld0 {
  570. s.zki.IsOld0_1[s.i] = big.NewInt(1)
  571. }
  572. s.zki.OldKey1[s.i] = p.OldKey.BigInt()
  573. s.zki.OldValue1[s.i] = p.OldValue.BigInt()
  574. }
  575. // update receiver account in localStateDB
  576. p, err = s.UpdateAccount(tx.ToIdx, accReceiver)
  577. if err != nil {
  578. return err
  579. }
  580. if s.zki != nil {
  581. s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
  582. s.zki.Nonce2[s.i] = accReceiver.Nonce.BigInt()
  583. if babyjub.PointCoordSign(accReceiver.PublicKey.X) {
  584. s.zki.Sign2[s.i] = big.NewInt(1)
  585. }
  586. s.zki.Ay2[s.i] = accReceiver.PublicKey.Y
  587. s.zki.Balance2[s.i] = accReceiver.Balance
  588. s.zki.EthAddr2[s.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  589. s.zki.Siblings2[s.i] = siblingsToZKInputFormat(p.Siblings)
  590. }
  591. s.idx = s.idx + 1
  592. return s.setIdx(s.idx)
  593. }
  594. // It returns the ExitAccount and a boolean determining if the Exit created a
  595. // new Leaf in the ExitTree.
  596. func (s *StateDB) applyExit(exitTree *merkletree.MerkleTree, tx common.Tx) (*common.Account, bool, error) {
  597. // 0. subtract tx.Amount from current Account in StateMT
  598. // add the tx.Amount into the Account (tx.FromIdx) in the ExitMT
  599. acc, err := s.GetAccount(tx.FromIdx)
  600. if err != nil {
  601. return nil, false, err
  602. }
  603. acc.Balance = new(big.Int).Sub(acc.Balance, tx.Amount)
  604. p, err := s.UpdateAccount(tx.FromIdx, acc)
  605. if err != nil {
  606. return nil, false, err
  607. }
  608. if s.zki != nil {
  609. s.zki.TokenID1[s.i] = acc.TokenID.BigInt()
  610. s.zki.Nonce1[s.i] = acc.Nonce.BigInt()
  611. if babyjub.PointCoordSign(acc.PublicKey.X) {
  612. s.zki.Sign1[s.i] = big.NewInt(1)
  613. }
  614. s.zki.Ay1[s.i] = acc.PublicKey.Y
  615. s.zki.Balance1[s.i] = acc.Balance
  616. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(acc.EthAddr)
  617. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
  618. }
  619. if exitTree == nil {
  620. return nil, false, nil
  621. }
  622. exitAccount, err := getAccountInTreeDB(exitTree.DB(), tx.FromIdx)
  623. if err == db.ErrNotFound {
  624. // 1a. if idx does not exist in exitTree:
  625. // add new leaf 'ExitTreeLeaf', where ExitTreeLeaf.Balance = exitAmount (exitAmount=tx.Amount)
  626. exitAccount := &common.Account{
  627. TokenID: acc.TokenID,
  628. Nonce: common.Nonce(1),
  629. Balance: tx.Amount,
  630. PublicKey: acc.PublicKey,
  631. EthAddr: acc.EthAddr,
  632. }
  633. _, err = createAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  634. return exitAccount, true, err
  635. } else if err != nil {
  636. return exitAccount, false, err
  637. }
  638. // 1b. if idx already exist in exitTree:
  639. // update account, where account.Balance += exitAmount
  640. exitAccount.Balance = new(big.Int).Add(exitAccount.Balance, tx.Amount)
  641. _, err = updateAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  642. return exitAccount, false, err
  643. }
  644. // getIdx returns the stored Idx from the localStateDB, which is the last Idx
  645. // used for an Account in the localStateDB.
  646. func (s *StateDB) getIdx() (common.Idx, error) {
  647. idxBytes, err := s.DB().Get(keyidx)
  648. if err == db.ErrNotFound {
  649. return 0, nil
  650. }
  651. if err != nil {
  652. return 0, err
  653. }
  654. return common.IdxFromBytes(idxBytes[:4])
  655. }
  656. // setIdx stores Idx in the localStateDB
  657. func (s *StateDB) setIdx(idx common.Idx) error {
  658. tx, err := s.DB().NewTx()
  659. if err != nil {
  660. return err
  661. }
  662. idxBytes, err := idx.Bytes()
  663. if err != nil {
  664. return err
  665. }
  666. err = tx.Put(keyidx, idxBytes[:])
  667. if err != nil {
  668. return err
  669. }
  670. if err := tx.Commit(); err != nil {
  671. return err
  672. }
  673. return nil
  674. }