Skip to content

Commit

Permalink
backport of #78
Browse files Browse the repository at this point in the history
  • Loading branch information
pr0n00gler committed Oct 11, 2023
1 parent 1cd5d88 commit d07d7c5
Show file tree
Hide file tree
Showing 30 changed files with 550 additions and 41 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ fn is_subdao_legit(deps: &DepsMut, subdao_core: &Addr) -> Result<bool, PrePropos
);

match subdao {
Ok(subdao) => Ok(subdao.addr == *subdao_core),
Ok(subdao) => {
// sanity check to make sure that query returned correct subdao
let correct_subdao = subdao.addr == *subdao_core;
Ok(correct_subdao)
}
Err(_) => Ok(false),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2801,6 +2801,30 @@
},
"additionalProperties": false
},
{
"description": "Returns errors of the failed proposal. Expected in the form of \"codespace=? code=?\". Returns `Option<String>`",
"type": "object",
"required": [
"proposal_execution_error"
],
"properties": {
"proposal_execution_error": {
"type": "object",
"required": [
"proposal_id"
],
"properties": {
"proposal_id": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down Expand Up @@ -7568,6 +7592,14 @@
}
}
},
"proposal_execution_error": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Nullable_String",
"type": [
"string",
"null"
]
},
"proposal_hooks": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "HooksResponse",
Expand Down
24 changes: 24 additions & 0 deletions contracts/dao/proposal/cwd-proposal-multiple/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,30 @@
},
"additionalProperties": false
},
{
"description": "Returns errors of the failed proposal. Expected in the form of \"codespace=? code=?\". Returns `Option<String>`",
"type": "object",
"required": [
"proposal_execution_error"
],
"properties": {
"proposal_execution_error": {
"type": "object",
"required": [
"proposal_id"
],
"properties": {
"proposal_id": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Nullable_String",
"type": [
"string",
"null"
]
}
24 changes: 21 additions & 3 deletions contracts/dao/proposal/cwd-proposal-multiple/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Reply, Response, StdResult,
Storage, SubMsg, WasmMsg,
to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Reply, Response, StdError,
StdResult, Storage, SubMsg, WasmMsg,
};

use cw2::set_contract_version;
Expand All @@ -26,6 +26,7 @@ use cwd_voting::{
voting::{get_total_power, get_voting_power, validate_voting_period},
};

use crate::state::PROPOSAL_EXECUTION_ERRORS;
use crate::{msg::MigrateMsg, state::CREATION_POLICY};
use crate::{
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
Expand Down Expand Up @@ -730,6 +731,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::ProposalHooks {} => to_binary(&PROPOSAL_HOOKS.query_hooks(deps)?),
QueryMsg::VoteHooks {} => to_binary(&VOTE_HOOKS.query_hooks(deps)?),
QueryMsg::Dao {} => query_dao(deps),
QueryMsg::ProposalExecutionError { proposal_id } => {
query_proposal_execution_error(deps, proposal_id)
}
}
}

Expand Down Expand Up @@ -841,6 +845,11 @@ pub fn query_info(deps: Deps) -> StdResult<Binary> {
to_binary(&cwd_interface::voting::InfoResponse { info })
}

pub fn query_proposal_execution_error(deps: Deps, proposal_id: u64) -> StdResult<Binary> {
let error = PROPOSAL_EXECUTION_ERRORS.may_load(deps.storage, proposal_id)?;
to_binary(&error)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
let repl = TaggedReplyId::new(msg.id)?;
Expand All @@ -853,7 +862,16 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractE
}
None => Err(ContractError::NoSuchProposal { id: proposal_id }),
})?;
Ok(Response::new().add_attribute("proposal execution failed", proposal_id.to_string()))
// Error is reduced before cosmwasm reply and is expected in form of "codespace=? code=?"
let error = msg.result.into_result().err().ok_or_else(|| {
// should never happen since we reply only on failure
ContractError::Std(StdError::generic_err(
"must be an error in the failed result",
))
})?;
PROPOSAL_EXECUTION_ERRORS.save(deps.storage, proposal_id, &error)?;

