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

TLS and timeouts for endpoints when fetching genesis data #439

Merged
merged 2 commits into from
Jan 29, 2025
Merged
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
53 changes: 29 additions & 24 deletions backend-rust/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ impl IndexerService {
registry: &mut Registry,
config: IndexerServiceConfig,
) -> anyhow::Result<Self> {
// Handle TLS configuration and set timeouts according to the configuration for
limemloh marked this conversation as resolved.
Show resolved Hide resolved
// every endpoint.
let endpoints: Vec<v2::Endpoint> = endpoints
.into_iter()
.map(|endpoint| {
let endpoint = if endpoint
.uri()
.scheme()
.map_or(false, |x| x == &concordium_rust_sdk::v2::Scheme::HTTPS)
{
endpoint
.tls_config(tonic::transport::ClientTlsConfig::new())
.context("Unable to construct TLS configuration for the Concordium node.")?
} else {
endpoint
};
Ok(endpoint
.timeout(Duration::from_secs(config.node_request_timeout))
.connect_timeout(Duration::from_secs(config.node_connect_timeout)))
})
.collect::<anyhow::Result<_>>()?;

let last_height_stored = sqlx::query!(
"
SELECT height FROM blocks ORDER BY height DESC LIMIT 1
Expand All @@ -125,7 +147,9 @@ SELECT height FROM blocks ORDER BY height DESC LIMIT 1
let start_height = if let Some(height) = last_height_stored {
u64::try_from(height)? + 1
} else {
save_genesis_data(endpoints[0].clone(), &pool).await?;
save_genesis_data(endpoints[0].clone(), &pool)
.await
.context("Failed initializing the database with the genesis block")?;
1
};
let genesis_block_hash: sdk_types::hashes::BlockHash =
Expand Down Expand Up @@ -159,28 +183,7 @@ SELECT height FROM blocks ORDER BY height DESC LIMIT 1
/// Run the service. This future will only stop when signaled by the
/// `cancel_token`.
pub async fn run(self, cancel_token: CancellationToken) -> anyhow::Result<()> {
// Set up endpoints to the node.
let mut endpoints_with_schema = Vec::new();
for endpoint in self.endpoints {
let endpoint = if endpoint
.uri()
.scheme()
.map_or(false, |x| x == &concordium_rust_sdk::v2::Scheme::HTTPS)
{
endpoint
.tls_config(tonic::transport::ClientTlsConfig::new())
.context("Unable to construct TLS configuration for the Concordium node.")?
} else {
endpoint
};
endpoints_with_schema.push(
endpoint
.timeout(Duration::from_secs(self.config.node_request_timeout))
.connect_timeout(Duration::from_secs(self.config.node_connect_timeout)),
);
}

let traverse_config = TraverseConfig::new(endpoints_with_schema, self.start_height.into())
let traverse_config = TraverseConfig::new(self.endpoints, self.start_height.into())
.context("Failed setting up TraverseConfig")?
.set_max_parallel(self.config.max_parallel_block_preprocessors)
.set_max_behind(std::time::Duration::from_secs(self.config.node_max_behind));
Expand Down Expand Up @@ -702,7 +705,9 @@ struct BlockData {
/// Function for initializing the database with the genesis block.
/// This should only be called if the database is empty.
async fn save_genesis_data(endpoint: v2::Endpoint, pool: &PgPool) -> anyhow::Result<()> {
let mut client = v2::Client::new(endpoint).await?;
let mut client = v2::Client::new(endpoint)
.await
.context("Failed to establish connection to Concordium Node")?;
let mut tx = pool.begin().await.context("Failed to create SQL transaction")?;
let genesis_height = v2::BlockIdentifier::AbsoluteHeight(0.into());
{
Expand Down