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.

376 lines
10 KiB

  1. package test
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. "strconv"
  7. "time"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. "github.com/hermeznetwork/hermez-node/common"
  10. "github.com/iden3/go-iden3-crypto/babyjub"
  11. "github.com/iden3/go-merkletree"
  12. )
  13. // WARNING: the generators in this file doesn't necessary follow the protocol
  14. // they are intended to check that the parsers between struct <==> DB are correct
  15. // GenBlocks generates block from, to block numbers. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
  16. func GenBlocks(from, to int64) []common.Block {
  17. var blocks []common.Block
  18. for i := from; i < to; i++ {
  19. blocks = append(blocks, common.Block{
  20. EthBlockNum: i,
  21. //nolint:gomnd
  22. Timestamp: time.Now().Add(time.Second * 13).UTC(),
  23. Hash: ethCommon.BigToHash(big.NewInt(int64(i))),
  24. })
  25. }
  26. return blocks
  27. }
  28. // GenTokens generates tokens. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
  29. func GenTokens(nTokens int, blocks []common.Block) []common.Token {
  30. tokens := []common.Token{}
  31. for i := 0; i < nTokens; i++ {
  32. token := common.Token{
  33. TokenID: common.TokenID(i),
  34. Name: fmt.Sprint(i),
  35. Symbol: fmt.Sprint(i),
  36. Decimals: uint64(i),
  37. EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
  38. EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  39. }
  40. if i%2 == 0 {
  41. token.USD = 3
  42. token.USDUpdate = time.Now()
  43. }
  44. tokens = append(tokens, token)
  45. }
  46. return tokens
  47. }
  48. // GenBatches generates batches. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
  49. func GenBatches(nBatches int, blocks []common.Block) []common.Batch {
  50. batches := []common.Batch{}
  51. collectedFees := make(map[common.TokenID]*big.Int)
  52. for i := 0; i < 64; i++ {
  53. collectedFees[common.TokenID(i)] = big.NewInt(int64(i))
  54. }
  55. for i := 0; i < nBatches; i++ {
  56. batch := common.Batch{
  57. BatchNum: common.BatchNum(i + 1),
  58. EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
  59. //nolint:gomnd
  60. ForgerAddr: ethCommon.BigToAddress(big.NewInt(6886723)),
  61. CollectedFees: collectedFees,
  62. StateRoot: common.Hash([]byte("duhdqlwiucgwqeiu")),
  63. //nolint:gomnd
  64. NumAccounts: 30,
  65. ExitRoot: common.Hash([]byte("tykertheuhtgenuer3iuw3b")),
  66. SlotNum: common.SlotNum(i),
  67. }
  68. if i%2 == 0 {
  69. batch.ForgeL1TxsNum = int64(i)
  70. }
  71. batches = append(batches, batch)
  72. }
  73. return batches
  74. }
  75. // GenAccounts generates accounts. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
  76. func GenAccounts(totalAccounts, userAccounts int, tokens []common.Token, userAddr *ethCommon.Address, userBjj *babyjub.PublicKey, batches []common.Batch) []common.Account {
  77. if totalAccounts < userAccounts {
  78. panic("totalAccounts must be greater than userAccounts")
  79. }
  80. accs := []common.Account{}
  81. for i := 0; i < totalAccounts; i++ {
  82. var addr ethCommon.Address
  83. var pubK *babyjub.PublicKey
  84. if i < userAccounts {
  85. addr = *userAddr
  86. pubK = userBjj
  87. } else {
  88. addr = ethCommon.BigToAddress(big.NewInt(int64(i)))
  89. privK := babyjub.NewRandPrivKey()
  90. pubK = privK.Public()
  91. }
  92. accs = append(accs, common.Account{
  93. Idx: common.Idx(i),
  94. TokenID: tokens[i%len(tokens)].TokenID,
  95. EthAddr: addr,
  96. BatchNum: batches[i%len(batches)].BatchNum,
  97. PublicKey: pubK,
  98. })
  99. }
  100. return accs
  101. }
  102. // GenL1Txs generates L1 txs. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
  103. func GenL1Txs(
  104. fromIdx int,
  105. totalTxs, nUserTxs int,
  106. userAddr *ethCommon.Address,
  107. accounts []common.Account,
  108. tokens []common.Token,
  109. blocks []common.Block,
  110. batches []common.Batch,
  111. ) ([]common.L1Tx, []common.L1Tx) {
  112. if totalTxs < nUserTxs {
  113. panic("totalTxs must be greater than userTxs")
  114. }
  115. userTxs := []common.L1Tx{}
  116. othersTxs := []common.L1Tx{}
  117. for i := 0; i < totalTxs; i++ {
  118. var tx common.L1Tx
  119. if batches[i%len(batches)].ForgeL1TxsNum != 0 {
  120. tx = common.L1Tx{
  121. TxID: common.TxID(common.Hash([]byte("L1_" + strconv.Itoa(fromIdx+i)))),
  122. ToForgeL1TxsNum: batches[i%len(batches)].ForgeL1TxsNum,
  123. Position: i,
  124. UserOrigin: i%2 == 0,
  125. TokenID: tokens[i%len(tokens)].TokenID,
  126. Amount: big.NewInt(int64(i + 1)),
  127. LoadAmount: big.NewInt(int64(i + 1)),
  128. EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
  129. Type: randomTxType(i),
  130. }
  131. if i%4 == 0 {
  132. tx.BatchNum = batches[i%len(batches)].BatchNum
  133. }
  134. } else {
  135. continue
  136. }
  137. if i < nUserTxs {
  138. var from, to *common.Account
  139. var err error
  140. if i%2 == 0 {
  141. from, err = randomAccount(i, true, userAddr, accounts)
  142. if err != nil {
  143. panic(err)
  144. }
  145. to, err = randomAccount(i, false, userAddr, accounts)
  146. if err != nil {
  147. panic(err)
  148. }
  149. } else {
  150. from, err = randomAccount(i, false, userAddr, accounts)
  151. if err != nil {
  152. panic(err)
  153. }
  154. to, err = randomAccount(i, true, userAddr, accounts)
  155. if err != nil {
  156. panic(err)
  157. }
  158. }
  159. tx.FromIdx = from.Idx
  160. tx.FromEthAddr = from.EthAddr
  161. tx.FromBJJ = from.PublicKey
  162. tx.ToIdx = to.Idx
  163. userTxs = append(userTxs, tx)
  164. } else {
  165. from, err := randomAccount(i, false, userAddr, accounts)
  166. if err != nil {
  167. panic(err)
  168. }
  169. to, err := randomAccount(i, false, userAddr, accounts)
  170. if err != nil {
  171. panic(err)
  172. }
  173. tx.FromIdx = from.Idx
  174. tx.FromEthAddr = from.EthAddr
  175. tx.FromBJJ = from.PublicKey
  176. tx.ToIdx = to.Idx
  177. othersTxs = append(othersTxs, tx)
  178. }
  179. }
  180. return userTxs, othersTxs
  181. }
  182. // GenL2Txs generates L2 txs. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
  183. func GenL2Txs(
  184. fromIdx int,
  185. totalTxs, nUserTxs int,
  186. userAddr *ethCommon.Address,
  187. accounts []common.Account,
  188. tokens []common.Token,
  189. blocks []common.Block,
  190. batches []common.Batch,
  191. ) ([]common.L2Tx, []common.L2Tx) {
  192. if totalTxs < nUserTxs {
  193. panic("totalTxs must be greater than userTxs")
  194. }
  195. userTxs := []common.L2Tx{}
  196. othersTxs := []common.L2Tx{}
  197. for i := 0; i < totalTxs; i++ {
  198. tx := common.L2Tx{
  199. TxID: common.TxID(common.Hash([]byte("L2_" + strconv.Itoa(fromIdx+i)))),
  200. BatchNum: batches[i%len(batches)].BatchNum,
  201. Position: i,
  202. //nolint:gomnd
  203. Amount: big.NewInt(int64(i + 1)),
  204. //nolint:gomnd
  205. Fee: common.FeeSelector(i % 256),
  206. Nonce: common.Nonce(i + 1),
  207. EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
  208. Type: randomTxType(i),
  209. }
  210. if i < nUserTxs {
  211. var from, to *common.Account
  212. var err error
  213. if i%2 == 0 {
  214. from, err = randomAccount(i, true, userAddr, accounts)
  215. if err != nil {
  216. panic(err)
  217. }
  218. to, err = randomAccount(i, false, userAddr, accounts)
  219. if err != nil {
  220. panic(err)
  221. }
  222. } else {
  223. from, err = randomAccount(i, false, userAddr, accounts)
  224. if err != nil {
  225. panic(err)
  226. }
  227. to, err = randomAccount(i, true, userAddr, accounts)
  228. if err != nil {
  229. panic(err)
  230. }
  231. }
  232. tx.FromIdx = from.Idx
  233. tx.ToIdx = to.Idx
  234. userTxs = append(userTxs, tx)
  235. } else {
  236. from, err := randomAccount(i, false, userAddr, accounts)
  237. if err != nil {
  238. panic(err)
  239. }
  240. to, err := randomAccount(i, false, userAddr, accounts)
  241. if err != nil {
  242. panic(err)
  243. }
  244. tx.FromIdx = from.Idx
  245. tx.ToIdx = to.Idx
  246. othersTxs = append(othersTxs, tx)
  247. }
  248. }
  249. return userTxs, othersTxs
  250. }
  251. // GenCoordinators generates coordinators. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
  252. func GenCoordinators(nCoords int, blocks []common.Block) []common.Coordinator {
  253. coords := []common.Coordinator{}
  254. for i := 0; i < nCoords; i++ {
  255. coords = append(coords, common.Coordinator{
  256. EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
  257. Forger: ethCommon.BigToAddress(big.NewInt(int64(i))),
  258. WithdrawAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
  259. URL: "https://foo.bar",
  260. })
  261. }
  262. return coords
  263. }
  264. // GenBids generates bids. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
  265. func GenBids(nBids int, blocks []common.Block, coords []common.Coordinator) []common.Bid {
  266. bids := []common.Bid{}
  267. for i := 0; i < nBids; i++ {
  268. bids = append(bids, common.Bid{
  269. SlotNum: common.SlotNum(i),
  270. BidValue: big.NewInt(int64(i)),
  271. EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
  272. ForgerAddr: coords[i%len(blocks)].Forger,
  273. })
  274. }
  275. return bids
  276. }
  277. // GenExitTree generates an exitTree (as an array of Exits)
  278. //nolint:gomnd
  279. func GenExitTree(n int) []common.ExitInfo {
  280. exitTree := make([]common.ExitInfo, n)
  281. for i := 0; i < n; i++ {
  282. exitTree[i] = common.ExitInfo{
  283. BatchNum: common.BatchNum(i + 1),
  284. InstantWithdrawn: nil,
  285. DelayedWithdrawRequest: nil,
  286. DelayedWithdrawn: nil,
  287. AccountIdx: common.Idx(i * 10),
  288. MerkleProof: &merkletree.CircomVerifierProof{
  289. Root: &merkletree.Hash{byte(i), byte(i + 1)},
  290. Siblings: []*big.Int{
  291. big.NewInt(int64(i) * 10),
  292. big.NewInt(int64(i)*100 + 1),
  293. big.NewInt(int64(i)*1000 + 2)},
  294. OldKey: &merkletree.Hash{byte(i * 1), byte(i*1 + 1)},
  295. OldValue: &merkletree.Hash{byte(i * 2), byte(i*2 + 1)},
  296. IsOld0: i%2 == 0,
  297. Key: &merkletree.Hash{byte(i * 3), byte(i*3 + 1)},
  298. Value: &merkletree.Hash{byte(i * 4), byte(i*4 + 1)},
  299. Fnc: i % 2,
  300. },
  301. Balance: big.NewInt(int64(i) * 1000),
  302. }
  303. }
  304. return exitTree
  305. }
  306. func randomAccount(seed int, userAccount bool, userAddr *ethCommon.Address, accs []common.Account) (*common.Account, error) {
  307. i := seed % len(accs)
  308. firstI := i
  309. for {
  310. acc := accs[i]
  311. if userAccount && *userAddr == acc.EthAddr {
  312. return &acc, nil
  313. }
  314. if !userAccount && (userAddr == nil || *userAddr != acc.EthAddr) {
  315. return &acc, nil
  316. }
  317. i++
  318. i = i % len(accs)
  319. if i == firstI {
  320. return &acc, errors.New("Didnt found any account matchinng the criteria")
  321. }
  322. }
  323. }
  324. func randomTxType(seed int) common.TxType {
  325. //nolint:gomnd
  326. switch seed % 11 {
  327. case 0:
  328. return common.TxTypeExit
  329. //nolint:gomnd
  330. case 1:
  331. return common.TxTypeWithdrawn
  332. //nolint:gomnd
  333. case 2:
  334. return common.TxTypeTransfer
  335. //nolint:gomnd
  336. case 3:
  337. return common.TxTypeDeposit
  338. //nolint:gomnd
  339. case 4:
  340. return common.TxTypeCreateAccountDeposit
  341. //nolint:gomnd
  342. case 5:
  343. return common.TxTypeCreateAccountDepositTransfer
  344. //nolint:gomnd
  345. case 6:
  346. return common.TxTypeDepositTransfer
  347. //nolint:gomnd
  348. case 7:
  349. return common.TxTypeForceTransfer
  350. //nolint:gomnd
  351. case 8:
  352. return common.TxTypeForceExit
  353. //nolint:gomnd
  354. case 9:
  355. return common.TxTypeTransferToEthAddr
  356. //nolint:gomnd
  357. case 10:
  358. return common.TxTypeTransferToBJJ
  359. default:
  360. return common.TxTypeTransfer
  361. }
  362. }