Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to generate docker-compose.yml for single server execution. Fix queue issue. #140

Merged
merged 137 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
137 commits
Select commit Hold shift + click to select a range
d43aa67
add draft version
exfinen Jan 20, 2025
5cbe1c6
wip
exfinen Jan 20, 2025
bbd0c12
wip
exfinen Jan 20, 2025
4a5472e
wip
exfinen Jan 20, 2025
cb517db
wip
exfinen Jan 20, 2025
c37459b
wip
exfinen Jan 20, 2025
264bbfd
wip
exfinen Jan 20, 2025
a98bd6f
wip
exfinen Jan 20, 2025
98eb8ac
wip
exfinen Jan 20, 2025
b435b8e
generate Px.pem/key inside container
exfinen Jan 20, 2025
22be186
wip
exfinen Jan 20, 2025
4a06e00
wip
exfinen Jan 20, 2025
0b9aaf0
wip
exfinen Jan 20, 2025
1f35679
wip
exfinen Jan 20, 2025
c8771de
wip
exfinen Jan 20, 2025
58a3450
wip
exfinen Jan 20, 2025
3cb8225
wip
exfinen Jan 20, 2025
e008ade
wip
exfinen Jan 20, 2025
9c54408
wip
exfinen Jan 21, 2025
ac36bae
wip
exfinen Jan 21, 2025
76e46a1
wip
exfinen Jan 21, 2025
82ccf26
wip
exfinen Jan 21, 2025
d761fa6
wip
exfinen Jan 21, 2025
d3d33bd
wip
exfinen Jan 21, 2025
5f48370
wip
exfinen Jan 21, 2025
521299e
wip
exfinen Jan 21, 2025
0167004
wip
exfinen Jan 21, 2025
fb69b43
wip
exfinen Jan 21, 2025
dd2fc7d
wip
exfinen Jan 21, 2025
0486dcf
wip
exfinen Jan 21, 2025
6912198
wip
exfinen Jan 21, 2025
314b14f
wip
exfinen Jan 21, 2025
e21ac04
wip
exfinen Jan 22, 2025
5c18db1
wip
exfinen Jan 22, 2025
d672887
wip
exfinen Jan 22, 2025
642a845
wip
exfinen Jan 22, 2025
1f027f6
wip
exfinen Jan 22, 2025
e2cd959
wip
exfinen Jan 22, 2025
9e3f059
wip
exfinen Jan 22, 2025
dea51b3
wip
exfinen Jan 22, 2025
ef4091b
wip
exfinen Jan 22, 2025
09d0341
wip
exfinen Jan 22, 2025
0ec19a5
wip
exfinen Jan 22, 2025
5fd370c
wip
exfinen Jan 22, 2025
a09b7d1
wip
exfinen Jan 22, 2025
6d47594
wip
exfinen Jan 22, 2025
3a287e3
wip
exfinen Jan 22, 2025
664eb0c
wip
exfinen Jan 22, 2025
077daca
wip
exfinen Jan 22, 2025
91913a1
wip
exfinen Jan 22, 2025
040eb54
wip
exfinen Jan 24, 2025
bdf5707
wip
exfinen Jan 24, 2025
03f939d
wip
exfinen Jan 24, 2025
4210bb8
wi
exfinen Jan 24, 2025
75293ae
wi
exfinen Jan 24, 2025
375ce7f
wi
exfinen Jan 25, 2025
e96eb26
wip
exfinen Jan 25, 2025
8f3da47
wip
exfinen Jan 25, 2025
e1ca38e
wip
exfinen Jan 25, 2025
baa1839
wip
exfinen Jan 25, 2025
bd1d87a
wip
exfinen Jan 25, 2025
4e28cdd
wip
exfinen Jan 25, 2025
6375ad1
iwp
exfinen Jan 25, 2025
9324029
wip
exfinen Jan 25, 2025
637e9c4
wip
exfinen Jan 25, 2025
be5d3e1
wip
exfinen Jan 25, 2025
ee8b1ab
wip
exfinen Jan 25, 2025
900bd3b
wip
exfinen Jan 25, 2025
5cf559c
wip
exfinen Jan 25, 2025
4e58020
gen self-cert
exfinen Jan 25, 2025
1f57bcb
wip
exfinen Jan 25, 2025
a678033
wip
exfinen Jan 25, 2025
0dccf39
wip
exfinen Jan 25, 2025
f036c4e
wip
exfinen Jan 25, 2025
1bd4bb0
wip
exfinen Jan 25, 2025
57aec24
wip
exfinen Jan 25, 2025
354a89c
wip
exfinen Jan 25, 2025
130c3e2
wip
exfinen Jan 26, 2025
fef84d1
wip
exfinen Jan 26, 2025
21227b0
wip
exfinen Jan 26, 2025
3af3022
wip
exfinen Jan 26, 2025
0f9a843
wip
exfinen Jan 26, 2025
c10bc5d
wip
exfinen Jan 26, 2025
63ce60f
wip
exfinen Jan 26, 2025
cf7b5ac
wip
exfinen Jan 26, 2025
d6adbc3
wip
exfinen Jan 26, 2025
68e7bca
wip
exfinen Jan 26, 2025
a37815e
wip
exfinen Jan 26, 2025
90706b8
wip
exfinen Jan 26, 2025
f90bb54
wip
exfinen Jan 26, 2025
dffbc00
wip
exfinen Jan 26, 2025
ba118db
wip
exfinen Jan 26, 2025
40ff8d5
wip
exfinen Jan 26, 2025
43e1124
wip
exfinen Jan 26, 2025
ca9b2a3
wip
exfinen Jan 26, 2025
63e2ed7
wip
exfinen Jan 26, 2025
7e36a4a
wip
exfinen Jan 26, 2025
7b42df2
wip
exfinen Jan 26, 2025
dd48cee
wip
exfinen Jan 26, 2025
367af53
wip
exfinen Jan 26, 2025
13c7817
use mpspdz-compat
exfinen Jan 26, 2025
8d59634
wip
exfinen Jan 26, 2025
bf4507b
wip
exfinen Jan 26, 2025
81a8989
wip
exfinen Jan 26, 2025
88f4cb6
wip
exfinen Jan 26, 2025
c3a0d8b
finalize with access_key instead of eth address
exfinen Jan 26, 2025
de32210
wip
exfinen Jan 26, 2025
d06f9c9
wip
exfinen Jan 26, 2025
7a483fc
wip
exfinen Jan 26, 2025
59d34f8
wip
exfinen Jan 26, 2025
dcc8999
wip
exfinen Jan 26, 2025
5a7b260
wip
exfinen Jan 26, 2025
1be1c6b
wip
exfinen Jan 26, 2025
087074f
wip
exfinen Jan 26, 2025
c3177b5
wip
exfinen Jan 26, 2025
bd36e50
wip
exfinen Jan 26, 2025
766c9d9
wip
exfinen Jan 26, 2025
9303c46
wip
exfinen Jan 26, 2025
2a463d2
wip
exfinen Jan 26, 2025
7bfdf81
wip
exfinen Jan 26, 2025
226bd6b
wip
exfinen Jan 26, 2025
0647da4
wip
exfinen Jan 26, 2025
c84b216
wip
exfinen Jan 26, 2025
60cf622
wip
exfinen Jan 26, 2025
8dc62ba
wip
exfinen Jan 26, 2025
425b18a
wip
exfinen Jan 26, 2025
194fd5a
wip
exfinen Jan 26, 2025
2c916a6
wip
exfinen Jan 27, 2025
f3e3191
wip
exfinen Jan 27, 2025
f9a311e
wip
exfinen Jan 27, 2025
39fb97e
wip
exfinen Jan 27, 2025
b195f49
wip
exfinen Jan 27, 2025
fd00c84
wip
exfinen Jan 27, 2025
50e2c4e
wip
exfinen Jan 27, 2025
8ce12f3
wip
exfinen Jan 27, 2025
a81572e
wip
exfinen Jan 27, 2025
21fa36a
wip
exfinen Jan 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,5 @@ logs/
secret*.json
proof*.json
stress-test.sh
docker-compose.yml
notary.crt
250 changes: 250 additions & 0 deletions configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
#!/usr/bin/env python3

import argparse
import json
from pathlib import Path

def gen_env_consumer_api(
transport: str,
coord_host: str,
party_hosts: list[str],
party_ports: list[int],
):
output = f"""\
COORDINATION_SERVER_URL={transport}://{coord_host}:8005
CERTS_PATH=certs
PARTY_HOSTS={json.dumps(party_hosts)}
PARTY_PORTS={json.dumps(party_ports)}
PRIVKEY_PEM_PATH=ssl_certs/privkey.pem
FULLCHAIN_PEM_PATH=ssl_certs/fullchain.pem
PARTY_WEB_PROTOCOL={transport}
PORT=8004
"""
return output

def gen_env_coord(
transport: str,
party_hosts: list[str],
party_ports: list[int],
):
output = f"""\
PORT=8005
PARTY_HOSTS={json.dumps(party_hosts)}
PARTY_PORTS={json.dumps(party_ports)}
PARTY_API_KEY=81f47c24b9fbe22421ea3ae92a9cc8f6
PARTY_WEB_PROTOCOL={transport}
PROHIBIT_MULTIPLE_CONTRIBUTIONS=False
USER_QUEUE_HEAD_TIMEOUT=60
PRIVKEY_PEM_PATH=ssl_certs/privkey.pem
FULLCHAIN_PEM_PATH=ssl_certs/fullchain.pem
"""
return output

