Skip to content

Commit

Permalink
merged feat--add-near-calls-nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Dec 23, 2024
2 parents dcac961 + 652c004 commit d73e5e5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 51 deletions.
105 changes: 56 additions & 49 deletions contracts/near/context-config/src/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,63 +23,66 @@ impl ContextConfigs {
.parse(|i| *i.signer_id)
.expect("failed to parse input");

match request.kind {
let (context_id, kind) = match request.kind {
RequestKind::Context(ContextRequest {
context_id, kind, ..
}) => match kind {
ContextRequestKind::Add {
author_id,
application,
} => {
let _is_sent_on_drop =
self.add_context(&request.signer_id, context_id, author_id, application);
}
ContextRequestKind::UpdateApplication { application } => {
self.check_and_increment_nonce(&request.nonce, &request.signer_id, context_id);
self.update_application(&request.signer_id, context_id, application);
}
ContextRequestKind::AddMembers { members } => {
self.check_and_increment_nonce(&request.nonce, &request.signer_id, context_id);
self.add_members(&request.signer_id, context_id, members.into_owned());
}
ContextRequestKind::RemoveMembers { members } => {
self.check_and_increment_nonce(&request.nonce, &request.signer_id, context_id);
self.remove_members(&request.signer_id, context_id, members.into_owned());
}
ContextRequestKind::Grant { capabilities } => {
self.check_and_increment_nonce(&request.nonce, &request.signer_id, context_id);
self.grant(&request.signer_id, context_id, capabilities.into_owned());
}
ContextRequestKind::Revoke { capabilities } => {
self.check_and_increment_nonce(&request.nonce, &request.signer_id, context_id);
self.revoke(&request.signer_id, context_id, capabilities.into_owned());
}
ContextRequestKind::UpdateProxyContract => {
let _is_sent_on_drop =
self.update_proxy_contract(&request.signer_id, context_id);
}
},
}) => (context_id, kind),
};

self.check_and_increment_nonce(
*context_id,
request.signer_id.rt().expect("infallible conversion"),
request.nonce,
);

match kind {
ContextRequestKind::Add {
author_id,
application,
} => {
let _is_sent_on_drop =
self.add_context(&request.signer_id, context_id, author_id, application);
}
ContextRequestKind::UpdateApplication { application } => {
self.update_application(&request.signer_id, context_id, application);
}
ContextRequestKind::AddMembers { members } => {
self.add_members(&request.signer_id, context_id, members.into_owned());
}
ContextRequestKind::RemoveMembers { members } => {
self.remove_members(&request.signer_id, context_id, members.into_owned())
}
ContextRequestKind::Grant { capabilities } => {
self.grant(&request.signer_id, context_id, capabilities.into_owned());
}
ContextRequestKind::Revoke { capabilities } => {
self.revoke(&request.signer_id, context_id, capabilities.into_owned());
}
ContextRequestKind::UpdateProxyContract => {
let _is_sent_on_drop = self.update_proxy_contract(&request.signer_id, context_id);
}
}
}
}

impl ContextConfigs {
fn check_and_increment_nonce(
&mut self,
nonce: &u64,
signer_id: &SignerId,
context_id: Repr<ContextId>,
context_id: ContextId,
member_id: ContextIdentity,
nonce: u64,
) {
let context: &mut Context = self
.contexts
.get_mut(&context_id)
.expect("context does not exist");
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 _ignored = context
.member_nonces
.insert(context_identity.clone(), *nonce + 1);
let Some(context) = self.contexts.get_mut(&context_id) else {
return;
};

let Some(current_nonce) = context.member_nonces.get_mut(&member_id) else {
return;
};

require!(*current_nonce == nonce, "invalid nonce");

*current_nonce += 1;
}

fn add_context(
Expand Down Expand Up @@ -137,6 +140,7 @@ impl ContextConfigs {
account_id.clone(),
),
};

let _ignored = context.member_nonces.insert(*author_id, 0);

if self.contexts.insert(*context_id, context).is_some() {
Expand Down Expand Up @@ -202,7 +206,7 @@ impl ContextConfigs {
for member in members {
env::log_str(&format!("Added `{member}` as a member of `{context_id}`"));

let _ignored = context.member_nonces.insert(*member, 0);
let _ignored = context.member_nonces.entry(*member).or_default();

let _ignored = ctx_members.insert(*member);
}
Expand All @@ -227,6 +231,9 @@ impl ContextConfigs {

for member in members {
let _ignored = ctx_members.remove(&member);

let _ignored = context.member_nonces.remove(&member);

let member = member.rt().expect("infallible conversion");

env::log_str(&format!(
Expand Down Expand Up @@ -345,7 +352,7 @@ impl ContextConfigs {
code_mut.take().expect("proxy code not set")
}

pub fn init_proxy_contract(
fn init_proxy_contract(
&mut self,
context_id: Repr<ContextId>,
account_id: AccountId,
Expand Down
8 changes: 6 additions & 2 deletions crates/merod/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ impl InitCommand {
ContextConfig {
client: ClientConfig {
signer: ClientSigner {
selected: ClientSelectedSigner::Relayer,
selected: match self.protocol {
ConfigProtocol::Near => ClientSelectedSigner::Relayer,
ConfigProtocol::Starknet => ClientSelectedSigner::Relayer,
ConfigProtocol::Icp => ClientSelectedSigner::Local,
},
relayer: ClientRelayerSigner { url: relayer },
local: LocalConfig {
near: [
Expand Down Expand Up @@ -286,7 +290,7 @@ impl InitCommand {
network: match self.protocol {
ConfigProtocol::Near => "testnet".into(),
ConfigProtocol::Starknet => "sepolia".into(),
ConfigProtocol::Icp => "ic".into(),
ConfigProtocol::Icp => "local".into(),
},
protocol: self.protocol.as_str().to_owned(),
contract_id: match self.protocol {
Expand Down

0 comments on commit d73e5e5

Please sign in to comment.