diff --git a/contracts/near/context-config/src/mutate.rs b/contracts/near/context-config/src/mutate.rs index aef67545b..b84845479 100644 --- a/contracts/near/context-config/src/mutate.rs +++ b/contracts/near/context-config/src/mutate.rs @@ -83,7 +83,7 @@ impl ContextConfigs { let context_identity = signer_id.rt().expect("Infallible"); let current_nonce = *context.member_nonces.get(&context_identity).unwrap_or(&0); require!(current_nonce == *nonce, "invalid nonce"); - let _ = context + let _ignored = context .member_nonces .insert(context_identity.clone(), *nonce + 1); } @@ -101,7 +101,7 @@ impl ContextConfigs { ); let mut members = IterableSet::new(Prefix::Members(*context_id)); - let _ = members.insert(*author_id); + let _ignored = members.insert(*author_id); // Create incremental account ID let account_id: AccountId = format!("{}.{}", self.next_proxy_id, env::current_account_id()) @@ -110,7 +110,7 @@ impl ContextConfigs { self.next_proxy_id += 1; - let context = Context { + let mut context = Context { application: Guard::new( Prefix::Privileges(PrivilegeScope::Context( *context_id, @@ -143,6 +143,7 @@ impl ContextConfigs { account_id.clone(), ), }; + let _ignored = context.member_nonces.insert(*author_id, 0); if self.contexts.insert(*context_id, context).is_some() { env::panic_str("context already exists"); @@ -207,9 +208,9 @@ impl ContextConfigs { for member in members { env::log_str(&format!("Added `{member}` as a member of `{context_id}`")); - let _ = context.member_nonces.insert(*member, 0); + let _ignored = context.member_nonces.insert(*member, 0); - let _ = ctx_members.insert(*member); + let _ignored = ctx_members.insert(*member); } } @@ -231,7 +232,7 @@ impl ContextConfigs { .get_mut(); for member in members { - let _ = ctx_members.remove(&member); + let _ignored = ctx_members.remove(&member); let member = member.rt().expect("infallible conversion"); env::log_str(&format!( diff --git a/contracts/near/context-config/src/query.rs b/contracts/near/context-config/src/query.rs index 81a635fcb..a1fdbc7b2 100644 --- a/contracts/near/context-config/src/query.rs +++ b/contracts/near/context-config/src/query.rs @@ -125,10 +125,13 @@ impl ContextConfigs { pub fn fetch_nonce( &self, - member_id: Repr, context_id: Repr, + member_id: Repr, ) -> Option<&u64> { - let context = self.contexts.get(&context_id)?; + let context = self + .contexts + .get(&context_id) + .expect("context does not exist"); context.member_nonces.get(&member_id) } } diff --git a/crates/context/config/src/client/env/config/query.rs b/crates/context/config/src/client/env/config/query.rs index 5bb54a6b1..bf03307fd 100644 --- a/crates/context/config/src/client/env/config/query.rs +++ b/crates/context/config/src/client/env/config/query.rs @@ -107,7 +107,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> { &self, context_id: ContextId, member_id: ContextIdentity, - ) -> Result> { + ) -> Result, ClientError> { let params = fetch_nonce::FetchNonceRequest::new(context_id, member_id); utils::send(&self.client, Operation::Read(params)).await diff --git a/crates/context/config/src/client/env/config/query/fetch_nonce.rs b/crates/context/config/src/client/env/config/query/fetch_nonce.rs index 356457a64..6b1acbb63 100644 --- a/crates/context/config/src/client/env/config/query/fetch_nonce.rs +++ b/crates/context/config/src/client/env/config/query/fetch_nonce.rs @@ -16,14 +16,14 @@ use crate::types::{ContextId, ContextIdentity}; #[derive(Copy, Clone, Debug, Serialize)] pub(super) struct FetchNonceRequest { pub(super) context_id: Repr, - pub(super) member: Repr, + pub(super) member_id: Repr, } impl FetchNonceRequest { - pub const fn new(context_id: ContextId, member: ContextIdentity) -> Self { + pub const fn new(context_id: ContextId, member_id: ContextIdentity) -> Self { Self { context_id: Repr::new(context_id), - member: Repr::new(member), + member_id: Repr::new(member_id), } } } @@ -31,21 +31,19 @@ impl FetchNonceRequest { impl Method for FetchNonceRequest { const METHOD: &'static str = "fetch_nonce"; - type Returns = u64; + type Returns = Option; fn encode(self) -> eyre::Result> { serde_json::to_vec(&self).map_err(Into::into) } fn decode(response: Vec) -> eyre::Result { - let nonce: u64 = serde_json::from_slice(&response)?; - - Ok(nonce) + serde_json::from_slice(&response).map_err(Into::into) } } impl Method for FetchNonceRequest { - type Returns = u64; + type Returns = Option; const METHOD: &'static str = "fetch_nonce"; @@ -56,8 +54,8 @@ impl Method for FetchNonceRequest { let context_id: StarknetContextId = (*self.context_id).into(); context_id.encode(&mut call_data)?; - let member: StarknetContextIdentity = (*self.member).into(); - member.encode(&mut call_data)?; + let member_id: StarknetContextIdentity = (*self.member_id).into(); + member_id.encode(&mut call_data)?; Ok(call_data.0) } @@ -76,26 +74,26 @@ impl Method for FetchNonceRequest { .map_err(|_| eyre::eyre!("Failed to convert response to u64"))?, ); - Ok(nonce) + Ok(Some(nonce)) } } impl Method for FetchNonceRequest { - type Returns = u64; + type Returns = Option; const METHOD: &'static str = "fetch_nonce"; fn encode(self) -> eyre::Result> { let context_id = ICRepr::new(*self.context_id); - let member = ICRepr::new(*self.member); + let member_id = ICRepr::new(*self.member_id); - let payload = (context_id, member); + let payload = (context_id, member_id); Encode!(&payload).map_err(Into::into) } fn decode(response: Vec) -> eyre::Result { - let decoded = Decode!(&response, u64)?; + let decoded = Decode!(&response, Option)?; Ok(decoded) } diff --git a/crates/context/src/lib.rs b/crates/context/src/lib.rs index a9403b776..cdf7d4b95 100644 --- a/crates/context/src/lib.rs +++ b/crates/context/src/lib.rs @@ -217,22 +217,6 @@ impl ContextManager { ) } - let nonce: u64 = this - .config_client - .query::( - this.client_config.new.protocol.as_str().into(), - this.client_config.new.network.as_str().into(), - this.client_config.new.contract_id.as_str().into(), - ) - .fetch_nonce( - context.id.rt().expect("infallible conversion"), - identity_secret - .public_key() - .rt() - .expect("infallible conversion"), - ) - .await?; - this.config_client .mutate::( this.client_config.new.protocol.as_str().into(), @@ -253,7 +237,7 @@ impl ContextManager { ApplicationMetadataConfig(Repr::new(application.metadata.into())), ), ) - .send(*context_secret, nonce) + .send(*context_secret, 0) .await?; let proxy_contract = this @@ -427,17 +411,19 @@ impl ContextManager { return Ok(None); }; - let member_id = inviter_id.rt().expect("infallible conversion"); - - let nonce: u64 = self + let nonce = self .config_client .query::( context_config.protocol.as_ref().into(), context_config.network.as_ref().into(), context_config.contract.as_ref().into(), ) - .fetch_nonce(context_id.rt().expect("infallible conversion"), member_id) - .await?; + .fetch_nonce( + context_id.rt().expect("infallible conversion"), + inviter_id.rt().expect("infallible conversion"), + ) + .await? + .ok_or_eyre("The inviter doesen't exist")?; self.config_client .mutate::( @@ -957,7 +943,7 @@ impl ContextManager { ); }; - let nonce: u64 = self + let nonce = self .config_client .query::( context_config.protocol.as_ref().into(), @@ -968,7 +954,7 @@ impl ContextManager { context_id.rt().expect("infallible conversion"), signer_id.rt().expect("infallible conversion"), ) - .await?; + .await?.ok_or_eyre("Not a member")?; let _ = self .config_client