Skip to content

Commit

Permalink
added another test where the submsg issues a failing bank message
Browse files Browse the repository at this point in the history
  • Loading branch information
eshelB committed Sep 20, 2023
1 parent d19bb15 commit 169795a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use core::time;
use std::{thread, vec};

use cosmwasm_std::{
attr, coins, entry_point, to_binary, BankMsg, Binary, CosmosMsg, Deps, DepsMut, Empty, Env,
Event, MessageInfo, QueryRequest, Reply, ReplyOn, Response, StdError, StdResult, Storage,
SubMsg, SubMsgResponse, SubMsgResult, WasmMsg, WasmQuery, from_binary, CanonicalAddr,
};
use cosmwasm_std::{attr, coins, entry_point, to_binary, BankMsg, Binary, CosmosMsg, Deps, DepsMut, Empty, Env, Event, MessageInfo, QueryRequest, Reply, ReplyOn, Response, StdError, StdResult, Storage, SubMsg, SubMsgResponse, SubMsgResult, WasmMsg, WasmQuery, from_binary, CanonicalAddr, Coin, Uint128};
use cosmwasm_storage::PrefixedStorage;
use secp256k1::Secp256k1;
use serde::Serialize;

use crate::msg::{
ExecuteMsg, ExternalMessages, IBCLifecycleComplete, InstantiateMsg, QueryMsg, QueryRes, SudoMsg,
Expand Down Expand Up @@ -653,6 +650,13 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> S

Ok(response)
},
ExecuteMsg::IncrementAndSendSubmessageWithBankFail { reply_on } => {
increment_simple(deps)?;
let mut response = send_failing_submsg_with_bank_fail(env, reply_on)?;
response.data = Some((count as u32).to_be_bytes().into());

Ok(response)
},
ExecuteMsg::InitV10 {
counter,
code_id,
Expand Down Expand Up @@ -1241,6 +1245,13 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> S

return res;
}
ExecuteMsg::IncrementAndBankMsgSend { to, amount } => {
increment_simple(deps)?;
Ok(Response::new().add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: to,
amount,
})))
}
ExecuteMsg::BankMsgSend { to, amount } => {
Ok(Response::new().add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: to,
Expand Down Expand Up @@ -1756,6 +1767,38 @@ pub fn send_multiple_sub_messages_with_reply_with_error(
Ok(resp)
}

pub fn send_failing_submsg_with_bank_fail(
env: Env,
reply_on: ReplyOn,
) -> StdResult<Response> {
let failing_bank_msg = ExecuteMsg::IncrementAndBankMsgSend {
amount: vec![ Coin::new(100, "non-existent")],
to: "non-existent".to_string()
};

let bank_binary_msg = Binary::from(
serde_json_wasm::to_string(&failing_bank_msg)
.unwrap()
.as_bytes()
.to_vec()
);

let mut resp = Response::default();
resp.messages.push(SubMsg {
id: 9200,
msg: CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: env.contract.address.clone().into_string(),
code_hash: env.contract.code_hash.clone(),
msg: bank_binary_msg,
funds: vec![],
}),
gas_limit: Some(10000000_u64),
reply_on,
});

Ok(resp)
}

pub fn send_failing_submsg(
env: Env,
reply_on: ReplyOn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ pub enum ExecuteMsg {
IncrementAndSendFailingSubmessage {
reply_on: ReplyOn,
},
IncrementAndSendSubmessageWithBankFail {
reply_on: ReplyOn,
},
InitV10 {
code_id: u64,
code_hash: String,
Expand Down Expand Up @@ -413,6 +416,10 @@ pub enum ExecuteMsg {
privkey: Binary,
iterations: u32,
},
IncrementAndBankMsgSend {
amount: Vec<Coin>,
to: String,
},
BankMsgSend {
amount: Vec<Coin>,
to: String,
Expand Down
23 changes: 23 additions & 0 deletions x/compute/internal/keeper/secret_contracts_submsgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/binary"
"encoding/json"
"fmt"
"math"
"testing"

Expand Down Expand Up @@ -77,6 +78,28 @@ func TestV1StatePersistsAfterSubmessageFailsNoReply(t *testing.T) {
require.Equal(t, uint32(11), resp.Get.Count)
}

func TestV1StatePersistsAfterSubmessageThatGeneratesBankMsgFails(t *testing.T) {
ctx, keeper, codeID, _, walletA, privKeyA, _, _ := setupTest(t, TestContractPaths[v1Contract], sdk.NewCoins())

fmt.Println("ESHELDEBUG before init") //todo remove
_, _, contractAddress, _, _ := initHelper(t, keeper, ctx, codeID, walletA, nil, privKeyA, `{"counter":{"counter":10, "expires":100}}`, true, true, defaultGasForTests)
fmt.Println("ESHELDEBUG before exec") //todo remove
_, _, _, _, _, err := execHelper(t, keeper, ctx, contractAddress, walletA, privKeyA, `{"increment_and_send_submessage_with_bank_fail":{"reply_on":"never"}}`, false, true, math.MaxUint64, 0)

fmt.Println("ESHELDEBUG before error check") //todo remove
require.NotEmpty(t, err)
fmt.Println("ESHELDEBUG after error check") //todo remove

queryRes, qErr := queryHelper(t, keeper, ctx, contractAddress, `{"get":{}}`, true, true, math.MaxUint64)
fmt.Println("ESHELDEBUG after query") //todo remove
require.Empty(t, qErr)

var resp v1QueryResponse
e := json.Unmarshal([]byte(queryRes), &resp)
require.NoError(t, e)
require.Equal(t, uint32(11), resp.Get.Count)
}

func TestSendEncryptedAttributesFromInitWithoutSubmessageWithoutReply(t *testing.T) {
for _, testContract := range testContracts {
t.Run(testContract.CosmWasmVersion, func(t *testing.T) {
Expand Down

0 comments on commit 169795a

Please sign in to comment.