Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.

Prepare for latest updates and restructure #55

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
.git
551 changes: 308 additions & 243 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 22 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,50 @@ rocket = "0.4"
blake2 = "0.8.0"
hex = "0.3.2"
crypto-mac = "0.7.0"
diesel = { version = "1.3", features = ["postgres", "r2d2"] }
diesel_migrations = "1.3"
diesel = "1.3"
failure = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = "2"
slog = "2.4"
rand = "0.4"
diesel-derive-enum = { version = "0.4.4", features = ["postgres"] }
byteorder = "1.2.7"
memmap = "0.7.0"
tempfile = "3.0.5"
flate2 = {version = "1.0.6", default-features = false, features = ["rust_backend"]}
paired = "0.15"

diesel_migrations = {version = "1.3", optional = true}
diesel-derive-enum = { version = "0.4.4", optional = true }
reqwest = "0.9.18"
ff = "0.4.0"

[dependencies.filecoin-proofs]
version = "0.3"
git = "https://github.com/filecoin-project/rust-fil-proofs"
branch = "master"

[dependencies.storage-proofs]
version = "0.3"
git = "https://github.com/filecoin-project/rust-fil-proofs"
branch = "master"

[dependencies.rocket_contrib]
version = "0.4"
default-features = false
features = ["diesel_postgres_pool", "json", "serve"]
features = ["json", "serve"]

[dev-dependencies]
parking_lot = "0.7.1"
lazy_static = "1.2.0"

[features]
default = ["postgres"]
postgres = [
"diesel/postgres",
"diesel/r2d2",
"diesel-derive-enum",
"diesel-derive-enum/postgres",
"diesel_migrations",
"rocket_contrib/diesel_postgres_pool"
]
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# build with: docker build --tag replication-game .
# run with: docker run -it -v `pwd`:/code/ replication-game

FROM debian:stretch

RUN apt-get update && \
apt-get install -y curl file gcc g++ git make openssh-client \
autoconf automake cmake libtool libcurl4-openssl-dev libssl-dev \
libelf-dev libdw-dev binutils-dev zlib1g-dev libiberty-dev wget \
xz-utils pkg-config python clang jq

RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly-2019-07-03

ENV PATH "$PATH:/root/.cargo/bin"
ENV RUSTFLAGS "-C link-dead-code"
ENV CFG_RELEASE_CHANNEL "nightly"

RUN bash -l -c 'echo $(rustc --print sysroot)/lib >> /etc/ld.so.conf'
RUN bash -l -c 'echo /usr/local/lib >> /etc/ld.so.conf'
RUN ldconfig

