Skip to content

Commit

Permalink
feat: tally type
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed May 29, 2024
1 parent 80ae168 commit 4eb2d81
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 41 deletions.
31 changes: 29 additions & 2 deletions chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ async fn crawling_fn(
let proposals = block.governance_proposal(next_governance_proposal_id);
tracing::info!("Creating {} governance proposals...", proposals.len());

let proposals_with_tally =
namada_service::query_tallies(&client, proposals)
.await
.into_rpc_error()?;

let proposals_votes = block.governance_votes();
tracing::info!("Creating {} governance votes...", proposals_votes.len());

Expand Down Expand Up @@ -187,7 +192,10 @@ async fn crawling_fn(
balances,
)?;

repository::gov::insert_proposals(transaction_conn, proposals)?;
repository::gov::insert_proposals(
transaction_conn,
proposals_with_tally,
)?;
repository::gov::insert_votes(
transaction_conn,
proposals_votes,
Expand Down Expand Up @@ -261,6 +269,17 @@ async fn initial_query(

tracing::info!("Querying proposals...");
let proposals = query_all_proposals(client).await.into_rpc_error()?;
let proposals_with_tally =
namada_service::query_tallies(&client, proposals.clone())
.await
.into_rpc_error()?;

let proposals_votes = namada_service::query_all_votes(
&client,
proposals.iter().map(|p| p.id).collect(),
)
.await
.into_rpc_error()?;

let crawler_state = CrawlerState::new(block_height, epoch);

Expand All @@ -275,7 +294,15 @@ async fn initial_query(
balances,
)?;

repository::gov::insert_proposals(transaction_conn, proposals)?;
repository::gov::insert_proposals(
transaction_conn,
proposals_with_tally,
)?;

repository::gov::insert_votes(
transaction_conn,
proposals_votes,
)?;

repository::pos::insert_bonds(transaction_conn, bonds)?;
repository::pos::insert_unbonds(transaction_conn, unbonds)?;
Expand Down
8 changes: 4 additions & 4 deletions chain/src/repository/gov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ use anyhow::Context;
use diesel::{PgConnection, RunQueryDsl};
use orm::governance_proposal::GovernanceProposalInsertDb;
use orm::governance_votes::GovernanceProposalVoteInsertDb;
use shared::proposal::GovernanceProposal;
use shared::proposal::{GovernanceProposal, TallyType};
use shared::vote::GovernanceVote;

pub fn insert_proposals(
transaction_conn: &mut PgConnection,
proposals: Vec<GovernanceProposal>,
proposals: Vec<(GovernanceProposal, TallyType)>,
) -> anyhow::Result<()> {
diesel::insert_into(orm::schema::governance_proposals::table)
.values::<&Vec<GovernanceProposalInsertDb>>(
&proposals
.into_iter()
.map(|proposal| {
.map(|(proposal, tally_type)| {
GovernanceProposalInsertDb::from_governance_proposal(
proposal,
proposal, tally_type,
)
})
.collect::<Vec<_>>(),
Expand Down
64 changes: 63 additions & 1 deletion chain/src/services/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use shared::balance::{Amount, Balance, Balances};
use shared::block::{BlockHeight, Epoch};
use shared::bond::{Bond, BondAddresses, Bonds};
use shared::id::Id;
use shared::proposal::GovernanceProposal;
use shared::proposal::{GovernanceProposal, TallyType};
use shared::unbond::{Unbond, UnbondAddresses, Unbonds};
use shared::utils::BalanceChange;
use shared::vote::{GovernanceVote, ProposalVoteKind};
use subtle_encoding::hex;
use tendermint_rpc::HttpClient;

Expand Down Expand Up @@ -416,6 +417,67 @@ pub async fn query_tx_code_hash(
}
}

pub async fn is_steward(
client: &HttpClient,
address: &Id,
) -> anyhow::Result<bool> {
let address = NamadaSdkAddress::from_str(&address.to_string())
.context("Failed to parse address")?;

let is_steward = rpc::is_steward(client, &address).await;

Ok(is_steward)
}

pub async fn query_tallies(
client: &HttpClient,
proposals: Vec<GovernanceProposal>,
) -> anyhow::Result<Vec<(GovernanceProposal, TallyType)>> {
let proposals = futures::stream::iter(proposals)
.filter_map(|proposal| async move {
let is_steward =
is_steward(&client, &proposal.author).await.ok()?;

Check failure on line 439 in chain/src/services/namada.rs

View workflow job for this annotation

GitHub Actions / Clippy

this expression creates a reference which is immediately dereferenced by the compiler

let tally_type = TallyType::from(&proposal.r#type, is_steward);

Some((proposal, tally_type))
})
.map(futures::future::ready)
.buffer_unordered(20)
.collect::<Vec<_>>()
.await;

anyhow::Ok(proposals)
}

pub async fn query_all_votes(
client: &HttpClient,
proposals_ids: Vec<u64>,
) -> anyhow::Result<Vec<GovernanceVote>> {
let votes = futures::stream::iter(proposals_ids)
.filter_map(|proposal_id| async move {
let votes =
rpc::query_proposal_votes(client, proposal_id).await.ok()?;

let votes = votes
.into_iter()
.map(|vote| GovernanceVote {
proposal_id,
vote: ProposalVoteKind::from(vote.data),
address: Id::from(vote.delegator),
})
.collect::<Vec<_>>();

Some(votes)
})
.map(futures::future::ready)
.buffer_unordered(20)
.collect::<Vec<_>>()
.await;

anyhow::Ok(votes.iter().cloned().flatten().collect())

Check failure on line 478 in chain/src/services/namada.rs

View workflow job for this annotation

GitHub Actions / Clippy

unnecessarily eager cloning of iterator items
}

fn to_block_height(block_height: u32) -> NamadaSdkBlockHeight {
NamadaSdkBlockHeight::from(block_height as u64)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
DROP TABLE governance_proposals;

DROP TYPE GOVERNANCE_KIND;
DROP TYPE GOVERNANCE_RESULT;
DROP TYPE GOVERNANCE_RESULT;
DROP TYPE GOVERNANCE_TALLY_TYPE;
4 changes: 3 additions & 1 deletion orm/migrations/2024-05-08-130928_governance_proposals/up.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
CREATE TYPE GOVERNANCE_KIND AS ENUM ('pgf_steward', 'pgf_funding', 'default', 'default_with_wasm');
CREATE TYPE GOVERNANCE_RESULT AS ENUM ('passed', 'rejected', 'pending', 'unknown', 'voting_period');
CREATE TYPE GOVERNANCE_TALLY_TYPE AS ENUM ('two_thirds', 'one_half_over_one_third', 'less_one_half_over_one_third_nay');

CREATE TABLE governance_proposals (
id INT PRIMARY KEY,
content VARCHAR NOT NULL,
data VARCHAR,
kind GOVERNANCE_KIND NOT NULL,
tally_type GOVERNANCE_TALLY_TYPE NOT NULL,
author VARCHAR NOT NULL,
start_epoch INT NOT NULL,
end_epoch INT NOT NULL,
Expand All @@ -17,4 +19,4 @@ CREATE TABLE governance_proposals (
);

CREATE INDEX index_governance_proposals_kind ON governance_proposals USING HASH (kind);
CREATE INDEX index_governance_proposals_result ON governance_proposals USING HASH (result);
CREATE INDEX index_governance_proposals_result ON governance_proposals USING HASH (result);
30 changes: 28 additions & 2 deletions orm/src/governance_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use diesel::{Insertable, Queryable, Selectable};
use serde::{Deserialize, Serialize};
use shared::proposal::{
GovernanceProposal, GovernanceProposalKind, GovernanceProposalResult,
GovernanceProposalStatus,
GovernanceProposalStatus, TallyType,
};

use crate::schema::governance_proposals;
Expand All @@ -28,6 +28,26 @@ impl From<GovernanceProposalKind> for GovernanceProposalKindDb {
}
}

#[derive(Debug, Clone, Serialize, Deserialize, diesel_derive_enum::DbEnum)]
#[ExistingTypePath = "crate::schema::sql_types::GovernanceTallyType"]
pub enum GovernanceProposalTallyTypeDb {
TwoThirds,
OneHalfOverOneThird,
LessOneHalfOverOneThirdNay,
}

impl From<TallyType> for GovernanceProposalTallyTypeDb {
fn from(value: TallyType) -> Self {
match value {
TallyType::TwoThirds => Self::TwoThirds,
TallyType::OneHalfOverOneThird => Self::OneHalfOverOneThird,
TallyType::LessOneHalfOverOneThirdNay => {
Self::LessOneHalfOverOneThirdNay
}
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, diesel_derive_enum::DbEnum)]
#[ExistingTypePath = "crate::schema::sql_types::GovernanceResult"]
pub enum GovernanceProposalResultDb {
Expand Down Expand Up @@ -57,6 +77,7 @@ pub struct GovernanceProposalDb {
pub content: String,
pub data: Option<String>,
pub kind: GovernanceProposalKindDb,
pub tally_type: GovernanceProposalTallyTypeDb,
pub author: String,
pub start_epoch: i32,
pub end_epoch: i32,
Expand All @@ -75,19 +96,24 @@ pub struct GovernanceProposalInsertDb {
pub content: String,
pub data: Option<String>,
pub kind: GovernanceProposalKindDb,
pub tally_type: GovernanceProposalTallyTypeDb,
pub author: String,
pub start_epoch: i32,
pub end_epoch: i32,
pub activation_epoch: i32,
}

impl GovernanceProposalInsertDb {
pub fn from_governance_proposal(proposal: GovernanceProposal) -> Self {
pub fn from_governance_proposal(
proposal: GovernanceProposal,
tally_type: TallyType,
) -> Self {
Self {
id: proposal.id as i32,
content: proposal.content,
data: proposal.data,
kind: proposal.r#type.into(),
tally_type: tally_type.into(),
author: proposal.author.to_string(),
start_epoch: proposal.voting_start_epoch as i32,
end_epoch: proposal.voting_end_epoch as i32,
Expand Down
6 changes: 6 additions & 0 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub mod sql_types {
#[diesel(postgres_type(name = "governance_result"))]
pub struct GovernanceResult;

#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "governance_tally_type"))]
pub struct GovernanceTallyType;

#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "vote_kind"))]
pub struct VoteKind;
Expand Down Expand Up @@ -50,13 +54,15 @@ diesel::table! {
diesel::table! {
use diesel::sql_types::*;
use super::sql_types::GovernanceKind;
use super::sql_types::GovernanceTallyType;
use super::sql_types::GovernanceResult;

governance_proposals (id) {
id -> Int4,
content -> Varchar,
data -> Nullable<Varchar>,
kind -> GovernanceKind,
tally_type -> GovernanceTallyType,
author -> Varchar,
start_epoch -> Int4,
end_epoch -> Int4,
Expand Down
40 changes: 40 additions & 0 deletions shared/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,43 @@ impl Distribution<GovernanceProposalResult> for Standard {
}
}
}

pub enum TallyType {
TwoThirds,
OneHalfOverOneThird,
LessOneHalfOverOneThirdNay,
}

// TODO: copied from namada for time being
impl TallyType {
pub fn from(
proposal_type: &GovernanceProposalKind,
is_steward: bool,
) -> Self {
match (proposal_type, is_steward) {
(GovernanceProposalKind::Default, _) => TallyType::TwoThirds,
(GovernanceProposalKind::DefaultWithWasm, _) => {
TallyType::TwoThirds
}
(GovernanceProposalKind::PgfSteward, _) => {
TallyType::OneHalfOverOneThird
}
(GovernanceProposalKind::PgfFunding, true) => {
TallyType::LessOneHalfOverOneThirdNay
}
(GovernanceProposalKind::PgfFunding, false) => {
TallyType::OneHalfOverOneThird
}
}
}
}

impl Distribution<TallyType> for Standard {
fn sample<R: rand::prelude::Rng + ?Sized>(&self, rng: &mut R) -> TallyType {
match rng.gen_range(0..=2) {
0 => TallyType::TwoThirds,
1 => TallyType::OneHalfOverOneThird,
_ => TallyType::LessOneHalfOverOneThirdNay,
}
}
}
22 changes: 12 additions & 10 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ paths:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/VotingPower'
$ref: '#/components/schemas/VotingPower'
/api/v1/gov/proposal:
get:
summary: Get a list of governance proposals
Expand Down Expand Up @@ -364,15 +362,18 @@ components:
type: string
Proposal:
type: object
required: [id, content, type, author, startEpoch, endEpoch, activationEpoch, startTime, endTime, currentTime, status, yayVotes, nayVotes, abstainVotes]
required: [id, content, type, author, startEpoch, endEpoch, activationEpoch, startTime, endTime, currentTime, status, yayVotes, nayVotes, abstainVotes, tallyType]
properties:
id:
type: integer
content:
type: string
type:
type: string
enum: [default, default_with_wasm, pgf_steward, pgf_funding]
enum: [default, defaultWithWasm, pgfSteward, pgfFunding]
tallyType:
type: string
enum: [twoThirds, oneHalfOverOneThird, lessOneHalfOverOneThirdNay]
data:
type: string
author:
Expand All @@ -393,11 +394,11 @@ components:
type: string
enum: [pending, voting, passed, rejected]
yayVotes:
type: integer
type: string
nayVotes:
type: integer
type: string
abstainVotes:
type: integer
type: string
Vote:
type: object
required: [proposal_id, vote, voterAddress]
Expand Down Expand Up @@ -453,11 +454,12 @@ components:
format: float
minimum: 0
withdrawEpoch:
type: integer
type: integer
VotingPower:
type: object
required: [totalVotingPower]
properties:
votingPower:
totalVotingPower:
type: integer
Balance:
type: object
Expand Down
Loading

0 comments on commit 4eb2d81

Please sign in to comment.