Skip to content

Commit

Permalink
Merge branch 'grarco/proposal-result-in-storage' (#1979)
Browse files Browse the repository at this point in the history
* origin/grarco/proposal-result-in-storage:
  Changelog #1979
  Client first looks for governance proposal result in storage
  Writes the result of a governance proposal in storage
  • Loading branch information
tzemanovic committed Oct 24, 2023
2 parents 9eb3ec3 + 79c85cb commit a8a7487
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Persist the results of governance proposals in storage to allow recovering old
results. ([\#1979](https://github.com/anoma/namada/pull/1979))
49 changes: 31 additions & 18 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,26 +1117,39 @@ pub async fn query_proposal_result<'a>(
return;
};

let is_author_steward = query_pgf_stewards(context.client())
.await
.iter()
.any(|steward| steward.address.eq(&proposal.author));
let tally_type = proposal.get_tally_type(is_author_steward);
let total_voting_power = get_total_staked_tokens(
context.client(),
proposal.voting_end_epoch,
)
.await;
let proposal_result_key =
governance_storage::get_proposal_result_key(proposal_id);
let proposal_result =
// Try to directly query the result in storage first
match query_storage_value(context.client(), &proposal_result_key).await {
Ok(result) => result,
Err(_) => {
// If failure, run the tally
let is_author_steward = query_pgf_stewards(context.client())
.await
.iter()
.any(|steward| steward.address.eq(&proposal.author));
let tally_type = proposal.get_tally_type(is_author_steward);
let total_voting_power = get_total_staked_tokens(
context.client(),
proposal.voting_end_epoch,
)
.await;

let votes = compute_proposal_votes(
context.client(),
proposal_id,
proposal.voting_end_epoch,
)
.await;
let votes = compute_proposal_votes(
context.client(),
proposal_id,
proposal.voting_end_epoch,
)
.await;

let proposal_result =
compute_proposal_result(votes, total_voting_power, tally_type);
compute_proposal_result(
votes,
total_voting_power,
tally_type,
)
}
};

display_line!(context.io(), "Proposal Id: {} ", proposal_id);
display_line!(context.io(), "{:4}{}", "", proposal_result);
Expand Down
4 changes: 4 additions & 0 deletions apps/src/lib/node/ledger/shell/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ where
)?;
let proposal_result =
compute_proposal_result(votes, total_voting_power, tally_type);
let proposal_result_key = gov_storage::get_proposal_result_key(id);
shell
.wl_storage
.write(&proposal_result_key, proposal_result)?;

let transfer_address = match proposal_result.result {
TallyResult::Passed => {
Expand Down
10 changes: 10 additions & 0 deletions core/src/ledger/governance/storage/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct Keys {
min_grace_epoch: &'static str,
counter: &'static str,
pending: &'static str,
result: &'static str,
}

/// Check if key is inside governance address space
Expand Down Expand Up @@ -459,6 +460,15 @@ pub fn get_proposal_execution_key(id: u64) -> Key {
.expect("Cannot obtain a storage key")
}

/// Get the proposal result key
pub fn get_proposal_result_key(id: u64) -> Key {
proposal_prefix()
.push(&id.to_string())
.expect("Cannot obtain a storage key")
.push(&Keys::VALUES.result.to_owned())
.expect("Cannot obtain a storage key")
}

/// Get proposal id from key
pub fn get_proposal_id(key: &Key) -> Option<u64> {
match key.get_at(2) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/ledger/governance/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl TallyType {
}

/// The result of a proposal
#[derive(Copy, Clone, BorshSerialize, BorshDeserialize)]
pub enum TallyResult {
/// Proposal was accepted with the associated value
Passed,
Expand Down Expand Up @@ -126,6 +127,7 @@ impl TallyResult {
}

/// The result with votes of a proposal
#[derive(Clone, Copy, BorshDeserialize, BorshSerialize)]
pub struct ProposalResult {
/// The result of a proposal
pub result: TallyResult,
Expand Down

0 comments on commit a8a7487

Please sign in to comment.