def gen_env_party(
transport: str,
coord_host: str,
party_hosts: list[str],
party_ports: list[int],
):
output = f"""\
PORT=8006
PARTY_ID=0
COORDINATION_SERVER_URL={transport}://{coord_host}:8005
PARTY_API_KEY=81f47c24b9fbe22421ea3ae92a9cc8f6
PARTY_HOSTS={json.dumps(party_hosts)}
PARTY_PORTS={json.dumps(party_ports)}
PARTY_WEB_PROTOCOL={transport}
MAX_DATA_PROVIDERS=1000
PERFORM_COMMITMENT_CHECK=False
PRIVKEY_PEM_PATH=ssl_certs/privkey.pem
FULLCHAIN_PEM_PATH=ssl_certs/fullchain.pem
"""
return output

def gen_docker_compose(notary_ip: str, add_data_consumer_api: bool):
s = f"""\
services:
coord:
build:
context: ./mpc_demo_infra/coordination_server/docker
ports:
- "8005:8005"
volumes:
- coord-data:/root/mpc-demo-infra/
stdin_open: true
tty: true
init: true
extra_hosts:
- "tlsnotaryserver.io:127.0.0.1"
depends_on:
- party_0
- party_1
- party_2
notary:
build:
context: ./mpc_demo_infra/notary_server/docker
args:
NORTARY_IP: {notary_ip}
ports:
- "8003:8003"
environment:
- NOTARY_IP={notary_ip}
stdin_open: true
tty: true
init: true
extra_hosts:
- "tlsnotaryserver.io:127.0.0.1"
"""

if add_data_consumer_api:
s += """\
data_consumer_api:
build:
context: ./mpc_demo_infra/data_consumer_api/docker
ports:
- "8004:8004"
stdin_open: true
tty: true
init: true
"""

