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.

1180 lines
37 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. accSender, err := s.GetAccount(tx.FromIdx)
  641. if err != nil {
  642. return tracerr.Wrap(err)
  643. }
  644. if s.zki != nil {
  645. s.zki.TokenID1[s.i] = accSender.TokenID.BigInt()
  646. s.zki.Nonce1[s.i] = accSender.Nonce.BigInt()
  647. if babyjub.PointCoordSign(accSender.PublicKey.X) {
  648. s.zki.Sign1[s.i] = big.NewInt(1)
  649. }
  650. s.zki.Ay1[s.i] = accSender.PublicKey.Y
  651. s.zki.Balance1[s.i] = accSender.Balance
  652. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(accSender.EthAddr)
  653. }
  654. // add the deposit to the sender
  655. accSender.Balance = new(big.Int).Add(accSender.Balance, tx.EffectiveDepositAmount)
  656. // subtract amount to the sender
  657. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.EffectiveAmount)
  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.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
  665. // IsOld0_1, OldKey1, OldValue1 not needed as this is not an insert
  666. }
  667. // in case that the tx is a L1Tx>DepositTransfer
  668. var accReceiver *common.Account
  669. if transfer {
  670. if tx.ToIdx == tx.FromIdx {
  671. accReceiver = accSender
  672. } else {
  673. accReceiver, err = s.GetAccount(tx.ToIdx)
  674. if err != nil {
  675. return tracerr.Wrap(err)
  676. }
  677. }
  678. if s.zki != nil {
  679. s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
  680. s.zki.Nonce2[s.i] = accReceiver.Nonce.BigInt()
  681. if babyjub.PointCoordSign(accReceiver.PublicKey.X) {
  682. s.zki.Sign2[s.i] = big.NewInt(1)
  683. }
  684. s.zki.Ay2[s.i] = accReceiver.PublicKey.Y
  685. s.zki.Balance2[s.i] = accReceiver.Balance
  686. s.zki.EthAddr2[s.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  687. }
  688. // add amount to the receiver
  689. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.EffectiveAmount)
  690. // update receiver account in localStateDB
  691. p, err := s.UpdateAccount(tx.ToIdx, accReceiver)
  692. if err != nil {
  693. return tracerr.Wrap(err)
  694. }
  695. if s.zki != nil {
  696. s.zki.Siblings2[s.i] = siblingsToZKInputFormat(p.Siblings)
  697. // IsOld0_2, OldKey2, OldValue2 not needed as this is not an insert
  698. }
  699. }
  700. return nil
  701. }
  702. // applyTransfer updates the balance & nonce in the account of the sender, and
  703. // the balance in the account of the receiver.
  704. // Parameter 'toIdx' should be at 0 if the tx already has tx.ToIdx!=0, if
  705. // tx.ToIdx==0, then toIdx!=0, and will be used the toIdx parameter as Idx of
  706. // the receiver. This parameter is used when the tx.ToIdx is not specified and
  707. // the real ToIdx is found trhrough the ToEthAddr or ToBJJ.
  708. func (s *StateDB) applyTransfer(coordIdxsMap map[common.TokenID]common.Idx,
  709. collectedFees map[common.TokenID]*big.Int,
  710. tx common.Tx, auxToIdx common.Idx) error {
  711. if auxToIdx == common.Idx(0) {
  712. auxToIdx = tx.ToIdx
  713. }
  714. // get sender and receiver accounts from localStateDB
  715. accSender, err := s.GetAccount(tx.FromIdx)
  716. if err != nil {
  717. log.Error(err)
  718. return tracerr.Wrap(err)
  719. }
  720. if s.zki != nil {
  721. // Set the State1 before updating the Sender leaf
  722. s.zki.TokenID1[s.i] = accSender.TokenID.BigInt()
  723. s.zki.Nonce1[s.i] = accSender.Nonce.BigInt()
  724. if babyjub.PointCoordSign(accSender.PublicKey.X) {
  725. s.zki.Sign1[s.i] = big.NewInt(1)
  726. }
  727. s.zki.Ay1[s.i] = accSender.PublicKey.Y
  728. s.zki.Balance1[s.i] = accSender.Balance
  729. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(accSender.EthAddr)
  730. }
  731. if !tx.IsL1 {
  732. // increment nonce
  733. accSender.Nonce++
  734. // compute fee and subtract it from the accSender
  735. fee, err := common.CalcFeeAmount(tx.Amount, *tx.Fee)
  736. if err != nil {
  737. return tracerr.Wrap(err)
  738. }
  739. feeAndAmount := new(big.Int).Add(tx.Amount, fee)
  740. accSender.Balance = new(big.Int).Sub(accSender.Balance, feeAndAmount)
  741. if _, ok := coordIdxsMap[accSender.TokenID]; ok {
  742. accCoord, err := s.GetAccount(coordIdxsMap[accSender.TokenID])
  743. if err != nil {
  744. 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]))
  745. }
  746. // accumulate the fee for the Coord account
  747. accumulated := s.accumulatedFees[accCoord.Idx]
  748. accumulated.Add(accumulated, fee)
  749. if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
  750. collected := collectedFees[accCoord.TokenID]
  751. collected.Add(collected, fee)
  752. }
  753. } else {
  754. log.Debugw("No coord Idx to receive fee", "tx", tx)
  755. }
  756. } else {
  757. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.Amount)
  758. }
  759. // update sender account in localStateDB
  760. pSender, err := s.UpdateAccount(tx.FromIdx, accSender)
  761. if err != nil {
  762. log.Error(err)
  763. return tracerr.Wrap(err)
  764. }
  765. if s.zki != nil {
  766. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(pSender.Siblings)
  767. }
  768. var accReceiver *common.Account
  769. if auxToIdx == tx.FromIdx {
  770. // if Sender is the Receiver, reuse 'accSender' pointer,
  771. // because in the DB the account for 'auxToIdx' won't be
  772. // updated yet
  773. accReceiver = accSender
  774. } else {
  775. accReceiver, err = s.GetAccount(auxToIdx)
  776. if err != nil {
  777. log.Error(err)
  778. return tracerr.Wrap(err)
  779. }
  780. }
  781. if s.zki != nil {
  782. // Set the State2 before updating the Receiver leaf
  783. s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
  784. s.zki.Nonce2[s.i] = accReceiver.Nonce.BigInt()
  785. if babyjub.PointCoordSign(accReceiver.PublicKey.X) {
  786. s.zki.Sign2[s.i] = big.NewInt(1)
  787. }
  788. s.zki.Ay2[s.i] = accReceiver.PublicKey.Y
  789. s.zki.Balance2[s.i] = accReceiver.Balance
  790. s.zki.EthAddr2[s.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  791. }
  792. // add amount-feeAmount to the receiver
  793. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.Amount)
  794. // update receiver account in localStateDB
  795. pReceiver, err := s.UpdateAccount(auxToIdx, accReceiver)
  796. if err != nil {
  797. return tracerr.Wrap(err)
  798. }
  799. if s.zki != nil {
  800. s.zki.Siblings2[s.i] = siblingsToZKInputFormat(pReceiver.Siblings)
  801. }
  802. return nil
  803. }
  804. // applyCreateAccountDepositTransfer, in a single tx, creates a new account,
  805. // makes a deposit, and performs a transfer to another account
  806. func (s *StateDB) applyCreateAccountDepositTransfer(tx *common.L1Tx) error {
  807. auxFromIdx := common.Idx(s.idx + 1)
  808. accSender := &common.Account{
  809. TokenID: tx.TokenID,
  810. Nonce: 0,
  811. Balance: tx.EffectiveDepositAmount,
  812. PublicKey: tx.FromBJJ,
  813. EthAddr: tx.FromEthAddr,
  814. }
  815. if s.zki != nil {
  816. // Set the State1 before updating the Sender leaf
  817. s.zki.TokenID1[s.i] = tx.TokenID.BigInt()
  818. s.zki.Nonce1[s.i] = big.NewInt(0)
  819. if babyjub.PointCoordSign(tx.FromBJJ.X) {
  820. s.zki.Sign1[s.i] = big.NewInt(1)
  821. }
  822. s.zki.Ay1[s.i] = tx.FromBJJ.Y
  823. s.zki.Balance1[s.i] = tx.EffectiveDepositAmount
  824. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(tx.FromEthAddr)
  825. }
  826. // subtract amount to the sender
  827. accSender.Balance = new(big.Int).Sub(accSender.Balance, tx.EffectiveAmount)
  828. // create Account of the Sender
  829. p, err := s.CreateAccount(common.Idx(s.idx+1), accSender)
  830. if err != nil {
  831. return tracerr.Wrap(err)
  832. }
  833. if s.zki != nil {
  834. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
  835. if p.IsOld0 {
  836. s.zki.IsOld0_1[s.i] = big.NewInt(1)
  837. }
  838. s.zki.OldKey1[s.i] = p.OldKey.BigInt()
  839. s.zki.OldValue1[s.i] = p.OldValue.BigInt()
  840. s.zki.Metadata.NewLastIdxRaw = s.idx + 1
  841. s.zki.AuxFromIdx[s.i] = auxFromIdx.BigInt()
  842. s.zki.NewAccount[s.i] = big.NewInt(1)
  843. // intermediate states
  844. s.zki.ISOnChain[s.i] = big.NewInt(1)
  845. }
  846. var accReceiver *common.Account
  847. if tx.ToIdx == auxFromIdx {
  848. accReceiver = accSender
  849. } else {
  850. accReceiver, err = s.GetAccount(tx.ToIdx)
  851. if err != nil {
  852. log.Error(err)
  853. return tracerr.Wrap(err)
  854. }
  855. }
  856. if s.zki != nil {
  857. // Set the State2 before updating the Receiver leaf
  858. s.zki.TokenID2[s.i] = accReceiver.TokenID.BigInt()
  859. s.zki.Nonce2[s.i] = accReceiver.Nonce.BigInt()
  860. if babyjub.PointCoordSign(accReceiver.PublicKey.X) {
  861. s.zki.Sign2[s.i] = big.NewInt(1)
  862. }
  863. s.zki.Ay2[s.i] = accReceiver.PublicKey.Y
  864. s.zki.Balance2[s.i] = accReceiver.Balance
  865. s.zki.EthAddr2[s.i] = common.EthAddrToBigInt(accReceiver.EthAddr)
  866. }
  867. // add amount to the receiver
  868. accReceiver.Balance = new(big.Int).Add(accReceiver.Balance, tx.EffectiveAmount)
  869. // update receiver account in localStateDB
  870. p, err = s.UpdateAccount(tx.ToIdx, accReceiver)
  871. if err != nil {
  872. return tracerr.Wrap(err)
  873. }
  874. if s.zki != nil {
  875. s.zki.Siblings2[s.i] = siblingsToZKInputFormat(p.Siblings)
  876. }
  877. s.idx = s.idx + 1
  878. return s.setIdx(s.idx)
  879. }
  880. // It returns the ExitAccount and a boolean determining if the Exit created a
  881. // new Leaf in the ExitTree.
  882. func (s *StateDB) applyExit(coordIdxsMap map[common.TokenID]common.Idx,
  883. collectedFees map[common.TokenID]*big.Int, exitTree *merkletree.MerkleTree,
  884. tx common.Tx) (*common.Account, bool, error) {
  885. // 0. subtract tx.Amount from current Account in StateMT
  886. // add the tx.Amount into the Account (tx.FromIdx) in the ExitMT
  887. acc, err := s.GetAccount(tx.FromIdx)
  888. if err != nil {
  889. return nil, false, tracerr.Wrap(err)
  890. }
  891. if !tx.IsL1 {
  892. // increment nonce
  893. acc.Nonce++
  894. // compute fee and subtract it from the accSender
  895. fee, err := common.CalcFeeAmount(tx.Amount, *tx.Fee)
  896. if err != nil {
  897. return nil, false, tracerr.Wrap(err)
  898. }
  899. feeAndAmount := new(big.Int).Add(tx.Amount, fee)
  900. acc.Balance = new(big.Int).Sub(acc.Balance, feeAndAmount)
  901. if _, ok := coordIdxsMap[acc.TokenID]; ok {
  902. accCoord, err := s.GetAccount(coordIdxsMap[acc.TokenID])
  903. if err != nil {
  904. 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]))
  905. }
  906. // accumulate the fee for the Coord account
  907. accumulated := s.accumulatedFees[accCoord.Idx]
  908. accumulated.Add(accumulated, fee)
  909. if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
  910. collected := collectedFees[accCoord.TokenID]
  911. collected.Add(collected, fee)
  912. }
  913. } else {
  914. log.Debugw("No coord Idx to receive fee", "tx", tx)
  915. }
  916. } else {
  917. acc.Balance = new(big.Int).Sub(acc.Balance, tx.Amount)
  918. }
  919. p, err := s.UpdateAccount(tx.FromIdx, acc)
  920. if err != nil {
  921. return nil, false, tracerr.Wrap(err)
  922. }
  923. if s.zki != nil {
  924. s.zki.TokenID1[s.i] = acc.TokenID.BigInt()
  925. s.zki.Nonce1[s.i] = acc.Nonce.BigInt()
  926. if babyjub.PointCoordSign(acc.PublicKey.X) {
  927. s.zki.Sign1[s.i] = big.NewInt(1)
  928. }
  929. s.zki.Ay1[s.i] = acc.PublicKey.Y
  930. s.zki.Balance1[s.i] = acc.Balance
  931. s.zki.EthAddr1[s.i] = common.EthAddrToBigInt(acc.EthAddr)
  932. s.zki.Siblings1[s.i] = siblingsToZKInputFormat(p.Siblings)
  933. }
  934. if exitTree == nil {
  935. return nil, false, nil
  936. }
  937. exitAccount, err := getAccountInTreeDB(exitTree.DB(), tx.FromIdx)
  938. if tracerr.Unwrap(err) == db.ErrNotFound {
  939. // 1a. if idx does not exist in exitTree:
  940. // add new leaf 'ExitTreeLeaf', where ExitTreeLeaf.Balance = exitAmount (exitAmount=tx.Amount)
  941. exitAccount := &common.Account{
  942. TokenID: acc.TokenID,
  943. Nonce: common.Nonce(1),
  944. Balance: tx.Amount,
  945. PublicKey: acc.PublicKey,
  946. EthAddr: acc.EthAddr,
  947. }
  948. _, err = createAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  949. return exitAccount, true, tracerr.Wrap(err)
  950. } else if err != nil {
  951. return exitAccount, false, tracerr.Wrap(err)
  952. }
  953. // 1b. if idx already exist in exitTree:
  954. // update account, where account.Balance += exitAmount
  955. exitAccount.Balance = new(big.Int).Add(exitAccount.Balance, tx.Amount)
  956. _, err = updateAccountInTreeDB(exitTree.DB(), exitTree, tx.FromIdx, exitAccount)
  957. return exitAccount, false, tracerr.Wrap(err)
  958. }
  959. // computeEffectiveAmounts checks that the L1Tx data is correct
  960. func (s *StateDB) computeEffectiveAmounts(tx *common.L1Tx) {
  961. if !tx.UserOrigin {
  962. // case where the L1Tx is generated by the Coordinator
  963. tx.EffectiveAmount = big.NewInt(0)
  964. tx.EffectiveDepositAmount = big.NewInt(0)
  965. return
  966. }
  967. tx.EffectiveAmount = tx.Amount
  968. tx.EffectiveDepositAmount = tx.DepositAmount
  969. if tx.Type == common.TxTypeCreateAccountDeposit {
  970. return
  971. }
  972. if tx.ToIdx >= common.UserThreshold && tx.FromIdx == common.Idx(0) {
  973. // CreateAccountDepositTransfer case
  974. cmp := tx.DepositAmount.Cmp(tx.Amount)
  975. if cmp == -1 { // DepositAmount<Amount
  976. tx.EffectiveAmount = big.NewInt(0)
  977. return
  978. }
  979. return
  980. }
  981. accSender, err := s.GetAccount(tx.FromIdx)
  982. if err != nil {
  983. log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: can not get account for tx.FromIdx: %d", tx.FromIdx)
  984. tx.EffectiveDepositAmount = big.NewInt(0)
  985. tx.EffectiveAmount = big.NewInt(0)
  986. return
  987. }
  988. // check that tx.TokenID corresponds to the Sender account TokenID
  989. if tx.TokenID != accSender.TokenID {
  990. log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: tx.TokenID (%d) !=sender account TokenID (%d)", tx.TokenID, accSender.TokenID)
  991. tx.EffectiveDepositAmount = big.NewInt(0)
  992. tx.EffectiveAmount = big.NewInt(0)
  993. return
  994. }
  995. // check that Sender has enough balance
  996. bal := accSender.Balance
  997. if tx.DepositAmount != nil {
  998. bal = new(big.Int).Add(bal, tx.EffectiveDepositAmount)
  999. }
  1000. cmp := bal.Cmp(tx.Amount)
  1001. if cmp == -1 {
  1002. log.Debugf("EffectiveAmount = 0: Not enough funds (%s<%s)", bal.String(), tx.Amount.String())
  1003. tx.EffectiveAmount = big.NewInt(0)
  1004. return
  1005. }
  1006. // check that the tx.FromEthAddr is the same than the EthAddress of the
  1007. // Sender
  1008. if !bytes.Equal(tx.FromEthAddr.Bytes(), accSender.EthAddr.Bytes()) {
  1009. 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())
  1010. tx.EffectiveDepositAmount = big.NewInt(0)
  1011. tx.EffectiveAmount = big.NewInt(0)
  1012. return
  1013. }
  1014. if tx.ToIdx == common.Idx(1) || tx.ToIdx == common.Idx(0) {
  1015. // if transfer is Exit type, there are no more checks
  1016. return
  1017. }
  1018. // check that TokenID is the same for Sender & Receiver account
  1019. accReceiver, err := s.GetAccount(tx.ToIdx)
  1020. if err != nil {
  1021. log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: can not get account for tx.ToIdx: %d", tx.ToIdx)
  1022. tx.EffectiveDepositAmount = big.NewInt(0)
  1023. tx.EffectiveAmount = big.NewInt(0)
  1024. return
  1025. }
  1026. if accSender.TokenID != accReceiver.TokenID {
  1027. log.Debugf("EffectiveAmount & EffectiveDepositAmount = 0: sender account TokenID (%d) != receiver account TokenID (%d)", tx.TokenID, accSender.TokenID)
  1028. tx.EffectiveDepositAmount = big.NewInt(0)
  1029. tx.EffectiveAmount = big.NewInt(0)
  1030. return
  1031. }
  1032. }
  1033. // GetIdx returns the stored Idx from the localStateDB, which is the last Idx
  1034. // used for an Account in the localStateDB.
  1035. func (s *StateDB) GetIdx() (common.Idx, error) {
  1036. idxBytes, err := s.DB().Get(keyidx)
  1037. if tracerr.Unwrap(err) == db.ErrNotFound {
  1038. return 0, nil
  1039. }
  1040. if err != nil {
  1041. return 0, tracerr.Wrap(err)
  1042. }
  1043. return common.IdxFromBytes(idxBytes[:])
  1044. }
  1045. // setIdx stores Idx in the localStateDB
  1046. func (s *StateDB) setIdx(idx common.Idx) error {
  1047. tx, err := s.DB().NewTx()
  1048. if err != nil {
  1049. return tracerr.Wrap(err)
  1050. }
  1051. idxBytes, err := idx.Bytes()
  1052. if err != nil {
  1053. return tracerr.Wrap(err)
  1054. }
  1055. err = tx.Put(keyidx, idxBytes[:])
  1056. if err != nil {
  1057. return tracerr.Wrap(err)
  1058. }
  1059. if err := tx.Commit(); err != nil {
  1060. return tracerr.Wrap(err)
  1061. }
  1062. return nil
  1063. }