Ok(Response::new().add_attribute("proposal_execution_failed", proposal_id.to_string()))
}
TaggedReplyId::FailedProposalHook(idx) => {
let addr = PROPOSAL_HOOKS.remove_hook_by_index(deps.storage, idx)?;
Expand Down
5 changes: 5 additions & 0 deletions contracts/dao/proposal/cwd-proposal-multiple/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ pub enum QueryMsg {
/// module.
#[returns(HooksResponse)]
VoteHooks {},
/// Returns errors of the failed proposal.
/// Expected in the form of "codespace=? code=?".
/// Returns `Option<String>`
#[returns(Option<String>)]
ProposalExecutionError { proposal_id: u64 },
}

#[cw_serde]
Expand Down
2 changes: 2 additions & 0 deletions contracts/dao/proposal/cwd-proposal-multiple/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ pub const VOTE_HOOKS: Hooks = Hooks::new("vote_hooks");
/// The address of the pre-propose module associated with this
/// proposal module (if any).
pub const CREATION_POLICY: Item<ProposalCreationPolicy> = Item::new("creation_policy");
/// Execution errors for proposals that do not close on failure (Config.close_proposal_on_execution_failure set to false)
pub const PROPOSAL_EXECUTION_ERRORS: Map<u64, String> = Map::new("proposal_execution_errors");
64 changes: 62 additions & 2 deletions contracts/dao/proposal/cwd-proposal-multiple/src/testing/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::{
coins, to_binary, Addr, Api, Coin, CosmosMsg, Decimal, Empty, Storage, Timestamp, Uint128,
WasmMsg,
coins, from_binary, to_binary, Addr, Api, Attribute, Coin, CosmosMsg, Decimal, Empty, Reply,
Storage, SubMsgResult, Timestamp, Uint128, WasmMsg,
};
use cw20::Cw20Coin;
use cw_denom::{CheckedDenom, UncheckedDenom};
Expand Down Expand Up @@ -44,8 +45,10 @@ use crate::{
};
use cwd_pre_propose_multiple as cppm;

use crate::contract::query_proposal_execution_error;
use crate::testing::execute::mint_natives;
use cwd_testing::ShouldExecute;
use cwd_voting::reply::mask_proposal_execution_proposal_id;

pub const CREATOR_ADDR: &str = "creator";

Expand Down Expand Up @@ -2128,3 +2131,60 @@ fn test_close_failed_proposal() {
// not reverted
assert_eq!(updated.proposal.status, Status::Passed);
}

#[test]
fn test_reply_proposal_mock() {
use crate::contract::reply;
use crate::state::PROPOSALS;

let mut deps = mock_dependencies();
let env = mock_env();

let m_proposal_id = mask_proposal_execution_proposal_id(1);
PROPOSALS
.save(
deps.as_mut().storage,
1,
&MultipleChoiceProposal {
title: "A simple text proposal".to_string(),
description: "This is a simple text proposal".to_string(),
proposer: Addr::unchecked(CREATOR_ADDR),
start_height: env.block.height,
expiration: Duration::Height(6).after(&env.block),
min_voting_period: None,
allow_revoting: false,
total_power: Uint128::new(100_000_000),
status: Status::Open,
votes: MultipleChoiceVotes {
vote_weights: vec![Uint128::zero(); 3],
},
choices: vec![],
voting_strategy: VotingStrategy::SingleChoice {
quorum: PercentageThreshold::Majority {},
},
},
)
.unwrap();

// PROPOSALS
let reply_msg = Reply {
id: m_proposal_id,
result: SubMsgResult::Err("error".to_string()),
};
let res = reply(deps.as_mut(), env, reply_msg).unwrap();
assert_eq!(
res.attributes[0],
Attribute {
key: "proposal_execution_failed".to_string(),
value: 1.to_string()
}
);

let prop = PROPOSALS.load(deps.as_mut().storage, 1).unwrap();
assert_eq!(prop.status, Status::ExecutionFailed);

// reply writes the failed proposal error
let query_res = query_proposal_execution_error(deps.as_ref(), 1).unwrap();
let error: Option<String> = from_binary(&query_res).unwrap();
assert_eq!(error, Some("error".to_string()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2863,6 +2863,30 @@
},
"additionalProperties": false
},
{
"description": "Returns errors of the failed proposal. Expected in the form of \"codespace=? code=?\". Returns `Option<String>`",
"type": "object",
"required": [
"proposal_execution_error"
],
"properties": {
"proposal_execution_error": {
"type": "object",
"required": [
"proposal_id"
],
"properties": {
"proposal_id": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down Expand Up @@ -7502,6 +7526,14 @@
}
}
},
"proposal_execution_error": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Nullable_String",
"type": [
"string",
"null"
]
},
"proposal_hooks": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "HooksResponse",
Expand Down
24 changes: 24 additions & 0 deletions contracts/dao/proposal/cwd-proposal-single/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,30 @@
},
"additionalProperties": false
},
{
"description": "Returns errors of the failed proposal. Expected in the form of \"codespace=? code=?\". Returns `Option<String>`",
"type": "object",
"required": [
"proposal_execution_error"
],
"properties": {
"proposal_execution_error": {
"type": "object",
"required": [
"proposal_id"
],
"properties": {
"proposal_id": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Nullable_String",
"type": [
"string",
"null"
]
}
Loading

0 comments on commit d07d7c5

Please sign in to comment.