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

feat: frontend integration #35

Merged
merged 8 commits into from
Jun 4, 2024
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ chrono = { version = "0.4.30", features = ["serde"] }
async-trait = "0.1.73"
anyhow = "1.0.75"
namada_core = { git = "https://github.com/anoma/namada", tag = "v0.38.1" }
namada_sdk = { git = "https://github.com/anoma/namada", tag = "v0.38.1" }
namada_sdk = { git = "https://github.com/anoma/namada", tag = "v0.38.1", default-features = false, features = ["tendermint-rpc", "std", "async-send", "download-params", "rand"] }
namada_tx = { git = "https://github.com/anoma/namada", tag = "v0.38.1" }
namada_governance = { git = "https://github.com/anoma/namada", tag = "v0.38.1" }
namada_ibc = { git = "https://github.com/anoma/namada", tag = "v0.38.1" }
namada_token = { git = "https://github.com/anoma/namada", tag = "v0.38.1" }
namada_parameters = { git = "https://github.com/anoma/namada", tag = "v0.38.1" }
tendermint = "0.36.0"
tendermint-config = "0.36.0"
tendermint-rpc = { version = "0.36.0", features = ["http-client"] }
Expand Down
52 changes: 45 additions & 7 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 All @@ -172,6 +177,12 @@ async fn crawling_fn(
.into_rpc_error()?;
tracing::info!("Updating unbonds for {} addresses", unbonds.len());

let revealed_pks = block.revealed_pks();
tracing::info!(
"Updating revealed pks for {} addresses",
revealed_pks.len()
);

let metadata_change = block.validator_metadata();

let reward_claimers = block.pos_rewards();
Expand All @@ -187,7 +198,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 All @@ -196,11 +210,6 @@ async fn crawling_fn(
repository::pos::insert_bonds(transaction_conn, bonds)?;
repository::pos::insert_unbonds(transaction_conn, unbonds)?;

repository::crawler::insert_crawler_state(
transaction_conn,
crawler_state,
)?;

repository::pos::delete_claimed_rewards(
transaction_conn,
reward_claimers,
Expand All @@ -211,6 +220,16 @@ async fn crawling_fn(
metadata_change,
)?;

repository::revealed_pk::insert_revealed_pks(
transaction_conn,
revealed_pks,
)?;

repository::crawler::insert_crawler_state(
transaction_conn,
crawler_state,
)?;

anyhow::Ok(())
})
})
Expand Down Expand Up @@ -261,6 +280,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 +305,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
1 change: 1 addition & 0 deletions chain/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod balance;
pub mod crawler;
pub mod gov;
pub mod pos;
pub mod revealed_pk;
1 change: 1 addition & 0 deletions chain/src/repository/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fn update_validator_metadata(
for metadata in metadata_change {
let metadata_change_db = ValidatorUpdateMetadataDb {
commission: metadata.commission,
name: metadata.name,
email: metadata.email,
website: metadata.website,
description: metadata.description,
Expand Down
22 changes: 22 additions & 0 deletions chain/src/repository/revealed_pk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use anyhow::Context;
use diesel::{PgConnection, RunQueryDsl};
use orm::{revealed_pk::RevealedPkInsertDb, schema::revealed_pk};
use shared::{id::Id, public_key::PublicKey};

pub fn insert_revealed_pks(
transaction_conn: &mut PgConnection,
revealed_pks: Vec<(PublicKey, Id)>,
) -> anyhow::Result<()> {
diesel::insert_into(revealed_pk::table)
.values::<&Vec<RevealedPkInsertDb>>(
&revealed_pks
.into_iter()
.map(|(pk, address)| RevealedPkInsertDb::from(pk, address))
.collect::<Vec<_>>(),
)
.on_conflict_do_nothing()
.execute(transaction_conn)
.context("Failed to update balances in db")?;

anyhow::Ok(())
}
63 changes: 62 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,66 @@ 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()?;

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().flatten().cloned().collect())
}

fn to_block_height(block_height: u32) -> NamadaSdkBlockHeight {
NamadaSdkBlockHeight::from(block_height as u64)
}
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ services:
environment:
DATABASE_URL: postgres://postgres:password@postgres:5435/namada-indexer
CACHE_URL: redis://dragonfly:6379
TENDERMINT_URL: http://localhost:27657
healthcheck:
test: curl --fail http://localhost:5000/health || exit 1
interval: 15s
timeout: 10s
retries: 3
start_period: 10s


Empty file removed orm/migrations/.keep
Empty file.
1 change: 1 addition & 0 deletions orm/migrations/2024-04-30-081808_init_validators/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CREATE TABLE validators (
voting_power INT NOT NULL,
max_commission VARCHAR NOT NULL,
commission VARCHAR NOT NULL,
name VARCHAR,
email VARCHAR,
website VARCHAR,
description VARCHAR,
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);
3 changes: 3 additions & 0 deletions orm/migrations/2024-06-03-084149_init_revealed_pk/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`

DROP TABLE IF EXISTS revealed_pk;
9 changes: 9 additions & 0 deletions orm/migrations/2024-06-03-084149_init_revealed_pk/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Your SQL goes here

CREATE TABLE revealed_pk (
id SERIAL PRIMARY KEY,
address VARCHAR NOT NULL,
pk VARCHAR NOT NULL
);

ALTER TABLE revealed_pk ADD UNIQUE (address, pk);
17 changes: 15 additions & 2 deletions orm/src/bond.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use diesel::{Insertable, Queryable, Selectable};
use diesel::{
associations::Associations, Identifiable, Insertable, Queryable, Selectable,
};
use shared::bond::Bond;

use crate::validators::ValidatorDb;

use crate::schema::bonds;

#[derive(Insertable, Clone, Queryable, Selectable)]
Expand All @@ -12,7 +16,16 @@ pub struct BondInsertDb {
pub raw_amount: String,
}

pub type BondDb = BondInsertDb;
#[derive(Identifiable, Clone, Queryable, Selectable, Associations)]
#[diesel(table_name = bonds)]
#[diesel(check_for_backend(diesel::pg::Pg))]
#[diesel(belongs_to(ValidatorDb, foreign_key = validator_id))]
pub struct BondDb {
pub id: i32,
pub address: String,
pub validator_id: i32,
pub raw_amount: String,
}

impl BondInsertDb {
pub fn from_bond(bond: Bond, validator_id: i32) -> Self {
Expand Down
Loading
Loading