Skip to content

Commit

Permalink
🐛 Add contracts binries in docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
BertrandD committed Nov 27, 2024
1 parent 2b95888 commit 937a2bc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
3 changes: 3 additions & 0 deletions Dockerfile.server
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ COPY ./crates ./crates
COPY Cargo.toml .
COPY Cargo.lock .

RUN mkdir -p /hyle/contracts/hyllar && wget https://raw.githubusercontent.com/Hyle-org/hyle/refs/heads/main/contracts/hyllar/hyllar.img -O /hyle/contracts/hyllar/hyllar.img
RUN mkdir -p /hyle/contracts/hydentity && wget https://raw.githubusercontent.com/Hyle-org/hyle/refs/heads/main/contracts/hydentity/hydentity.img -O /hyle/contracts/hydentity/hydentity.img

RUN cargo build -p server

# RUNNER
Expand Down
30 changes: 7 additions & 23 deletions crates/server/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use borsh::to_vec;
use hyle::{
indexer::model::ContractDb,
model::{BlobTransaction, ProofData, ProofTransaction},
node_state::model::Contract,
rest::client::ApiHttpClient,
};
use sdk::{
Expand All @@ -13,6 +12,7 @@ use sdk::{
pub async fn run<State, Builder>(
client: &ApiHttpClient,
contract_name: &ContractName,
binary: &'static [u8],
build_contract_input: Builder,
) -> Result<ProofData>
where
Expand All @@ -27,7 +27,7 @@ where

println!("{}", "-".repeat(20));
println!("Checking transition for {contract_name}...");
let execute_info = execute(contract_name, &contract_input)?;
let execute_info = execute(binary, &contract_input)?;
let output = execute_info.journal.decode::<HyleOutput>().unwrap();
if !output.success {
let program_error = std::str::from_utf8(&output.program_outputs).unwrap();
Expand All @@ -42,7 +42,7 @@ where

println!("{}", "-".repeat(20));
println!("Proving transition for {contract_name}...");
let prove_info = prove(contract_name, &contract_input)?;
let prove_info = prove(binary, &contract_input)?;

let receipt = prove_info.receipt;

Expand Down Expand Up @@ -80,7 +80,7 @@ where
}

fn execute<State>(
contract_name: &ContractName,
binary: &'static [u8],
contract_input: &ContractInput<State>,
) -> Result<risc0_zkvm::SessionInfo>
where
Expand All @@ -93,19 +93,11 @@ where
.unwrap();

let prover = risc0_zkvm::default_executor();
let file_path = format!("../hyle/contracts/{}/{}.img", contract_name, contract_name);
if let Ok(binary) = std::fs::read(file_path.as_str()) {
Ok(prover.execute(env, &binary).unwrap())
} else {
println!("Could not read ELF binary at {}.", file_path);
println!("Please ensure that the ELF binary is built and located at the specified path.");
println!("\x1b[93m--> Tip: Did you run build_contracts.sh ?\x1b[0m");
bail!("Could not read ELF binary");
}
Ok(prover.execute(env, binary).unwrap())
}

fn prove<State>(
contract_name: &ContractName,
binary: &'static [u8],
contract_input: &ContractInput<State>,
) -> Result<risc0_zkvm::ProveInfo>
where
Expand All @@ -118,15 +110,7 @@ where
.unwrap();

let prover = risc0_zkvm::default_prover();
let file_path = format!("../hyle/contracts/{}/{}.img", contract_name, contract_name);
if let Ok(binary) = std::fs::read(file_path.as_str()) {
Ok(prover.prove(env, &binary).unwrap())
} else {
println!("Could not read ELF binary at {}.", file_path);
println!("Please ensure that the ELF binary is built and located at the specified path.");
println!("\x1b[93m--> Tip: Did you run build_contracts.sh ?\x1b[0m");
bail!("Could not read ELF binary");
}
Ok(prover.prove(env, binary).unwrap())
}

pub async fn send_proof(
Expand Down
6 changes: 6 additions & 0 deletions crates/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use utils::AppError;
mod contract;
mod utils;

static HYLLAR_BIN: &[u8] = include_bytes!("../../../../hyle/contracts/hyllar/hyllar.img");
static HYDENTITY_BIN: &[u8] = include_bytes!("../../../../hyle/contracts/hydentity/hydentity.img");

#[tokio::main]
async fn main() {
// Créer un middleware CORS
Expand Down Expand Up @@ -127,6 +130,7 @@ async fn register_identity(username: String, password: String) -> Result<TxHash,
let hydentity_proof = contract::run(
&client,
&"hydentity".into(),
HYDENTITY_BIN,
|token: hydentity::Hydentity| -> ContractInput<hydentity::Hydentity> {
ContractInput::<Hydentity> {
initial_state: token,
Expand Down Expand Up @@ -196,6 +200,7 @@ async fn do_transfer(
let hydentity_proof = contract::run(
&client,
&"hydentity".into(),
HYDENTITY_BIN,
|token: hydentity::Hydentity| -> ContractInput<hydentity::Hydentity> {
ContractInput::<Hydentity> {
initial_state: token,
Expand All @@ -211,6 +216,7 @@ async fn do_transfer(
let transfer_proof = contract::run(
&client,
&"hyllar".into(),
HYLLAR_BIN,
|token: hyllar::HyllarToken| -> ContractInput<hyllar::HyllarToken> {
ContractInput::<HyllarToken> {
initial_state: token,
Expand Down
2 changes: 2 additions & 0 deletions crates/ui/Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ address = "127.0.0.1"
# The port to serve on.
port = 8002

[watch]
ignore = ["../server/"]

0 comments on commit 937a2bc

Please sign in to comment.