WORKDIR /code
CMD ["/code/main.sh"]
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,11 @@ Set your player name:
export REPL_GAME_ID="ReadyPlayerOne"
```

Get the seed from our server:

```bash
curl https://replication-game.herokuapp.com/api/seed > seed.json
export REPL_GAME_SEED=$(cat seed.json| jq -r '.seed')
export REPL_GAME_TIMESTAMP=$(cat seed.json| jq -r '.timestamp')
```

Play the game:

```bash
./target/release/replication-game \
--prover $REPL_GAME_ID \
--seed $REPL_GAME_SEED \
--timestamp $REPL_GAME_TIMESTAMP \
--size 10240 \
zigzag > proof.json
```
Expand Down Expand Up @@ -162,10 +152,11 @@ This server requires Postgresql to work. The details of the expected configurati

### API

- GET `/api/seed`:
- Returns a `timestamp` (unix time) and a `seed` to be used as `replica_id` in the proof of replication
- POST `/api/seed`:
- Inputs: `data`
- Returns a `timestamp` (unix time) and a `seed`
- POST `/api/proof`
- Inputs: `timestamp`, `seed`, `prover_id` and `proof`
- Inputs: `timestamp`, `seed`, `seed_challenge`, `prover_id` and `proof`
- Checks authenticity of the seed (using the timestamp and a secret on the server)
- Checks that the `proof` is correct
- Computes `replication_time = timestamp - current_time`
Expand Down
14 changes: 3 additions & 11 deletions bin/play
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,22 @@ else die "wrong number of arguments: $#"
fi

require curl
require jq

# the URL of the replication game server
SERVER=https://replication-game.herokuapp.com/api
# SERVER=https://replication-game.herokuapp.com/api
SERVER=http://localhost:8000/api

# local temporary files
SEEDFILE=seed.json
PROOFFILE=proof.json

# print out commands from here.

# get the seed and timestamp
# echo "getting the challenge seed"
prun "curl -s $SERVER/seed >$SEEDFILE"
SEED=$(cat seed.json | jq -r '.seed')
TIMESTAMP=$(cat seed.json | jq -r '.timestamp')

# generate the proof and save it to a local file
# echo "invoking replication-game with params"
prun "target/release/replication-game \
--prover '$NAME' \
--seed '$SEED' \
--timestamp '$TIMESTAMP' \
--size '$SIZE' \
--url '$SERVER' \
'$ALGORITHM' >'$PROOFFILE'"

# zip up the proofs file
Expand Down
21 changes: 21 additions & 0 deletions main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

echo "building"

RUSTFLAGS="-C codegen-units=1 -C target-cpu=native" cargo build --release --bin replication-game --no-default-features

REPL_GAME_ID="simple"
SIZE=1048576
# SIZE=10240

# export RUST_BACKTRACE=1
export FIL_PROOFS_MAXIMIZE_CACHING=1

time ./target/release/replication-game \
--prover $REPL_GAME_ID \
--size $SIZE \
--vde 0\
--expansion-degree 8\
--layers 10\
--degree 5\
zigzag > proof.json
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2019-05-19
nightly-2019-07-03
47 changes: 25 additions & 22 deletions src/bin/game.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{value_t, App, AppSettings, Arg, SubCommand};

use replication_game::models::proof;
use replication_game::models::seed::Seed;
use replication_game::models::seed::SeedInput;
use replication_game::proofs::*;

fn main() {
Expand All @@ -18,7 +18,7 @@ fn main() {
Arg::with_name("degree")
.help("The degree")
.long("degree")
.default_value("6")
.default_value("5")
.takes_value(true),
)
.arg(
Expand All @@ -32,7 +32,7 @@ fn main() {
Arg::with_name("expansion-degree")
.help("The expansion degree for Zigzag")
.long("expansion-degree")
.default_value("6")
.default_value("8")
.takes_value(true),
)
.arg(
Expand All @@ -43,18 +43,11 @@ fn main() {
.takes_value(true),
)
.arg(
Arg::with_name("seed")
.long("seed")
.help("The seed from the seed server")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("timestamp")
.long("timestamp")
.help("The timestamp given from the seed server")
.required(true)
.takes_value(true),
Arg::with_name("url")
.long("url")
.help("The url of the replication game api")
.takes_value(true)
.default_value("http://localhost:8000/api"),
)
.arg(
Arg::with_name("prover")
Expand All @@ -68,11 +61,6 @@ fn main() {
.subcommand(SubCommand::with_name("zigzag"))
.get_matches();

let seed = Seed {
timestamp: value_t!(matches, "timestamp", i32).unwrap(),
seed: value_t!(matches, "seed", String).unwrap(),
};

let (typ, zigzag) = match matches.subcommand().0 {
"drgporep" => (proof::ProofType::DrgPoRep, None),
"zigzag" => (
Expand All @@ -95,13 +83,28 @@ fn main() {
vde: value_t!(matches, "vde", usize).unwrap(),
challenge_count: 200,
zigzag,
seed: None, // gets filled in manually
};

let prover = value_t!(matches, "prover", String).unwrap();
let host = format!("{}/seed", value_t!(matches, "url", String).unwrap());

let get_seed = |data| {
let client = reqwest::Client::new();
let seed_input = SeedInput { data };

client
.post(&host)
.json(&seed_input)
.send()
.expect("failed to get challenge")
.json()
.expect("invalid seed challenge response")
};

let res = match typ {
proof::ProofType::DrgPoRep => porep_work(prover, params, seed),
proof::ProofType::Zigzag => zigzag_work(prover, params, seed),
proof::ProofType::DrgPoRep => porep_work(prover, params, get_seed),
proof::ProofType::Zigzag => zigzag_work(prover, params, get_seed),
};

println!("{}", res);
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,43 @@

#[macro_use]
extern crate diesel;
#[cfg(feature = "postgres")]
#[macro_use]
extern crate diesel_migrations;

pub mod models;
pub mod proofs;

#[cfg(feature = "postgres")]
mod db;
mod error;
#[cfg(feature = "postgres")]
mod gzip;
#[cfg(feature = "postgres")]
mod routes;
#[cfg(feature = "postgres")]
mod schema;

#[cfg(test)]
mod tests;

#[cfg(feature = "postgres")]
use rocket::fairing::AdHoc;
#[cfg(feature = "postgres")]
use rocket::{catchers, routes, Rocket};
#[cfg(feature = "postgres")]
use rocket_contrib::serve::StaticFiles;

#[cfg(feature = "postgres")]
use crate::db::DbConn;

// This macro from `diesel_migrations` defines an `embedded_migrations` module
// containing a function named `run`. This allows the example to be run and
// tested without any outside setup of the database.
#[cfg(feature = "postgres")]
embed_migrations!();

#[cfg(feature = "postgres")]
pub fn rocket() -> (Rocket, Option<DbConn>) {
let rocket = rocket::ignite()
.attach(DbConn::fairing())
Expand Down
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "postgres")]
pub mod leaderboard;
pub mod proof;
pub mod seed;
29 changes: 22 additions & 7 deletions src/models/proof.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[cfg(feature = "postgres")]
use diesel_derive_enum::DbEnum;
use serde::{Deserialize, Serialize};
use storage_proofs::hasher::blake2s::Blake2sDomain;
use storage_proofs::hasher::pedersen::PedersenDomain;
use storage_proofs::hasher::Blake2sHasher;
use storage_proofs::hasher::PedersenHasher;
use storage_proofs::layered_drgporep::LayerChallenges;
use storage_proofs::{drgporep, layered_drgporep, porep};
Expand All @@ -10,12 +13,13 @@ use crate::models::seed::Seed;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Response {
pub prover: String,
pub seed: Seed,
pub seed_start: Seed,
pub seed_challenge: Seed,
pub proof_params: Params,
pub proof: Proof,
pub tau: porep::Tau<PedersenDomain>,
pub tau: porep::Tau<Blake2sDomain>,
// only set for zigzag,
pub comm_r_star: Option<PedersenDomain>,
pub comm_r_star: Option<Blake2sDomain>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -26,6 +30,7 @@ pub struct Params {
pub vde: usize,
pub degree: usize,
pub zigzag: Option<ZigZagParams>,
pub seed: Option<Blake2sDomain>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -55,15 +60,25 @@ impl Params {
}
}

#[derive(Debug, Clone, Serialize, Deserialize, DbEnum)]
#[cfg_attr(feature = "postgres", derive(DbEnum))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ProofType {
Zigzag,
#[db_rename = "drgporep"]
#[cfg_attr(feature = "postgres", db_rename = "drgporep")]
DrgPoRep,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Proof {
Zigzag(Vec<layered_drgporep::Proof<PedersenHasher>>),
DrgPoRep(drgporep::Proof<PedersenHasher>),
Zigzag(Vec<layered_drgporep::Proof<Blake2sHasher>>),
DrgPoRep(drgporep::Proof<Blake2sHasher>),
}

impl Proof {
pub fn get_replica_root(&self) -> &Blake2sDomain {
match self {
Proof::Zigzag(ref proof) => &proof[0].tau[proof[0].tau.len() - 1].comm_r,
Proof::DrgPoRep(ref proof) => &proof.replica_root,
}
}
}
Loading