arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/zkproof/zkproof_test.go
test/zkproof/zkproof_test.goRaw
 1package zkproof
 2
 3import (
 4	"context"
 5	"encoding/json"
 6	"fmt"
 7	"io/ioutil"
 8	"os"
 9	"testing"
10	"time"
11
12	"github.com/hermeznetwork/hermez-node/db/statedb"
13	"github.com/hermeznetwork/hermez-node/prover"
14	"github.com/hermeznetwork/hermez-node/test/txsets"
15	"github.com/hermeznetwork/hermez-node/txprocessor"
16	"github.com/stretchr/testify/assert"
17	"github.com/stretchr/testify/require"
18)
19
20var proofServerURL string
21
22const pollInterval = 200 * time.Millisecond
23
24func TestMain(m *testing.M) {
25	exitVal := 0
26	proofServerURL = os.Getenv("PROOF_SERVER_URL")
27	if proofServerURL != "" {
28		exitVal = m.Run()
29	}
30	os.Exit(exitVal)
31}
32
33const MaxTx = 376
34const NLevels = 32
35const MaxL1Tx = 256
36const MaxFeeTx = 64
37const ChainID uint16 = 1
38
39func TestZKInputs5(t *testing.T) {
40	dir, err := ioutil.TempDir("", "tmpdb")
41	require.NoError(t, err)
42	defer assert.Nil(t, os.RemoveAll(dir))
43
44	sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeBatchBuilder, NLevels)
45	require.NoError(t, err)
46
47	_, coordIdxs, l1UserTxs, l1CoordTxs, l2Txs := txsets.GenerateTxsZKInputs5(t, ChainID)
48
49	config := txprocessor.Config{
50		NLevels:  uint32(NLevels),
51		MaxTx:    MaxTx,
52		MaxL1Tx:  MaxL1Tx,
53		MaxFeeTx: MaxFeeTx,
54		ChainID:  ChainID,
55	}
56	tp := txprocessor.NewTxProcessor(sdb, config)
57
58	// skip first batch to do the test with BatchNum=1
59	_, err = tp.ProcessTxs(nil, nil, nil, nil)
60	require.NoError(t, err)
61
62	ptOut, err := tp.ProcessTxs(coordIdxs, l1UserTxs, l1CoordTxs, l2Txs)
63	require.NoError(t, err)
64
65	// Store zkinputs json for debugging purposes
66	zkInputsJSON, err := json.Marshal(ptOut.ZKInputs)
67	require.NoError(t, err)
68	err = ioutil.WriteFile("/tmp/dbgZKInputs.json", zkInputsJSON, 0640) //nolint:gosec
69	require.NoError(t, err)
70
71	proofServerClient := prover.NewProofServerClient(proofServerURL, pollInterval)
72	err = proofServerClient.WaitReady(context.Background())
73	require.NoError(t, err)
74	err = proofServerClient.CalculateProof(context.Background(), ptOut.ZKInputs)
75	require.NoError(t, err)
76	proof, pubInputs, err := proofServerClient.GetProof(context.Background())
77	require.NoError(t, err)
78	fmt.Printf("proof: %#v\n", proof)
79	fmt.Printf("pubInputs: %#v\n", pubInputs)
80}