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.

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