From c5119c45f70bb934f6217966892e9ea4b1bea822 Mon Sep 17 00:00:00 2001 From: "nacho.d.g" Date: Sun, 24 Nov 2024 12:57:18 +0100 Subject: [PATCH] Add ws api response improvements (#24) * feat: add node queries request * add new ev * fix put contract request * fix get request * do not rename get request params * Make debug impl more readable --------- Co-authored-by: Hector Santos --- rust/src/client_api/client_events.rs | 26 +++++++++++++++++--------- rust/src/code_hash.rs | 8 +++++++- rust/src/contract_interface.rs | 28 +++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/rust/src/client_api/client_events.rs b/rust/src/client_api/client_events.rs index 80ae624..4056528 100644 --- a/rust/src/client_api/client_events.rs +++ b/rust/src/client_api/client_events.rs @@ -201,7 +201,7 @@ pub enum ContractError { MissingRelated { key: crate::contract_interface::ContractInstanceId, }, - #[error("missing related contract: {key}")] + #[error("missing contract: {key}")] MissingContract { key: crate::contract_interface::ContractInstanceId, }, @@ -275,10 +275,10 @@ impl ClientRequest<'_> { } ContractRequest::Get { key, - fetch_contract, + return_contract_code, } => ContractRequest::Get { key, - fetch_contract, + return_contract_code, }, ContractRequest::Subscribe { key, summary } => ContractRequest::Subscribe { key, @@ -366,7 +366,7 @@ pub enum ContractRequest<'a> { /// Key of the contract. key: ContractKey, /// If this flag is set then fetch also the contract itself. - fetch_contract: bool, + return_contract_code: bool, }, /// Subscribe to the changes in a given contract. Implicitly starts a get operation /// if the contract is not present yet. @@ -394,10 +394,10 @@ impl ContractRequest<'_> { }, Self::Get { key, - fetch_contract, + return_contract_code: fetch_contract, } => ContractRequest::Get { key, - fetch_contract, + return_contract_code: fetch_contract, }, Self::Subscribe { key, summary } => ContractRequest::Subscribe { key, @@ -424,7 +424,7 @@ impl<'a> TryFromFbs<&FbsContractRequest<'a>> for ContractRequest<'a> { let fetch_contract = get.fetch_contract(); ContractRequest::Get { key, - fetch_contract, + return_contract_code: fetch_contract, } } ContractRequestType::Put => { @@ -560,7 +560,7 @@ impl Display for ClientRequest<'_> { ContractRequest::Update { key, .. } => write!(f, "update request for {key}"), ContractRequest::Get { key, - fetch_contract: contract, + return_contract_code: contract, .. } => { write!( @@ -1110,6 +1110,7 @@ impl HostResponse { finish_host_response_buffer(&mut builder, host_response_offset); Ok(builder.finished_data().to_vec()) } + ContractResponse::SubscribeResponse { .. } => todo!(), }, HostResponse::DelegateResponse { key, values } => { let key_data = builder.create_vector(key.bytes()); @@ -1346,6 +1347,9 @@ impl std::fmt::Display for HostResponse { ContractResponse::UpdateNotification { key, .. } => { f.write_fmt(format_args!("update notification for `{key}`")) } + ContractResponse::SubscribeResponse { key, .. } => { + f.write_fmt(format_args!("subscribe response for `{key}`")) + } }, HostResponse::DelegateResponse { .. } => write!(f, "delegate responses"), HostResponse::Ok => write!(f, "ok response"), @@ -1378,6 +1382,10 @@ pub enum ContractResponse { #[serde(deserialize_with = "StateSummary::deser_state_summary")] summary: StateSummary<'static>, }, + SubscribeResponse { + key: ContractKey, + subscribed: bool, + }, } impl From> for HostResponse { @@ -1453,7 +1461,7 @@ mod client_request_test { match request { ContractRequest::Get { key, - fetch_contract, + return_contract_code: fetch_contract, } => { assert_eq!(key.encoded_contract_id(), EXPECTED_ENCODED_CONTRACT_ID); assert!(!fetch_contract); diff --git a/rust/src/code_hash.rs b/rust/src/code_hash.rs index 838819e..2d3f182 100644 --- a/rust/src/code_hash.rs +++ b/rust/src/code_hash.rs @@ -7,7 +7,7 @@ use serde_with::serde_as; const CONTRACT_KEY_SIZE: usize = 32; #[serde_as] -#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)] +#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)] #[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))] pub struct CodeHash(#[serde_as(as = "[_; CONTRACT_KEY_SIZE]")] pub(crate) [u8; CONTRACT_KEY_SIZE]); @@ -71,3 +71,9 @@ impl Display for CodeHash { write!(f, "{}", self.encode()) } } + +impl std::fmt::Debug for CodeHash { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("CodeHash").field(&self.encode()).finish() + } +} diff --git a/rust/src/contract_interface.rs b/rust/src/contract_interface.rs index 2af9875..67d6f42 100644 --- a/rust/src/contract_interface.rs +++ b/rust/src/contract_interface.rs @@ -777,7 +777,7 @@ impl<'a> DerefMut for StateSummary<'a> { /// It is the part of the executable belonging to the full specification /// and does not include any other metadata (like the parameters). #[serde_as] -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Serialize, Deserialize, Clone)] #[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))] pub struct ContractCode<'a> { // TODO: conver this to Arc<[u8]> instead @@ -900,9 +900,17 @@ impl std::fmt::Display for ContractCode<'_> { } } +impl std::fmt::Debug for ContractCode<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ContractCode") + .field("hash", &self.code_hash); + Ok(()) + } +} + /// The key representing the hash of the contract executable code hash and a set of `parameters`. #[serde_as] -#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)] +#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Hash)] #[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))] #[repr(transparent)] pub struct ContractInstanceId(#[serde_as(as = "[_; CONTRACT_KEY_SIZE]")] [u8; CONTRACT_KEY_SIZE]); @@ -970,6 +978,14 @@ impl Display for ContractInstanceId { } } +impl std::fmt::Debug for ContractInstanceId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("ContractInstanceId") + .field(&self.encode()) + .finish() + } +} + /// A complete key specification, that represents a cryptographic hash that identifies the contract. #[serde_as] #[derive(Debug, Eq, Copy, Clone, Serialize, Deserialize)] @@ -1137,7 +1153,7 @@ fn internal_fmt_key( // TODO: get rid of this when State is internally an Arc<[u8]> /// The state for a contract. -#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)] +#[derive(PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)] #[cfg_attr(any(feature = "testing", test), derive(arbitrary::Arbitrary))] pub struct WrappedState( #[serde( @@ -1223,6 +1239,12 @@ impl std::fmt::Display for WrappedState { } } +impl std::fmt::Debug for WrappedState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + ::fmt(self, f) + } +} + /// Just as `freenet_stdlib::Contract` but with some convenience impl. #[non_exhaustive] #[derive(Clone, Debug, Serialize, Deserialize)]