Skip to content

Commit

Permalink
Merge pull request #2 from enviodev/tx-status-query
Browse files Browse the repository at this point in the history
feat: added tx-status to query selections and schema/format for receipts, inputs, outputs
  • Loading branch information
JossDuff authored May 29, 2024
2 parents edf3365 + c0f0c04 commit d68e704
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 55 deletions.
8 changes: 4 additions & 4 deletions hyperfuel-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyperfuel-client"
version = "1.0.0"
version = "2.0.0"
edition = "2021"
description = "client library for Envio's HyperSync support of the Fuel Network"
license = "MIT"
Expand Down Expand Up @@ -35,9 +35,9 @@ alloy-dyn-abi = "0.5.0"
alloy-json-abi = "0.5.0"
xxhash-rust = { version = "0.8", features = ["xxh3"] }

hyperfuel-net-types = { package = "hyperfuel-net-types", path = "../hyperfuel-net-types", version = "1.0.0" }
hyperfuel-format = { package = "hyperfuel-format", path = "../hyperfuel-format", version = "1.0.0" }
hyperfuel-schema = { package = "hyperfuel-schema", path = "../hyperfuel-schema", version = "1.0.0" }
hyperfuel-net-types = { package = "hyperfuel-net-types", path = "../hyperfuel-net-types", version = "2.0.0" }
hyperfuel-format = { package = "hyperfuel-format", path = "../hyperfuel-format", version = "2.0.0" }
hyperfuel-schema = { package = "hyperfuel-schema", path = "../hyperfuel-schema", version = "2.0.0" }

