Skip to content

Commit

Permalink
rename parser functions
Browse files Browse the repository at this point in the history
  • Loading branch information
capossele committed Jan 30, 2024
1 parent d8ccbaa commit 11f8a97
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
12 changes: 6 additions & 6 deletions cli/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ pub fn set(x: U256, post_state_digest: FixedBytes<32>, seal: Vec<u8>) -> Vec<u8>
calldata.abi_encode()
}

/// Input parser from string to an encoded `Vec<u8>` compatible with the zkVM and Bonsai.
pub fn parse_input(input: String) -> Result<Vec<u8>> {
/// Input serializer returning an encoded `Vec<u8>` compatible with the zkVM and Bonsai.
pub fn serialize_input(input: String) -> Result<Vec<u8>> {
serialize(U256::from_str(&input)?)
}

/// Parses a proof to extract the required output from the journal.
pub fn parse_output(proof: Proof) -> Result<Vec<u8>> {
let output = U256::abi_decode(&proof.journal, true)?;
let calldata = set(output, proof.post_state_digest, proof.seal);
/// Parses a proof to extract the required calldata.
pub fn calldata(proof: Proof) -> Result<Vec<u8>> {
let x = U256::abi_decode(&proof.journal, true)?;
let calldata = set(x, proof.post_state_digest, proof.seal);
Ok(calldata)
}
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
// limitations under the License.

use anyhow::Result;
use interface::{parse_input, parse_output};
use interface::{calldata, serialize_input};
use methods::IS_EVEN_ELF;
use risc0_ethereum_sdk::cli;

mod interface;

fn main() -> Result<()> {
env_logger::init();
cli::run(IS_EVEN_ELF, parse_input, parse_output)
cli::run(IS_EVEN_ELF, serialize_input, calldata)
}
22 changes: 11 additions & 11 deletions sdk/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ enum Command {
pub fn query<T: serde::Serialize + Sized>(
guest_binary: String,
input: Option<T>,
parser: fn(input: T) -> Result<Vec<u8>>,
serialize_input: fn(input: T) -> Result<Vec<u8>>,
) -> Result<()> {
let elf = resolve_guest_entry(GUEST_LIST, &guest_binary)?;
let image_id = compute_image_id(&elf)?;
let output = match input {
// Input provided. Return the Ethereum ABI encoded proof.
Some(input) => {
let proof = prover::generate_proof(&elf, parser(input)?)?;
let proof = prover::generate_proof(&elf, serialize_input(input)?)?;
hex::encode(proof.abi_encode())
}
// No input. Return the Ethereum ABI encoded bytes32 image ID.
Expand All @@ -91,8 +91,8 @@ pub fn publish<T: serde::Serialize + Sized>(
rpc_url: String,
contract: String,
input: T,
parse_input: fn(input: T) -> Result<Vec<u8>>,
parse_output: fn(proof: Proof) -> Result<Vec<u8>>,
serialize_input: fn(input: T) -> Result<Vec<u8>>,
calldata: fn(proof: Proof) -> Result<Vec<u8>>,
) -> Result<()> {
let tx_sender = match std::env::var("ETH_WALLET_PRIVATE_KEY") {
Ok(private_key) => Some(eth::TxSender::new(
Expand All @@ -107,8 +107,8 @@ pub fn publish<T: serde::Serialize + Sized>(
if tx_sender.is_some() {
println!("Private key is set; transaction will be sent");
}
let proof = prover::generate_proof(elf, parse_input(input)?)?;
let calldata = parse_output(proof)?;
let proof = prover::generate_proof(elf, serialize_input(input)?)?;
let calldata = calldata(proof)?;

let runtime = tokio::runtime::Runtime::new()?;
runtime.block_on(send_transaction(tx_sender, calldata))?;
Expand All @@ -126,14 +126,14 @@ async fn send_transaction(tx_sender: Option<TxSender>, calldata: Vec<u8>) -> Res
/// Run the CLI.
pub fn run(
elf: &[u8],
parse_input: fn(input: String) -> Result<Vec<u8>>,
parse_output: fn(proof: Proof) -> Result<Vec<u8>>,
serialize_input: fn(input: String) -> Result<Vec<u8>>,
calldata: fn(proof: Proof) -> Result<Vec<u8>>,
) -> Result<()> {
match Command::parse() {
Command::Query {
guest_binary,
input,
} => query(guest_binary, input, parse_input)?,
} => query(guest_binary, input, serialize_input)?,
Command::Publish {
chain_id,
rpc_url,
Expand All @@ -145,8 +145,8 @@ pub fn run(
rpc_url,
contract,
input,
parse_input,
parse_output,
serialize_input,
calldata,
)?,
}

Expand Down

0 comments on commit 11f8a97

Please sign in to comment.