Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
CHr15F0x committed Jan 17, 2024
1 parent 71d8f81 commit 77683f9
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pathfinder-common = { path = "../common" }
pathfinder-crypto = { path = "../crypto" }
prost = "0.12.1"
rand = { workspace = true }
rayon = "1.8.0"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sha2 = "0.10.7"
Expand Down
53 changes: 52 additions & 1 deletion crates/p2p/src/client/peer_agnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use p2p_proto::event::EventsRequest;
use p2p_proto::receipt::{Receipt, ReceiptsRequest};
use p2p_proto::transaction::TransactionsRequest;
use pathfinder_common::{
event::Event, transaction::TransactionVariant, BlockHash, BlockNumber, TransactionHash,
event::Event, transaction::TransactionVariant, BlockHash, BlockNumber, ContractAddress,
TransactionHash,
};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use tokio::sync::RwLock;

use crate::sync::protocol;
Expand Down Expand Up @@ -237,6 +239,55 @@ impl Client {
match response_receiver {
Ok(rx) => {
if let Some(parsed) = parse::<parse::transactions::State>(&peer, rx).await {
let deploy_account = parsed.deploy_account;
let mut parsed = parsed.other;

let (tx, rx) = tokio::sync::oneshot::channel();

rayon::spawn(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 {
TransactionVariant::DeployAccountV0V1(mut x) => {
let contract_address =
ContractAddress::deployed_contract_address(
x.constructor_calldata.iter().copied(),
&x.contract_address_salt,
&x.class_hash,
);
x.contract_address = contract_address;
TransactionVariant::DeployAccountV0V1(x)
}
TransactionVariant::DeployAccountV3(mut x) => {
let contract_address =
ContractAddress::deployed_contract_address(
x.constructor_calldata.iter().copied(),
&x.contract_address_salt,
&x.class_hash,
);
x.contract_address = contract_address;
TransactionVariant::DeployAccountV3(x)
}
_ => unreachable!(),
})
.collect::<Vec<_>>(),
)
})
.collect();

// Send the result back to Tokio.
let _ = tx.send(computed);
});

let computed = rx.await?;
parsed.extend(computed);

return Ok(parsed);
}
}
Expand Down
137 changes: 100 additions & 37 deletions crates/p2p/src/client/peer_agnostic/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,24 +296,85 @@ pub(crate) mod transactions {
Uninitialized,
Transactions {
last_id: BlockId,
transactions: HashMap<BlockId, Vec<TransactionVariant>>,
transactions: InnerTransactions,
},
Delimited {
transactions: HashMap<BlockId, Vec<TransactionVariant>>,
transactions: InnerTransactions,
},
DelimitedWithError {
error: Error,
transactions: HashMap<BlockId, Vec<TransactionVariant>>,
transactions: InnerTransactions,
},
Empty {
error: Option<Error>,
},
}

#[derive(Debug)]
pub struct InnerTransactions {
pub deploy_account: HashMap<BlockId, Vec<TransactionVariant>>,
pub other: HashMap<BlockId, Vec<TransactionVariant>>,
}

impl InnerTransactions {
pub fn is_empty(&self) -> bool {
self.deploy_account.is_empty() && self.other.is_empty()
}

pub fn contains_key(&self, id: &BlockId) -> bool {
self.deploy_account.contains_key(id) || self.other.contains_key(id)
}
}

#[derive(Debug)]
pub struct ParsedTransactions {
pub deploy_account: HashMap<BlockHash, Vec<TransactionVariant>>,
pub other: HashMap<BlockHash, Vec<TransactionVariant>>,
}

impl From<InnerTransactions> for ParsedTransactions {
fn from(inner: InnerTransactions) -> Self {
Self {
deploy_account: inner
.deploy_account
.into_iter()
.map(|(k, v)| (BlockHash(k.hash.0), v))
.collect(),
other: inner
.other
.into_iter()
.map(|(k, v)| (BlockHash(k.hash.0), v))
.collect(),
}
}
}

