arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/proofserver/proofserver.go
test/proofserver/proofserver.goRaw
  1package proofserver
  2
  3import (
  4	"context"
  5	"fmt"
  6	"io/ioutil"
  7	"net"
  8	"net/http"
  9	"sync"
 10	"time"
 11
 12	"github.com/gin-contrib/cors"
 13	"github.com/gin-gonic/gin"
 14	"github.com/hermeznetwork/hermez-node/log"
 15	"github.com/hermeznetwork/hermez-node/prover"
 16	"github.com/hermeznetwork/tracerr"
 17)
 18
 19type msg struct {
 20	value string
 21	ackCh chan bool
 22}
 23
 24func newMsg(value string) msg {
 25	return msg{
 26		value: value,
 27		ackCh: make(chan bool),
 28	}
 29}
 30
 31// Mock proof server
 32type Mock struct {
 33	addr   string
 34	status prover.StatusCode
 35	sync.RWMutex
 36	proof           string
 37	pubData         string
 38	counter         int
 39	msgCh           chan msg
 40	wg              sync.WaitGroup
 41	provingDuration time.Duration
 42}
 43
 44// NewMock creates a new mock server
 45func NewMock(addr string, provingDuration time.Duration) *Mock {
 46	return &Mock{
 47		addr:            addr,
 48		status:          prover.StatusCodeReady,
 49		proof:           "",
 50		pubData:         "",
 51		counter:         0,
 52		msgCh:           make(chan msg),
 53		provingDuration: provingDuration,
 54	}
 55}
 56
 57func (s *Mock) err(c *gin.Context, err error) {
 58	c.JSON(http.StatusInternalServerError, prover.ErrorServer{
 59		Status:  "error",
 60		Message: err.Error(),
 61	})
 62}
 63
 64func (s *Mock) handleCancel(c *gin.Context) {
 65	msg := newMsg("cancel")
 66	s.msgCh <- msg
 67	<-msg.ackCh
 68	c.JSON(http.StatusOK, "OK")
 69}
 70
 71/* Status example from the real server proof:
 72
 73Status:
 74{
 75  "proof": "{\n    \"pi_a\": [\n        \"1368015179489954701390400359078579693043519447331113978918064868415326638035\",\n        \"9918110051302171585080402603319702774565515993150576347155970296011118125764\",\n        \"1\"\n    ],\n    \"pi_b\": [\n        [\n            \"10857046999023057135944570762232829481370756359578518086990519993285655852781\",\n            \"11559732032986387107991004021392285783925812861821192530917403151452391805634\"\n        ],\n        [\n            \"8495653923123431417604973247489272438418190587263600148770280649306958101930\",\n            \"4082367875863433681332203403145435568316851327593401208105741076214120093531\"\n        ],\n        [\n            \"1\",\n            \"0\"\n        ]\n    ],\n    \"pi_c\": [\n        \"1368015179489954701390400359078579693043519447331113978918064868415326638035\",\n        \"9918110051302171585080402603319702774565515993150576347155970296011118125764\",\n        \"1\"\n    ],\n    \"protocol\": \"groth\"\n}\n",
 76  "pubData": "[\n    \"8863150934551775031093873719629424744398133643983814385850330952980893030086\"\n]\n",
 77  "status": "success"
 78}
 79
 80proof:
 81{
 82    "pi_a": [
 83        "1368015179489954701390400359078579693043519447331113978918064868415326638035",
 84        "9918110051302171585080402603319702774565515993150576347155970296011118125764",
 85        "1"
 86    ],
 87    "pi_b": [
 88        [
 89            "10857046999023057135944570762232829481370756359578518086990519993285655852781",
 90            "11559732032986387107991004021392285783925812861821192530917403151452391805634"
 91        ],
 92        [
 93            "8495653923123431417604973247489272438418190587263600148770280649306958101930",
 94            "4082367875863433681332203403145435568316851327593401208105741076214120093531"
 95        ],
 96        [
 97            "1",
 98            "0"
 99        ]
100    ],
101    "pi_c": [
102        "1368015179489954701390400359078579693043519447331113978918064868415326638035",
103        "9918110051302171585080402603319702774565515993150576347155970296011118125764",
104        "1"
105    ],
106    "protocol": "groth"
107}
108
109pubData:
110[
111    "8863150934551775031093873719629424744398133643983814385850330952980893030086"
112]
113*/
114
115func (s *Mock) handleStatus(c *gin.Context) {
116	s.RLock()
117	c.JSON(http.StatusOK, prover.Status{
118		Status:  s.status,
119		Proof:   s.proof,
120		PubData: s.pubData,
121	})
122	s.RUnlock()
123}
124
125func (s *Mock) handleInput(c *gin.Context) {
126	s.RLock()
127	if !s.status.IsReady() {
128		s.err(c, fmt.Errorf("not ready"))
129		s.RUnlock()
130		return
131	}
132	s.RUnlock()
133	_, err := ioutil.ReadAll(c.Request.Body)
134	if err != nil {
135		s.err(c, err)
136		return
137	}
138	msg := newMsg("prove")
139	s.msgCh <- msg
140	<-msg.ackCh
141	c.JSON(http.StatusOK, "OK")
142}
143
144const longWaitDuration = 999 * time.Hour
145
146// const provingDuration = 2 * time.Second
147
148func (s *Mock) runProver(ctx context.Context) {
149	waitCh := time.After(longWaitDuration)
150	for {
151		select {
152		case <-ctx.Done():
153			return
154		case msg := <-s.msgCh:
155			switch msg.value {
156			case "cancel":
157				waitCh = time.After(longWaitDuration)
158				s.Lock()
159				if !s.status.IsReady() {
160					s.status = prover.StatusCodeAborted
161				}
162				s.Unlock()
163			case "prove":
164				waitCh = time.After(s.provingDuration)
165				s.Lock()
166				s.status = prover.StatusCodeBusy
167				s.Unlock()
168			}
169			msg.ackCh <- true
170		case <-waitCh:
171			waitCh = time.After(longWaitDuration)
172			s.Lock()
173			if s.status != prover.StatusCodeBusy {
174				s.Unlock()
175				continue
176			}
177			i := s.counter * 100 //nolint:gomnd
178			s.counter++
179			// Mock data
180			s.proof = fmt.Sprintf(`{
181				"pi_a": ["%v", "%v", "1"],
182				"pi_b": [["%v", "%v"],["%v", "%v"],["1", "0"]],
183				"pi_c": ["%v", "%v", "1"],
184				"protocol": "groth"
185			}`, i, i+1, i+2, i+3, i+4, i+5, i+6, i+7) //nolint:gomnd
186			s.pubData = fmt.Sprintf(`[
187				"%v"
188			]`, i+42) //nolint:gomnd
189			s.status = prover.StatusCodeSuccess
190			s.Unlock()
191		}
192	}
193}
194
195// Run the mock server.  Use ctx to stop it via cancel
196func (s *Mock) Run(ctx context.Context) error {
197	api := gin.Default()
198	api.Use(cors.Default())
199
200	apiGroup := api.Group("/api")
201	apiGroup.GET("/status", s.handleStatus)
202	apiGroup.POST("/input", s.handleInput)
203	apiGroup.POST("/cancel", s.handleCancel)
204
205	debugAPIServer := &http.Server{
206		Handler: api,
207		// Use some hardcoded numberes that are suitable for testing
208		ReadTimeout:    30 * time.Second, //nolint:gomnd
209		WriteTimeout:   30 * time.Second, //nolint:gomnd
210		MaxHeaderBytes: 1 << 20,          //nolint:gomnd
211	}
212	listener, err := net.Listen("tcp", s.addr)
213	if err != nil {
214		return tracerr.Wrap(err)
215	}
216	log.Infof("prover.MockServer is ready at %v", s.addr)
217	go func() {
218		if err := debugAPIServer.Serve(listener); err != nil &&
219			tracerr.Unwrap(err) != http.ErrServerClosed {
220			log.Fatalf("Listen: %s\n", err)
221		}
222	}()
223	s.wg.Add(1)
224	go func() {
225		s.runProver(ctx)
226		s.wg.Done()
227	}()
228
229	<-ctx.Done()
230	log.Info("Stopping prover.MockServer...")
231
232	s.wg.Wait()
233	ctxTimeout, cancel := context.WithTimeout(context.Background(), 10*time.Second) //nolint:gomnd
234	defer cancel()
235	if err := debugAPIServer.Shutdown(ctxTimeout); err != nil {
236		return tracerr.Wrap(err)
237	}
238	log.Info("prover.MockServer done")
239	return nil
240}