Skip to content

Commit

Permalink
prover separated
Browse files Browse the repository at this point in the history
  • Loading branch information
gluk64 committed Mar 29, 2019
1 parent cecfa28 commit 2567ca8
Show file tree
Hide file tree
Showing 19 changed files with 929 additions and 6,452 deletions.
1,376 changes: 735 additions & 641 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
[workspace]
members = [
"ff",
"ff/ff_derive",
"pairing",
"bellman",
"sapling-crypto",
"plasma",
"plasma/server",
"plasma/server/models",
"plasma/server/storage",
]
2,640 changes: 0 additions & 2,640 deletions plasma/Cargo.lock

This file was deleted.

3,040 changes: 0 additions & 3,040 deletions plasma/server/Cargo.lock

This file was deleted.

3 changes: 2 additions & 1 deletion plasma/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ path = "bin/server.rs"

[dependencies]
plasma = { path = ".." }
models= { path = "models" }
server_models = { path = "server_models" }
storage = { path = "storage" }
prover = { path = "prover" }

ff = { path = "../../ff", features = ["derive"] }
pairing = { path = "../../pairing" }
Expand Down
12 changes: 7 additions & 5 deletions plasma/server/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
extern crate server;
extern crate storage;
extern crate models;
extern crate server_models;
extern crate prover;
extern crate ctrlc;
extern crate signal_hook;

use std::sync::mpsc::{channel};

use server::prover::{BabyProver, start_prover};
use prover::{BabyProver, start_prover};

use server::state_keeper::{PlasmaStateKeeper, start_state_keeper};
use server::rest_api::start_api_server;
use server::committer;
use server::eth_watch::{EthWatch, start_eth_watch};

