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

parsing ack tx #218

Closed
wants to merge 5 commits into from
Closed
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
21 changes: 7 additions & 14 deletions chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,9 @@ async fn crawling_fn(
.await
.into_rpc_error()?;

let block = Block::from(
tm_block_response,
&block_results,
checksums,
epoch,
block_height,
);
tracing::debug!(
block = block_height,
txs = block.transactions.len(),
"Deserialized {} txs...",
block.transactions.len()
);
let block =
Block::from(tm_block_response, &block_results, checksums, block_height);
tracing::info!("Deserialized {} txs...", block.transactions.len());

let native_token = namada_service::get_native_token(&client)
.await
Expand Down Expand Up @@ -207,7 +197,10 @@ async fn crawling_fn(
};

let validators_state_change = block.update_validators_state();
tracing::debug!("Updating {} validators state", validators_state_change.len());
tracing::debug!(
"Updating {} validators state",
validators_state_change.len()
);

let addresses = block.bond_addresses();
let bonds = query_bonds(&client, addresses).await.into_rpc_error()?;
Expand Down
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;
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 BIGINT NOT NULL,
status IBC_STATUS NOT NULL
);
3 changes: 3 additions & 0 deletions orm/migrations/2024-12-10-104502_transaction_types/down.sql
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
-- This file should undo anything in `up.sql`
DROP TABLE ibc_ack;

DROP TYPE IBC_STATUS;
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: i64,
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 i64,
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
38 changes: 38 additions & 0 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ 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,
Expand Down Expand Up @@ -196,6 +204,18 @@ diesel::table! {
}
}

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

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

diesel::table! {
ibc_token (address) {
#[max_length = 45]
Expand Down Expand Up @@ -230,6 +250,21 @@ 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,
}
}

diesel::table! {
revealed_pk (id) {
id -> Int4,
Expand Down Expand Up @@ -301,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 @@ -312,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
1 change: 0 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ format_macro_matchers = true
format_strings = true
group_imports = "StdExternalCrate"
hard_tabs = false
show_parse_errors = true
ignore = []
imports_granularity = "Module"
imports_indent = "Block"
Expand Down
3 changes: 0 additions & 3 deletions shared/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ pub struct Block {
pub hash: Id,
pub header: BlockHeader,
pub transactions: Vec<(WrapperTransaction, Vec<InnerTransaction>)>,
pub epoch: Epoch,
}

impl Block {
pub fn from(
block_response: TendermintBlockResponse,
block_results: &BlockResult,
checksums: Checksums,
epoch: Epoch,
block_height: BlockHeight,
) -> Self {
let transactions = block_response
Expand Down Expand Up @@ -150,7 +148,6 @@ impl Block {
app_hash: Id::from(block_response.block.header.app_hash),
},
transactions,
epoch,
}
}

Expand Down
81 changes: 72 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,59 @@ 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 timeout_timestamp: u64,
pub sequence: String,
}

#[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_or(&"0".to_string())
.parse::<u64>()
.unwrap_or_default()
.to_owned();

tracing::error!("{}", timeout_timestamp);

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 +200,7 @@ impl TxAttributes {
})
.unwrap(),
info: attributes.get("info").unwrap().to_owned(),
}),
})),
}
}
}
Expand All @@ -177,7 +224,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 +245,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 +268,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 +292,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