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.

747 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. if !tx.IsL1 {
  497. // compute fee and subtract it from the accSender
  498. fee := common.CalcFeeAmount(tx.Amount, *tx.Fee)
  499. feeAndAmount := new(big.Int).Add(tx.Amount, fee)
  500. accSender.Balance = new(big.Int).Sub(accSender.Balance, feeAndAmount)
  501. // TODO send the fee to the Fee Idx of the Coordinator for the
  502. // TokenID
  503. }
  504. // add amount-feeAmount to the receiver
  505. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  506. // update sender account in localStateDB
  507. pSender, err := s.UpdateAccount(tx.FromIdx, accSender)
  508. if err != nil {
  509. log.Error(err)
  510. return err
  511. }
  512. if s.zki != nil {
  513. s.zki.TokenID1[s.i] = accSender.TokenID.BigInt()
  514. s.zki.Nonce1[s.i] = accSender.Nonce.BigInt()
  515. if babyjub.PointCoordSign(accSender.PublicKey.X) {
  516. s.zki.Sign1[s.i] = big.NewInt(1)
  517. }
  518. s.zki.Ay1[s.i] = accSender.PublicKey.Y
  519. s.zki.Balance1[s.i] = accSender.Balance
  520. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(accSender.EthAddr)
  521. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(pSender.Siblings)
  522. }
  523. // update receiver account in localStateDB
  524. pReceiver, err := s.UpdateAccount(auxToIdx, accReceiver)
  525. if err != nil {
  526. return err
  527. }
  528. if s.zki != nil {
  529. s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
  530. s.zki.Nonce2[s.i] = accReceiver.Nonce.BigInt()
  531. if babyjub.PointCoordSign(accReceiver.PublicKey.X) {
  532. s.zki.Sign2[s.i] = big.NewInt(1)
  533. }
  534. s.zki.Ay2[s.i] = accReceiver.PublicKey.Y
  535. s.zki.Balance2[s.i] = accReceiver.Balance
  536. s.zki.EthAddr2[s.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  537. s.zki.Siblings2[s.i] = siblingsToZKInputFormat(pReceiver.Siblings)
  538. }
  539. return nil
  540. }
  541. // applyCreateAccountDepositTransfer, in a single tx, creates a new account,
  542. // makes a deposit, and performs a transfer to another account
  543. func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
  544. accSender := &common.Account{
  545. TokenID: tx.TokenID,
  546. Nonce: 0,
  547. Balance: tx.LoadAmount,
  548. PublicKey: tx.FromBJJ,
  549. EthAddr: tx.FromEthAddr,
  550. }
  551. accSender.Balance = new(big.Int).Add(accSender.Balance, tx.LoadAmount)
  552. accReceiver, err := s.GetAccount(tx.ToIdx)
  553. if err != nil {
  554. return err
  555. }
  556. // subtract amount to the sender
  557. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  558. // add amount to the receiver
  559. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  560. // create Account of the Sender
  561. p, err := s.CreateAccount(common.Idx(s.idx+1), accSender)
  562. if err != nil {
  563. return err
  564. }
  565. if s.zki != nil {
  566. s.zki.TokenID1[s.i] = tx.TokenID.BigInt()
  567. s.zki.Nonce1[s.i] = big.NewInt(0)
  568. if babyjub.PointCoordSign(tx.FromBJJ.X) {
  569. s.zki.Sign1[s.i] = big.NewInt(1)
  570. }
  571. s.zki.Ay1[s.i] = tx.FromBJJ.Y
  572. s.zki.Balance1[s.i] = tx.LoadAmount
  573. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(tx.FromEthAddr)
  574. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
  575. if p.IsOld0 {
  576. s.zki.IsOld0_1[s.i] = big.NewInt(1)
  577. }
  578. s.zki.OldKey1[s.i] = p.OldKey.BigInt()
  579. s.zki.OldValue1[s.i] = p.OldValue.BigInt()
  580. }
  581. // update receiver account in localStateDB
  582. p, err = s.UpdateAccount(tx.ToIdx, accReceiver)
  583. if err != nil {
  584. return err
  585. }
  586. if s.zki != nil {
  587. s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
  588. s.zki.Nonce2[s.i] = accReceiver.Nonce.BigInt()
  589. if babyjub.PointCoordSign(accReceiver.PublicKey.X) {
  590. s.zki.Sign2[s.i] = big.NewInt(1)
  591. }
  592. s.zki.Ay2[s.i] = accReceiver.PublicKey.Y
  593. s.zki.Balance2[s.i] = accReceiver.Balance
  594. s.zki.EthAddr2[s.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  595. s.zki.Siblings2[s.i] = siblingsToZKInputFormat(p.Siblings)
  596. }
  597. s.idx = s.idx + 1
  598. return s.setIdx(s.idx)
  599. }
  600. // It returns the ExitAccount and a boolean determining if the Exit created a
  601. // new Leaf in the ExitTree.
  602. func (s *StateDB) applyExit(exitTree *merkletree.MerkleTree, tx common.Tx) (*common.Account, bool, error) {
  603. // 0. subtract tx.Amount from current Account in StateMT
  604. // add the tx.Amount into the Account (tx.FromIdx) in the ExitMT
  605. acc, err := s.GetAccount(tx.FromIdx)
  606. if err != nil {
  607. return nil, false, err
  608. }
  609. if !tx.IsL1 {
  610. // compute fee and subtract it from the accSender
  611. fee := common.CalcFeeAmount(tx.Amount, *tx.Fee)
  612. feeAndAmount := new(big.Int).Add(tx.Amount, fee)
  613. acc.Balance = new(big.Int).Sub(acc.Balance, feeAndAmount)
  614. // TODO send the fee to the Fee Idx of the Coordinator for the
  615. // TokenID
  616. }
  617. p, err := s.UpdateAccount(tx.FromIdx, acc)
  618. if err != nil {
  619. return nil, false, err
  620. }
  621. if s.zki != nil {
  622. s.zki.TokenID1[s.i] = acc.TokenID.BigInt()
  623. s.zki.Nonce1[s.i] = acc.Nonce.BigInt()
  624. if babyjub.PointCoordSign(acc.PublicKey.X) {
  625. s.zki.Sign1[s.i] = big.NewInt(1)
  626. }
  627. s.zki.Ay1[s.i] = acc.PublicKey.Y
  628. s.zki.Balance1[s.i] = acc.Balance
  629. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(acc.EthAddr)
  630. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
  631. }
  632. if exitTree == nil {
  633. return nil, false, nil
  634. }
  635. exitAccount, err := getAccountInTreeDB(exitTree.DB(), tx.FromIdx)
  636. if err == db.ErrNotFound {
  637. // 1a. if idx does not exist in exitTree:
  638. // add new leaf 'ExitTreeLeaf', where ExitTreeLeaf.Balance = exitAmount (exitAmount=tx.Amount)
  639. exitAccount := &common.Account{
  640. TokenID: acc.TokenID,
  641. Nonce: common.Nonce(1),
  642. Balance: tx.Amount,
  643. PublicKey: acc.PublicKey,
  644. EthAddr: acc.EthAddr,
  645. }
  646. _, err = createAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  647. return exitAccount, true, err
  648. } else if err != nil {
  649. return exitAccount, false, err
  650. }
  651. // 1b. if idx already exist in exitTree:
  652. // update account, where account.Balance += exitAmount
  653. exitAccount.Balance = new(big.Int).Add(exitAccount.Balance, tx.Amount)
  654. _, err = updateAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  655. return exitAccount, false, err
  656. }
  657. // getIdx returns the stored Idx from the localStateDB, which is the last Idx
  658. // used for an Account in the localStateDB.
  659. func (s *StateDB) getIdx() (common.Idx, error) {
  660. idxBytes, err := s.DB().Get(keyidx)
  661. if err == db.ErrNotFound {
  662. return 0, nil
  663. }
  664. if err != nil {
  665. return 0, err
  666. }
  667. return common.IdxFromBytes(idxBytes[:4])
  668. }
  669. // setIdx stores Idx in the localStateDB
  670. func (s *StateDB) setIdx(idx common.Idx) error {
  671. tx, err := s.DB().NewTx()
  672. if err != nil {
  673. return err
  674. }
  675. idxBytes, err := idx.Bytes()
  676. if err != nil {
  677. return err
  678. }
  679. err = tx.Put(keyidx, idxBytes[:])
  680. if err != nil {
  681. return err
  682. }
  683. if err := tx.Commit(); err != nil {
  684. return err
  685. }
  686. return nil
  687. }