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

Write governance proposal results to storage #1979

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
46 changes: 31 additions & 15 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,23 +1045,39 @@ pub async fn query_proposal_result<
return;
};

let is_author_steward = query_pgf_stewards(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(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(client, &proposal_result_key).await {
Ok(result) => result,
Err(_) => {
// If failure, run the tally
let is_author_steward = query_pgf_stewards(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(
client,
proposal.voting_end_epoch,
)
.await;

let votes = compute_proposal_votes(
client,
proposal_id,
proposal.voting_end_epoch,
)
.await;
let votes = compute_proposal_votes(
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!(IO, "Proposal Id: {} ", proposal_id);
display_line!(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
Loading