more refactor

This commit is contained in:
John Smith
2023-02-25 22:02:13 -05:00
parent e328bdc270
commit 1fba8831e8
10 changed files with 203 additions and 127 deletions
+17 -25
View File
@@ -165,10 +165,10 @@ impl VeilidAPI {
// Private route allocation
/// Allocate a new private route set with default cryptography and network options
/// Returns a set of keys and a publishable 'blob' with the route encrypted with each crypto kind
/// Returns a route id and a publishable 'blob' with the route encrypted with each crypto kind
/// Those nodes importing the blob will have their choice of which crypto kind to use
#[instrument(level = "debug", skip(self))]
pub async fn new_private_route(&self) -> Result<(TypedKeySet, Vec<u8>), VeilidAPIError> {
pub async fn new_private_route(&self) -> Result<(RouteId, Vec<u8>), VeilidAPIError> {
self.new_custom_private_route(
&VALID_CRYPTO_KINDS,
Stability::default(),
@@ -184,7 +184,7 @@ impl VeilidAPI {
crypto_kinds: &[CryptoKind],
stability: Stability,
sequencing: Sequencing,
) -> Result<(TypedKeySet, Vec<u8>), VeilidAPIError> {
) -> Result<(RouteId, Vec<u8>), VeilidAPIError> {
let default_route_hop_count: usize = {
let config = self.config()?;
let c = config.get();
@@ -202,56 +202,48 @@ impl VeilidAPI {
&[],
)
.map_err(VeilidAPIError::internal)?;
let Some(pr_keys) = r else {
let Some(route_id) = r else {
apibail_generic!("unable to allocate route");
};
if !rss
.test_route(&pr_keys)
.test_route(route_id.clone())
.await
.map_err(VeilidAPIError::no_connection)?
{
rss.release_route(&pr_pubkey);
rss.release_route(route_id);
apibail_generic!("allocated route failed to test");
}
let private_route = rss
.assemble_private_route(&pr_pubkey, Some(true))
let private_routes = rss
.assemble_private_routes(&route_id, Some(true))
.map_err(VeilidAPIError::generic)?;
let blob = match RouteSpecStore::private_route_to_blob(&private_route) {
let blob = match RouteSpecStore::private_routes_to_blob(&private_routes) {
Ok(v) => v,
Err(e) => {
rss.release_route(&pr_pubkey);
rss.release_route(route_id);
apibail_internal!(e);
}
};
rss.mark_route_published(&pr_pubkey, true)
rss.mark_route_published(&route_id, true)
.map_err(VeilidAPIError::internal)?;
Ok((pr_pubkey, blob))
Ok((route_id, blob))
}
#[instrument(level = "debug", skip(self))]
pub fn import_remote_private_route(
&self,
blob: Vec<u8>,
) -> Result<TypedKeySet, VeilidAPIError> {
pub fn import_remote_private_route(&self, blob: Vec<u8>) -> Result<RouteId, VeilidAPIError> {
let rss = self.routing_table()?.route_spec_store();
rss.import_remote_private_route(blob)
.map_err(|e| VeilidAPIError::invalid_argument(e, "blob", "private route blob"))
}
#[instrument(level = "debug", skip(self))]
pub fn release_private_route(&self, key: &PublicKey) -> Result<(), VeilidAPIError> {
pub fn release_private_route(&self, route_id: RouteId) -> Result<(), VeilidAPIError> {
let rss = self.routing_table()?.route_spec_store();
if rss.release_route(key) {
Ok(())
} else {
Err(VeilidAPIError::invalid_argument(
"release_private_route",
"key",
key,
))
if !rss.release_route(route_id) {
apibail_invalid_argument!("release_private_route", "key", route_id);
}
Ok(())
}
////////////////////////////////////////////////////////////////