admin管理员组

文章数量:1123270

While trying to create a rust API server using risczero for implementation of merkle tree, I came across the following error.

thread 'actix-rt|system:0|arbiter:0' panicked at host/src/main.rs:89:61:
REASON: NotSupported
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'actix-rt|system:0|arbiter:1' panicked at host/src/main.rs:89:61:
REASON: NotSupported

host/src/main.rs

use actix_web::{web, App, HttpServer, HttpResponse};
use methods::{GUEST_ELF, GUEST_ID};
use risc0_zkvm::{default_prover, ExecutorEnv};
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Serialize, Deserialize, Debug)]
struct Node {
    hash: Vec<u8>,
    index: u64,
}

#[derive(Deserialize)]
struct ProverRequest {
    operation: String,
    data_blocks: Option<Vec<String>>,
    start_index: Option<u64>,
    data: Option<String>,
    index: Option<u64>,
    leaves: Option<Vec<Node>>,
    nodes: Option<Vec<Node>>,
}

#[derive(Serialize)]
struct ProverResponse {
    receipt: String,
    output: Value,
}

async fn handle_proof(req: web::Json<ProverRequest>) -> HttpResponse {
    let prover = default_prover();
    
    // Convert request to JSON value that matches the guest's expected format
    let input = match req.operation.as_str() {
        "Insert" => {
            if let (Some(data_blocks), Some(start_index)) = (&req.data_blocks, req.start_index) {
                serde_json::json!({
                    "type": "Insert",
                    "data_blocks": data_blocks,
                    "start_index": start_index
                })
            } else {
                return HttpResponse::BadRequest().json("Missing required fields for Insert");
            }
        },
        "GenerateProof" => {
            if let (Some(data), Some(index), Some(leaves)) = (&req.data, req.index, &req.leaves) {
                serde_json::json!({
                    "type": "GenerateProof",
                    "data": data,
                    "index": index,
                    "leaves": leaves
                })
            } else {
                return HttpResponse::BadRequest().json("Missing required fields for GenerateProof");
            }
        },
        "GetParentLevel" => {
            if let Some(nodes) = &req.nodes {
                serde_json::json!({
                    "type": "GetParentLevel",
                    "nodes": nodes
                })
            } else {
                return HttpResponse::BadRequest().json("Missing nodes for GetParentLevel");
            }
        },
        _ => return HttpResponse::BadRequest().json("Invalid operation"),
    };

    // Serialize to bytes
    let input_bytes = serde_json::to_vec(&input).unwrap();
    
    let env = ExecutorEnv::builder()
        .write(&input_bytes)
        .unwrap()
        .build()
        .unwrap();

    let prove_info = match prover.prove(env, GUEST_ELF) {
        Ok(info) => info,
        Err(e) => return HttpResponse::InternalServerError().json(format!("Proving failed: {}", e)),
    };

    if let Err(e) = prove_info.receipt.verify(GUEST_ID) {
        return HttpResponse::InternalServerError().json(format!("Verification failed: {}", e));
    }

    let output: Value = prove_info.receipt.journal.decode().expect("REASON");

    let response = ProverResponse {
        receipt: base64::encode(&bincode::serialize(&prove_info.receipt).unwrap()),
        output,
    };

    HttpResponse::Ok().json(response)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    println!("Starting RISC0 Merkle Tree server on port 8080...");
    
    HttpServer::new(|| {
        App::new()
            .route("/prove", web::post().to(handle_proof))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

I'm trying to call the endpoint using the following data

curl -X POST http://localhost:8080/prove \
-H "Content-Type: application/json" \
-d '{
  "operation": "Insert",
  "data_blocks": ["0001", "0002", "0003", "0004"],
  "start_index": 0
}'

What could possibly be causing this issue and how do I resolve this?

本文标签: