Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to the latest, simpler p2p spec #1729

Merged
merged 16 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ impl From<u64> for GasPrice {
}
}

impl From<Felt> for GasPrice {
fn from(src: Felt) -> Self {
let mut bytes = [0u8; 16];
bytes.copy_from_slice(&src.as_be_bytes()[16..]);
Self(u128::from_be_bytes(bytes))
}
}

impl From<BlockNumber> for BlockId {
fn from(number: BlockNumber) -> Self {
Self::Number(number)
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use fake::Dummy;

use crate::BlockCommitmentSignatureElem;

#[derive(Default, Debug, Clone, PartialEq, Dummy)]
#[derive(Default, Debug, Clone, PartialEq, Eq, Dummy)]
pub struct BlockCommitmentSignature {
pub r: BlockCommitmentSignatureElem,
pub s: BlockCommitmentSignatureElem,
Expand Down
4 changes: 4 additions & 0 deletions crates/common/src/state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ impl ContractClassUpdate {
ContractClassUpdate::Replace(x) => *x,
}
}

pub fn is_replaced(&self) -> bool {
matches!(self, ContractClassUpdate::Replace(_))
}
}

impl StateUpdate {
Expand Down
18 changes: 0 additions & 18 deletions crates/gateway-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,6 @@ pub trait GatewayApi: Sync {
}
}

/// This is a **temporary** measure to keep the sync logic unchanged
///
/// TODO remove when p2p friendly sync is implemented
#[allow(unused_variables)]
#[mockall::automock]
#[async_trait::async_trait]
pub trait GossipApi: Sync {
async fn propagate_head(&self, block_number: BlockNumber, block_hash: BlockHash) {
// Intentionally does nothing for default impl
}
}

#[async_trait::async_trait]
impl<T: GatewayApi + Sync + Send> GatewayApi for std::sync::Arc<T> {
async fn block(&self, block: BlockId) -> Result<reply::MaybePendingBlock, SequencerError> {
Expand Down Expand Up @@ -574,12 +562,6 @@ impl GatewayApi for Client {
}
}

#[async_trait::async_trait]
impl GossipApi for Client {}

#[async_trait::async_trait]
impl GossipApi for () {}

pub mod test_utils {
use super::Client;
use starknet_gateway_types::error::KnownStarknetErrorCode;
Expand Down
4 changes: 2 additions & 2 deletions crates/gateway-types/src/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub(crate) mod transaction {
pub ec_op_builtin: u64,
pub keccak_builtin: u64,
pub poseidon_builtin: u64,
pub segment_arena_builtin: u64,
pub segment_arena_builtin: u64, // TODO REMOVE (?)
sistemd marked this conversation as resolved.
Show resolved Hide resolved
}

impl From<BuiltinCounters> for pathfinder_common::receipt::BuiltinCounters {
Expand Down Expand Up @@ -386,7 +386,7 @@ pub(crate) mod transaction {
ec_op_builtin: rng.next_u32() as u64,
keccak_builtin: rng.next_u32() as u64,
poseidon_builtin: rng.next_u32() as u64,
segment_arena_builtin: rng.next_u32() as u64,
segment_arena_builtin: 0, // Not used in p2p
}
}
}
Expand Down
38 changes: 26 additions & 12 deletions crates/p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ use libp2p::swarm::{
};
use libp2p::StreamProtocol;
use libp2p::{autonat, Multiaddr, PeerId};
use p2p_proto::block::{
BlockBodiesRequest, BlockBodiesResponse, BlockHeadersRequest, BlockHeadersResponse,
};
use p2p_proto::class::{ClassesRequest, ClassesResponse};
use p2p_proto::event::{EventsRequest, EventsResponse};
use p2p_proto::header::{BlockHeadersRequest, BlockHeadersResponse};
use p2p_proto::receipt::{ReceiptsRequest, ReceiptsResponse};
use p2p_proto::state::{StateDiffsRequest, StateDiffsResponse};
use p2p_proto::transaction::{TransactionsRequest, TransactionsResponse};
use pathfinder_common::ChainId;
use std::{cmp, task};
Expand Down Expand Up @@ -58,7 +58,8 @@ pub struct Inner {
kademlia: kad::Behaviour<MemoryStore>,
gossipsub: gossipsub::Behaviour,
headers_sync: p2p_stream::Behaviour<codec::Headers>,
bodies_sync: p2p_stream::Behaviour<codec::Bodies>,
classes_sync: p2p_stream::Behaviour<codec::Classes>,
state_diffs_sync: p2p_stream::Behaviour<codec::StateDiffs>,
transactions_sync: p2p_stream::Behaviour<codec::Transactions>,
receipts_sync: p2p_stream::Behaviour<codec::Receipts>,
events_sync: p2p_stream::Behaviour<codec::Events>,
Expand Down Expand Up @@ -468,7 +469,8 @@ impl Behaviour {
.expect("valid gossipsub params");

let headers_sync = request_response_behavior::<codec::Headers>();
let bodies_sync = request_response_behavior::<codec::Bodies>();
let classes_sync = request_response_behavior::<codec::Classes>();
let state_diffs_sync = request_response_behavior::<codec::StateDiffs>();
let transactions_sync = request_response_behavior::<codec::Transactions>();
let receipts_sync = request_response_behavior::<codec::Receipts>();
let events_sync = request_response_behavior::<codec::Events>();
Expand Down Expand Up @@ -496,7 +498,8 @@ impl Behaviour {
kademlia,
gossipsub,
headers_sync,
bodies_sync,
classes_sync,
state_diffs_sync,
transactions_sync,
receipts_sync,
events_sync,
Expand Down Expand Up @@ -787,8 +790,12 @@ impl Behaviour {
&mut self.inner.headers_sync
}

pub fn bodies_sync_mut(&mut self) -> &mut p2p_stream::Behaviour<codec::Bodies> {
&mut self.inner.bodies_sync
pub fn classes_sync_mut(&mut self) -> &mut p2p_stream::Behaviour<codec::Classes> {
&mut self.inner.classes_sync
}

pub fn state_diffs_sync_mut(&mut self) -> &mut p2p_stream::Behaviour<codec::StateDiffs> {
&mut self.inner.state_diffs_sync
}

pub fn transactions_sync_mut(&mut self) -> &mut p2p_stream::Behaviour<codec::Transactions> {
Expand Down Expand Up @@ -847,7 +854,8 @@ pub enum Event {
Kademlia(kad::Event),
Gossipsub(gossipsub::Event),
HeadersSync(p2p_stream::Event<BlockHeadersRequest, BlockHeadersResponse>),
BodiesSync(p2p_stream::Event<BlockBodiesRequest, BlockBodiesResponse>),
ClassesSync(p2p_stream::Event<ClassesRequest, ClassesResponse>),
StateDiffsSync(p2p_stream::Event<StateDiffsRequest, StateDiffsResponse>),
TransactionsSync(p2p_stream::Event<TransactionsRequest, TransactionsResponse>),
ReceiptsSync(p2p_stream::Event<ReceiptsRequest, ReceiptsResponse>),
EventsSync(p2p_stream::Event<EventsRequest, EventsResponse>),
Expand Down Expand Up @@ -901,9 +909,15 @@ impl From<p2p_stream::Event<BlockHeadersRequest, BlockHeadersResponse>> for Even
}
}

impl From<p2p_stream::Event<BlockBodiesRequest, BlockBodiesResponse>> for Event {
fn from(event: p2p_stream::Event<BlockBodiesRequest, BlockBodiesResponse>) -> Self {
Event::BodiesSync(event)
impl From<p2p_stream::Event<ClassesRequest, ClassesResponse>> for Event {
fn from(event: p2p_stream::Event<ClassesRequest, ClassesResponse>) -> Self {
Event::ClassesSync(event)
}
}

impl From<p2p_stream::Event<StateDiffsRequest, StateDiffsResponse>> for Event {
fn from(event: p2p_stream::Event<StateDiffsRequest, StateDiffsResponse>) -> Self {
Event::StateDiffsSync(event)
}
}

Expand Down
Loading
Loading