Skip to content

Commit

Permalink
[fix] validate proposal in acknowledge/reject proposal (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj authored Jun 16, 2023
1 parent 42a4cf2 commit 6df84b0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pallets/dkg-proposals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ mod tests;
pub mod types;
pub mod utils;
use dkg_runtime_primitives::{
traits::OnAuthoritySetChangeHandler, ProposalHandlerTrait, ProposalNonce, ResourceId,
TypedChainId,
handlers::decode_proposals::decode_proposal_identifier, traits::OnAuthoritySetChangeHandler,
ProposalHandlerTrait, ProposalNonce, ResourceId, TypedChainId,
};
use frame_support::{
pallet_prelude::{ensure, DispatchResultWithPostInfo},
Expand Down Expand Up @@ -381,6 +381,8 @@ pub mod pallet {
ProposerCountIsZero,
/// Input is out of bounds
OutOfBounds,
/// Invalid proposal
InvalidProposal,
}

#[pallet::genesis_config]
Expand Down Expand Up @@ -541,6 +543,12 @@ pub mod pallet {
ensure!(Self::is_proposer(&who), Error::<T>::MustBeProposer);
ensure!(Self::chain_whitelisted(src_chain_id), Error::<T>::ChainNotWhitelisted);
ensure!(Self::resource_exists(r_id), Error::<T>::ResourceDoesNotExist);
match decode_proposal_identifier(&prop) {
Ok(ident) => {
ensure!(ident.typed_chain_id == src_chain_id, Error::<T>::InvalidProposal);
},
Err(_) => return Err(Error::<T>::InvalidProposal.into()),
};

Self::vote_for(who, nonce, src_chain_id, &prop)
}
Expand All @@ -563,6 +571,12 @@ pub mod pallet {
ensure!(Self::is_proposer(&who), Error::<T>::MustBeProposer);
ensure!(Self::chain_whitelisted(src_chain_id), Error::<T>::ChainNotWhitelisted);
ensure!(Self::resource_exists(r_id), Error::<T>::ResourceDoesNotExist);
match decode_proposal_identifier(&prop) {
Ok(ident) => {
ensure!(ident.typed_chain_id == src_chain_id, Error::<T>::InvalidProposal);
},
Err(_) => return Err(Error::<T>::InvalidProposal.into()),
};

Self::vote_against(who, nonce, src_chain_id, &prop)
}
Expand Down
38 changes: 38 additions & 0 deletions pallets/dkg-proposals/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,44 @@ pub fn make_proposal<const N: usize>(
}
}

#[test]
fn test_invalid_proposal_is_rejected() {
let typed_chain_id = TypedChainId::Evm(1);
let r_id = derive_resource_id(typed_chain_id.underlying_chain_id(), 0x0100, b"remark");

new_test_ext_initialized(typed_chain_id, r_id, b"System.remark".to_vec()).execute_with(|| {
let prop_id = ProposalNonce::from(1u32);
let proposal = Proposal::Unsigned {
kind: ProposalKind::AnchorUpdate,
data: vec![].try_into().unwrap(),
};

// The proposal is invalid and should be rejected
assert_noop!(
DKGProposals::acknowledge_proposal(
RuntimeOrigin::signed(mock_pub_key(PROPOSER_A)),
prop_id,
typed_chain_id,
r_id,
proposal.clone(),
),
Error::<Test>::InvalidProposal
);

// The proposal is invalid and should be rejected
assert_noop!(
DKGProposals::reject_proposal(
RuntimeOrigin::signed(mock_pub_key(PROPOSER_A)),
prop_id,
typed_chain_id,
r_id,
proposal.clone(),
),
Error::<Test>::InvalidProposal
);
});
}

#[test]
fn create_successful_proposal() {
let typed_chain_id = TypedChainId::Evm(1);
Expand Down

0 comments on commit 6df84b0

Please sign in to comment.