Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberHoward committed May 30, 2024
1 parent 66bc36d commit 09dac0b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 28 deletions.
57 changes: 46 additions & 11 deletions contracts/interchain-gov/src/handlers/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use crate::msg::InterchainGovQueryMsg;
use crate::msg::{InterchainGovIbcCallbackMsg, InterchainGovIbcMsg};
use crate::state::{
Governance, GovernanceVote, Members, Proposal, ProposalAction, ProposalId, ProposalMsg,
ProposalOutcome, TallyResult, Vote, FINALIZED_PROPOSALS, GOV_VOTE_QUERIES, MEMBERS,
MEMBERS_STATE_SYNC, PENDING_REPLIES, PROPOSAL_STATE_SYNC, TEMP_REMOTE_GOV_MODULE_ADDRS, VOTE,
VOTE_RESULTS,
ProposalOutcome, TallyResult, Vote, ALLOW_JOINING_GOV, FINALIZED_PROPOSALS, GOV_VOTE_QUERIES,
MEMBERS, MEMBERS_STATE_SYNC, PENDING_REPLIES, PROPOSAL_STATE_SYNC,
TEMP_REMOTE_GOV_MODULE_ADDRS, VOTE, VOTE_RESULTS,
};
use crate::{
contract::{AdapterResult, InterchainGov},
Expand Down Expand Up @@ -63,6 +63,10 @@ pub fn execute_handler(
temporary_register_remote_gov_module_addrs(deps, adapter, modules)
}
InterchainGovExecuteMsg::Execute { prop_id } => execute_prop(deps, env, adapter, prop_id),
InterchainGovExecuteMsg::SetAcceptGovInvite { members } => {
ALLOW_JOINING_GOV.save(deps.storage, &members)?;
Ok(adapter.response("set_accept_gov_invite"))
}
_ => todo!(),
}
}
Expand Down Expand Up @@ -146,16 +150,44 @@ fn execute_prop(
let external_members = MEMBERS_STATE_SYNC.external_members(deps.storage, &env)?;

// Execute the prop
match prop.action {
ProposalAction::UpdateMembers { members } => {
let new_member_msgs = match prop.action {
ProposalAction::UpdateMembers { mut members } => {
// If new members exclude self, update members to only be self
if !members.members.contains(&ChainName::new(&env)) {
MEMBERS_STATE_SYNC.save_members(deps.storage, &Members::new(&env))?;
}
let old_members = MEMBERS_STATE_SYNC.load_members(deps.storage)?;
MEMBERS_STATE_SYNC.save_members(deps.storage, &members)?;
let ibc_client = app.ibc_client(deps.as_ref());
let exec_msg = InterchainGovIbcMsg::JoinGov {
members: members.clone(),
};

// if new members, send them an inclusion msg
members.members.retain(|c| !old_members.members.contains(c));
let new_members = members;
// send inclusion messages to these chains

let mut msgs = vec![];
let target_module = this_module(&app)?;
for host in new_members.members.iter() {
let callback = CallbackInfo::new(
PROPOSE_CALLBACK_ID,
Some(to_json_binary(&InterchainGovIbcCallbackMsg::JoinGov {
proposed_to: host.clone(),
})?),
);
msgs.push(ibc_client.module_ibc_action(
host.to_string(),
target_module.clone(),
&exec_msg,
Some(callback.clone()),
)?);
}
msgs
}
ProposalAction::Signal => {}
}
ProposalAction::Signal => vec![],
};

// Send mgs to other members to report vote outcome

Expand Down Expand Up @@ -189,6 +221,7 @@ fn execute_prop(
return Ok(app
.response("propose_members")
.add_messages(msgs)
.add_messages(new_member_msgs)
.add_attribute("prop_id", prop_id));
}

Expand Down Expand Up @@ -245,9 +278,11 @@ fn request_vote_results(
host.to_string(),
WasmQuery::Smart {
contract_addr: module_addr,
msg: to_json_binary(&crate::msg::QueryMsg::Module(InterchainGovQueryMsg::Vote {
prop_id: prop_id.clone(),
}))?,
msg: to_json_binary(&crate::msg::QueryMsg::Module(
InterchainGovQueryMsg::Vote {
prop_id: prop_id.clone(),
},
))?,
},
CallbackInfo::new(REGISTER_VOTE_ID, None),
)?;
Expand Down Expand Up @@ -594,7 +629,7 @@ pub fn finalize(
Ok(msg)
})
.collect::<AbstractSdkResult<Vec<CosmosMsg>>>()?;

Ok(app
.response("finalize")
.add_attribute("prop_id", prop_id)
Expand Down
6 changes: 5 additions & 1 deletion contracts/interchain-gov/src/handlers/module_ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ pub fn module_ibc_handler(
let allowed_gov = ALLOW_JOINING_GOV.load(deps.storage)?;

// assert that the governance members are the whitelisted members
if !members.members.iter().all(|a| allowed_gov.members.contains(a)) {
if !members
.members
.iter()
.all(|a| allowed_gov.members.contains(a))
{
return Err(InterchainGovError::UnauthorizedIbcMessage {});
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/interchain-gov/src/ibc_callbacks/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn finalize_callback(
prop_hash: _prop_id,
} => {
PROPOSAL_STATE_SYNC.apply_ack(deps.storage, proposed_to)?;

if !PROPOSAL_STATE_SYNC.has_outstanding_acks(deps.storage)? {
// finalize my proposal
MEMBERS_STATE_SYNC.finalize_members(deps.storage, None)?;
Expand Down
7 changes: 5 additions & 2 deletions contracts/interchain-gov/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ pub mod members_sync_state {

self.members.save(storage, &members)?;

self.item_state_controller
.finalize_item_state(storage, MEMBERS_KEY.to_string(), set)?;
self.item_state_controller.finalize_item_state(
storage,
MEMBERS_KEY.to_string(),
set,
)?;
Ok(())
}

Expand Down
40 changes: 27 additions & 13 deletions contracts/interchain-gov/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use abstract_adapter::std::{adapter, adapter::AdapterRequestMsg, objects::namesp
use abstract_client::{AbstractClient, Account, Application, Environment, Publisher};
use abstract_cw_orch_polytone::Polytone;
// Use prelude to get all the necessary imports
use cw_orch_interchain::{ChannelCreator, MockBech32InterchainEnv, Starship};
use cw_orch_interchain::InterchainEnv;
use cw_orch_interchain::{ChannelCreator, MockBech32InterchainEnv, Starship};

use cw_orch::{anyhow, prelude::*};
// use cw_orch_interchain::{prelude::*};
Expand Down Expand Up @@ -236,11 +236,7 @@ impl<Env: CwEnv> TestEnv<Env> {
}
}

fn test_proposal(
title: impl Into<String>,
action: ProposalAction,
exp_height: u64,
) -> ProposalMsg {
fn test_proposal(title: impl Into<String>, action: ProposalAction, exp_height: u64) -> ProposalMsg {
ProposalMsg {
title: title.into(),
description: "This is a test proposal".to_string(),
Expand Down Expand Up @@ -536,15 +532,14 @@ mod members {
// ibc_connect_polytone_and_abstract(&interchain, B_CHAIN_ID, C_CHAIN_ID)?;
// ibc_connect_polytone_and_abstract(&interchain, C_CHAIN_ID, A_CHAIN_ID)?;
// ibc_connect_polytone_and_abstract(&interchain, C_CHAIN_ID, B_CHAIN_ID)?;

ibc_connect_abstract(&interchain, A_CHAIN_ID, B_CHAIN_ID)?;
ibc_connect_abstract(&interchain, A_CHAIN_ID, C_CHAIN_ID)?;
ibc_connect_abstract(&interchain, B_CHAIN_ID, A_CHAIN_ID)?;
ibc_connect_abstract(&interchain, B_CHAIN_ID, C_CHAIN_ID)?;
ibc_connect_abstract(&interchain, C_CHAIN_ID, A_CHAIN_ID)?;
ibc_connect_abstract(&interchain, C_CHAIN_ID, B_CHAIN_ID)?;


let a_gov = a_env.gov.clone();
let b_gov = b_env.gov.clone();
let c_gov = c_env.gov.clone();
Expand All @@ -567,7 +562,7 @@ mod members {
a_env.assert_prop_state(prop_id.clone(), None)?;
b_env.assert_prop_state(prop_id.clone(), None)?;

// A and B are friends now.
// A and B are friends now.
// Propose to add C

let (res, prop_id) = a_env.propose_proposal(
Expand All @@ -582,7 +577,7 @@ mod members {

a_env.assert_prop_state(prop_id.clone(), None)?;
b_env.assert_prop_state(prop_id.clone(), Some(DataState::Proposed))?;

let a = a_gov.finalize(prop_id.clone())?;
let res = interchain.wait_ibc(A_CHAIN_ID, a)?;
dbg!(&res.packets[0].outcome);
Expand All @@ -597,8 +592,16 @@ mod members {
let prop = b_gov.proposal(prop_id.clone())?;
assert_eq!(prop.prop_id, prop_id.clone());

a_gov.vote_proposal(interchain_gov::state::Governance::Manual { }, prop_id.clone(), Vote::Yes)?;
b_gov.vote_proposal(interchain_gov::state::Governance::Manual { }, prop_id.clone(), Vote::Yes)?;
a_gov.vote_proposal(
interchain_gov::state::Governance::Manual {},
prop_id.clone(),
Vote::Yes,
)?;
b_gov.vote_proposal(
interchain_gov::state::Governance::Manual {},
prop_id.clone(),
Vote::Yes,
)?;

// Wait the test blocks after voting
a_env.wait_blocks(TEST_PROP_LEN + 1)?;
Expand All @@ -609,10 +612,21 @@ mod members {
let res = interchain.wait_ibc(A_CHAIN_ID, res)?;
dbg!(&res.packets[0].outcome);

a_gov.execute_proposal(prop_id.clone())?;
// let c accept invite
c_gov.set_accept_gov_invite(
vec![a_env.chain_name(), b_env.chain_name(), c_env.chain_name()].into(),
)?;

let res = a_gov.execute_proposal(prop_id.clone())?;

let a_members = dbg!(a_gov.members()?);
assert_eq!(a_members.members.members.len(), 3);

let res = interchain.wait_ibc(A_CHAIN_ID, res)?;
dbg!(&res.packets[0].outcome);

let c_members = dbg!(c_gov.members()?);
assert_eq!(c_members.members.members.len(), 3);
Ok(())
}
}
Expand Down

0 comments on commit 09dac0b

Please sign in to comment.