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.

182 lines
5.3 KiB

  1. package zkproof
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "math/big"
  9. "os"
  10. "strconv"
  11. "testing"
  12. "time"
  13. ethCommon "github.com/ethereum/go-ethereum/common"
  14. "github.com/hermeznetwork/hermez-node/common"
  15. "github.com/hermeznetwork/hermez-node/db/statedb"
  16. "github.com/hermeznetwork/hermez-node/prover"
  17. "github.com/hermeznetwork/hermez-node/test/til"
  18. "github.com/hermeznetwork/hermez-node/txprocessor"
  19. "github.com/iden3/go-iden3-crypto/babyjub"
  20. "github.com/stretchr/testify/assert"
  21. "github.com/stretchr/testify/require"
  22. )
  23. var proofServerURL string
  24. const pollInterval = 200 * time.Millisecond
  25. func TestMain(m *testing.M) {
  26. exitVal := 0
  27. proofServerURL = os.Getenv("PROOF_SERVER_URL")
  28. if proofServerURL != "" {
  29. exitVal = m.Run()
  30. }
  31. os.Exit(exitVal)
  32. }
  33. func generateJsUsers(t *testing.T) []til.User {
  34. // same values than in the js test
  35. // skJsHex is equivalent to the 0000...000i js private key in commonjs
  36. skJsHex := []string{"7eb258e61862aae75c6c1d1f7efae5006ffc9e4d5596a6ff95f3df4ea209ea7f", "c005700f76f4b4cec710805c21595688648524df0a9d467afae537b7a7118819", "b373d14c67fb2a517bf4ac831c93341eec8e1b38dbc14e7d725b292a7cf84707", "2064b68d04a7aaae0ac3b36bf6f1850b380f1423be94a506c531940bd4a48b76"}
  37. addrHex := []string{"0x7e5f4552091a69125d5dfcb7b8c2659029395bdf", "0x2b5ad5c4795c026514f8317c7a215e218dccd6cf", "0x6813eb9362372eef6200f3b1dbc3f819671cba69", "0x1eff47bc3a10a45d4b230b5d10e37751fe6aa718"}
  38. var users []til.User
  39. for i := 0; i < len(skJsHex); i++ {
  40. skJs, err := hex.DecodeString(skJsHex[i])
  41. require.NoError(t, err)
  42. var sk babyjub.PrivateKey
  43. copy(sk[:], skJs)
  44. // bjj := sk.Public()
  45. user := til.User{
  46. Name: strconv.Itoa(i),
  47. BJJ: &sk,
  48. Addr: ethCommon.HexToAddress(addrHex[i]),
  49. }
  50. users = append(users, user)
  51. }
  52. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", users[0].BJJ.Public().String())
  53. assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", users[1].BJJ.Public().String())
  54. assert.Equal(t, "38ffa002724562eb2a952a2503e206248962406cf16392ff32759b6f2a41fe11", users[2].BJJ.Public().String())
  55. assert.Equal(t, "c719e6401190be7fa7fbfcd3448fe2755233c01575341a3b09edadf5454f760b", users[3].BJJ.Public().String())
  56. return users
  57. }
  58. func signL2Tx(t *testing.T, chainID uint16, user til.User, l2Tx common.PoolL2Tx) common.PoolL2Tx {
  59. toSign, err := l2Tx.HashToSign(chainID)
  60. require.NoError(t, err)
  61. sig := user.BJJ.SignPoseidon(toSign)
  62. l2Tx.Signature = sig.Compress()
  63. return l2Tx
  64. }
  65. const MaxTx = 376
  66. const NLevels = 32
  67. const MaxL1Tx = 256
  68. const MaxFeeTx = 64
  69. const ChainID uint16 = 1
  70. func TestTxProcessor(t *testing.T) {
  71. dir, err := ioutil.TempDir("", "tmpdb")
  72. require.NoError(t, err)
  73. defer assert.Nil(t, os.RemoveAll(dir))
  74. sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, NLevels)
  75. require.NoError(t, err)
  76. // same values than in the js test
  77. users := generateJsUsers(t)
  78. l1Txs := []common.L1Tx{
  79. {
  80. FromIdx: 0,
  81. DepositAmount: big.NewInt(16000000),
  82. Amount: big.NewInt(0),
  83. TokenID: 1,
  84. FromBJJ: users[0].BJJ.Public().Compress(),
  85. FromEthAddr: users[0].Addr,
  86. ToIdx: 0,
  87. Type: common.TxTypeCreateAccountDeposit,
  88. UserOrigin: true,
  89. },
  90. {
  91. FromIdx: 0,
  92. DepositAmount: big.NewInt(16000000),
  93. Amount: big.NewInt(0),
  94. TokenID: 1,
  95. FromBJJ: users[1].BJJ.Public().Compress(),
  96. FromEthAddr: users[1].Addr,
  97. ToIdx: 0,
  98. Type: common.TxTypeCreateAccountDeposit,
  99. UserOrigin: true,
  100. },
  101. {
  102. FromIdx: 257,
  103. DepositAmount: big.NewInt(0),
  104. Amount: big.NewInt(1000),
  105. TokenID: 1,
  106. FromBJJ: users[1].BJJ.Public().Compress(),
  107. FromEthAddr: users[1].Addr,
  108. ToIdx: 1,
  109. Type: common.TxTypeForceExit,
  110. UserOrigin: true,
  111. },
  112. }
  113. l2Txs := []common.PoolL2Tx{
  114. {
  115. FromIdx: 256,
  116. ToIdx: 257,
  117. TokenID: 1,
  118. Amount: big.NewInt(1000),
  119. Nonce: 0,
  120. Fee: 126,
  121. Type: common.TxTypeTransfer,
  122. },
  123. {
  124. FromIdx: 256,
  125. ToIdx: 1,
  126. TokenID: 1,
  127. Amount: big.NewInt(1000),
  128. Nonce: 1,
  129. Fee: 126,
  130. Type: common.TxTypeExit,
  131. },
  132. }
  133. l2Txs[0] = signL2Tx(t, ChainID, users[0], l2Txs[0])
  134. l2Txs[1] = signL2Tx(t, ChainID, users[0], l2Txs[1])
  135. config := txprocessor.Config{
  136. NLevels: uint32(NLevels),
  137. MaxTx: MaxTx,
  138. MaxL1Tx: MaxL1Tx,
  139. MaxFeeTx: MaxFeeTx,
  140. ChainID: ChainID,
  141. }
  142. tp := txprocessor.NewTxProcessor(sdb, config)
  143. // skip first batch to do the test with BatchNum=1
  144. _, err = tp.ProcessTxs(nil, nil, nil, nil)
  145. require.NoError(t, err)
  146. coordIdxs := []common.Idx{257}
  147. ptOut, err := tp.ProcessTxs(coordIdxs, l1Txs, nil, l2Txs)
  148. require.NoError(t, err)
  149. // Store zkinputs json for debugging purposes
  150. zkInputsJSON, err := json.Marshal(ptOut.ZKInputs)
  151. require.NoError(t, err)
  152. err = ioutil.WriteFile("/tmp/dbgZKInputs.json", zkInputsJSON, 0640) //nolint:gosec
  153. require.NoError(t, err)
  154. proofServerClient := prover.NewProofServerClient(proofServerURL, pollInterval)
  155. err = proofServerClient.WaitReady(context.Background())
  156. require.NoError(t, err)
  157. err = proofServerClient.CalculateProof(context.Background(), ptOut.ZKInputs)
  158. require.NoError(t, err)
  159. proof, pubInputs, err := proofServerClient.GetProof(context.Background())
  160. require.NoError(t, err)
  161. fmt.Printf("proof: %#v\n", proof)
  162. fmt.Printf("pubInputs: %#v\n", pubInputs)
  163. }