use storage::{ConnectionPool};
use models::StateKeeperRequest;
use server_models::StateKeeperRequest;

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand All @@ -33,7 +35,7 @@ fn main() {

let connection_pool = ConnectionPool::new();
let state_keeper = PlasmaStateKeeper::new(connection_pool.clone());
let prover = BabyProver::create(connection_pool.clone()).unwrap();
//let prover = BabyProver::create(connection_pool.clone()).unwrap();
let eth_watch = EthWatch::new(0, 0, connection_pool.clone());

// spawn threads for different processes
Expand All @@ -54,7 +56,7 @@ fn main() {
start_eth_watch(eth_watch, tx_for_state.clone());

start_state_keeper(state_keeper, rx_for_state, tx_for_ops.clone());
start_prover(prover, rx_for_proof_requests, tx_for_ops);
start_prover(connection_pool.clone(), rx_for_proof_requests, tx_for_ops);

let tx_for_eth = committer::start_eth_sender(connection_pool.clone());

Expand Down
46 changes: 46 additions & 0 deletions plasma/server/prover/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "prover"
version = "0.0.1"

workspace = "../../.."

[lib]
path = "prover.rs"

[dependencies]
plasma = { path = "../.." }
server_models = { path = "../server_models" }
storage = { path = "../storage" }

ff = { path = "../../../ff", features = ["derive"] }
pairing = { path = "../../../pairing" }
bellman = { path = "../../../bellman" }
sapling-crypto = { path = "../../../sapling-crypto" }

rand = "0.4"
# hex = "0.3.2"
# time = "0.1"
# num-bigint = "0.2"
rust-crypto = "0.2"
# fnv = "1.0.3"
# ethabi = "6.1.0"
# web3 = { git = "https://github.com/tomusdrw/rust-web3" }
rustc-hex = "2.0.1"
# futures = "0.1.25"
# hyper = "0.12.16"
# serde = "1.0.80"

# serde_json = "1.0.33"
# serde_derive = "1.0.80"

# serde_bytes = "0.10.4"

# #r2d2 = "0.8.3"
# #r2d2_postgres = "0.14.0"

# im = "12.2.0"
# priority-queue = "0.5.2"
# num-traits = "0.2"

# diesel = { version = "1.3.3", features = ["postgres", "serde_json", "r2d2"] }
# bigdecimal = { version = "0.0.11", features = ["serde"]}
157 changes: 57 additions & 100 deletions plasma/server/src/prover.rs → plasma/server/prover/prover.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
extern crate rustc_hex;
extern crate plasma;
extern crate server_models;
extern crate storage;

extern crate rand;
extern crate crypto;

extern crate ff;
extern crate bellman;
extern crate sapling_crypto;

use std::error::Error;
use std::fmt;
Expand All @@ -21,10 +31,10 @@ use plasma::models::{Engine, Fr, AccountId};
use plasma::models::{self, params, TransferBlock, DepositBlock, ExitBlock, Block, PlasmaState, AccountMap};
use plasma::models::circuit::{Account, AccountTree};

use super::config::{TRANSFER_BATCH_SIZE, DEPOSIT_BATCH_SIZE, EXIT_BATCH_SIZE};

use super::models::{EncodedProof, Operation, Action, EthBlockData, ProverRequest};
use super::storage::{ConnectionPool, StorageProcessor};
use server_models::encoder;
use server_models::config::{TRANSFER_BATCH_SIZE, DEPOSIT_BATCH_SIZE, EXIT_BATCH_SIZE};
use server_models::{EncodedProof, Operation, Action, EthBlockData, ProverRequest};
use storage::{ConnectionPool, StorageProcessor};

use plasma::circuit::utils::be_bit_vector_into_bytes;
use plasma::circuit::transfer::transaction::{Transaction};
Expand Down Expand Up @@ -133,6 +143,45 @@ fn extend_accounts<I: Sized + Iterator<Item=(AccountId, plasma::models::Account)

impl BabyProver {

// Outputs
// - 8 uint256 for encoding of the field elements
// - one uint256 for new root hash
// - uint256 block number
// - uint256 total fees
// - Bytes public data
//
// Old root is available to take from the storage of the smart-contract
pub fn encode_proof(proof: &FullBabyProof) -> Result<EncodedProof, Err> {

// proof
// pub a: E::G1Affine,
// pub b: E::G2Affine,
// pub c: E::G1Affine

let (a_x, a_y) = serialize_g1_for_ethereum(proof.proof.a);

let ((b_x_0, b_x_1), (b_y_0, b_y_1)) = serialize_g2_for_ethereum(proof.proof.b);

let (c_x, c_y) = serialize_g1_for_ethereum(proof.proof.c);

// let new_root = serialize_fe_for_ethereum(proof.inputs[1]);

// let total_fees = serialize_fe_for_ethereum(proof.total_fees);

// let block_number = serialize_fe_for_ethereum(proof.block_number);

// let public_data = proof.public_data.clone();

let p = [a_x, a_y, b_x_0, b_x_1, b_y_0, b_y_1, c_x, c_y];

// EncodedProof{
// groth_proof: [a_x, a_y, b_x_0, b_x_1, b_y_0, b_y_1, c_x, c_y],
// //block_number: block_number,
// };

Ok(p)
}

pub fn create(pool: ConnectionPool) -> Result<BabyProver, BabyProverErr> {

let connection = pool.pool.get();
Expand Down Expand Up @@ -217,99 +266,6 @@ type Err = BabyProverErr;

impl BabyProver {

// Outputs
// - 8 uint256 for encoding of the field elements
// - one uint256 for new root hash
// - uint256 block number
// - uint256 total fees
// - Bytes public data
//
// Old root is available to take from the storage of the smart-contract
pub fn encode_proof(proof: &FullBabyProof) -> Result<EncodedProof, Err> {

// proof
// pub a: E::G1Affine,
// pub b: E::G2Affine,
// pub c: E::G1Affine

let (a_x, a_y) = serialize_g1_for_ethereum(proof.proof.a);

let ((b_x_0, b_x_1), (b_y_0, b_y_1)) = serialize_g2_for_ethereum(proof.proof.b);

let (c_x, c_y) = serialize_g1_for_ethereum(proof.proof.c);

// let new_root = serialize_fe_for_ethereum(proof.inputs[1]);

// let total_fees = serialize_fe_for_ethereum(proof.total_fees);

// let block_number = serialize_fe_for_ethereum(proof.block_number);

// let public_data = proof.public_data.clone();

let p = [a_x, a_y, b_x_0, b_x_1, b_y_0, b_y_1, c_x, c_y];

// EncodedProof{
// groth_proof: [a_x, a_y, b_x_0, b_x_1, b_y_0, b_y_1, c_x, c_y],
// //block_number: block_number,
// };

Ok(p)
}

pub fn encode_transfer_transactions(block: &TransferBlock) -> Result<Vec<u8>, Err> {
let mut encoding: Vec<u8> = vec![];

let transactions = &block.transactions;

for tx in transactions {
let tx = models::circuit::TransferTx::try_from(tx).map_err(|e| BabyProverErr::InvalidTransaction(e.to_string()))?;
let tx_bits = tx.public_data_into_bits();
let tx_encoding = be_bit_vector_into_bytes(&tx_bits);
encoding.extend(tx_encoding.into_iter());
}

Ok(encoding)
}

pub fn encode_deposit_transactions(block: &DepositBlock) -> Result<Vec<u8>, Err> {
let mut encoding: Vec<u8> = vec![];

// let sorted_block = sorted_deposit_block(&block);

let transactions = &block.transactions;

for tx in transactions {
let tx = models::circuit::DepositRequest::try_from(tx).map_err(|e| BabyProverErr::InvalidTransaction(e.to_string()))?;
let tx_bits = tx.public_data_into_bits();
let tx_encoding = be_bit_vector_into_bytes(&tx_bits);
encoding.extend(tx_encoding.into_iter());
}

Ok(encoding)
}

// this method is different, it actually reads the state
pub fn encode_exit_transactions(block: &ExitBlock) -> Result<Vec<u8>, Err> {
let mut encoding: Vec<u8> = vec![];

let transactions = &block.transactions;

for tx in transactions {
let tx = models::circuit::ExitRequest::try_from(tx).map_err(|e| BabyProverErr::InvalidTransaction(e.to_string()))?;
if (tx.amount == Fr::zero()) {
println!("Trying to exit a zero balance");
}
let tx_bits = tx.public_data_into_bits();
let tx_encoding = be_bit_vector_into_bytes(&tx_bits);
encoding.extend(tx_encoding.into_iter());
}

// let public_data_hex: String = encoding.clone().to_hex();
// println!("Final encoding = {}", public_data_hex);

Ok(encoding)
}

pub fn apply_and_prove(&mut self, block: Block) -> Result<FullBabyProof, Err> {
match block {
Block::Deposit(block, batch_number) => {
Expand All @@ -333,7 +289,7 @@ impl BabyProver {
}
let block_final_root = block.new_root_hash.clone();

let public_data: Vec<u8> = BabyProver::encode_transfer_transactions(block).unwrap();
let public_data: Vec<u8> = encoder::encode_transfer_transactions(block).unwrap();

let transactions = &block.transactions;
let num_txes = transactions.len();
Expand Down Expand Up @@ -614,7 +570,7 @@ impl BabyProver {
}
let block_final_root = block.new_root_hash.clone();

let public_data: Vec<u8> = BabyProver::encode_deposit_transactions(block).unwrap();
let public_data: Vec<u8> = encoder::encode_deposit_transactions(block).unwrap();

let transactions = &block.transactions;
let num_txes = transactions.len();
Expand Down Expand Up @@ -1032,11 +988,12 @@ impl BabyProver {
}

pub fn start_prover(
mut prover: BabyProver,
connection_pool: ConnectionPool,
rx_for_blocks: mpsc::Receiver<ProverRequest>,
tx_for_ops: mpsc::Sender<Operation>
)
{
let mut prover = BabyProver::create(connection_pool.clone()).unwrap();
std::thread::Builder::new().name("prover".to_string()).spawn(move || {
prover.run(rx_for_blocks, tx_for_ops)
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[package]
name = "models"
name = "server_models"
version = "0.0.1"

workspace = "../../.."

[lib]
path = "models.rs"
path = "lib.rs"

[dependencies]
plasma = { path = "../.." }
Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions plasma/server/server_models/encoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use super::{DepositBlock, TransferBlock, ExitBlock};
use plasma::circuit::utils::be_bit_vector_into_bytes;
use plasma::models;

pub fn encode_transfer_transactions(block: &TransferBlock) -> Result<Vec<u8>, String> {
let mut encoding: Vec<u8> = vec![];

let transactions = &block.transactions;

for tx in transactions {
let tx = models::circuit::TransferTx::try_from(tx).map_err(|e| e.to_string())?; // BabyProverErr::InvalidTransaction(e.to_string())
let tx_bits = tx.public_data_into_bits();
let tx_encoding = be_bit_vector_into_bytes(&tx_bits);
encoding.extend(tx_encoding.into_iter());
}

Ok(encoding)
}

pub fn encode_deposit_transactions(block: &DepositBlock) -> Result<Vec<u8>, String> {
let mut encoding: Vec<u8> = vec![];

// let sorted_block = sorted_deposit_block(&block);

let transactions = &block.transactions;

for tx in transactions {
let tx = models::circuit::DepositRequest::try_from(tx).map_err(|e| e.to_string())?;
let tx_bits = tx.public_data_into_bits();
let tx_encoding = be_bit_vector_into_bytes(&tx_bits);
encoding.extend(tx_encoding.into_iter());
}

Ok(encoding)
}

// this method is different, it actually reads the state
pub fn encode_exit_transactions(block: &ExitBlock) -> Result<Vec<u8>, String> {
let mut encoding: Vec<u8> = vec![];

let transactions = &block.transactions;

for tx in transactions {
let tx = models::circuit::ExitRequest::try_from(tx).map_err(|e| e.to_string())?;
// if tx.amount == Fr::zero() {
// println!("Trying to exit a zero balance");
// }
let tx_bits = tx.public_data_into_bits();
let tx_encoding = be_bit_vector_into_bytes(&tx_bits);
encoding.extend(tx_encoding.into_iter());
}

// let public_data_hex: String = encoding.clone().to_hex();
// println!("Final encoding = {}", public_data_hex);

Ok(encoding)
}
Loading

0 comments on commit 2567ca8

Please sign in to comment.