Skip to content

Commit

Permalink
Download range of blocks during syncing (FuelLabs#1270)
Browse files Browse the repository at this point in the history
FuelLabs#1133

---------

Co-authored-by: Green Baneling <[email protected]>
  • Loading branch information
MitchTurner and xgreenx authored Aug 21, 2023
1 parent aaae2b3 commit 078446b
Show file tree
Hide file tree
Showing 21 changed files with 964 additions and 769 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Description of the upcoming release here.
- [#1302](https://github.com/FuelLabs/fuel-core/pull/1302): Removed the usage of flake and building of the bridge contract ABI.
It simplifies the maintenance and updating of the events, requiring only putting the event definition into the codebase of the relayer.
- [#1293](https://github.com/FuelLabs/fuel-core/issues/1293): Parallelized the `estimate_predicates` endpoint to utilize all available threads.
- [#1270](https://github.com/FuelLabs/fuel-core/pull/1270): Modify the way block headers are retrieved from peers to be done in batches.

#### Breaking
- [#1279](https://github.com/FuelLabs/fuel-core/pull/1279): Added a new CLI flag to enable the Relayer service `--enable-relayer`, and disabled the Relayer service by default. When supplying the `--enable-relayer` flag, the `--relayer` argument becomes mandatory, and omitting it is an error. Similarly, providing a `--relayer` argument without the `--enable-relayer` flag is an error. Lastly, providing the `--keypair` or `--network` arguments will also produce an error if the `--enable-p2p` flag is not set.
Expand Down
101 changes: 30 additions & 71 deletions Cargo.lock

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

17 changes: 13 additions & 4 deletions bin/fuel-core/src/cli/run/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub struct P2PArgs {
#[clap(long = "max-block-size", default_value = MAX_RESPONSE_SIZE_STR, env)]
pub max_block_size: usize,

/// Max number of headers in a single headers request response
#[clap(long = "max-headers-per-request", default_value = "100", env)]
pub max_headers_per_request: u32,

/// Addresses of the bootstrap nodes
/// They should contain PeerId within their `Multiaddr`
#[clap(long = "bootstrap-nodes", value_delimiter = ',', env)]
Expand Down Expand Up @@ -175,12 +179,15 @@ pub struct P2PArgs {

#[derive(Debug, Clone, Args)]
pub struct SyncArgs {
/// The maximum number of get header requests to make in a single batch.
#[clap(long = "sync-max-get-header", default_value = "10", env)]
pub max_get_header_requests: usize,
/// The maximum number of get transaction requests to make in a single batch.
#[clap(long = "sync-max-get-txns", default_value = "10", env)]
pub max_get_txns_requests: usize,
/// The maximum number of headers to request in a single batch.
#[clap(long = "sync-header-batch-size", default_value = "10", env)]
pub header_batch_size: u32,
/// The maximum number of header batch requests to have active at one time.
#[clap(long = "sync-max-header-batch-requests", default_value = "10", env)]
pub max_header_batch_requests: usize,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -211,8 +218,9 @@ impl KeypairArg {
impl From<SyncArgs> for fuel_core::sync::Config {
fn from(value: SyncArgs) -> Self {
Self {
max_get_header_requests: value.max_get_header_requests,
max_get_txns_requests: value.max_get_txns_requests,
header_batch_size: value.header_batch_size,
max_header_batch_requests: value.max_header_batch_requests,
}
}
}
Expand Down Expand Up @@ -282,6 +290,7 @@ impl P2PArgs {
public_address: self.public_address,
tcp_port: self.peering_port,
max_block_size: self.max_block_size,
max_headers_per_request: self.max_headers_per_request,
bootstrap_nodes: self.bootstrap_nodes,
reserved_nodes: self.reserved_nodes,
reserved_nodes_only_mode: self.reserved_nodes_only_mode,
Expand Down
15 changes: 15 additions & 0 deletions crates/fuel-core/src/database/sealed_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use fuel_core_types::{
fuel_tx::Transaction,
fuel_types::BlockHeight,
};
use std::ops::Range;

impl DatabaseColumn for SealedBlockConsensus {
fn column() -> Column {
Expand Down Expand Up @@ -93,6 +94,20 @@ impl Database {
self.get_sealed_block_header(&block_id)
}

pub fn get_sealed_block_headers(
&self,
block_height_range: Range<u32>,
) -> StorageResult<Vec<SealedBlockHeader>> {
let headers = block_height_range
.map(BlockHeight::from)
.map(|height| self.get_sealed_block_header_by_height(&height))
.collect::<StorageResult<Vec<_>>>()?
.into_iter()
.flatten()
.collect();
Ok(headers)
}

pub fn get_sealed_block_header(
&self,
block_id: &BlockId,
Expand Down
8 changes: 8 additions & 0 deletions crates/fuel-core/src/service/adapters/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use fuel_core_types::{
fuel_tx::Transaction,
fuel_types::BlockHeight,
};
use std::ops::Range;

impl P2pDb for Database {
fn get_sealed_block(
Expand All @@ -31,6 +32,13 @@ impl P2pDb for Database {
self.get_sealed_block_header_by_height(height)
}

fn get_sealed_headers(
&self,
block_height_range: Range<u32>,
) -> StorageResult<Vec<SealedBlockHeader>> {
self.get_sealed_block_headers(block_height_range)
}

fn get_transactions(
&self,
block_id: &BlockId,
Expand Down
28 changes: 20 additions & 8 deletions crates/fuel-core/src/service/adapters/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ use fuel_core_types::{
},
fuel_tx::Transaction,
fuel_types::BlockHeight,
services::p2p::SourcePeer,
services::p2p::{
PeerId,
SourcePeer,
},
};
use std::ops::Range;

#[async_trait::async_trait]
impl PeerToPeerPort for P2PAdapter {
Expand All @@ -39,17 +43,25 @@ impl PeerToPeerPort for P2PAdapter {
}
}

async fn get_sealed_block_header(
async fn get_sealed_block_headers(
&self,
height: BlockHeight,
) -> anyhow::Result<Option<SourcePeer<SealedBlockHeader>>> {
block_range_height: Range<u32>,
) -> anyhow::Result<Option<Vec<SourcePeer<SealedBlockHeader>>>> {
if let Some(service) = &self.service {
Ok(service
.get_sealed_block_header(height)
.get_sealed_block_headers(block_range_height)
.await?
.map(|(peer_id, header)| SourcePeer {
peer_id: peer_id.into(),
data: header,
.and_then(|(peer_id, headers)| {
let peer_id: PeerId = peer_id.into();
headers.map(|headers| {
headers
.into_iter()
.map(|header| SourcePeer {
peer_id: peer_id.clone(),
data: header,
})
.collect()
})
}))
} else {
Ok(None)
Expand Down
Loading

0 comments on commit 078446b

Please sign in to comment.