This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmd.go
122 lines (101 loc) · 2.23 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"sync"
"github.com/aragon/ovote-node/types"
"github.com/gin-gonic/gin"
flag "github.com/spf13/pflag"
"go.vocdoni.io/dvote/db"
"go.vocdoni.io/dvote/db/pebbledb"
"go.vocdoni.io/dvote/log"
)
var port, dir string
type api struct {
r *gin.Engine
sync.Mutex
lastID int
db db.Database
}
func main() {
flag.StringVarP(&port, "port", "p", "9000", "network port for the HTTP API")
flag.StringVarP(&dir, "dir", "d", "~/.proverserver", "db & files directory")
flag.Parse()
opts := db.Options{Path: dir}
database, err := pebbledb.New(opts)
if err != nil {
log.Fatal(err)
}
a := api{}
a.db = database
a.r = gin.Default()
a.lastID = 0
a.r.GET("/status", a.getStatus)
a.r.POST("/proof", a.genProof)
a.r.GET("/proof/:id", a.getProof)
a.r.GET("/proof/:id/public", a.getPublicInputs)
err = a.r.Run(":" + port)
if err != nil {
log.Fatal(err)
}
}
type errorMsg struct {
Message string `json:"message"`
}
func returnErr(c *gin.Context, err error) {
log.Warnw("HTTP API Bad request error", "err", err)
c.JSON(http.StatusBadRequest, errorMsg{
Message: err.Error(),
})
}
func (a *api) getStatus(c *gin.Context) {
if !a.isBusy() {
c.JSON(http.StatusLocked, gin.H{
"status": "prover busy",
})
}
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
func (a *api) genProof(c *gin.Context) {
a.lastID++
if !a.isBusy() {
c.JSON(http.StatusLocked, gin.H{
"status": "prover busy",
})
}
// get zkinputs.json and store it in disk
var zki types.ZKInputs
if err := c.ShouldBindJSON(&zki); err != nil {
returnErr(c, err)
return
}
file, err := json.MarshalIndent(zki, "", " ")
if err != nil {
returnErr(c, err)
return
}
err = ioutil.WriteFile("zkinputs"+strconv.Itoa(a.lastID)+".json",
file, 0600)
if err != nil {
returnErr(c, err)
return
}
go a.genWitnessAndProof(strconv.Itoa(a.lastID))
// return the id, so the client knows which id to use to
// retrieve the proof later
c.JSON(http.StatusOK, gin.H{
"id": a.lastID,
})
}
func (a *api) getProof(c *gin.Context) {
idStr := c.Param("id")
c.File("proof" + idStr + ".json")
}
func (a *api) getPublicInputs(c *gin.Context) {
idStr := c.Param("id")
c.File("public" + idStr + ".json")
}