s += """\
party_0:
build:
context: ./mpc_demo_infra/computation_party_server/docker
args:
PORT: 8006
PARTY_ID: 0
NUM_PARTIES: 3
ports:
- "8006:8006"
- "8013:8013"
environment:
- PARTY_ID=0
volumes:
- party0-data:/root/MP-SPDZ/
stdin_open: true
tty: true
init: true
extra_hosts:
- "tlsnotaryserver.io:127.0.0.1"

party_1:
build:
context: ./mpc_demo_infra/computation_party_server/docker
args:
PORT: 8007
PARTY_ID: 1
NUM_PARTIES: 3
ports:
- "8007:8007"
- "8014:8014"
environment:
- PARTY_ID=1
volumes:
- party1-data:/root/MP-SPDZ/
stdin_open: true
tty: true
init: true
extra_hosts:
- "tlsnotaryserver.io:127.0.0.1"
party_2:
build:
context: ./mpc_demo_infra/computation_party_server/docker
args:
PORT: 8008
PARTY_ID: 2
NUM_PARTIES: 3
ports:
- "8008:8008"
- "8015:8015"
environment:
- PARTY_ID=2
volumes:
- party2-data:/root/MP-SPDZ/
stdin_open: true
tty: true
init: true
extra_hosts:
- "tlsnotaryserver.io:127.0.0.1"
volumes:
coord-data:
party0-data:
party1-data:
party2-data:
"""
return s

def parse_args():
parser = argparse.ArgumentParser(description="config-file generation script")
parser.add_argument(
'--transport',
choices=['http', 'https'],
default='http',
help=f"Transport to use. http is used by default",
)
parser.add_argument(
'--notary-ip',
type=str,
default='127.0.0.1',
help="IP address of the server on which the notary server runs",
)
parser.add_argument(
'--dry-run',
action='store_true',
help='Print out the contents of config files',
)
parser.add_argument(
'--data-consumer-api',
action='store_true',
help='Add Data Consumer API server',
)
return parser.parse_args()

args = parse_args()

def write_file(file_path: Path, content: str, args):
if args.dry_run:
print(f"----> {file_path}")
print(content)
else:
with open(file_path, 'w') as f:
f.write(content)
print(f"Created {str(file_path)}")

party_hosts = ["party_0", "party_1", "party_2"]
party_ports =[8006, 8007, 8008]

mpc_demo_infra = Path('mpc_demo_infra')

# write .env.consumer_api if needed
if args.data_consumer_api:
dot_env_consumer_api = gen_env_consumer_api(
args.transport,
args.notary_ip,
party_hosts,
party_ports,
)
write_file(mpc_demo_infra / 'data_consumer_api' / 'docker' / '.env.consumer_api', dot_env_consumer_api, args)

# write .env.coord
dot_env_coord = gen_env_coord(
args.transport,
party_hosts,
party_ports,
)
write_file(mpc_demo_infra / 'coordination_server' / 'docker' / '.env.coord', dot_env_coord, args)

# write .env.party for partys
dot_env_party = gen_env_party(
args.transport,
args.notary_ip,
party_hosts,
party_ports,
)
write_file(mpc_demo_infra / 'computation_party_server' / 'docker' / '.env.party', dot_env_party, args)

# write docker-compose.yml
docker_compose_yml = gen_docker_compose(args.notary_ip, args.data_consumer_api)
write_file(Path('docker-compose.yml'), docker_compose_yml, args)

2 changes: 2 additions & 0 deletions mpc_demo_infra/client_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Settings(BaseSettings):
max_bytes_mb = 20
backup_count = 10

max_client_wait = 1000

class Config:
env_file = ".env.client_cli"

Expand Down
29 changes: 23 additions & 6 deletions mpc_demo_infra/client_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import secrets
import logging
from datetime import datetime
from typing import Optional

from ..client_lib.lib import fetch_parties_certs, share_data, query_computation, add_user_to_queue, poll_queue_until_ready, mark_queue_computation_to_be_finished
from .config import settings
Expand Down Expand Up @@ -42,16 +43,21 @@ def locate_binance_prover():
logger.info(f"Found binance_prover in {binance_prover_dir}")
return binance_prover_dir, binance_prover_exec_cmd

async def generate_tlsn_proof(api_key: str, api_secret: str):
async def generate_tlsn_proof(api_key: str, api_secret: str, notary_crt_path: Optional[str]):
binance_prover_dir, binance_prover_exec_cmd = locate_binance_prover()

