# Coordinator Service for MACI
I have finished the feature for MACI coordinator service. Two PR have been merged: [v1.0](https://github.com/appliedzkp/maci/pull/327) and [v0.10](https://github.com/appliedzkp/maci/pull/338).
## Overal structure
There are two roles in the cordinator service: admin (i.e. maci coordinator) and user (i.e. normal user). The responsibility for admin is to maintain the code updated and the backend services for normal users. The user just need sends http request to the backend server to interact with the MACI service such as signup and publish messages.
The coordinator service has been wrapped into two docker instances: one for the backend server to accept user request; one for mongodb service to store all necessary informations of the current state such as smart contract addresses, zero knowledge proof keys etc.
![](https://i.imgur.com/ND645nU.png)
There are two modes: development mode and production mode. For developers and cordinator, it's convenient for them to be able to quickly change the code and test the results. Hence the backend docker instance mounts the code repo to the developer's local repo. For production mode, all the code repos will be included into the docker instance instead of mount to local repo.
## Project Setup
The project can be setup in two different ways. One for developing purpose, which the codes are linked to the developers' local machine, so it is easy to modify the codes and test the results. The other is for production purpose, where the codes are stored in docker image, so there will be no code changes when starting the docker services.
### development environment
#### setup
```bash=
########################
### dependency setup ###
########################
# needed for both v0.10.x and v1.0.x versions
sudo apt-get install build-essential libgmp-dev && \
libsodium-dev nasm git nlohmann-json3-dev g++ jq
########################
### maci v1.0.x setup###
########################
# reference: https://appliedzkp.github.io/maci/installation.html
# install rapicsnark for v1.0.0
git clone https://github.com/iden3/rapidsnark.git && \
cd rapidsnark && \
git checkout 1c13721de4a316b0b254c310ccec9341f5e2208e
npm install && \
git submodule init && \
git submodule update && \
npx task createFieldSources && \
npx task buildProver
# install maci for v1.0
git clone https://github.com/appliedzkp/maci.git && \
cd maci && git checkout v1\
npm i && npm run bootstrap && npm run build
cd contracts && npm run compileSol
# zkey setup for v1.0
npx zkey-manager compile -c zkeys.config.yml
npx zkey-manager downloadPtau -c zkeys.config.yml
npx zkey-manager genZkeys -c zkeys.config.yml
# setup docker environment
docker build -t maci-node:v0.5 - < LightDockerfile
cd docker
./setup.sh
#########################
### maci v0.10.x setup###
#########################
# install rust if haven't done it before
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# install zkutil
cargo install zkutil --version 0.3.2
# install maci for v0.10.x
git clone https://github.com/appliedzkp/maci.git maci_v0_10
cd maci_v0_10 && npm i && npm run bootstrap && npm run build
cd circuits && ./scripts/buildBatchUpdateStateTreeSnark.sh && \
./scripts/buildQuadVoteTallySnark.sh
cd ../contracts && npm run compileSol
####################
### docker setup ###
####################
# step 1: install docker
# reference: https://docs.docker.com/engine/install/ubuntu/
# step 2: run docker as non-root user
# https://docs.docker.com/engine/install/linux-postinstall/
# step 3: install docker-compose
# https://docs.docker.com/compose/install/
# pull and update docker image
# make sure the image name (maci-node:v0.5) match the name in docker-compose.yml
cd maci/docker
docker build -t maci-node:v0.5 - < LightDockerfile
# change host.docker.internal to docker network ip (for Linux machine)
./setup.sh
```
#### development and test
```bash=
### Setup for developer
# (1) docker/docker-compose.yml: manually replace ETH_PROVIDER from 127.0.0.1 to docker network ip
# (2) server/docker.sh: manually replace SCRIPT location to local maci repo location.
# This local maci location should match in docker-compose.yml.
# In my case, it's
# SCRIPT='node /root/dev-maci/server/index.js'
### In one terminal
# if for v1.0
cd contracts && npm run hardhat
# if for v0.10
cd contracts && npm run ganache
### In second terminal
cd server && ./docker.sh -u
# whenever modified the code, just restart the docker instance by Ctrl+c and restart the service
./docker.sh -r
### In third terminal
# if for v1.0
cd server && ./test.sh
# if for v0.10
cd server && ./test_v0_10.sh
### turnoff docker after finish
cd server && ./docker.sh -d
```
### production environment
#### setup
```bash=
git clone https://github.com/appliedzkp/maci.git
git checkout v1
#### setup zkeys if you haven't download it in local machine
# (1) in this case, remove the zkey mapping in docker/docker-compose.yml
# (2) login into docker
cd maci/server && ./docker.sh -u # then Ctrl-C
./docker.sh -l
# (3) generate zkey inside docker instance
cd ~/maci/cli && ./gen_zkey.sh
```
#### test
```bash=
### In one terminal
cd server && ./docker.sh -u
## In second terminal
cd server && ./docker.sh -l
# now in docker
# if for v1.0
cd /root/maci/contracts && npm run hardhat
# if for v0.10
cd /root/maci_v0_10/contracts && npm run ganache
### In third terminal, admin role
cd server && ./docker.sh -l
# now in docker
cd /root/maci/server
# run any commands for admin, e.g.
# if for v1.0
./admin.sh deploy
./admin.sh store
# if for v0.10
./admin_v0_10.sh deploy
./admin_v0_10.sh store
### In fourth terminal, user role
# in local machine, not inside docker
cd server
# run any commands for user, e.g.
# if for v1.0
./user.sh signup -p $pk -x $maci
# if for v0.10
./user_v0_10.sh signup -p $pk -x $maci
```
###### tags: `public`