Skip to content

Commit

Permalink
Node: Deserialize the receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
johnyob committed Oct 30, 2023
1 parent c06e196 commit 9ca3216
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions jstz_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 9 additions & 1 deletion jstz_node/src/services/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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::<Receipt>(&value)
.map_err(|_| anyhow!("Failed to deserialize receipt"))?,
None => return Ok(HttpResponse::NotFound().finish()),
};

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

pub struct OperationsService;
Expand Down
10 changes: 0 additions & 10 deletions jstz_proto/src/error.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -13,15 +12,6 @@ pub enum Error {
}
pub type Result<T> = std::result::Result<T, Error>;

impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format!("{}", self))
}
}

impl From<Error> for JsError {
fn from(value: Error) -> Self {
match value {
Expand Down
21 changes: 9 additions & 12 deletions jstz_proto/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, String>;

#[derive(Debug, Serialize, Deserialize)]
pub struct Receipt {
hash: OperationHash,
pub inner: Result<Content>,
pub inner: ReceiptResult<Content>,
}

impl Receipt {
pub fn new(hash: OperationHash, inner: Result<Content>) -> Self {
let inner = inner.map_err(|e| e.to_string());
Self { hash, inner }
}

Expand All @@ -20,18 +23,12 @@ impl Receipt {
}
}

impl AsRef<Result<Content>> for Receipt {
fn as_ref(&self) -> &Result<Content> {
&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")]
Expand All @@ -40,7 +37,7 @@ pub struct RunContract {
pub headers: HeaderMap,
}

#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Deserialize)]
pub enum Content {
DeployContract(DeployContract),
RunContract(RunContract),
Expand Down

0 comments on commit 9ca3216

Please sign in to comment.