Skip to content

Commit

Permalink
fix(p2p): drop partial types
Browse files Browse the repository at this point in the history
  • Loading branch information
CHr15F0x committed Feb 22, 2024
1 parent c839d53 commit 6596582
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 706 deletions.
13 changes: 10 additions & 3 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,18 @@ impl From<u64> for GasPrice {
}
}

impl From<Felt> for GasPrice {
fn from(src: Felt) -> Self {
impl TryFrom<Felt> for GasPrice {
type Error = anyhow::Error;

fn try_from(src: Felt) -> Result<Self, Self::Error> {
anyhow::ensure!(
src.as_be_bytes()[0..16] == [0; 16],
"Gas price fits into u128"
);

let mut bytes = [0u8; 16];
bytes.copy_from_slice(&src.as_be_bytes()[16..]);
Self(u128::from_be_bytes(bytes))
Ok(Self(u128::from_be_bytes(bytes)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/p2p/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod conv;
pub mod peer_agnostic;
pub mod peer_aware;
pub mod types;
367 changes: 367 additions & 0 deletions crates/p2p/src/client/conv.rs

Large diffs are not rendered by default.

80 changes: 4 additions & 76 deletions crates/p2p/src/client/peer_agnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@ use std::{
time::Duration,
};

use anyhow::Context;
use futures::StreamExt;
use libp2p::PeerId;
use p2p_proto::common::{Direction, Iteration};
use p2p_proto::header::{BlockHeadersRequest, BlockHeadersResponse};
use pathfinder_common::{
transaction::{DeployAccountTransactionV0V1, DeployAccountTransactionV3, TransactionVariant},
BlockNumber,
};
use pathfinder_common::{BlockHash, ContractAddress};

use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use pathfinder_common::{BlockNumber, SignedBlockHeader};
use tokio::sync::RwLock;

use crate::client::peer_aware;
use crate::client::types::{RawDeployAccountTransaction, SignedBlockHeader};
use crate::client::{conv::TryFromDto, peer_aware};
use crate::sync::protocol;

/// Data received from a specific peer.
Expand Down Expand Up @@ -145,7 +137,8 @@ impl Client {

while let Some(signed_header) = responses.next().await {
let signed_header = match signed_header {
BlockHeadersResponse::Header(hdr) => match SignedBlockHeader::try_from(*hdr) {
BlockHeadersResponse::Header(hdr) =>
match SignedBlockHeader::try_from_dto(*hdr) {
Ok(hdr) => hdr,
Err(error) => {
tracing::debug!(%peer, %error, "Header stream failed");
Expand Down Expand Up @@ -175,71 +168,6 @@ impl Client {
}
}

// TODO
/// Does not block the current thread.
async fn _compute_contract_addresses(
deploy_account: HashMap<BlockHash, Vec<super::types::RawDeployAccountTransaction>>,
) -> anyhow::Result<Vec<(BlockHash, Vec<TransactionVariant>)>> {
let jh = tokio::task::spawn_blocking(move || {
// Now we can compute the missing addresses
let computed: Vec<_> = deploy_account
.into_par_iter()
.map(|(block_hash, transactions)| {
(
block_hash,
transactions
.into_par_iter()
.map(|t| match t {
RawDeployAccountTransaction::DeployAccountV0V1(x) => {
let contract_address = ContractAddress::deployed_contract_address(
x.constructor_calldata.iter().copied(),
&x.contract_address_salt,
&x.class_hash,
);
TransactionVariant::DeployAccountV0V1(
DeployAccountTransactionV0V1 {
contract_address,
max_fee: x.max_fee,
version: x.version,
signature: x.signature,
nonce: x.nonce,
contract_address_salt: x.contract_address_salt,
constructor_calldata: x.constructor_calldata,
class_hash: x.class_hash,
},
)
}
RawDeployAccountTransaction::DeployAccountV3(x) => {
let contract_address = ContractAddress::deployed_contract_address(
x.constructor_calldata.iter().copied(),
&x.contract_address_salt,
&x.class_hash,
);
TransactionVariant::DeployAccountV3(DeployAccountTransactionV3 {
contract_address,
signature: x.signature,
nonce: x.nonce,
nonce_data_availability_mode: x.nonce_data_availability_mode,
fee_data_availability_mode: x.fee_data_availability_mode,
resource_bounds: x.resource_bounds,
tip: x.tip,
paymaster_data: x.paymaster_data,
contract_address_salt: x.contract_address_salt,
constructor_calldata: x.constructor_calldata,
class_hash: x.class_hash,
})
}
})
.collect::<Vec<_>>(),
)
})
.collect();
computed
});
let computed = jh.await.context("task ended unexpectedly")?;
Ok(computed)
}

#[derive(Clone, Debug)]
struct PeersWithCapability {
set: HashMap<String, HashSet<PeerId>>,
Expand Down
Loading

0 comments on commit 6596582

Please sign in to comment.