logger.info(f"Generating Binance ETH balance TLSN proof with notary server {settings.notary_server_host}:{settings.notary_server_port}...")
timestamp = int(datetime.now().timestamp() * 100) % MAX_CLIENT_ID
proof_file = PROJECT_ROOT / f"proof{timestamp}.json"
secret_file = PROJECT_ROOT/ f"secret{timestamp}.json"

cmd = f"{binance_prover_exec_cmd} {settings.notary_server_host} {settings.notary_server_port} {api_key} {api_secret} {str(proof_file.resolve())} {str(secret_file.resolve())}"
if notary_crt_path:
cmd += f" {notary_crt_path}"
logger.info(f"Executing: {cmd}")

process = await asyncio.create_subprocess_shell(
f"{binance_prover_exec_cmd} {settings.notary_server_host} {settings.notary_server_port} {api_key} {api_secret} {str(proof_file.resolve())} {str(secret_file.resolve())}",
cmd,
cwd=binance_prover_dir,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
Expand All @@ -68,7 +74,7 @@ async def generate_tlsn_proof(api_key: str, api_secret: str):
nonce = bytes(secret_data["nonce"]).hex()
return tlsn_proof, secret_input, nonce, timestamp

async def notarize_and_share_data(eth_address: str, api_key: str, api_secret: str):
async def notarize_and_share_data(eth_address: str, api_key: str, api_secret: str, notary_crt_path: Optional[str]):
logger.info(f"Sharing Binance ETH balance data to MPC parties...")

# Wait to get the computation key
Expand All @@ -78,7 +84,7 @@ async def notarize_and_share_data(eth_address: str, api_key: str, api_secret: st

try:
# Generate TLSN proof
tlsn_proof, secret_input, nonce, timestamp = await generate_tlsn_proof(api_key, api_secret)
tlsn_proof, secret_input, nonce, timestamp = await generate_tlsn_proof(api_key, api_secret, notary_crt_path)
logger.info(f"Generated TLSN proof")

# Fetch party certificates
Expand All @@ -103,10 +109,15 @@ async def notarize_and_share_data(eth_address: str, api_key: str, api_secret: st
access_key,
computation_key,
timestamp,
settings.max_client_wait,
)
logger.info("Sharing data fisnihed")

except Exception as e:
logger.error(f"Faield to share data: {e}")
finally:
# Call the server to mark the computation as finished whether it succeeds or not.
await mark_queue_computation_to_be_finished(settings.coordination_server_url, eth_address, computation_key)
await mark_queue_computation_to_be_finished(settings.coordination_server_url, access_key, computation_key)
logger.info(f"Binance ETH balance data has been shared secretly to MPC parties.")


Expand All @@ -132,6 +143,9 @@ async def query_computation_and_verify():
access_key,
computation_key,
)
logger.info("Query computation fisnihed")
except Exception as e:
logger.error(f"Faield to query computation: {e}")
finally:
await mark_queue_computation_to_be_finished(settings.coordination_server_url, access_key, computation_key)
logger.info(f"{results=}")
Expand All @@ -142,15 +156,18 @@ def notarize_and_share_data_cli():
parser.add_argument("eth_address", type=str, help="The voucher code")
parser.add_argument("api_key", type=str, help="The API key")
parser.add_argument("api_secret", type=str, help="The API secret")
parser.add_argument("--notary-crt-path", type=str, default=None, help="Path to notary.crt file")
args = parser.parse_args()
try:
asyncio.run(notarize_and_share_data(args.eth_address, args.api_key, args.api_secret))
logger.info(f"Started with settings: {settings}")
asyncio.run(notarize_and_share_data(args.eth_address, args.api_key, args.api_secret, args.notary_crt_path))
except Exception as e:
logger.error(e)


def query_computation_and_verify_cli():
try:
logger.info(f"Started with settings: {settings}")
asyncio.run(query_computation_and_verify())
logger.info("Computation finished")
except Exception as e:
Expand Down
Loading
Loading