Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frdomovic committed Dec 20, 2024
1 parent 589f176 commit edb97e4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 48 deletions.
13 changes: 7 additions & 6 deletions contracts/near/context-config/src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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())
Expand All @@ -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,
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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!(
Expand Down
7 changes: 5 additions & 2 deletions contracts/near/context-config/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,13 @@ impl ContextConfigs {

pub fn fetch_nonce(
&self,
member_id: Repr<ContextIdentity>,
context_id: Repr<ContextId>,
member_id: Repr<ContextIdentity>,
) -> 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)
}
}
2 changes: 1 addition & 1 deletion crates/context/config/src/client/env/config/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> {
&self,
context_id: ContextId,
member_id: ContextIdentity,
) -> Result<u64, ClientError<T>> {
) -> Result<Option<u64>, ClientError<T>> {
let params = fetch_nonce::FetchNonceRequest::new(context_id, member_id);

utils::send(&self.client, Operation::Read(params)).await
Expand Down
28 changes: 13 additions & 15 deletions crates/context/config/src/client/env/config/query/fetch_nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,34 @@ use crate::types::{ContextId, ContextIdentity};
#[derive(Copy, Clone, Debug, Serialize)]
pub(super) struct FetchNonceRequest {
pub(super) context_id: Repr<ContextId>,
pub(super) member: Repr<ContextIdentity>,
pub(super) member_id: Repr<ContextIdentity>,
}

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),
}
}
}

impl Method<Near> for FetchNonceRequest {
const METHOD: &'static str = "fetch_nonce";

type Returns = u64;
type Returns = Option<u64>;

fn encode(self) -> eyre::Result<Vec<u8>> {
serde_json::to_vec(&self).map_err(Into::into)
}

fn decode(response: Vec<u8>) -> eyre::Result<Self::Returns> {
let nonce: u64 = serde_json::from_slice(&response)?;

Ok(nonce)
serde_json::from_slice(&response).map_err(Into::into)
}
}

impl Method<Starknet> for FetchNonceRequest {
type Returns = u64;
type Returns = Option<u64>;

const METHOD: &'static str = "fetch_nonce";

Expand All @@ -56,8 +54,8 @@ impl Method<Starknet> 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)
}
Expand All @@ -76,26 +74,26 @@ impl Method<Starknet> for FetchNonceRequest {
.map_err(|_| eyre::eyre!("Failed to convert response to u64"))?,
);

Ok(nonce)
Ok(Some(nonce))
}
}

impl Method<Icp> for FetchNonceRequest {
type Returns = u64;
type Returns = Option<u64>;

const METHOD: &'static str = "fetch_nonce";

fn encode(self) -> eyre::Result<Vec<u8>> {
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<u8>) -> eyre::Result<Self::Returns> {
let decoded = Decode!(&response, u64)?;
let decoded = Decode!(&response, Option<u64>)?;

Ok(decoded)
}
Expand Down
34 changes: 10 additions & 24 deletions crates/context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,22 +217,6 @@ impl ContextManager {
)
}

let nonce: u64 = this
.config_client
.query::<ContextConfigEnv>(
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::<ContextConfigEnv>(
this.client_config.new.protocol.as_str().into(),
Expand All @@ -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
Expand Down Expand Up @@ -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::<ContextConfigEnv>(
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::<ContextConfigEnv>(
Expand Down Expand Up @@ -957,7 +943,7 @@ impl ContextManager {
);
};

let nonce: u64 = self
let nonce = self
.config_client
.query::<ContextConfigEnv>(
context_config.protocol.as_ref().into(),
Expand All @@ -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
Expand Down

0 comments on commit edb97e4

Please sign in to comment.