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.

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