Skip to content

Commit

Permalink
feat: add aggregation_merkle_sum_tree (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
enricobottazzi authored Nov 13, 2023
1 parent f750790 commit 4412e10
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 53 deletions.
15 changes: 8 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const_env = "0.1.2"
num-bigint = "0.4.4"
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
summa-backend = { git = "https://github.com/summa-dev/summa-solvency", branch = "v1-for-summa-aggregation", version = "0.1.0" }
summa-backend = { git = "https://github.com/summa-dev/summa-solvency", branch = "v1-improvements-and-consolidation", version = "0.1.0" }
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2", tag = "v2023_04_20"}
tokio = { version = "1.34.0", features = ["full"] }
rand = "0.8"

[[bin]]
name = "mini-tree-server"
Expand Down
85 changes: 43 additions & 42 deletions bin/mini_tree_server.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
use axum::{extract::Json, http::StatusCode, response::IntoResponse, routing::post, Router};
use const_env::from_env;
use axum::{
extract::Json,
response::IntoResponse,
routing::post,
Router,
http::StatusCode,
};
use num_bigint::BigUint;
use std::net::SocketAddr;
use num_bigint::{BigUint};
use summa_backend::merkle_sum_tree::{Entry, MerkleSumTree, Node, Tree};

use summa_backend::{MerkleSumTree, Entry, Node, Tree};
use serde::{Deserialize, Serialize};

use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonNode {
pub hash: String,
pub balances: Vec<String>,
pub hash: String,
pub balances: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -42,11 +35,10 @@ const N_BYTES: usize = 14;
#[tokio::main]
async fn main() {
// Define the app with a route
let app = Router::new()
.route("/", post(create_mst));
let app = Router::new().route("/", post(create_mst));

// Define the address to serve on
let addr = SocketAddr::from(([0, 0, 0, 0], 4000)); // TODO: assign ports from env variable
let addr = SocketAddr::from(([0, 0, 0, 0], 4000)); // TODO: assign ports from env variable

// Start the server
axum::Server::bind(&addr)
Expand All @@ -56,40 +48,49 @@ async fn main() {
}

fn convert_node_to_json(node: &Node<N_ASSETS>) -> JsonNode {
JsonNode {
hash: format!("{:?}", node.hash),
balances: node.balances.iter().map(|b| format!("{:?}", b)).collect(),
}
JsonNode {
hash: format!("{:?}", node.hash),
balances: node.balances.iter().map(|b| format!("{:?}", b)).collect(),
}
}

async fn create_mst(Json(json_entries): Json<Vec<JsonEntry>>) -> Result<impl IntoResponse, (StatusCode, Json<JsonMerkleSumTree>)> {
async fn create_mst(
Json(json_entries): Json<Vec<JsonEntry>>,
) -> Result<impl IntoResponse, (StatusCode, Json<JsonMerkleSumTree>)> {
// Convert `JsonEntry` -> `Entry<N_ASSETS>`
let entries = json_entries.iter().map(|entry| {
let mut balances: [BigUint; N_ASSETS] = std::array::from_fn(|_| BigUint::from(0u32));
entry.balances.iter().enumerate().for_each(|(i, balance)| {
balances[i] = balance.parse::<BigUint>().unwrap();
});
Entry::new(entry.username.clone(), balances).unwrap()
}).collect::<Vec<Entry<N_ASSETS>>>();
let entries = json_entries
.iter()
.map(|entry| {
let mut balances: [BigUint; N_ASSETS] = std::array::from_fn(|_| BigUint::from(0u32));
entry.balances.iter().enumerate().for_each(|(i, balance)| {
balances[i] = balance.parse::<BigUint>().unwrap();
});
Entry::new(entry.username.clone(), balances).unwrap()
})
.collect::<Vec<Entry<N_ASSETS>>>();

// Create `MerkleSumTree<N_ASSETS, N_BYTES>` from `parsed_entries`
let tree = MerkleSumTree::<N_ASSETS, N_BYTES>::from_entries(entries, false).unwrap();

// Convert `MerkleSumTree<N_ASSETS, N_BYTES>` to `JsonMerkleSumTree`
let json_tree = JsonMerkleSumTree {
root: convert_node_to_json(&tree.root()),
nodes: tree.nodes().iter().map(|layer| {
layer.iter().map(convert_node_to_json).collect()
}).collect(),
depth: tree.depth().clone(),
entries: tree.entries().iter().map(|entry| {
JsonEntry {
balances: entry.balances().iter().map(|b| b.to_string()).collect(),
username: entry.username().to_string(),
}
}).collect(),
is_sorted: false, // TODO: assign from request data
};

root: convert_node_to_json(&tree.root()),
nodes: tree
.nodes()
.iter()
.map(|layer| layer.iter().map(convert_node_to_json).collect())
.collect(),
depth: tree.depth().clone(),
entries: tree
.entries()
.iter()
.map(|entry| JsonEntry {
balances: entry.balances().iter().map(|b| b.to_string()).collect(),
username: entry.username().to_string(),
})
.collect(),
is_sorted: false, // TODO: assign from request data
};

Ok((StatusCode::OK, Json(json_tree)))
}
Loading

0 comments on commit 4412e10

Please sign in to comment.