hermez-node/prover/prover_test.go
prover/prover_test.goRaw
1package prover
2
3import (
4 "context"
5 "math/big"
6 "os"
7 "testing"
8 "time"
9
10 "github.com/hermeznetwork/hermez-node/common"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13)
14
15const apiURL = "http://localhost:3000/api"
16const timeCons = 1 * time.Second
17
18var proofServerClient *ProofServerClient
19
20func TestMain(m *testing.M) {
21 exitVal := 0
22 if os.Getenv("INTEGRATION") != "" {
23 proofServerClient = NewProofServerClient(apiURL, timeCons)
24 err := proofServerClient.WaitReady(context.Background())
25 if err != nil {
26 panic(err)
27 }
28 exitVal = m.Run()
29 }
30 os.Exit(exitVal)
31}
32
33func TestApiServer(t *testing.T) {
34 t.Run("testAPIStatus", testAPIStatus)
35 t.Run("testCalculateProof", testCalculateProof)
36 time.Sleep(time.Second / 4)
37 err := proofServerClient.WaitReady(context.Background())
38 require.NoError(t, err)
39 t.Run("testGetProof", testGetProof)
40 t.Run("testCancel", testCancel)
41}
42
43func testAPIStatus(t *testing.T) {
44 status, err := proofServerClient.apiStatus(context.Background())
45 require.NoError(t, err)
46 assert.Equal(t, true, status.Status.IsReady())
47}
48
49func testCalculateProof(t *testing.T) {
50 zkInputs := common.NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
51 err := proofServerClient.CalculateProof(context.Background(), zkInputs)
52 require.NoError(t, err)
53}
54
55func testGetProof(t *testing.T) {
56 proof, err := proofServerClient.GetProof(context.Background())
57 require.NoError(t, err)
58 require.NotNil(t, proof)
59 require.NotNil(t, proof.PiA)
60 require.NotNil(t, proof.PiB)
61 require.NotNil(t, proof.PiC)
62 require.NotNil(t, proof.Protocol)
63}
64
65func testCancel(t *testing.T) {
66 zkInputs := common.NewZKInputs(100, 16, 512, 24, 32, big.NewInt(1))
67 err := proofServerClient.CalculateProof(context.Background(), zkInputs)
68 require.NoError(t, err)
69 // TODO: remove sleep when the server has been reviewed
70 time.Sleep(time.Second / 4)
71 err = proofServerClient.Cancel(context.Background())
72 require.NoError(t, err)
73 status, err := proofServerClient.apiStatus(context.Background())
74 require.NoError(t, err)
75 for status.Status == StatusCodeBusy {
76 time.Sleep(proofServerClient.timeCons)
77 status, err = proofServerClient.apiStatus(context.Background())
78 require.NoError(t, err)
79 }
80 assert.Equal(t, StatusCodeAborted, status.Status)
81}