[dependencies.reqwest]
version = "0.11"
Expand Down
45 changes: 41 additions & 4 deletions hyperfuel-client/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ fn receipt_selections_to_filter(
let rb = batch.column::<UInt64Array>("rb").ok();
let rc = batch.column::<UInt64Array>("rc").ok();
let rd = batch.column::<UInt64Array>("rd").ok();
let tx_status = batch.column::<UInt8Array>("tx_status").ok();

let chunk_len = batch.chunk.len();
let mut filter = unset_bool_array(chunk_len);
Expand All @@ -117,6 +118,7 @@ fn receipt_selections_to_filter(
rb,
rc,
rd,
tx_status,
selection,
chunk_len,
);
Expand All @@ -139,6 +141,7 @@ fn receipt_selection_to_filter(
rb: Option<&UInt64Array>,
rc: Option<&UInt64Array>,
rd: Option<&UInt64Array>,
tx_status: Option<&UInt8Array>,
selection: &ReceiptSelection,
chunk_len: usize,
) -> BooleanArray {
Expand Down Expand Up @@ -179,7 +182,8 @@ fn receipt_selection_to_filter(
}

if !selection.contract_id.is_empty() && contract_id.is_some() {
let set = selection.contract_id.iter().map(|t| t.as_slice()).collect();
let set: StdHashSet<&[u8], Xxh3Builder> =
selection.contract_id.iter().map(|t| t.as_slice()).collect();
filter = compute::boolean::and(&filter, &in_set_binary(contract_id.unwrap(), &set));
}

Expand All @@ -203,6 +207,13 @@ fn receipt_selection_to_filter(
filter = compute::boolean::and(&filter, &in_set_u64(rd.unwrap(), &set));
}

if !selection.tx_status.is_empty() && tx_status.is_some() {
filter = compute::boolean::and(
&filter,
&in_set_u8(tx_status.unwrap(), &selection.tx_status),
);
}

filter
}

Expand Down Expand Up @@ -257,13 +268,15 @@ fn input_selections_to_filter(
let sender = batch.column::<BinaryArray<i32>>("sender").ok();
let recipient = batch.column::<BinaryArray<i32>>("recipient").ok();
let input_type = batch.column::<UInt8Array>("input_type").ok();
let tx_status = batch.column::<UInt8Array>("tx_status").ok();

let chunk_len = batch.chunk.len();
let mut filter = unset_bool_array(chunk_len);

for selection in selections.iter() {
let selection = input_selection_to_filter(
owner, asset_id, contract, sender, recipient, input_type, selection, chunk_len,
owner, asset_id, contract, sender, recipient, input_type, tx_status, selection,
chunk_len,
);
filter = compute::boolean::or(&filter, &selection);
}
Expand All @@ -279,6 +292,7 @@ fn input_selection_to_filter(
sender: Option<&BinaryArray<i32>>,
recipient: Option<&BinaryArray<i32>>,
input_type: Option<&UInt8Array>,
tx_status: Option<&UInt8Array>,
selection: &InputSelection,
chunk_len: usize,
) -> BooleanArray {
Expand Down Expand Up @@ -314,6 +328,13 @@ fn input_selection_to_filter(
filter = compute::boolean::and(&filter, &in_set_u8(input_type.unwrap(), &set));
}

if !selection.tx_status.is_empty() && tx_status.is_some() {
filter = compute::boolean::and(
&filter,
&in_set_u8(tx_status.unwrap(), &selection.tx_status),
);
}

filter
}

Expand All @@ -325,13 +346,21 @@ fn output_selections_to_filter(
let asset_id = batch.column::<BinaryArray<i32>>("asset_id").ok();
let contract = batch.column::<BinaryArray<i32>>("contract").ok();
let output_type = batch.column::<UInt8Array>("output_type").ok();
let tx_status = batch.column::<UInt8Array>("tx_status").ok();

let chunk_len = batch.chunk.len();
let mut filter = unset_bool_array(chunk_len);

for selection in selections.iter() {
let selection =
output_selection_to_filter(to, asset_id, contract, output_type, selection, chunk_len);
let selection = output_selection_to_filter(
to,
asset_id,
contract,
output_type,
tx_status,
selection,
chunk_len,
);
filter = compute::boolean::or(&filter, &selection);
}

Expand All @@ -343,6 +372,7 @@ fn output_selection_to_filter(
asset_id: Option<&BinaryArray<i32>>,
contract: Option<&BinaryArray<i32>>,
output_type: Option<&UInt8Array>,
tx_status: Option<&UInt8Array>,
selection: &OutputSelection,
chunk_len: usize,
) -> BooleanArray {
Expand All @@ -368,5 +398,12 @@ fn output_selection_to_filter(
filter = compute::boolean::and(&filter, &in_set_u8(output_type.unwrap(), &set));
}

if !selection.tx_status.is_empty() && tx_status.is_some() {
filter = compute::boolean::and(
&filter,
&in_set_u8(tx_status.unwrap(), &selection.tx_status),
);
}

filter
}
16 changes: 16 additions & 0 deletions hyperfuel-client/src/from_arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@ impl FromArrow for Receipt {
}
}

if let Ok(col) = batch.column::<UInt8Array>("tx_status") {
for (target, val) in out.iter_mut().zip(col.values_iter()) {
target.tx_status = TransactionStatus::from_u8(*val).unwrap();
}
}

if let Ok(col) = batch.column::<UInt64Array>("block_height") {
for (target, &val) in out.iter_mut().zip(col.values_iter()) {
target.block_height = val.into();
Expand Down Expand Up @@ -611,6 +617,11 @@ impl FromArrow for Input {
target.tx_id = val.try_into().unwrap();
}
}
if let Ok(col) = batch.column::<UInt8Array>("tx_status") {
for (target, val) in out.iter_mut().zip(col.values_iter()) {
target.tx_status = TransactionStatus::from_u8(*val).unwrap();
}
}
if let Ok(col) = batch.column::<BinaryArray<i32>>("utxo_id") {
for (target, val) in out.iter_mut().zip(col.iter()) {
target.utxo_id = val.map(|v| v.try_into().unwrap());
Expand Down Expand Up @@ -720,6 +731,11 @@ impl FromArrow for Output {
target.tx_id = val.try_into().unwrap();
}
}
if let Ok(col) = batch.column::<UInt8Array>("tx_status") {
for (target, val) in out.iter_mut().zip(col.values_iter()) {
target.tx_status = TransactionStatus::from_u8(*val).unwrap();
}
}
if let Ok(col) = batch.column::<BinaryArray<i32>>("to") {
for (target, val) in out.iter_mut().zip(col.iter()) {
target.to = val.map(|v| v.try_into().unwrap());
Expand Down
55 changes: 13 additions & 42 deletions hyperfuel-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::{
collections::{BTreeSet, HashSet},
time::Duration,
};
use std::{collections::BTreeSet, time::Duration};

use anyhow::{anyhow, Context, Result};
use arrow2::{array::Array, chunk::Chunk};

use filter::filter_out_unselected_data;
use from_arrow::{receipts_from_arrow_data, typed_data_from_arrow_data, FromArrow};
use hyperfuel_format::{Hash, Receipt, Transaction, TransactionStatus};
use from_arrow::{receipts_from_arrow_data, typed_data_from_arrow_data};
use hyperfuel_format::{Hash, Receipt};
use hyperfuel_net_types::{
hyperfuel_net_types_capnp, ArchiveHeight, FieldSelection, Query, ReceiptSelection,
};
Expand Down Expand Up @@ -179,13 +176,10 @@ impl Client {
from_block: u64,
to_block: Option<u64>,
) -> Result<LogResponse> {
let mut transaction_field_selection = BTreeSet::new();
transaction_field_selection.insert("id".to_owned());
transaction_field_selection.insert("status".to_owned());

let mut receipt_field_selection = BTreeSet::new();
receipt_field_selection.insert("block_height".to_owned());
receipt_field_selection.insert("tx_id".to_owned());
receipt_field_selection.insert("tx_status".to_owned());
receipt_field_selection.insert("receipt_index".to_owned());
receipt_field_selection.insert("receipt_type".to_owned());
receipt_field_selection.insert("contract_id".to_owned());
Expand All @@ -206,20 +200,13 @@ impl Client {
let query = Query {
from_block,
to_block,
receipts: vec![
ReceiptSelection {
root_contract_id: emitting_contracts.clone(),
receipt_type: vec![5, 6],
..Default::default()
},
ReceiptSelection {
contract_id: emitting_contracts,
receipt_type: vec![5, 6],
..Default::default()
},
],
receipts: vec![ReceiptSelection {
root_contract_id: emitting_contracts.clone(),
receipt_type: vec![5, 6],
tx_status: vec![1],
..Default::default()
}],
field_selection: FieldSelection {
transaction: transaction_field_selection,
receipt: receipt_field_selection,
..Default::default()
},
Expand All @@ -239,32 +226,16 @@ impl Client {

sort_receipts(&mut typed_receipts);

let mut failed_txns = HashSet::new();
for batch in filtered_data.transactions.iter() {
let data = Transaction::from_arrow(batch).context("transaction from arrow")?;
for transaction in data {
if transaction.status == TransactionStatus::Failure {
failed_txns.insert(transaction.id);
}
}
}

let successful_logs: Vec<LogContext> = typed_receipts
let logs: Vec<LogContext> = typed_receipts
.into_iter()
.filter_map(|receipt| {
if !failed_txns.contains(&receipt.tx_id) {
Some(receipt.into())
} else {
None
}
})
.map(|receipt| receipt.into())
.collect();

Ok(LogResponse {
archive_height: res.archive_height,
next_block: res.next_block,
total_execution_time: res.total_execution_time,
data: successful_logs,
data: logs,
})
}

Expand Down
2 changes: 1 addition & 1 deletion hyperfuel-format/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyperfuel-format"
version = "1.0.0"
version = "2.0.0"
edition = "2021"
description = "fuel network format library"
license = "MIT"
Expand Down
6 changes: 6 additions & 0 deletions hyperfuel-format/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ pub struct Receipt {
pub root_contract_id: Option<ContractId>,
/// transaction that this receipt originated from
pub tx_id: Hash,
/// The status type of the transaction this receipt originated from
pub tx_status: TransactionStatus,
/// block that the receipt originated in
pub block_height: UInt,
/// The value of the program counter register $pc, which is the memory address of the current instruction.
Expand Down Expand Up @@ -217,6 +219,8 @@ pub struct Receipt {
pub struct Input {
/// transaction that this input originated from
pub tx_id: Hash,
/// The status type of the transaction this input originated from
pub tx_status: TransactionStatus,
/// block that the input originated in
pub block_height: UInt,
/// InputCoin, InputContract, or InputMessage
Expand Down Expand Up @@ -263,6 +267,8 @@ pub struct Input {
pub struct Output {
/// transaction that this out originated from
pub tx_id: Hash,
/// The status type of the transaction this output originated from
pub tx_status: TransactionStatus,
/// block that the output originated in
pub block_height: UInt,
/// CoinOutput, ContractOutput, ChangeOutput, VariableOutput, or ContractCreated
Expand Down
4 changes: 2 additions & 2 deletions hyperfuel-net-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyperfuel-net-types"
version = "1.0.0"
version = "2.0.0"
edition = "2021"
description = "hyperfuel types for transport over network"
license = "MIT"
Expand All @@ -10,7 +10,7 @@ capnp = "0.18"
serde = { version = "1", features = ["derive"] }
arrayvec = { version = "0.7", features = ["serde"] }

hyperfuel-format = { package = "hyperfuel-format", path = "../hyperfuel-format", version = "1.0.0" }
hyperfuel-format = { package = "hyperfuel-format", path = "../hyperfuel-format", version = "2.0.0" }

[build-dependencies]
capnpc = "0.18"
6 changes: 6 additions & 0 deletions hyperfuel-net-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct ReceiptSelection {
pub rc: Vec<u64>,
#[serde(default)]
pub rd: Vec<u64>,
#[serde(default)]
pub tx_status: Vec<u8>,
}

#[derive(Default, Serialize, Deserialize, Clone, Debug)]
Expand All @@ -49,6 +51,8 @@ pub struct InputSelection {
pub recipient: Vec<Hash>,
#[serde(default)]
pub input_type: Vec<u8>,
#[serde(default)]
pub tx_status: Vec<u8>,
}

#[derive(Default, Serialize, Deserialize, Clone, Debug)]
Expand All @@ -61,6 +65,8 @@ pub struct OutputSelection {
pub contract: Vec<Hash>,
#[serde(default)]
pub output_type: Vec<u8>,
#[serde(default)]
pub tx_status: Vec<u8>,
}

#[derive(Default, Serialize, Deserialize, Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion hyperfuel-schema/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hyperfuel-schema"
version = "1.0.0"
version = "2.0.0"
edition = "2021"
description = "schema utilities for hyperfuel"
license = "MIT"
Expand Down
Loading

0 comments on commit d68e704

Please sign in to comment.