Skip to content

Commit

Permalink
Merge pull request #14 from privacy-scaling-explorations/feat/6-8-2-3
Browse files Browse the repository at this point in the history
Implement updated API design and revise demo HTML
  • Loading branch information
baumstern authored Jun 21, 2023
2 parents 909cf67 + 7137c3b commit 26b12ed
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 125 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Download artifacts
run: |
mkdir -p ./instruments
wget -P ./instruments https://maci-develop-fra.s3.eu-central-1.amazonaws.com/6f45848_20230309/ProcessMessages_6-8-2-3_test.wasm
wget -P ./instruments https://maci-develop-fra.s3.eu-central-1.amazonaws.com/6f45848_20230309/ProcessMessages_6-8-2-3_test.0.zkey
- name: Build
run: make docker-gen VERSION=${{ github.ref_name }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ coverage/**


# Ignore MACI artifacts
instruments
data/zkeys/**
*.wtns

Expand Down
6 changes: 3 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ linters-settings:
# - io/ioutil.ReadFile

errorlint:
# check whether fmt.Errorf uses the %w verb for formatting errors.
# check whether fmt.Printf uses the %w verb for formatting errors.
# read https://github.com/polyfloyd/go-errorlint for more info
errorf: true
# check for plain type assertions and type switches
Expand Down Expand Up @@ -149,8 +149,8 @@ linters-settings:
# support this tag (yet)
# - security

# disabled-tags:
# - experimental
disabled-tags:
- experimental

# settings passed to gocritic.
# the settings key is the name of a supported gocritic checker.
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ run-debug:
docker-gen:
echo "Building docker image \`ghcr.io/$(REPOSITORY):$(VERSION)\`..."
docker build --rm \
--platform linux/amd64 \
--build-arg final_image=scratch \
--build-arg build_mode=production \
-t ghcr.io/$(REPOSITORY):$(VERSION) . \
Expand All @@ -134,6 +135,7 @@ docker-gen:
docker-debug:
echo "Building docker image \`ghcr.io/$(REPOSITORY):$(VERSION)\`..."
docker build --rm=false \
--platform linux/amd64 \
--build-arg final_image=golang:1.18 \
--build-arg build_mode=debug \
-t ghcr.io/$(REPOSITORY):$(VERSION) . \
Expand Down
116 changes: 94 additions & 22 deletions demo/api_demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,115 @@
<html>
<head>
<title>MACI Coordinator API Demo</title>
<style>
#request, #response, #sentRequest {
width: 30%;
display: inline-block;
vertical-align: top;
}
#request {
margin-right: 2%;
}
#sentRequest pre {
max-height: 200px;
overflow-y: auto;
}
#response {
margin-right: 2%;
}
</style>
</head>
<body>
<h1>MACI Coordinator API Demo</h1>
<div id="request">
<button onclick="generateProof()">Generate Proof</button>
<button onclick="getResult()">Get Result</button>
<br/>
<select id="circuitName">
<option value="ProcessMessages">ProcessMessages</option>
<option value="TallyVotes">TallyVotes</option>
</select>
<br/>
<textarea id="circuitInput" rows="10" cols="50" placeholder="circuitInput (JSON format)"></textarea>
</div>
<div id="sentRequest">
<h2>Sent Request</h2>
<pre id="requestData"></pre>
</div>
<div id="response">
<h2>Response</h2>
<p id="responseStatus"></p>
<pre id="responseData"></pre>
</div>

<input type="text" id="maciPollId" placeholder="maciPollId" />
<button onclick="generateProof()">Generate Proof</button>
<button onclick="checkStatus()">Check Status</button>
<button onclick="getResult()">Get Result</button>
<h2>API Design</h2>
<pre>
API: /generateProof
- Request (POST):
{
"circuitName": "ProcessMessages",
"circuitInput": {}
}

<h2>Response</h2>
<pre id="response"></pre>
- Response:
{
"status": "ok",
"data": {}
}

API: /getResult
- Request (GET):

- Response:
{
"circuit": {
"name": "ProcessMessages",
"status": "Finished",
"result": {
"proof": {},
"publicInput": {}
}
}
}
</pre>

<!-- Rest of the code -->

<script>
function generateProof() {
var maciPollId = document.getElementById('maciPollId').value;
fetch(`/api/genproof`, {
function generateProof() {
var circuitName = document.getElementById('circuitName').value;
var circuitInput = document.getElementById('circuitInput').value;
var requestBody = JSON.stringify({circuitName: circuitName, circuitInput: JSON.parse(circuitInput)});

var request = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({maciPollId: maciPollId, name: "Test Name", authToken: "Test Token"}),
body: requestBody,
};

// Display the sent request
document.getElementById('requestData').textContent = JSON.stringify(request);

fetch(`/api/generateProof`, request)
.then(response => {
document.getElementById('responseStatus').textContent = 'Status: ' + response.status;
return response.json();
})
.then(response => response.json())
.then(data => document.getElementById('response').textContent = JSON.stringify(data, null, 2));
}
function checkStatus() {
var maciPollId = document.getElementById('maciPollId').value;
fetch(`/api/checkStatus/${maciPollId}`)
.then(response => response.json())
.then(data => document.getElementById('response').textContent = JSON.stringify(data, null, 2));
.then(data => document.getElementById('responseData').textContent = JSON.stringify(data, null, 2));
}

function getResult() {
var maciPollId = document.getElementById('maciPollId').value;
fetch(`/api/getResult/${maciPollId}`)
.then(response => response.json())
.then(data => document.getElementById('response').textContent = JSON.stringify(data, null, 2));
// Display the sent request
document.getElementById('requestData').textContent = 'GET /api/getResult';

fetch(`/api/getResult`)
.then(response => {
document.getElementById('responseStatus').textContent = 'Status: ' + response.status;
return response.json();
})
.then(data => document.getElementById('responseData').textContent = JSON.stringify(data, null, 2));
}
</script>
</body>
Expand Down
39 changes: 27 additions & 12 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
# Uses multi-stage build to reduce the size of docker image. The second image will use
# the binary generated by the first image

# Global arg to create second build image -- value will be `scratch` or `golang:1.18`
# depending on the build type needed
ARG final_image=scratch

# Can optionally use the Alpine Go image as the base - in general, images built on
# Apline are slimmer, but the variant is not officially support and is highly
Expand All @@ -15,17 +12,31 @@ ARG final_image=scratch
# out the current base)
#
# FROM golang:1.18-alpine as builder
FROM golang:1.18 as builder
FROM --platform=linux/amd64 ubuntu:20.04

# Setup environmet variables - chores.
ENV GO111MODULE=on \
CGO_ENABLED=0 \
CGO_ENABLED=1 \
GOOS=linux \
GOARCH=amd64

# Create a build directory if one does not exist, and use it as the work directory
WORKDIR "/src/__maci-coordinator__"


RUN apt-get update
RUN apt-get install -y wget build-essential ca-certificates
RUN wget https://go.dev/dl/go1.20.3.linux-amd64.tar.gz

# Configure Go
ENV GOROOT /usr/local/go
ENV GOPATH /go
ENV PATH /usr/local/go/bin:/go/bin:$PATH
ENV GOBIN /service/bin

RUN tar -xvf go1.20.3.linux-amd64.tar.gz
RUN mv go /usr/local

# Selectively copy the requirements into the image - using `*go.sum` prevents failure
# if the file does not exist!
COPY go.mod *go.sum ./
Expand All @@ -37,18 +48,12 @@ RUN go mod download
COPY . .

# Generate a binary -- custom name to avoid collisions with existing files/directories
RUN go build -v -o "../__maci-coordinator_build_output__"
RUN go build -v -o "/builds/maci-coordinator"


# Second stage - `scratch` for production builds, `golang:1.18` for debug builds
FROM ${final_image}

WORKDIR "/builds"

# Copy generated binary from previous image to this one - rename for readability
COPY --from=builder "/src/__maci-coordinator_build_output__" \
"./maci-coordinator"

# Set environment variables to distuinguish between build modes
ARG build_mode=production
ENV __BUILD_MODE__=${build_mode}
Expand All @@ -59,5 +64,15 @@ ENV ${build_mode}_mode=${build_mode}
COPY "./demo" \
"./demo"

# Copy instruments needed to generate proof
# Before building the image, you'll need to prepare these files
COPY "./instruments/ProcessMessages_6-8-2-3_test.0.zkey" \
"./instruments/ProcessMessages_6-8-2-3_test.0.zkey"

COPY "./instruments/ProcessMessages_6-8-2-3_test.wasm" \
"./instruments/ProcessMessages_6-8-2-3_test.wasm"

RUN mkdir -p /builds/data/processMessages/6-8-2-3

# Run the binary
CMD ["./maci-coordinator"]
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ module github.com/privacy-scaling-explorations/maci-coordinator

go 1.18

// To avoid following error:
// imports github.com/iden3/go-rapidsnark/prover: build constraints exclude all Go files in /go/pkg/mod/github.com/iden3/go-rapidsnark/prover@v0.0.10
// replace github.com/iden3/go-rapidsnark/prover => ./prover

require (
github.com/gin-gonic/gin v1.9.1
github.com/iden3/go-rapidsnark/prover v0.0.10
github.com/stretchr/testify v1.8.4
)

require (
github.com/iden3/go-rapidsnark/types v0.0.2 // indirect
github.com/iden3/wasmer-go v0.0.1 // indirect
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
Expand All @@ -16,7 +26,8 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.2
github.com/iden3/go-rapidsnark/witness v0.0.6
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/iden3/go-rapidsnark/prover v0.0.10 h1:NvOfRPpex/k646UsqOcUy7a7uVl17t4ok9kWvpQg4+k=
github.com/iden3/go-rapidsnark/prover v0.0.10/go.mod h1:wgDsmKOGCuWGtgVtuW9ARWNguNr4NJAIyg2G7+uTax0=
github.com/iden3/go-rapidsnark/types v0.0.2 h1:CjJSrlbWchHzuMRdxSYrEh7n/akP+Z2PLNbwT5yBmQY=
github.com/iden3/go-rapidsnark/types v0.0.2/go.mod h1:ApgcaUxKIgSRA6fAeFxK7p+lgXXfG4oA2HN5DhFlfF4=
github.com/iden3/go-rapidsnark/witness v0.0.6 h1:p+6QBymSV3XWm1kB2PL2vlXxKXJN8GyWuqNJv/0PGGU=
github.com/iden3/go-rapidsnark/witness v0.0.6/go.mod h1:57IHQpnvx0CeS6cAT07oN/lqwT5LRc4zicYoZ1cnAPE=
github.com/iden3/wasmer-go v0.0.1 h1:TZKh8Se8B/73PvWrcu+FTU9L1k5XYAmtFbioj7l0Uog=
github.com/iden3/wasmer-go v0.0.1/go.mod h1:ZnZBAO012M7o+Q1INXLRIxKQgEcH2FuwL0Iga8A4ufg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ func main() {
firstCheck()
secondCheck()

r := src.NewRouter()
prover := src.Prover{
ProcessMessagesCircuit: src.Circuit{
Result: src.Result{},
Status: src.WaitingForRequest,
},
TallyVotesCircuit: src.Circuit{
Result: src.Result{},
Status: src.WaitingForRequest,
},
}

r := src.NewRouter(prover)
r.LoadHTMLFiles("demo/api_demo.html")

// listen and serve on localhost:8080
Expand Down
Loading

0 comments on commit 26b12ed

Please sign in to comment.