Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl #[derive(UserData)] and improve naming #284

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"crates/cli",
"crates/common",
"crates/contracts/*",
"crates/contracts/core/derive",
"crates/enclave/*",
"crates/utils/*",
]
Expand Down Expand Up @@ -128,6 +129,7 @@ mc-attestation-verifier = { version = "0.4.3", default-features = false }
quartz-cw-proof = { version = "0.1.0", path = "crates/enclave/cw-proof", default-features = false }
quartz-common = { version = "0.1.0", path = "crates/common", default-features = false }
quartz-contract-core = { version = "0.1.0", path = "crates/contracts/core", default-features = false }
quartz-contract-core-derive = { version = "0.1.0", path = "crates/contracts/core/derive", default-features = false }
quartz-dcap-verifier-msgs = { version = "0.1.0", path = "crates/contracts/dcap-verifier/msgs", default-features = false }
quartz-enclave-core = { version = "0.1.1", path = "crates/enclave/core", default-features = false }
quartz-proto = { version = "0.1.0", path = "crates/enclave/proto", default-features = false }
Expand Down
26 changes: 26 additions & 0 deletions crates/contracts/core/derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "quartz-contract-core-derive"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true
homepage.workspace = true
categories = ["cryptography::cryptocurrencies", "wasm"]
keywords = ["cosmos", "cosmwasm", "cycles", "quartz", "sgx"]
readme = "README.md"
description = """
Derive macros for Quartz contracts.
"""

[dependencies]
proc-macro2 = "1.0"
quartz-contract-core.workspace = true
quote = "1.0"
serde_json.workspace = true
sha2.workspace = true
syn = { version = "1.0", features = ["full"] }

[lib]
proc-macro = true
27 changes: 27 additions & 0 deletions crates/contracts/core/derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(UserData)]
pub fn user_data_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;

let expanded = quote! {
impl ::quartz_contract_core::msg::execute::attested::HasUserData for #name {
fn user_data(&self) -> ::quartz_contract_core::state::UserData {
use ::sha2::Digest;

let mut hasher = ::sha2::Sha256::new();
hasher.update(::serde_json::to_string(&self).expect("infallible serializer"));
let digest: [u8; 32] = hasher.finalize().into();

let mut user_data = [0u8; 64];
user_data[0..32].copy_from_slice(&digest);
user_data
}
}
};

TokenStream::from(expanded)
}
4 changes: 2 additions & 2 deletions crates/contracts/core/src/handler/execute/attested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
error::Error,
handler::Handler,
msg::execute::attested::{
Attestation, Attested, DcapAttestation, HasUserData, MockAttestation, MsgSansHandler, Quote,
Attestation, Attested, DcapAttestation, HasUserData, MockAttestation, Noop, Quote,
},
state::CONFIG,
};
Expand Down Expand Up @@ -180,7 +180,7 @@ where
}
}

