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

wip: parse more block events #160

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions orm/migrations/2024-07-04-103941_crawler_state/down.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- This file should undo anything in `up.sql`

DROP TABLE crawler_state;

DROP TYPE CRAWLER_NAME;
4 changes: 4 additions & 0 deletions orm/migrations/2024-12-01-170248_ibc_ack/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- This file should undo anything in `up.sql`
DROP TABLE ibc_ack;

DROP TYPE IBC_STATUS;
9 changes: 9 additions & 0 deletions orm/migrations/2024-12-01-170248_ibc_ack/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Your SQL goes here
CREATE TYPE IBC_STATUS AS ENUM ('fail', 'success', 'timeout', 'unknown');

CREATE TABLE ibc_ack (
id VARCHAR PRIMARY KEY,
tx_hash VARCHAR NOT NULL,
timeout INT NOT NULL,
status IBC_STATUS NOT NULL
);
56 changes: 56 additions & 0 deletions orm/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use diesel::prelude::Queryable;
use diesel::{AsChangeset, Insertable, Selectable};
use serde::{Deserialize, Serialize};
use shared::transaction::{IbcAckStatus, IbcSequence};

use crate::schema::ibc_ack;

#[derive(Debug, Clone, Serialize, Deserialize, diesel_derive_enum::DbEnum)]
#[ExistingTypePath = "crate::schema::sql_types::IbcStatus"]
pub enum IbcAckStatusDb {
Unknown,
Timeout,
Fail,
Success,
}

impl From<IbcAckStatus> for IbcAckStatusDb {
fn from(value: IbcAckStatus) -> Self {
match value {
IbcAckStatus::Success => Self::Success,
IbcAckStatus::Fail => Self::Fail,
IbcAckStatus::Timeout => Self::Timeout,
IbcAckStatus::Unknown => Self::Unknown,
}
}
}

#[derive(Serialize, Queryable, Insertable, Selectable, Clone, Debug)]
#[diesel(table_name = ibc_ack)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct IbcAckDb {
pub id: String,
pub tx_hash: String,
pub timeout: i32,
pub status: IbcAckStatusDb,
}

pub type IbcAckInsertDb = IbcAckDb;

impl From<IbcSequence> for IbcAckInsertDb {
fn from(value: IbcSequence) -> Self {
Self {
id: value.id(),
tx_hash: value.tx_id.to_string(),
timeout: value.timeout as i32,
status: IbcAckStatusDb::Unknown,
}
}
}

#[derive(Serialize, AsChangeset, Clone)]
#[diesel(table_name = ibc_ack)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct IbcSequencekStatusUpdateDb {
pub status: IbcAckStatusDb,
}
1 change: 1 addition & 0 deletions orm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod governance_proposal;
pub mod governance_votes;
pub mod group_by_macros;
pub mod helpers;
pub mod ibc;
pub mod migrations;
pub mod parameters;
pub mod pos_rewards;
Expand Down
55 changes: 55 additions & 0 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ pub mod sql_types {
#[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 = "ibc_status"))]
pub struct IbcStatus;

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

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

#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
Expand Down Expand Up @@ -179,6 +203,18 @@ diesel::table! {
}
}

diesel::table! {
use diesel::sql_types::*;
use super::sql_types::IbcStatus;

ibc_ack (id) {
id -> Varchar,
tx_hash -> Varchar,
timeout -> Int4,
status -> IbcStatus,
}
}

diesel::table! {
ibc_token (address) {
#[max_length = 45]
Expand Down Expand Up @@ -213,6 +249,22 @@ diesel::table! {
}
}

diesel::table! {
use diesel::sql_types::*;
use super::sql_types::PaymentRecurrence;
use super::sql_types::PaymentKind;

public_good_funding (id) {
id -> Int4,
proposal_id -> Int4,
payment_recurrence -> PaymentRecurrence,
payment_kind -> PaymentKind,
receipient -> Varchar,
amount -> Numeric,
last_paid_epoch -> Int4,
}
}

diesel::table! {
revealed_pk (id) {
id -> Int4,
Expand Down Expand Up @@ -284,6 +336,7 @@ diesel::joinable!(governance_votes -> governance_proposals (proposal_id));
diesel::joinable!(ibc_token -> token (address));
diesel::joinable!(inner_transactions -> wrapper_transactions (wrapper_id));
diesel::joinable!(pos_rewards -> validators (validator_id));
diesel::joinable!(public_good_funding -> governance_proposals (proposal_id));
diesel::joinable!(unbonds -> validators (validator_id));

diesel::allow_tables_to_appear_in_same_query!(
Expand All @@ -295,9 +348,11 @@ diesel::allow_tables_to_appear_in_same_query!(
gas_price,
governance_proposals,
governance_votes,
ibc_ack,
ibc_token,
inner_transactions,
pos_rewards,
public_good_funding,
revealed_pk,
token,
unbonds,
Expand Down
78 changes: 69 additions & 9 deletions shared/src/block_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ use crate::transaction::TransactionExitStatus;
#[derive(Debug, Clone)]
pub enum EventKind {
Applied,
SendPacket,
Unknown,
}

impl From<&String> for EventKind {
fn from(value: &String) -> Self {
match value.as_str() {
"tx/applied" => Self::Applied,
"send_packet" => Self::SendPacket,
_ => Self::Unknown,
}
}
Expand All @@ -32,7 +34,7 @@ pub struct BlockResult {
#[derive(Debug, Clone)]
pub struct Event {
pub kind: EventKind,
pub attributes: Option<TxAttributes>,
pub attributes: Option<TxAttributesType>,
}

#[derive(Debug, Clone, Default, Copy)]
Expand Down Expand Up @@ -107,7 +109,7 @@ impl BatchResults {
}

#[derive(Debug, Clone, Default)]
pub struct TxAttributes {
pub struct TxApplied {
pub code: TxEventStatusCode,
pub gas: u64,
pub hash: Id,
Expand All @@ -116,14 +118,56 @@ pub struct TxAttributes {
pub info: String,
}

impl TxAttributes {
#[derive(Debug, Clone, Default)]
pub struct SendPacket {
pub source_port: String,
pub dest_port: String,
pub source_channel: String,
pub dest_channel: String,
pub sequence: String,
pub timeout_timestamp: u64,
}

#[derive(Debug, Clone)]
pub enum TxAttributesType {
TxApplied(TxApplied),
SendPacket(SendPacket),
}

impl TxAttributesType {
pub fn deserialize(
event_kind: &EventKind,
attributes: &BTreeMap<String, String>,
) -> Option<Self> {
match event_kind {
EventKind::Unknown => None,
EventKind::Applied => Some(Self {
EventKind::SendPacket => {
let source_port =
attributes.get("packet_src_port").unwrap().to_owned();
let dest_port =
attributes.get("packet_dst_port").unwrap().to_owned();
let source_channel =
attributes.get("packet_src_channel").unwrap().to_owned();
let dest_channel =
attributes.get("packet_dst_channel").unwrap().to_owned();
let sequence =
attributes.get("packet_sequence").unwrap().to_owned();
let timeout_timestamp = attributes
.get("packet_timeout_timestamp")
.unwrap()
.parse::<u64>()
.unwrap()
.to_owned();
Some(Self::SendPacket(SendPacket {
source_port,
dest_port,
source_channel,
dest_channel,
timeout_timestamp,
sequence,
}))
}
EventKind::Applied => Some(Self::TxApplied(TxApplied {
code: attributes
.get("code")
.map(|code| TxEventStatusCode::from(code.as_str()))
Expand Down Expand Up @@ -153,7 +197,7 @@ impl TxAttributes {
})
.unwrap(),
info: attributes.get("info").unwrap().to_owned(),
}),
})),
}
}
}
Expand All @@ -177,7 +221,7 @@ impl From<TendermintBlockResultResponse> for BlockResult {
},
);
let attributes =
TxAttributes::deserialize(&kind, &raw_attributes);
TxAttributesType::deserialize(&kind, &raw_attributes);
Event { kind, attributes }
})
.collect::<Vec<Event>>();
Expand All @@ -198,7 +242,7 @@ impl From<TendermintBlockResultResponse> for BlockResult {
},
);
let attributes =
TxAttributes::deserialize(&kind, &raw_attributes);
TxAttributesType::deserialize(&kind, &raw_attributes);
Event { kind, attributes }
})
.collect::<Vec<Event>>();
Expand All @@ -221,7 +265,15 @@ impl BlockResult {
let exit_status = self
.end_events
.iter()
.filter_map(|event| event.attributes.clone())
.filter_map(|event| {
if let Some(TxAttributesType::TxApplied(data)) =
&event.attributes
{
Some(data.clone())
} else {
None
}
})
.find(|attributes| attributes.hash.eq(tx_hash))
.map(|attributes| attributes.clone().code)
.map(TransactionExitStatus::from);
Expand All @@ -237,7 +289,15 @@ impl BlockResult {
let exit_status = self
.end_events
.iter()
.filter_map(|event| event.attributes.clone())
.filter_map(|event| {
if let Some(TxAttributesType::TxApplied(data)) =
&event.attributes
{
Some(data.clone())
} else {
None
}
})
.find(|attributes| attributes.hash.eq(wrapper_hash))
.map(|attributes| attributes.batch.is_successful(inner_hash))
.map(|successful| match successful {
Expand Down
Loading
Loading