Skip to content

Commit

Permalink
fix: unwrap_amp_msg empty case
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Dec 27, 2024
1 parent 4e52beb commit 219c9ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 11 additions & 1 deletion e2e-tests/tests/macro_tests/exec_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,21 @@ fn test_unwrap_amp_msg() {
let msg = ExecuteMsg::AMPReceive(amp_pkt);

let (ctx, msg, _) = (|| -> Result<(ExecuteContext, ExecuteMsg, Response), ContractError> {
let (ctx, msg, resp) = unwrap_amp_msg!(deps.as_mut(), info, env, msg);
let (ctx, msg, resp) = unwrap_amp_msg!(deps.as_mut(), info.clone(), env.clone(), msg);
Ok((ctx, msg, resp))
})().unwrap();

assert_eq!(ctx.info.sender, "sender");
assert_eq!(ctx.raw_info.sender, MOCK_KERNEL_CONTRACT);
assert_eq!(msg, sent_msg);

let empty_amp_pkt = AMPPkt::new("sender".to_string(), "sender".to_string(), vec![]);
let msg = ExecuteMsg::AMPReceive(empty_amp_pkt);

let err = (|| -> Result<(), ContractError> {
let _ = unwrap_amp_msg!(deps.as_mut(), info, env, msg);
Ok(())
})().unwrap_err();

assert_eq!(err, ContractError::InvalidPacket { error: Some("AMP Packet received with no messages".to_string()) });
}
11 changes: 10 additions & 1 deletion packages/std/src/ado_contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,16 @@ macro_rules! unwrap_amp_msg {
ctx.deps
.api
.debug(&format!("Set new sender: {}", ctx.info.sender));
msg = ::cosmwasm_std::from_json(&pkt.messages.pop().unwrap().message)?;
let maybe_amp_msg = pkt.messages.pop();

::cosmwasm_std::ensure!(
maybe_amp_msg.is_some(),
ContractError::InvalidPacket {
error: Some("AMP Packet received with no messages".to_string()),
}
);
let amp_msg = maybe_amp_msg.unwrap();
msg = ::cosmwasm_std::from_json(&amp_msg.message)?;
::cosmwasm_std::ensure!(
!msg.must_be_direct(),
ContractError::InvalidPacket {
Expand Down

0 comments on commit 219c9ba

Please sign in to comment.