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.

108 lines
3.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.PoolL2Tx) {
  46. accounts := GenerateKeys(t, instructions.Accounts)
  47. // debug
  48. // fmt.Println("accounts:")
  49. // for n, a := range accounts {
  50. // fmt.Printf(" %s: bjj:%s - addr:%s\n", n, a.BJJ.Public().String()[:10], a.Addr.Hex()[:10])
  51. // }
  52. var l1txs []*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. l1txs = append(l1txs, &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. idx++
  70. }
  71. case common.TxTypeTransfer:
  72. tx := common.PoolL2Tx{
  73. // TxID: nil,
  74. FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx,
  75. ToIdx: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx,
  76. ToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr,
  77. ToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(),
  78. TokenID: inst.TokenID,
  79. Amount: big.NewInt(int64(inst.Amount)),
  80. Fee: common.FeeSelector(inst.Fee),
  81. Nonce: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce,
  82. State: common.PoolL2TxStatePending,
  83. Timestamp: time.Now(),
  84. BatchNum: 0,
  85. Type: common.TxTypeTransfer,
  86. }
  87. // TODO once signature function is ready, perform
  88. // signature and set it to tx.Signature
  89. accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce++
  90. l2txs = append(l2txs, &tx)
  91. default:
  92. continue
  93. }
  94. }
  95. return l1txs, l2txs
  96. }