fn try_from_dto(
dtos: Vec<p2p_proto::transaction::Transaction>,
) -> anyhow::Result<(Vec<TransactionVariant>, Vec<TransactionVariant>)> {
let mut deploy_account_txns = Vec::new();
let mut other_txns = Vec::new();

for dto in dtos {
let transaction =
TransactionVariant::try_from_dto(dto).context("parsing transaction")?;
if matches!(
transaction,
TransactionVariant::DeployAccountV0V1(_) | TransactionVariant::DeployAccountV3(_)
) {
deploy_account_txns.push(transaction);
} else {
other_txns.push(transaction);
}
}

Ok((deploy_account_txns, other_txns))
}

impl super::ParserState for State {
type Dto = TransactionsResponse;
type Inner = HashMap<BlockId, Vec<TransactionVariant>>;
type Out = HashMap<BlockHash, Vec<TransactionVariant>>;
type Inner = InnerTransactions;
type Out = ParsedTransactions;

fn transition(self, next: Self::Dto) -> anyhow::Result<Self> {
let TransactionsResponse { id, kind } = next;
Expand All @@ -323,18 +384,25 @@ pub(crate) mod transactions {
State::Uninitialized,
Some(id),
TransactionsResponseKind::Transactions(Transactions { items }),
) => State::Transactions {
last_id: id,
transactions: [(
id,
items
.into_iter()
.map(TransactionVariant::try_from_dto)
.collect::<anyhow::Result<Vec<_>>>()
.context("parsing transactions")?,
)]
.into(),
},
) => {
let mut deploy_account = HashMap::new();
let mut other = HashMap::new();
let (new_deploy_account, new_other) = try_from_dto(items)?;
if !deploy_account.is_empty() {
deploy_account.insert(id, new_deploy_account);
}
if !other.is_empty() {
other.insert(id, new_other);
}

State::Transactions {
last_id: id,
transactions: InnerTransactions {
deploy_account,
other,
},
}
}
// The peer does not have anything we asked for
(State::Uninitialized, _, TransactionsResponseKind::Fin(Fin { error })) => {
State::Empty { error }
Expand All @@ -348,16 +416,17 @@ pub(crate) mod transactions {
Some(id),
TransactionsResponseKind::Transactions(Transactions { items }),
) if last_id == id => {
let (new_deploy_account, new_other) = try_from_dto(items)?;
transactions
.deploy_account
.get_mut(&id)
.expect("transactions for this id is present")
.extend(
items
.into_iter()
.map(TransactionVariant::try_from_dto)
.collect::<anyhow::Result<Vec<_>>>()
.context("parsing transactions")?,
);
.expect("this id is present")
.extend(new_deploy_account);
transactions
.other
.get_mut(&id)
.expect("this id is present")
.extend(new_other);

State::Transactions {
last_id,
Expand Down Expand Up @@ -391,17 +460,14 @@ pub(crate) mod transactions {
anyhow::bail!("unexpected response");
}

transactions.insert(
id,
items
.into_iter()
.map(TransactionVariant::try_from_dto)
.collect::<anyhow::Result<Vec<_>>>()
.context("parsing transactions")?,
);
let (new_deploy_account, new_other) = try_from_dto(items)?;

transactions.deploy_account.insert(id, new_deploy_account);
transactions.other.insert(id, new_other);

State::Transactions {
last_id: id,

transactions,
}
}
Expand All @@ -410,10 +476,7 @@ pub(crate) mod transactions {
}

fn from_inner(inner: Self::Inner) -> Self::Out {
inner
.into_iter()
.map(|(k, v)| (BlockHash(k.hash.0), v))
.collect()
inner.into()
}

impl_take_parsed_and_should_stop!(transactions);
Expand Down
2 changes: 1 addition & 1 deletion crates/p2p/src/client/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl TryFromDto<p2p_proto::transaction::Transaction> for TransactionVariant {
/// ## Important
///
/// This conversion does not compute deployed contract address for deploy account transactions
/// ([`TransactionVariant::DeployAccountTransactionV0V1`] and [`TransactionVariant::DeployAccountTransactionV3`]),
/// ([`TransactionVariant::DeployAccountV0V1`] and [`TransactionVariant::DeployAccountV3`]),
/// filling it with a zero address instead. The caller is responsible for performing the computation after the conversion succeeds.
fn try_from_dto(dto: p2p_proto::transaction::Transaction) -> anyhow::Result<Self>
where
Expand Down

0 comments on commit 77683f9

Please sign in to comment.