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

initial commit #1

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "backend_services"
version = "0.1.0"
authors = ["josojo <[email protected]>"]
edition = "2018"

[dependencies]
mongodb = "0.3.2"
serde_json = "1.0"
serde_derive = "1.0"
serde = "1.0"
bson = "0.9.1"
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Running pepper with snarks from this repo


## Prerequisits:

(These prerequisits will soon be dockerized)

Install mongodb("0.3.2") and run it using:

```sh
sudo systemctl start mongodb

```
- do not use any authentification for the database.


Install rust & cargo ("1.31") - older versions would not be compatible with newest mongodb drivers.



## Setup:

In order to load the first data into the data base, run:


```sh
cargo run --bin database_setup

```

## Running the code:

Running

```sh
cargo run --bin backend_services

```

will start a listener for the data base and run some c++ executions based on the data.
78 changes: 78 additions & 0 deletions src/bin/database_setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const ACCOUNTS: i32 = 8;
const TOKENS: i32 = 4;
const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize;

extern crate serde_json;
extern crate serde;

#[macro_use]
extern crate serde_derive;

#[derive(Serialize, Deserialize)]
struct State {
curState: String,
prevState: String,
nextStates: String,
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
slot: i32,
balances: [i64; SIZE_BALANCE]
josojo marked this conversation as resolved.
Show resolved Hide resolved
}

extern crate mongodb;
use mongodb::{bson, doc};
use mongodb::{Client, ThreadedClient};
use mongodb::db::ThreadedDatabase;


fn main() {

let client = Client::connect("localhost", 27017)
.expect("Failed to initialize standalone client.");

let coll = client.db("dfusion").collection("CurrentState");

let doc = doc! {
"CurrentState": "0000000000000000000000000000000000000000",
};

// Insert document into 'dfusion.CurrentState' collection
coll.insert_one(doc.clone(), None)
.ok().expect("Failed to insert CurrentState.");

let coll = client.db("dfusion").collection("State");


let state = State {
curState: "0000000000000000000000000000000000000000".to_owned(),
prevState: "0000000000000000000000000000000000000000".to_owned(),
josojo marked this conversation as resolved.
Show resolved Hide resolved
nextStates: "0000000000000000000000000000000000000000".to_owned(),
slot: 0,
balances: [0; SIZE_BALANCE]
};

let document = serde_json::to_string(&state).ok().expect("Failed to convert first State");

println!("{}", document);

// let document: String = String::from(r#"{"curState":"0000000000000000000000000000000000000000","prevState":"0000000000000000000000000000000000000000","nextStates":"0000000000000000000000000000000000000000","slot":0,"balances":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}"#);
// let temp = doc!( r#document#);
let temp = doc! {"curState":"0000000000000000000000000000000000000000","prevState":"0000000000000000000000000000000000000000","nextStates":"0000000000000000000000000000000000000000","slot":0,"balances":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]};
josojo marked this conversation as resolved.
Show resolved Hide resolved

// Insert document into 'dfusion.CurrentState' collection
coll.insert_one(temp.clone(), None)
.ok().expect("Failed to insert CurrentState.");

let coll = client.db("dfusion").collection("Deposits");

let doc2 = doc! {
"depositHash": "0000000000000000000000000000000000000000",
"depositIndex": "0000000000000000000000000000000000000000",
"slot": 1,
"addressIndex": 0,
josojo marked this conversation as resolved.
Show resolved Hide resolved
"tokenIndex": 1,
josojo marked this conversation as resolved.
Show resolved Hide resolved
"amount": 55465465,
};

// Insert document into 'dfusion.CurrentState' collection
coll.insert_one(doc2.clone(), None)
.ok().expect("Failed to insert Deposit");
}
67 changes: 67 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
extern crate serde_json;
extern crate serde;


const ACCOUNTS: i32 = 8;
josojo marked this conversation as resolved.
Show resolved Hide resolved
const TOKENS: i32 = 4;
const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize;

#[macro_use]
extern crate serde_derive;

#[derive(Serialize, Deserialize)]
struct State {
josojo marked this conversation as resolved.
Show resolved Hide resolved
curState: String,
prevState: String,
nextStates: String,
slot: i32,
balances: [i64; SIZE_BALANCE]
}
/*
impl State {
fn getNextState(&self) -> std::string::String {
self.nextStates
}
}*/
extern crate mongodb;
use mongodb::{Bson, bson, doc};
use mongodb::{Client, ThreadedClient};
use mongodb::db::ThreadedDatabase;


fn main() {
let client = Client::connect("localhost", 27017)
.expect("Failed to initialize standalone client.");

let coll = client.db("dfusion").collection("CurrentState");


// Find the document and receive a cursor
let mut cursor = coll.find(None, None)
.ok().expect("Failed to execute find.");

let item = cursor.next();

let cur_state;
// cursor.next() returns an Option<Result<Document>>
match item {
Some(Ok(doc)) => match doc.get("CurrentState") {
Some(&Bson::String(ref CurrentState)) => {cur_state = &CurrentState; println!("{}", cur_state)},
_ => panic!("Expected title to be a string!"),
},
Some(Err(_)) => panic!("Failed to get next from server!"),
None => panic!("Server returned no results!"),
}

let coll = client.db("dfusion").collection("State");


let cursor = match coll.find(Some(doc! { "curState" : "0x0000000000000000000000000000000000000000", },) , None) {
josojo marked this conversation as resolved.
Show resolved Hide resolved
Ok(cursor) => cursor,
Err(error) => panic!("The following error occured: {}", error)
};

for doc in cursor {
println!("{}", doc.unwrap());
}
}