Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cwd proposal delegate #562

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft

Cwd proposal delegate #562

wants to merge 25 commits into from

Conversation

baoskee
Copy link

@baoskee baoskee commented Nov 26, 2022

Need design review before I start writing tests! : )
Low prio, no hurries. Thought would be fun first contract to write for DaoDao.

@codecov-commenter
Copy link

codecov-commenter commented Nov 26, 2022

Codecov Report

Base: 95.07% // Head: 94.29% // Decreases project coverage by -0.77% ⚠️

Coverage data is based on head (b8e34e3) compared to base (0ef1a2a).
Patch coverage: 64.28% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #562      +/-   ##
==========================================
- Coverage   95.07%   94.29%   -0.78%     
==========================================
  Files          41       43       +2     
  Lines        3775     3873      +98     
==========================================
+ Hits         3589     3652      +63     
- Misses        186      221      +35     
Impacted Files Coverage Δ
...s/proposal/cwd-proposal-delegate/src/bin/schema.rs 0.00% <0.00%> (ø)
...cts/proposal/cwd-proposal-delegate/src/contract.rs 65.62% <65.62%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@0xekez 0xekez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫡 cool! super clean. let's land this :)

contracts/proposal/cwd-proposal-delegate/Cargo.toml Outdated Show resolved Hide resolved
contracts/proposal/cwd-proposal-delegate/Cargo.toml Outdated Show resolved Hide resolved
cosmwasm/rust-optimizer:0.12.6
"""

[dependencies]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these can be cosmwasm-x = { workspace = true }

REPLY_ID_EXECUTE_PROPOSAL_HOOK => {
let id = EXECUTE_CTX.load(deps.storage)?;
let Delegation {
policy_preserve_on_failure,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is sick

Comment on lines 90 to 92
if let SubMsgResult::Err(_) = msg.result {
// && with `let` expression above gives unstable Rust error
if policy_preserve_on_failure {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if let SubMsgResult::Err(_) = msg.result {
// && with `let` expression above gives unstable Rust error
if policy_preserve_on_failure {
if msg.result.is_err() && policy_preserve_on_failure {

Comment on lines +124 to +127
fn query_delegation(deps: Deps, delegation_id: u64) -> StdResult<DelegationResponse> {
let delegation = DELEGATIONS.load(deps.storage, delegation_id)?;
Ok(delegation)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably want to make this a paginated function so that FE can list all of the delegations pending. packages/cw-paginate should make that simple

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool!

Comment on lines 119 to 122
fn query_delegation_count(deps: Deps) -> StdResult<DelegationCountResponse> {
let count = DELEGATION_COUNT.load(deps.storage)?;
Ok(DelegationCountResponse { count })
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idea: why expose this at all? all we use it for is a nonce for delegations. let's make it a private state variable and only expose a function for incrementing it. gets us pretty close to correct-by-constructuon!

ex:

pub(crate) fn advance_current_id(store: &mut dyn Storage) -> StdResult<u64> {
    let CURRENT_ID: Item<u64> = Item::new("current_id");
    let id: u64 = CURRENT_ID.may_load(store)?.unwrap_or_default() + 1;
    CURRENT_ID.save(store, &id)?;
    Ok(id)
}

if info.sender != admin {
return Err(ContractError::Unauthorized {});
}
assert_not_expired(&delegation.expiration, &env.block)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice check

}
// If delegation is irrevocable, return Error
let Delegation {
policy_irrevocable, ..
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the reason for having this option? i feel things like this can give fake guarantees if the contract is instantiated with a DAO as it's admin. should we be instantiating this without an admin perhap?

Copy link
Author

@baoskee baoskee Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. The DAO could always remove the proposal module and all delegations would be revocable. However, that would eliminate all their existing delegations as well. There's 2 options to go with this:

  1. Rename the policy to policy_module_irrevocable and add a warning
  2. Remove the policy altogether

Comment on lines 11 to 12
// Add any other custom errors you like here.
// Look at https://docs.rs/thiserror/1.0.21/thiserror/ for details.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Add any other custom errors you like here.
// Look at https://docs.rs/thiserror/1.0.21/thiserror/ for details.

@baoskee
Copy link
Author

baoskee commented Dec 10, 2022

Will integrate changes and start on the tests! A bit slow on this, been grokking some Cosmos stuff. Need to learn Go, how dreadful

@github-actions
Copy link

github-actions bot commented Dec 28, 2022

Cosm-Orc Gas Usage

No gas diff larger than 0.5%

Raw Report for b8e34e3
Contract Op Name Gas Used Gas Wanted File
cw721_base Execute__mint_nft 140542 188097 ci/integration-tests/src/tests/dao_voting_cw721_staked_test.rs:87
cw721_base Execute__stake_nft 234591 329168 ci/integration-tests/src/tests/dao_voting_cw721_staked_test.rs:70
cw721_base Instantiate__instantiate_cw721_base 164837 224540 ci/integration-tests/src/tests/dao_voting_cw721_staked_test.rs:22
cw721_base Store__Store 3369402 5031314 ci/integration-tests/src/helpers/chain.rs:97
dao_voting_cw721_staked Execute__unstake_nfts 230319 322760 ci/integration-tests/src/tests/dao_voting_cw721_staked_test.rs:105
dao_voting_cw721_staked Instantiate__instantiate_dao_voting_cw721_staked 179007 245795 ci/integration-tests/src/tests/dao_voting_cw721_staked_test.rs:49
dao_voting_cw721_staked Store__Store 3327711 4968777 ci/integration-tests/src/helpers/chain.rs:97
dao_voting_cw721_staked Execute__claim_nfts 5710869 8543393 ci/integration-tests/src/tests/dao_voting_cw721_staked_test.rs:120
cw20_base Store__Store 4180849 6248484 ci/integration-tests/src/helpers/chain.rs:97
cw20_base Execute__exc_stake_stake_tokens 234799 321348 ci/integration-tests/src/tests/cw20_stake_test.rs:76
cw20_stake Store__Store 3693999 5518209 ci/integration-tests/src/helpers/chain.rs:97
cw4_group Store__Store 2783570 4152566 ci/integration-tests/src/helpers/chain.rs:97
cw_admin_factory Store__Store 2003453 2982390 ci/integration-tests/src/helpers/chain.rs:97
cw_token_swap Store__Store 2365061 3524802 ci/integration-tests/src/helpers/chain.rs:97
cwd_proposal_delegate Store__Store 3270082 4882334 ci/integration-tests/src/helpers/chain.rs:97
dao_core Store__Store 5663239 8472069 ci/integration-tests/src/helpers/chain.rs:97
dao_core Execute__exc_items_rm 192820 266513 ci/integration-tests/src/tests/cw_core_test.rs:171
dao_core Execute__exc_items_set 194488 269013 ci/integration-tests/src/tests/cw_core_test.rs:136
dao_core Instantiate__exc_items_create_dao 1273968 1888152 ci/integration-tests/src/helpers/helper.rs:98
dao_core Instantiate__inst_dao_no_admin 1272918 1886577 ci/integration-tests/src/helpers/helper.rs:98
dao_core Instantiate__exc_stake_create_dao 1272894 1886541 ci/integration-tests/src/helpers/helper.rs:98
dao_core Instantiate__inst_admin_create_dao 1273968 1888152 ci/integration-tests/src/helpers/helper.rs:98
dao_core Execute__exc_admin_msgs_pause_dao 194613 269202 ci/integration-tests/src/tests/cw_core_test.rs:76
dao_core Instantiate__exc_admin_msgs_create_dao 1272918 1886577 ci/integration-tests/src/helpers/helper.rs:98
dao_core Instantiate__exc_admin_msgs_create_dao_with_admin 1273968 1888152 ci/integration-tests/src/helpers/helper.rs:98
dao_pre_propose_approval_single Store__Store 4903233 7332060 ci/integration-tests/src/helpers/chain.rs:97
dao_pre_propose_approver Store__Store 3659835 5466963 ci/integration-tests/src/helpers/chain.rs:97
dao_pre_propose_multiple Store__Store 4233291 6327147 ci/integration-tests/src/helpers/chain.rs:97
dao_pre_propose_single Store__Store 4154927 6209601 ci/integration-tests/src/helpers/chain.rs:97
dao_proposal_multiple Store__Store 6360832 9518459 ci/integration-tests/src/helpers/chain.rs:97
dao_proposal_single Store__Store 6656556 9962045 ci/integration-tests/src/helpers/chain.rs:97
dao_voting_cw20_staked Store__Store 3623617 5412636 ci/integration-tests/src/helpers/chain.rs:97
dao_voting_cw4 Store__Store 2670847 3983481 ci/integration-tests/src/helpers/chain.rs:97
dao_voting_native_staked Store__Store 2960295 4417649 ci/integration-tests/src/helpers/chain.rs:97
stake_cw20_external_rewards Store__Store 3097923 4624095 ci/integration-tests/src/helpers/chain.rs:97
stake_cw20_reward_distributor Store__Store 2431608 3624623 ci/integration-tests/src/helpers/chain.rs:97
multiple_contracts Execute__batch_cw721_stake_max_claims 4196439 6254235 ci/integration-tests/src/tests/dao_voting_cw721_staked_test.rs:251

@baoskee
Copy link
Author

baoskee commented Dec 28, 2022

Ok, last commits were:

  • fixed Cargo.toml
  • made changes for PR
  • added pagination query (can remove the single delegation query if that's cleaner)
  • made delegation count private
  • renamed policy_revocable to policy_module_irrevocable. I think that having revocability at the module level is convenient even though it can expose user error (because DAO can remove the module to "revoke" the delegation). For example, the Constitution can technically disband the Supreme Court to prevent a particular judge-made decision to not be implemented (say McCulloch v. Maryland), but that would require high costs plus the dismantling of every other Court decision
  • added a bunch of unit tests covering the risks I laid out in README.md

Lmk.

@0xekez 0xekez self-requested a review January 2, 2023 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants