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.

151 lines
5.3 KiB

  1. package test
  2. import (
  3. "crypto/ecdsa"
  4. "math/big"
  5. "strconv"
  6. "testing"
  7. "time"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  10. "github.com/hermeznetwork/hermez-node/common"
  11. "github.com/iden3/go-iden3-crypto/babyjub"
  12. )
  13. type Account struct {
  14. BJJ *babyjub.PrivateKey
  15. Addr ethCommon.Address
  16. Idx common.Idx
  17. Nonce common.Nonce
  18. }
  19. // GenerateKeys generates BabyJubJub & Address keys for the given list of
  20. // account names in a deterministic way. This means, that for the same given
  21. // 'accNames' the keys will be always the same.
  22. func GenerateKeys(t *testing.T, accNames []string) map[string]*Account {
  23. acc := make(map[string]*Account)
  24. for i := 1; i < len(accNames)+1; i++ {
  25. // babyjubjub key
  26. var sk babyjub.PrivateKey
  27. copy(sk[:], []byte(strconv.Itoa(i))) // only for testing
  28. // eth address
  29. var key ecdsa.PrivateKey
  30. key.D = big.NewInt(int64(i)) // only for testing
  31. key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
  32. key.Curve = ethCrypto.S256()
  33. addr := ethCrypto.PubkeyToAddress(key.PublicKey)
  34. a := Account{
  35. BJJ: &sk,
  36. Addr: addr,
  37. Nonce: 0,
  38. }
  39. acc[accNames[i-1]] = &a
  40. }
  41. return acc
  42. }
  43. // GenerateTestTxs generates L1Tx & PoolL2Tx in a deterministic way for the
  44. // given Instructions.
  45. func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx, [][]*common.L1Tx, [][]*common.PoolL2Tx) {
  46. accounts := GenerateKeys(t, instructions.Accounts)
  47. l1CreatedAccounts := make(map[string]*Account)
  48. var batchL1txs []*common.L1Tx
  49. var batchCoordinatorL1txs []*common.L1Tx
  50. var batchL2txs []*common.PoolL2Tx
  51. var l1txs [][]*common.L1Tx
  52. var coordinatorL1txs [][]*common.L1Tx
  53. var l2txs [][]*common.PoolL2Tx
  54. idx := 1
  55. for _, inst := range instructions.Instructions {
  56. switch inst.Type {
  57. case common.TxTypeCreateAccountDeposit:
  58. tx := common.L1Tx{
  59. // TxID
  60. FromEthAddr: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Addr,
  61. FromBJJ: accounts[idxTokenIDToString(inst.From, inst.TokenID)].BJJ.Public(),
  62. TokenID: inst.TokenID,
  63. LoadAmount: big.NewInt(int64(inst.Amount)),
  64. Type: common.TxTypeCreateAccountDeposit,
  65. }
  66. batchL1txs = append(batchL1txs, &tx)
  67. if accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx == common.Idx(0) { // if account.Idx is not set yet, set it and increment idx
  68. accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx = common.Idx(idx)
  69. l1CreatedAccounts[idxTokenIDToString(inst.From, inst.TokenID)] = accounts[idxTokenIDToString(inst.From, inst.TokenID)]
  70. idx++
  71. }
  72. case common.TxTypeTransfer:
  73. // if account of receiver does not exist, create a new CoordinatorL1Tx creating the account
  74. if _, ok := l1CreatedAccounts[idxTokenIDToString(inst.To, inst.TokenID)]; !ok {
  75. tx := common.L1Tx{
  76. FromEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr,
  77. FromBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(),
  78. TokenID: inst.TokenID,
  79. LoadAmount: big.NewInt(int64(inst.Amount)),
  80. Type: common.TxTypeCreateAccountDeposit,
  81. }
  82. accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx = common.Idx(idx)
  83. l1CreatedAccounts[idxTokenIDToString(inst.To, inst.TokenID)] = accounts[idxTokenIDToString(inst.To, inst.TokenID)]
  84. batchCoordinatorL1txs = append(batchCoordinatorL1txs, &tx)
  85. idx++
  86. }
  87. tx := common.PoolL2Tx{
  88. // TxID: nil,
  89. FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx,
  90. ToIdx: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx,
  91. ToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr,
  92. ToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(),
  93. TokenID: inst.TokenID,
  94. Amount: big.NewInt(int64(inst.Amount)),
  95. Fee: common.FeeSelector(inst.Fee),
  96. Nonce: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce,
  97. State: common.PoolL2TxStatePending,
  98. Timestamp: time.Now(),
  99. BatchNum: 0,
  100. RqToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr,
  101. RqToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(),
  102. Type: common.TxTypeTransfer,
  103. }
  104. // perform signature and set it to tx.Signature
  105. toSign, err := tx.HashToSign()
  106. if err != nil {
  107. panic(err)
  108. }
  109. sig := accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.SignPoseidon(toSign)
  110. tx.Signature = sig
  111. accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce++
  112. batchL2txs = append(batchL2txs, &tx)
  113. case common.TxTypeExit, common.TxTypeForceExit:
  114. tx := common.L1Tx{
  115. FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx,
  116. ToIdx: common.Idx(1), // as is an Exit
  117. TokenID: inst.TokenID,
  118. Amount: big.NewInt(int64(inst.Amount)),
  119. Type: common.TxTypeExit,
  120. }
  121. batchL1txs = append(batchL1txs, &tx)
  122. case TypeNewBatch:
  123. l1txs = append(l1txs, batchL1txs)
  124. coordinatorL1txs = append(coordinatorL1txs, batchCoordinatorL1txs)
  125. l2txs = append(l2txs, batchL2txs)
  126. batchL1txs = []*common.L1Tx{}
  127. batchCoordinatorL1txs = []*common.L1Tx{}
  128. batchL2txs = []*common.PoolL2Tx{}
  129. default:
  130. continue
  131. }
  132. }
  133. l1txs = append(l1txs, batchL1txs)
  134. coordinatorL1txs = append(coordinatorL1txs, batchCoordinatorL1txs)
  135. l2txs = append(l2txs, batchL2txs)
  136. return l1txs, coordinatorL1txs, l2txs
  137. }