From 2c40064c893f6ca03336d4d6148cc86673243d99 Mon Sep 17 00:00:00 2001 From: Alistair Date: Thu, 26 Oct 2023 12:52:48 +0100 Subject: [PATCH] Node: Deserialize the receipt --- Cargo.lock | 2 ++ jstz_node/Cargo.toml | 2 ++ jstz_node/src/services/operations.rs | 10 +++++++++- jstz_proto/src/error.rs | 10 ---------- jstz_proto/src/receipt.rs | 21 +++++++++------------ 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee2de1035..8632a1173 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1717,9 +1717,11 @@ version = "0.1.0" dependencies = [ "actix-web", "anyhow", + "bincode", "clap", "env_logger", "hex", + "jstz_proto", "reqwest", "serde", "thiserror", diff --git a/jstz_node/Cargo.toml b/jstz_node/Cargo.toml index e2f184807..c60a26b01 100644 --- a/jstz_node/Cargo.toml +++ b/jstz_node/Cargo.toml @@ -10,9 +10,11 @@ repository.workspace = true [dependencies] actix-web = "4.4.0" anyhow = "1.0.75" +bincode = "1.3.3" clap = { version = "^4.0", features = ["derive"] } env_logger = "0.10.0" hex = "0.4.3" +jstz_proto.workspace = true reqwest = { version = "0.11.22", features = ["json"] } serde = { version = "1.0.183", features = ["derive"] } thiserror = "1.0.50" diff --git a/jstz_node/src/services/operations.rs b/jstz_node/src/services/operations.rs index c9c0fc44b..833c7c10d 100644 --- a/jstz_node/src/services/operations.rs +++ b/jstz_node/src/services/operations.rs @@ -3,6 +3,8 @@ use actix_web::{ web::{Data, Path, ServiceConfig}, HttpResponse, Responder, Scope, }; +use anyhow::anyhow; +use jstz_proto::receipt::Receipt; use crate::{rollup::RollupClient, Result}; @@ -15,7 +17,13 @@ async fn receipt( let value = rollup_client.get_value(&key).await?; - Ok(HttpResponse::Ok().json(value)) + let receipt = match value { + Some(value) => bincode::deserialize::(&value) + .map_err(|_| anyhow!("Failed to deserialize receipt"))?, + None => return Ok(HttpResponse::NotFound().finish()), + }; + + Ok(HttpResponse::Ok().json(receipt)) } pub struct OperationsSerivce; diff --git a/jstz_proto/src/error.rs b/jstz_proto/src/error.rs index 409be3b81..c70f664a3 100644 --- a/jstz_proto/src/error.rs +++ b/jstz_proto/src/error.rs @@ -1,6 +1,5 @@ use boa_engine::{JsError, JsNativeError}; use derive_more::{Display, Error, From}; -use serde::Serialize; #[derive(Display, Debug, Error, From)] pub enum Error { @@ -13,15 +12,6 @@ pub enum Error { } pub type Result = std::result::Result; -impl Serialize for Error { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: serde::Serializer, - { - serializer.serialize_str(&format!("{}", self)) - } -} - impl From for JsError { fn from(value: Error) -> Self { match value { diff --git a/jstz_proto/src/receipt.rs b/jstz_proto/src/receipt.rs index 1a16fa79a..ddc33d7f2 100644 --- a/jstz_proto/src/receipt.rs +++ b/jstz_proto/src/receipt.rs @@ -1,17 +1,20 @@ use http::{HeaderMap, StatusCode}; use jstz_api::http::body::HttpBody; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use crate::{context::account::Address, operation::OperationHash, Result}; -#[derive(Debug, Serialize)] +pub type ReceiptResult = std::result::Result; + +#[derive(Debug, Serialize, Deserialize)] pub struct Receipt { hash: OperationHash, - pub inner: Result, + pub inner: ReceiptResult, } impl Receipt { pub fn new(hash: OperationHash, inner: Result) -> Self { + let inner = inner.map_err(|e| e.to_string()); Self { hash, inner } } @@ -20,18 +23,12 @@ impl Receipt { } } -impl AsRef> for Receipt { - fn as_ref(&self) -> &Result { - &self.inner - } -} - -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct DeployContract { pub contract_address: Address, } -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct RunContract { pub body: HttpBody, #[serde(with = "http_serde::status_code")] @@ -40,7 +37,7 @@ pub struct RunContract { pub headers: HeaderMap, } -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, Deserialize)] pub enum Content { DeployContract(DeployContract), RunContract(RunContract),