Skip to content
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ members = [
[workspace.dependencies]

[workspace.dependencies.snarkvm]
#git = "https://github.com/ProvableHQ/snarkVM.git"
#rev = "57bfe32"
version = "=3.8.0"
git = "https://github.com/ProvableHQ/snarkVM.git"
rev = "c1ea0705c"
#version = "=3.8.0"
#snarkvm = { path = "../snarkVM" }

[profile.release]
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Service

## Testing

```
git clone git@github.com:ProvableHQ/snarkOS.git
cd snarkOS
./devnet.sh (use network ID 2)
```

```
git clone git@github.com:ProvableHQ/service.git
cargo build --release
./target/release/authorize-service --network canary
./target/release/execute-service --network canary
./target/release/transfer_client --generate-requests
```

30 changes: 28 additions & 2 deletions authorize-service/src/authorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ use super::*;

// Initialize a thread-local `ProcessVariant`.
thread_local! {
pub static PROCESS: RefCell<Option<ProcessVariant>> = const { RefCell::new(None) };
pub static PROCESS1: RefCell<Option<ProcessVariant>> = const { RefCell::new(None) };
pub static PROCESS2: RefCell<Option<ProcessVariant>> = const { RefCell::new(None) };
}

pub fn authorize<N: Network>(bytes: Bytes) -> Result<Value> {
PROCESS.with(|process| {
PROCESS1.with(|process| {
// Initialize the process if it is not already initialized.
if process.borrow().is_none() {
*process.borrow_mut() = match N::ID {
Expand All @@ -45,3 +46,28 @@ pub fn authorize<N: Network>(bytes: Bytes) -> Result<Value> {
process.borrow().as_ref().unwrap().authorize(&bytes)
})
}

pub fn authorize_signed<N: Network>(bytes: Bytes) -> Result<Value> {
PROCESS2.with(|process| {
// Initialize the process if it is not already initialized.
if process.borrow().is_none() {
*process.borrow_mut() = match N::ID {
MainnetV0::ID => {
println!("Loading mainnet process...");
Some(ProcessVariant::MainnetV0(Process::load()?))
}
TestnetV0::ID => {
println!("Loading testnet process...");
Some(ProcessVariant::TestnetV0(Process::load()?))
}
CanaryV0::ID => {
println!("Loading canary process...");
Some(ProcessVariant::CanaryV0(Process::load()?))
}
_ => panic!("Invalid network"),
};
};
// Compute the `Authorization`.
process.borrow().as_ref().unwrap().authorize_request(&bytes)
})
}
1 change: 1 addition & 0 deletions authorize-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async fn run<N: Network>(port: u16) {

let routes = keygen_route::<N>()
.or(authorize_route::<N>())
.or(authorize_signed_route::<N>())
.or(sign_route::<N>())
.or(verify_route::<N>())
.with(warp::trace(
Expand Down
36 changes: 36 additions & 0 deletions authorize-service/src/process_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,39 @@ impl ProcessVariant {
Ok(serde_json::to_value(response)?)
}
}

impl ProcessVariant {
pub fn authorize_request(&self, bytes: &[u8]) -> Result<Value> {
match self {
ProcessVariant::MainnetV0(process) => {
Self::handle_authorize_request::<AleoV0, MainnetV0>(process, bytes)
}
ProcessVariant::TestnetV0(process) => {
Self::handle_authorize_request::<AleoTestnetV0, TestnetV0>(process, bytes)
}
ProcessVariant::CanaryV0(process) => {
Self::handle_authorize_request::<AleoCanaryV0, CanaryV0>(process, bytes)
}
}
}

fn handle_authorize_request<A: Aleo<Network = N>, N: Network>(
process: &Process<N>,
bytes: &[u8],
) -> Result<Value> {
// Deserialize the request.
let request = serde_json::from_slice::<AuthorizeSignedRequest<N>>(bytes)?;

// Initialize the RNG.
let rng = &mut rand_chacha::ChaCha20Rng::from_entropy();

// Authorize the function.
let authorization = process.authorize_request::<A, _>(request.request, rng)?;

// Construct the response.
let response = AuthorizeSignedResponse::<N> { authorization };

// Return the response as JSON.
Ok(serde_json::to_value(response)?)
}
}
6 changes: 6 additions & 0 deletions authorize-service/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ pub struct AuthorizeRequest<N: Network> {
pub priority_fee_in_microcredits: U64<N>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AuthorizeSignedRequest<N: Network> {
#[serde(bound(deserialize = ""))]
pub request: snarkvm::prelude::Request<N>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SignRequest<N: Network> {
#[serde(bound(deserialize = ""))]
Expand Down
6 changes: 6 additions & 0 deletions authorize-service/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub struct AuthorizeResponse<N: Network> {
pub fee_authorization: Authorization<N>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AuthorizeSignedResponse<N: Network> {
#[serde(bound(deserialize = ""))]
pub authorization: Authorization<N>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SignResponse {
pub signed_message: Vec<u8>,
Expand Down
17 changes: 17 additions & 0 deletions authorize-service/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ pub fn authorize_route<N: Network>() -> impl Filter<Extract = impl Reply, Error
})
}

// POST /authorize_signed
pub fn authorize_signed_route<N: Network>(
) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::post()
.and(warp::path("authorize_signed"))
.and(warp::path::end())
.and(warp::body::content_length_limit(32 * 1024)) // 32 KiB
.and(warp::body::bytes())
.and_then(|bytes: Bytes| async move {
let response = match tokio_rayon::spawn_fifo(|| authorize_signed::<N>(bytes)).await {
Ok(response) => response,
Err(_) => return Err(warp::reject()),
};
Ok(warp::reply::json(&response))
})
}

// POST /sign
pub fn sign_route<N: Network>() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::post()
Expand Down
8 changes: 4 additions & 4 deletions block-parser/src/block_json/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ impl InputJSON {
None => bail!("Invalid input ID"),
};
// Get the value of the input.
let value = match json.get("value").and_then(|v| v.as_str()) {
Some(value) => Some(value.to_string()),
None => None,
};
let value = json
.get("value")
.and_then(|v| v.as_str())
.map(|v| v.to_string());
Ok(Self { type_, id, value })
}

Expand Down
4 changes: 2 additions & 2 deletions block-parser/src/decoders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub fn decode_block_unchecked<N: Network>(string: &str) -> Result<(Vec<CreditsOp
let withdrawal = inputs.get(1).unwrap().value().to_owned().unwrap();
let inputs_value = inputs.get(2).unwrap().value();
let amount = match inputs_value {
Some(v) => *U64::<N>::from_str(&v)?,
Some(v) => *U64::<N>::from_str(v)?,
None => bail!("Invalid JSON object"),
};
// Add the `bond_public` operation to the credits transactions.
Expand Down Expand Up @@ -187,7 +187,7 @@ pub fn decode_block_unchecked<N: Network>(string: &str) -> Result<(Vec<CreditsOp
};
let inputs_value = inputs.get(1).unwrap().value();
let amount = match inputs_value {
Some(v) => *U64::<N>::from_str(&v)?,
Some(v) => *U64::<N>::from_str(v)?,
None => bail!("Invalid JSON object"),
};
// Add the `unbond_public` operation to the credits transactions.
Expand Down
8 changes: 4 additions & 4 deletions block-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ pub fn process_block_transactions(

#[cfg(test)]
mod tests {
use super::*;
use snarkvm::prelude::{CanaryV0, TestnetV0};
// use super::*;
// use snarkvm::prelude::{CanaryV0};

use std::fs::File;
// use std::fs::File;

type CurrentNetwork = CanaryV0;
// type CurrentNetwork = CanaryV0;

// #[test]
// fn test_dont_add_bonded_map() {
Expand Down
5 changes: 5 additions & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ path = "../execute-service"
[dependencies.anyhow]
version = "1.0.75"

[dependencies.clap]
version = "4.3"
features = ["derive"]

[dependencies.rand_chacha]
version = "0.3.1"

Expand All @@ -24,6 +28,7 @@ features = [ "full" ]

[dependencies.reqwest]
version = "0.11.22"
features = [ "json" ]

[dependencies.warp]
version = "0.3.6"
Loading