Skip to content

Commit

Permalink
Merge pull request AleoNet#3408 from ProvableHQ/fix/cdn
Browse files Browse the repository at this point in the history
[Fix] Fix `Scan` and update CDN TLS version
  • Loading branch information
zosorock authored Oct 26, 2024
2 parents 63464a5 + e0da22f commit c7ccc4f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
72 changes: 69 additions & 3 deletions Cargo.lock

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

17 changes: 14 additions & 3 deletions cli/src/commands/developer/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#![allow(clippy::type_complexity)]

use crate::commands::CDN_BASE_URL;
use snarkvm::{
console::network::{CanaryV0, MainnetV0, Network, TestnetV0},
prelude::{Ciphertext, Field, FromBytes, Plaintext, PrivateKey, Record, ViewKey, block::Block},
Expand All @@ -31,8 +32,6 @@ use std::{
use zeroize::Zeroize;

const MAX_BLOCK_RANGE: u32 = 50;
// TODO (raychu86): This should be configurable based on network.
const CDN_ENDPOINT: &str = "https://s3.us-west-1.amazonaws.com/testnet3.blocks/phase3";

/// Scan the snarkOS node for records.
#[derive(Debug, Parser, Zeroize)]
Expand Down Expand Up @@ -172,6 +171,16 @@ impl Scan {
}
}

/// Returns the CDN to prefetch initial blocks from, from the given configurations.
fn parse_cdn<N: Network>() -> Result<String> {
match N::ID {
MainnetV0::ID => Ok(format!("{CDN_BASE_URL}/mainnet/v0")),
TestnetV0::ID => Ok(format!("{CDN_BASE_URL}/testnet/v0")),
CanaryV0::ID => Ok(format!("{CDN_BASE_URL}/canary/v0")),
_ => bail!("Unknown network ID ({})", N::ID),
}
}

/// Fetch owned ciphertext records from the endpoint.
fn fetch_records<N: Network>(
private_key: Option<PrivateKey<N>>,
Expand Down Expand Up @@ -215,11 +224,13 @@ impl Scan {
let mut request_start = match is_development_network {
true => start_height,
false => {
// Parse the CDN endpoint.
let cdn_endpoint = Self::parse_cdn::<N>()?;
// Scan the CDN first for records.
Self::scan_from_cdn(
start_height,
end_height,
CDN_ENDPOINT.to_string(),
cdn_endpoint,
endpoint.to_string(),
private_key,
*view_key,
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const DEVELOPMENT_MODE_RNG_SEED: u64 = 1234567890u64;
const DEVELOPMENT_MODE_NUM_GENESIS_COMMITTEE_MEMBERS: u16 = 4;

/// The CDN base url.
const CDN_BASE_URL: &str = "https://blocks.aleo.org";
pub(crate) const CDN_BASE_URL: &str = "https://blocks.aleo.org";

/// A mapping of `staker_address` to `(validator_address, withdrawal_address, amount)`.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions node/cdn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ optional = true

[dependencies.reqwest]
version = "0.11"
features = [ "rustls-tls" ]

[dependencies.serde]
version = "1"
Expand Down
8 changes: 4 additions & 4 deletions node/cdn/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub async fn load_blocks<N: Network>(
process: impl FnMut(Block<N>) -> Result<()> + Clone + Send + Sync + 'static,
) -> Result<u32, (u32, anyhow::Error)> {
// Create a Client to maintain a connection pool throughout the sync.
let client = match Client::builder().build() {
let client = match Client::builder().use_rustls_tls().build() {
Ok(client) => client,
Err(error) => {
return Err((start_height.saturating_sub(1), anyhow!("Failed to create a CDN request client - {error}")));
Expand Down Expand Up @@ -439,7 +439,7 @@ mod tests {

type CurrentNetwork = MainnetV0;

const TEST_BASE_URL: &str = "https://s3.us-west-1.amazonaws.com/testnet3.blocks/phase3";
const TEST_BASE_URL: &str = "https://blocks.aleo.org/mainnet/v0";

fn check_load_blocks(start: u32, end: Option<u32>, expected: usize) {
let blocks = Arc::new(RwLock::new(Vec::new()));
Expand Down Expand Up @@ -494,7 +494,7 @@ mod tests {
#[test]
fn test_cdn_height() {
let rt = tokio::runtime::Runtime::new().unwrap();
let client = reqwest::Client::builder().build().unwrap();
let client = reqwest::Client::builder().use_rustls_tls().build().unwrap();
rt.block_on(async {
let height = cdn_height::<BLOCKS_PER_FILE>(&client, TEST_BASE_URL).await.unwrap();
assert!(height > 0);
Expand All @@ -505,7 +505,7 @@ mod tests {
fn test_cdn_get() {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let client = reqwest::Client::new();
let client = reqwest::Client::builder().use_rustls_tls().build().unwrap();
let height =
cdn_get::<u32>(client, &format!("{TEST_BASE_URL}/mainnet/latest/height"), "height").await.unwrap();
assert!(height > 0);
Expand Down

0 comments on commit c7ccc4f

Please sign in to comment.