Skip to content

Commit

Permalink
Adapt arguments of getTransactionsByAddress to be compatible with the…
Browse files Browse the repository at this point in the history
… 1.0 client
  • Loading branch information
sisou committed Mar 14, 2023
1 parent c805f81 commit 8196d31
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
11 changes: 11 additions & 0 deletions consensus/src/consensus/consensus_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use nimiq_blockchain_interface::AbstractBlockchain;
use nimiq_hash::Blake2bHash;
use tokio::sync::broadcast::Sender as BroadcastSender;
use tokio_stream::wrappers::BroadcastStream;

Expand Down Expand Up @@ -59,6 +60,8 @@ impl<N: Network> ConsensusProxy<N> {
pub async fn request_transactions_by_address(
&self,
address: Address,
since_block_height: u32,
ignored_hashes: Vec<Blake2bHash>,
min_peers: usize,
max: Option<u16>,
) -> Result<Vec<ExtendedTransaction>, RequestError> {
Expand Down Expand Up @@ -119,6 +122,14 @@ impl<N: Network> ConsensusProxy<N> {
let current_head_number = blockchain.head().block_number();

for (hash, block_number) in response.receipts {
if block_number < since_block_height {
continue;
}

if ignored_hashes.contains(&hash) {
continue;
}

// If the transaction was already verified, then we don't need to verify it again
if verified_transactions.contains_key(&hash) {
continue;
Expand Down
36 changes: 32 additions & 4 deletions web-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ use nimiq_primitives::policy::Policy;

use crate::address::Address;
use crate::peer_info::PeerInfo;
use crate::transaction::{PlainTransactionDetails, PlainTransactionDetailsArrayType, Transaction};
use crate::transaction::{
PlainTransactionDetails, PlainTransactionDetailsArrayType, Transaction, TransactionState,
};
use crate::transaction_builder::TransactionBuilder;
use crate::utils::{from_network_id, to_network_id};

Expand Down Expand Up @@ -591,21 +593,47 @@ impl Client {
pub async fn get_transations_by_address(
&self,
address: Address,
max: Option<u16>,
since_block_height: Option<u32>,
known_transaction_details: Option<PlainTransactionDetailsArrayType>,
limit: Option<u16>,
min_peers: Option<usize>,
) -> Result<PlainTransactionDetailsArrayType, JsError> {
if let Some(max) = max {
if let Some(max) = limit {
if max > MAX_TRANSACTIONS_BY_ADDRESS {
return Err(JsError::new(
"The maximum number of transactions exceeds the one that is supported",
));
}
}

let mut known_hashes = vec![];

if let Some(array) = known_transaction_details {
let plain_tx_details =
serde_wasm_bindgen::from_value::<Vec<PlainTransactionDetails>>(array.into())?;
for obj in plain_tx_details {
match obj.state {
// Do not skip unconfirmed transactions
TransactionState::New
| TransactionState::Pending
| TransactionState::Included => continue,
_ => {
known_hashes.push(Blake2bHash::from_str(&obj.transaction.transaction_hash)?)
}
}
}
}

let transactions = self
.inner
.consensus_proxy()
.request_transactions_by_address(address.native(), min_peers.unwrap_or(1), max)
.request_transactions_by_address(
address.native(),
since_block_height.unwrap_or(0),
known_hashes,
min_peers.unwrap_or(1),
limit,
)
.await?;

let current_block = self.get_head_height();
Expand Down
4 changes: 2 additions & 2 deletions web-client/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ pub enum TransactionState {
/// JSON-compatible and human-readable format of transactions, including details about its state in the
/// blockchain. Contains all fields from {@link PlainTransaction}, plus additional fields such as
/// `blockHeight` and `timestamp` if the transaction is included in the blockchain.
#[derive(Tsify)]
#[derive(serde::Deserialize, Tsify)]
#[serde(rename_all = "camelCase")]
pub struct PlainTransactionDetails {
#[serde(flatten)]
transaction: PlainTransaction,
pub transaction: PlainTransaction,

pub state: TransactionState,
#[tsify(optional)]
Expand Down

0 comments on commit 8196d31

Please sign in to comment.