Skip to content

Commit 3070ec4

Browse files
in-progress work (does not buidl)
1 parent f7ab9f0 commit 3070ec4

File tree

8 files changed

+370
-573
lines changed

8 files changed

+370
-573
lines changed

ic-agent/src/agent/agent_config.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use crate::{
55
use reqwest::Client;
66
use std::{sync::Arc, time::Duration};
77

8-
use super::route_provider::RouteProvider;
8+
use super::{route_provider::RouteProvider, IC_ROOT_KEY};
99

1010
/// A configuration for an agent.
11+
#[derive(Clone)]
1112
#[non_exhaustive]
1213
pub struct AgentConfig {
1314
/// See [`with_nonce_factory`](super::AgentBuilder::with_nonce_factory).
@@ -31,6 +32,8 @@ pub struct AgentConfig {
3132
/// See [`with_call_v3_endpoint`](super::AgentBuilder::with_call_v3_endpoint).
3233
#[cfg(feature = "experimental_sync_call")]
3334
pub use_call_v3_endpoint: bool,
35+
/// See [`with_preset_root_key`](super::AgentBuilder::with_preset_root_key).
36+
pub root_key: Option<Vec<u8>>,
3437
}
3538

3639
impl Default for AgentConfig {
@@ -47,6 +50,7 @@ impl Default for AgentConfig {
4750
max_tcp_error_retries: 0,
4851
#[cfg(feature = "experimental_sync_call")]
4952
use_call_v3_endpoint: false,
53+
root_key: None,
5054
}
5155
}
5256
}

ic-agent/src/agent/builder.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
use url::Url;
22

33
use crate::{
4-
agent::{agent_config::AgentConfig, Agent},
4+
agent::{
5+
agent_config::AgentConfig,
6+
route_provider::{
7+
dynamic_routing::{
8+
dynamic_route_provider::{DynamicRouteProviderBuilder, IC0_SEED_DOMAIN},
9+
node::Node,
10+
snapshot::latency_based_routing::LatencyRoutingSnapshot,
11+
},
12+
RouteProvider,
13+
},
14+
Agent,
15+
},
516
AgentError, Identity, NonceFactory, NonceGenerator,
617
};
718
use std::sync::Arc;
819

9-
use super::route_provider::RouteProvider;
10-
1120
/// A builder for an [`Agent`].
1221
#[derive(Default)]
1322
pub struct AgentBuilder {
@@ -20,19 +29,8 @@ impl AgentBuilder {
2029
Agent::new(self.config)
2130
}
2231

23-
#[cfg(all(feature = "reqwest", not(target_family = "wasm")))]
2432
/// Set the dynamic transport layer for the [`Agent`], performing continuos discovery of the API boundary nodes and routing traffic via them based on the latencies.
2533
pub async fn with_discovery_transport(self, client: reqwest::Client) -> Self {
26-
use crate::agent::http_transport::{
27-
dynamic_routing::{
28-
dynamic_route_provider::{DynamicRouteProviderBuilder, IC0_SEED_DOMAIN},
29-
node::Node,
30-
snapshot::latency_based_routing::LatencyRoutingSnapshot,
31-
},
32-
route_provider::RouteProvider,
33-
ReqwestTransport,
34-
};
35-
3634
// TODO: This is a temporary solution to get the seed node.
3735
let seed = Node::new(IC0_SEED_DOMAIN).unwrap();
3836

@@ -46,10 +44,8 @@ impl AgentBuilder {
4644

4745
let route_provider = Arc::new(route_provider) as Arc<dyn RouteProvider>;
4846

49-
let transport = ReqwestTransport::create_with_client_route(route_provider, client)
50-
.expect("failed to create transport");
51-
52-
self.with_transport(transport)
47+
self.with_http_client(client)
48+
.with_arc_route_provider(route_provider)
5349
}
5450

5551
/// Set the URL of the [Agent].

ic-agent/src/agent/mod.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rangemap::{RangeInclusiveMap, RangeInclusiveSet, StepFns};
3030
use reqwest::{Body, Client, Request};
3131
use route_provider::RouteProvider;
3232
use time::OffsetDateTime;
33+
use url::Url;
3334

3435
#[cfg(test)]
3536
mod agent_test;
@@ -144,6 +145,7 @@ type AgentFuture<'a, V> = Pin<Box<dyn Future<Output = Result<V, AgentError>> + '
144145
/// This agent does not understand Candid, and only acts on byte buffers.
145146
#[derive(Clone)]
146147
pub struct Agent {
148+
// If adding any BN-specific fields, exclude them from clone_with_url.
147149
nonce_factory: Arc<dyn NonceGenerator>,
148150
identity: Arc<dyn Identity>,
149151
ingress_expiry: Duration,
@@ -176,11 +178,13 @@ impl Agent {
176178

177179
/// Create an instance of an [`Agent`].
178180
pub fn new(config: agent_config::AgentConfig) -> Result<Agent, AgentError> {
179-
Ok(Agent {
181+
let agent = Agent {
180182
nonce_factory: config.nonce_factory,
181183
identity: config.identity,
182184
ingress_expiry: config.ingress_expiry.unwrap_or(DEFAULT_INGRESS_EXPIRY),
183-
root_key: Arc::new(RwLock::new(IC_ROOT_KEY.to_vec())),
185+
root_key: Arc::new(RwLock::new(
186+
config.root_key.unwrap_or_else(|| IC_ROOT_KEY.to_vec()),
187+
)),
184188
client: config.client.unwrap_or_else(|| {
185189
#[cfg(not(target_family = "wasm"))]
186190
{
@@ -213,7 +217,9 @@ impl Agent {
213217
false
214218
}
215219
},
216-
})
220+
};
221+
agent.route_provider.notify_start(agent.clone());
222+
Ok(agent)
217223
}
218224

219225
/// Set the identity provider for signing messages.
@@ -1244,6 +1250,12 @@ impl Agent {
12441250
Ok((status, body))
12451251
}
12461252
}
1253+
1254+
fn clone_with_url(&self, url: Url) -> Self {
1255+
let mut clone = self.clone();
1256+
clone.route_provider = Arc::new(url);
1257+
clone
1258+
}
12471259
}
12481260

12491261
const DEFAULT_INGRESS_EXPIRY: Duration = Duration::from_secs(240);

ic-agent/src/agent/route_provider.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
};
66
use url::Url;
77

8-
use crate::agent::AgentError;
8+
use crate::{agent::AgentError, Agent};
99

1010
pub mod dynamic_routing;
1111

@@ -33,6 +33,8 @@ pub trait RouteProvider: std::fmt::Debug + Send + Sync {
3333
/// appearing first. The returned vector can contain fewer than `n` URLs if
3434
/// fewer are available.
3535
fn n_ordered_routes(&self, n: usize) -> Result<Vec<Url>, AgentError>;
36+
37+
fn notify_start(&self, agent: Agent) {}
3638
}
3739

3840
/// A simple implementation of the [`RouteProvider`] which produces an even distribution of the urls from the input ones.

0 commit comments

Comments
 (0)