impl<T> Handler for MsgSansHandler<T> {
impl<T> Handler for Noop<T> {
fn handle(
self,
_deps: DepsMut<'_>,
Expand Down
4 changes: 2 additions & 2 deletions crates/contracts/core/src/handler/execute/sequenced.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response, StdResult, Uint64};

use crate::{
error::Error, handler::Handler, msg::execute::sequenced::SequencedMsg, state::SEQUENCE_NUM,
error::Error, handler::Handler, msg::execute::sequenced::Sequenced, state::SEQUENCE_NUM,
};

impl<T: Handler> Handler for SequencedMsg<T> {
impl<T: Handler> Handler for Sequenced<T> {
fn handle(self, deps: DepsMut<'_>, env: &Env, info: &MessageInfo) -> Result<Response, Error> {
SEQUENCE_NUM.update(deps.storage, |mut counter| -> StdResult<_> {
counter += Uint64::one();
Expand Down
18 changes: 9 additions & 9 deletions crates/contracts/core/src/msg/execute/attested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,16 @@ impl Attestation for MockAttestation {
}

#[derive(Clone, Debug, PartialEq)]
pub struct MsgSansHandler<T>(pub T);
pub struct Noop<T>(pub T);

#[cw_serde]
pub struct RawMsgSansHandler<T>(pub T);
pub struct RawNoop<T>(pub T);

impl<T: Serialize> HasDomainType for RawMsgSansHandler<T> {
type DomainType = MsgSansHandler<T>;
impl<T: Serialize> HasDomainType for RawNoop<T> {
type DomainType = Noop<T>;
}

impl<T> HasUserData for MsgSansHandler<T>
impl<T> HasUserData for Noop<T>
where
T: HasUserData,
{
Expand All @@ -240,16 +240,16 @@ where
}
}

impl<T> TryFrom<RawMsgSansHandler<T>> for MsgSansHandler<T> {
impl<T> TryFrom<RawNoop<T>> for Noop<T> {
type Error = StdError;

fn try_from(value: RawMsgSansHandler<T>) -> Result<Self, Self::Error> {
fn try_from(value: RawNoop<T>) -> Result<Self, Self::Error> {
Ok(Self(value.0))
}
}

impl<T> From<MsgSansHandler<T>> for RawMsgSansHandler<T> {
fn from(value: MsgSansHandler<T>) -> Self {
impl<T> From<Noop<T>> for RawNoop<T> {
fn from(value: Noop<T>) -> Self {
Self(value.0)
}
}
16 changes: 8 additions & 8 deletions crates/contracts/core/src/msg/execute/sequenced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ use serde::Serialize;
use crate::msg::HasDomainType;

#[derive(Clone, Debug, PartialEq)]
pub struct SequencedMsg<D>(pub D);
pub struct Sequenced<D>(pub D);

#[cw_serde]
pub struct RawSequencedMsg<R>(pub R);
pub struct RawSequenced<R>(pub R);

impl<R: Serialize + HasDomainType> HasDomainType for RawSequencedMsg<R> {
type DomainType = SequencedMsg<R::DomainType>;
impl<R: Serialize + HasDomainType> HasDomainType for RawSequenced<R> {
type DomainType = Sequenced<R::DomainType>;
}

impl<R: HasDomainType> TryFrom<RawSequencedMsg<R>> for SequencedMsg<R::DomainType> {
impl<R: HasDomainType> TryFrom<RawSequenced<R>> for Sequenced<R::DomainType> {
type Error = StdError;

fn try_from(value: RawSequencedMsg<R>) -> Result<Self, Self::Error> {
fn try_from(value: RawSequenced<R>) -> Result<Self, Self::Error> {
Ok(Self(value.0.try_into()?))
}
}

impl<R: HasDomainType> From<SequencedMsg<R::DomainType>> for RawSequencedMsg<R> {
fn from(value: SequencedMsg<R::DomainType>) -> Self {
impl<R: HasDomainType> From<Sequenced<R::DomainType>> for RawSequenced<R> {
fn from(value: Sequenced<R::DomainType>) -> Self {
Self(value.0.into())
}
}
18 changes: 3 additions & 15 deletions examples/pingpong/contracts/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use cosmwasm_schema::cw_serde;
use quartz_common::contract::{
msg::execute::attested::{RawAttested, RawAttestedMsgSansHandler, RawDefaultAttestation},
msg::execute::attested::{RawAttested, RawAttestedNoop, RawDefaultAttestation},
prelude::*,
};

pub type AttestedMsg<M, RA = RawDefaultAttestation> = RawAttested<RawAttestedMsgSansHandler<M>, RA>;
pub type AttestedMsg<M, RA = RawDefaultAttestation> = RawAttested<RawAttestedNoop<M>, RA>;

#[cw_serde]
pub struct InstantiateMsg<RA = RawDefaultAttestation> {
Expand Down Expand Up @@ -34,24 +34,12 @@ pub mod execute {
pub message: HexBinary,
}

#[derive(UserData)]
#[cw_serde]
pub struct Pong {
pub pubkey: HexBinary,
pub response: HexBinary,
}

// TODO: make macro
impl HasUserData for Pong {
fn user_data(&self) -> UserData {
let mut hasher = Sha256::new();
hasher.update(serde_json::to_string(&self).expect("infallible serializer"));
let digest: [u8; 32] = hasher.finalize().into();

let mut user_data = [0u8; 64];
user_data[0..32].copy_from_slice(&digest);
user_data
}
}
}

#[cw_serde]
Expand Down
4 changes: 2 additions & 2 deletions examples/pingpong/enclave/src/wslistener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cosmrs::{tendermint::chain::Id as ChainId, AccountId};
use cw_client::{CwClient, GrpcClient};
use futures_util::StreamExt;
use quartz_common::{
contract::msg::execute::attested::{RawAttested, RawAttestedMsgSansHandler},
contract::msg::execute::attested::{RawAttested, RawAttestedNoop},
enclave::{
attestor::Attestor,
server::{WebSocketHandler, WsListenerConfig},
Expand Down Expand Up @@ -187,7 +187,7 @@ where
// Build on-chain response
// TODO add non-mock support
let pong_msg = ExecuteMsg::Pong(AttestedMsg {
msg: RawAttestedMsgSansHandler(attested.msg),
msg: RawAttestedNoop(attested.msg),
attestation: attested.attestation,
});

Expand Down
22 changes: 14 additions & 8 deletions examples/transfers/contracts/Cargo.lock

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

8 changes: 5 additions & 3 deletions examples/transfers/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ incremental = false
overflow-checks = true

[features]
mock-sgx = ["quartz-common/mock-sgx-cw"]
mock-sgx = ["quartz-contract-core/mock-sgx"]
library = []

[dependencies]
Expand All @@ -49,7 +49,8 @@ cw-storage-plus = { version = "2.0.0", default-features = false }
cw-utils = { version = "2.0.0", default-features = false }

# quartz
quartz-common = { version = "0.1.1", features = ["contract"] }
quartz-contract-core = { version = "0.1.0" }
quartz-contract-core-derive = { version = "0.1.0" }

# patch indirect deps
getrandom = { version = "0.2.15", features = ["js"] }
Expand All @@ -59,4 +60,5 @@ cw-multi-test = { version = "2.1.0", default-features = false }
serde_json = "1.0.122"

[patch.crates-io]
quartz-common = { path = "../../../crates/common" }
quartz-contract-core = { path = "../../../crates/contracts/core" }
quartz-contract-core-derive = { path = "../../../crates/contracts/core/derive" }
2 changes: 1 addition & 1 deletion examples/transfers/contracts/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, HexBinary, MessageInfo, Response,
StdResult,
};
use quartz_common::contract::handler::RawHandler;
use quartz_contract_core::handler::RawHandler;

use crate::{
error::ContractError,
Expand Down
2 changes: 1 addition & 1 deletion examples/transfers/contracts/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::StdError;
use cw_utils::PaymentError;
use quartz_common::contract::error::Error as QuartzError;
use quartz_contract_core::error::Error as QuartzError;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down
Loading
Loading