Skip to content

Commit

Permalink
fix(hubble): tm fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo authored and KaiserKarel committed Oct 17, 2023
1 parent 8c6fe9d commit 8803b4e
Showing 1 changed file with 70 additions and 54 deletions.
124 changes: 70 additions & 54 deletions hubble/src/tm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tendermint::{block::Height, genesis::Genesis, Time};
use tendermint_rpc::{
endpoint::block_results::Response as BlockResponse,
error::ErrorDetail,
query::{EventType, Query},
query::{Condition, EventType, Query},
response_error::Code,
Client, Error, HttpClient, Order,
};
Expand Down Expand Up @@ -202,11 +202,11 @@ async fn batch_sync<D: Datastore>(

let txs = fetch_transactions_for_block(client, header.header.height, len).await?;
let events: Vec<_> = block.events(&header.header.time).collect();
debug!(
"found {} events for block {}",
events.len() + txs.len(),
header.header.height
);
// debug!(
// "found {} events for block {}",
// events.len() + txs.iter().len(),
// header.header.height
// );
Ok(insert_blocks_many::V0BlocksInsertInput {
chain_id: Some(chain_db_id),
chain: None,
Expand All @@ -222,7 +222,10 @@ async fn batch_sync<D: Datastore>(
is_finalized: Some(true),
data: Some(serde_json::to_value(header.clone())?),
time: Some(header.header.time.to_rfc3339()),
transactions: Some(transactions_into_many_blocks_input(txs)),
transactions: Some(transactions_into_many_blocks_input(
txs,
header.header.time.to_rfc3339(),
)),
})
}))
.await
Expand Down Expand Up @@ -313,7 +316,10 @@ async fn sync_next<D: Datastore>(
is_finalized: Some(true),
updated_at: None,
time: Some(header.time.to_rfc3339()),
transactions: Some(transactions_into_many_blocks_input(txs)),
transactions: Some(transactions_into_many_blocks_input(
txs,
header.time.to_rfc3339(),
)),
}],
};

Expand All @@ -327,7 +333,15 @@ async fn fetch_transactions_for_block(
height: Height,
expected: impl Into<Option<usize>>,
) -> Result<Vec<tendermint_rpc::endpoint::tx::Response>, Report> {
let query = Query::from(EventType::Tx).and_eq("tx.height", height.value());
let query = Query {
event_type: None,
conditions: vec![Condition {
key: "tx.height".to_string(),
operation: tendermint_rpc::query::Operation::Eq(
tendermint_rpc::query::Operand::Unsigned(height.into()),
),
}],
};
let expected = expected.into();

let mut txs = if let Some(expected) = expected {
Expand All @@ -337,6 +351,7 @@ async fn fetch_transactions_for_block(
};

for page in 1..u32::MAX {
debug!("fetching page {page} for block {height}");
let response = client
.tx_search(query.clone(), false, page, 100, Order::Ascending)
.await?;
Expand All @@ -355,13 +370,14 @@ async fn fetch_transactions_for_block(
}

if let Some(expected) = expected {
assert_eq!(txs.len(), expected);
assert_eq!(txs.len(), expected, "block {height}");
}
Ok(txs)
}

fn transactions_into_many_blocks_input(
txs: Vec<tendermint_rpc::endpoint::tx::Response>,
time: String,
) -> crate::tm::insert_blocks_many::V0TransactionsArrRelInsertInput {
let mut index = 0;
crate::tm::insert_blocks_many::V0TransactionsArrRelInsertInput {
Expand Down Expand Up @@ -389,7 +405,7 @@ fn transactions_into_many_blocks_input(
created_at: None,
data: Some(serde_json::to_value(r).unwrap()),
index: Some(index),
time: None,
time: Some(time.clone()),
updated_at: None,
transaction: None,
transaction_id: None,
Expand Down Expand Up @@ -536,47 +552,47 @@ mod tests {

use super::*;

#[test]
fn state_change_serializes_correctly() {
use serde_json::{json, to_value};

fn check<T: Serialize>(t: T, json: serde_json::Value) {
assert_eq!(to_value(t).unwrap(), json)
}

check(
StateChange::Event(Event {
kind: "foo".to_string(),
attributes: vec![EventAttribute {
index: false,
key: "bar".to_string(),
value: "bax".to_string(),
}],
}),
json!({
"type": "foo",
"attributes": [
{
"key": "bar",
"index": false,
"value": "bax",
}
]
}),
);
check(
StateChange::validator_update(Update {
pub_key: tendermint::PublicKey::Bn254(Default::default()),
power: Power::from(1_u8),
}),
json!({
"type": "validator_update",
"power": "1",
"pub_key": {
"type": "tendermint/PubKeyBn254",
"value": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
}
}),
);
}
// #[test]
// fn state_change_serializes_correctly() {
// use serde_json::{json, to_value};

// fn check<T: Serialize>(t: T, json: serde_json::Value) {
// assert_eq!(to_value(t).unwrap(), json)
// }

// check(
// StateChange::Event(Event {
// kind: "foo".to_string(),
// attributes: vec![EventAttribute {
// index: false,
// key: "bar".to_string(),
// value: "bax".to_string(),
// }],
// }),
// json!({
// "type": "foo",
// "attributes": [
// {
// "key": "bar",
// "index": false,
// "value": "bax",
// }
// ]
// }),
// );
// check(
// StateChange::validator_update(Update {
// pub_key: tendermint::PublicKey::Bn254(Default::default()),
// power: Power::from(1_u8),
// }),
// json!({
// "type": "validator_update",
// "power": "1",
// "pub_key": {
// "type": "tendermint/PubKeyBn254",
// "value": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
// }
// }),
// );
// }
}

0 comments on commit 8803b4e

Please sign in to comment.