arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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
15var apiURL = "http://localhost:3000/api"
16
17const pollInterval = 1 * time.Second
18
19var proofServerClient *ProofServerClient
20
21func TestMain(m *testing.M) {
22	exitVal := 0
23	if os.Getenv("INTEGRATION") != "" {
24		_apiURL := os.Getenv("PROOF_SERVER_URL")
25		if _apiURL != "" {
26			apiURL = _apiURL
27		}
28		proofServerClient = NewProofServerClient(apiURL, pollInterval)
29		err := proofServerClient.WaitReady(context.Background())
30		if err != nil {
31			panic(err)
32		}
33		exitVal = m.Run()
34	}
35	os.Exit(exitVal)
36}
37
38func TestApiServer(t *testing.T) {
39	t.Run("testAPIStatus", testAPIStatus)
40	t.Run("testCalculateProof", testCalculateProof)
41	time.Sleep(time.Second / 4)
42	err := proofServerClient.WaitReady(context.Background())
43	require.NoError(t, err)
44	t.Run("testGetProof", testGetProof)
45	t.Run("testCancel", testCancel)
46}
47
48func testAPIStatus(t *testing.T) {
49	status, err := proofServerClient.apiStatus(context.Background())
50	require.NoError(t, err)
51	assert.Equal(t, true, status.Status.IsReady())
52}
53
54func testCalculateProof(t *testing.T) {
55	zkInputs := common.NewZKInputs(0, 100, 16, 512, 24, 32, big.NewInt(1))
56	err := proofServerClient.CalculateProof(context.Background(), zkInputs)
57	require.NoError(t, err)
58}
59
60func testGetProof(t *testing.T) {
61	proof, pubInputs, err := proofServerClient.GetProof(context.Background())
62	require.NoError(t, err)
63	assert.NotNil(t, proof.PiA)
64	assert.NotEqual(t, [2]*big.Int{}, proof.PiA)
65	assert.NotNil(t, proof.PiB)
66	assert.NotEqual(t, [3][2]*big.Int{}, proof.PiB)
67	assert.NotNil(t, proof.PiC)
68	assert.NotEqual(t, [2]*big.Int{}, proof.PiC)
69	assert.NotNil(t, proof.Protocol)
70	assert.NotEqual(t, 0, len(pubInputs))
71}
72
73func testCancel(t *testing.T) {
74	zkInputs := common.NewZKInputs(0, 100, 16, 512, 24, 32, big.NewInt(1))
75	err := proofServerClient.CalculateProof(context.Background(), zkInputs)
76	require.NoError(t, err)
77	// TODO: remove sleep when the server has been reviewed
78	time.Sleep(time.Second / 4)
79	err = proofServerClient.Cancel(context.Background())
80	require.NoError(t, err)
81	status, err := proofServerClient.apiStatus(context.Background())
82	require.NoError(t, err)
83	for status.Status == StatusCodeBusy {
84		time.Sleep(proofServerClient.pollInterval)
85		status, err = proofServerClient.apiStatus(context.Background())
86		require.NoError(t, err)
87	}
88	assert.Equal(t, StatusCodeAborted, status.Status)
89}