From 06081df22a43be49ba85478c8cedcee061691b87 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 4 Jun 2023 21:22:55 -0400 Subject: [PATCH] json schema generation --- veilid-core/proto/veilid.capnp | 10 +- veilid-core/src/attachment_manager.rs | 6 +- veilid-core/src/core_context.rs | 28 ++- veilid-core/src/intf/native/mod.rs | 4 + veilid-core/src/intf/wasm/mod.rs | 4 + veilid-core/src/network_manager/mod.rs | 6 + .../src/routing_table/tests/test_serialize.rs | 3 + .../rpc_processor/coders/operations/answer.rs | 10 + .../rpc_processor/coders/operations/mod.rs | 15 +- .../coders/operations/question.rs | 10 + veilid-core/src/rpc_processor/mod.rs | 9 +- veilid-core/src/storage_manager/mod.rs | 7 +- veilid-core/src/veilid_api/api.rs | 1 + .../src/veilid_api/json_api/crypto_system.rs | 231 +++++++++++++++--- veilid-core/src/veilid_api/json_api/mod.rs | 159 +++++++++--- .../veilid_api/json_api/routing_context.rs | 138 +++++++++-- veilid-core/src/veilid_api/mod.rs | 1 + veilid-core/src/veilid_api/routing_context.rs | 2 + .../serialize_helpers/serialize_json.rs | 21 ++ .../src/veilid_api/types/aligned_u64.rs | 5 +- .../src/veilid_api/types/app_message_call.rs | 25 +- .../src/veilid_api/types/dht/value_data.rs | 5 +- .../types/dht/value_subkey_range_set.rs | 6 +- 23 files changed, 552 insertions(+), 154 deletions(-) diff --git a/veilid-core/proto/veilid.capnp b/veilid-core/proto/veilid.capnp index 7e0ba4fb..b7f1a359 100644 --- a/veilid-core/proto/veilid.capnp +++ b/veilid-core/proto/veilid.capnp @@ -496,8 +496,9 @@ struct Question @0xd8510bc33492ef70 { getValueQ @5 :OperationGetValueQ; setValueQ @6 :OperationSetValueQ; watchValueQ @7 :OperationWatchValueQ; - supplyBlockQ @8 :OperationSupplyBlockQ; - findBlockQ @9 :OperationFindBlockQ; + # #[cfg(feature="unstable-blockstore")] + # supplyBlockQ @8 :OperationSupplyBlockQ; + # findBlockQ @9 :OperationFindBlockQ; # Tunnel operations # #[cfg(feature="unstable-tunnels")] @@ -534,8 +535,9 @@ struct Answer @0xacacb8b6988c1058 { getValueA @3 :OperationGetValueA; setValueA @4 :OperationSetValueA; watchValueA @5 :OperationWatchValueA; - supplyBlockA @6 :OperationSupplyBlockA; - findBlockA @7 :OperationFindBlockA; + # #[cfg(feature="unstable-blockstore")] + #supplyBlockA @6 :OperationSupplyBlockA; + #findBlockA @7 :OperationFindBlockA; # Tunnel operations # #[cfg(feature="unstable-tunnels")] diff --git a/veilid-core/src/attachment_manager.rs b/veilid-core/src/attachment_manager.rs index fca5650d..a373bea2 100644 --- a/veilid-core/src/attachment_manager.rs +++ b/veilid-core/src/attachment_manager.rs @@ -30,7 +30,7 @@ impl AttachmentManager { storage_manager: StorageManager, protected_store: ProtectedStore, table_store: TableStore, - block_store: BlockStore, + #[cfg(feature = "unstable-blockstore")] block_store: BlockStore, crypto: Crypto, ) -> AttachmentManagerUnlockedInner { AttachmentManagerUnlockedInner { @@ -40,6 +40,7 @@ impl AttachmentManager { storage_manager, protected_store, table_store, + #[cfg(feature = "unstable-blockstore")] block_store, crypto, ), @@ -60,7 +61,7 @@ impl AttachmentManager { storage_manager: StorageManager, protected_store: ProtectedStore, table_store: TableStore, - block_store: BlockStore, + #[cfg(feature = "unstable-blockstore")] block_store: BlockStore, crypto: Crypto, ) -> Self { Self { @@ -70,6 +71,7 @@ impl AttachmentManager { storage_manager, protected_store, table_store, + #[cfg(feature = "unstable-blockstore")] block_store, crypto, )), diff --git a/veilid-core/src/core_context.rs b/veilid-core/src/core_context.rs index 555bdcf2..630f7375 100644 --- a/veilid-core/src/core_context.rs +++ b/veilid-core/src/core_context.rs @@ -17,6 +17,7 @@ struct ServicesContext { pub protected_store: Option, pub table_store: Option, + #[cfg(feature = "unstable-blockstore")] pub block_store: Option, pub crypto: Option, pub attachment_manager: Option, @@ -30,6 +31,7 @@ impl ServicesContext { update_callback, protected_store: None, table_store: None, + #[cfg(feature = "unstable-blockstore")] block_store: None, crypto: None, attachment_manager: None, @@ -42,7 +44,7 @@ impl ServicesContext { update_callback: UpdateCallback, protected_store: ProtectedStore, table_store: TableStore, - block_store: BlockStore, + #[cfg(feature = "unstable-blockstore")] block_store: BlockStore, crypto: Crypto, attachment_manager: AttachmentManager, storage_manager: StorageManager, @@ -52,6 +54,7 @@ impl ServicesContext { update_callback, protected_store: Some(protected_store), table_store: Some(table_store), + #[cfg(feature = "unstable-blockstore")] block_store: Some(block_store), crypto: Some(crypto), attachment_manager: Some(attachment_manager), @@ -103,14 +106,17 @@ impl ServicesContext { self.crypto = Some(crypto.clone()); // Set up block store - trace!("init block store"); - let block_store = BlockStore::new(self.config.clone()); - if let Err(e) = block_store.init().await { - error!("failed to init block store: {}", e); - self.shutdown().await; - return Err(e); + #[cfg(feature = "unstable-blockstore")] + { + trace!("init block store"); + let block_store = BlockStore::new(self.config.clone()); + if let Err(e) = block_store.init().await { + error!("failed to init block store: {}", e); + self.shutdown().await; + return Err(e); + } + self.block_store = Some(block_store.clone()); } - self.block_store = Some(block_store.clone()); // Set up storage manager trace!("init storage manager"); @@ -119,6 +125,7 @@ impl ServicesContext { self.crypto.clone().unwrap(), self.protected_store.clone().unwrap(), self.table_store.clone().unwrap(), + #[cfg(feature = "unstable-blockstore")] self.block_store.clone().unwrap(), ); if let Err(e) = storage_manager.init().await { @@ -136,6 +143,7 @@ impl ServicesContext { storage_manager, protected_store, table_store, + #[cfg(feature = "unstable-blockstore")] block_store, crypto, ); @@ -162,6 +170,7 @@ impl ServicesContext { trace!("terminate storage manager"); storage_manager.terminate().await; } + #[cfg(feature = "unstable-blockstore")] if let Some(block_store) = &mut self.block_store { trace!("terminate block store"); block_store.terminate().await; @@ -198,6 +207,7 @@ pub struct VeilidCoreContext { pub storage_manager: StorageManager, pub protected_store: ProtectedStore, pub table_store: TableStore, + #[cfg(feature = "unstable-blockstore")] pub block_store: BlockStore, pub crypto: Crypto, pub attachment_manager: AttachmentManager, @@ -251,6 +261,7 @@ impl VeilidCoreContext { storage_manager: sc.storage_manager.unwrap(), protected_store: sc.protected_store.unwrap(), table_store: sc.table_store.unwrap(), + #[cfg(feature = "unstable-blockstore")] block_store: sc.block_store.unwrap(), crypto: sc.crypto.unwrap(), attachment_manager: sc.attachment_manager.unwrap(), @@ -264,6 +275,7 @@ impl VeilidCoreContext { self.update_callback.clone(), self.protected_store, self.table_store, + #[cfg(feature = "unstable-blockstore")] self.block_store, self.crypto, self.attachment_manager, diff --git a/veilid-core/src/intf/native/mod.rs b/veilid-core/src/intf/native/mod.rs index 018cba41..d8cc6811 100644 --- a/veilid-core/src/intf/native/mod.rs +++ b/veilid-core/src/intf/native/mod.rs @@ -1,8 +1,12 @@ +#[cfg(feature = "unstable-blockstore")] mod block_store; + mod protected_store; mod system; +#[cfg(feature = "unstable-blockstore")] pub use block_store::*; + pub use protected_store::*; pub use system::*; diff --git a/veilid-core/src/intf/wasm/mod.rs b/veilid-core/src/intf/wasm/mod.rs index b69ada7b..113d35db 100644 --- a/veilid-core/src/intf/wasm/mod.rs +++ b/veilid-core/src/intf/wasm/mod.rs @@ -1,8 +1,12 @@ +#[cfg(feature = "unstable-blockstore")] mod block_store; + mod protected_store; mod system; +#[cfg(feature = "unstable-blockstore")] pub use block_store::*; + pub use protected_store::*; pub use system::*; diff --git a/veilid-core/src/network_manager/mod.rs b/veilid-core/src/network_manager/mod.rs index 3a57e134..9456c625 100644 --- a/veilid-core/src/network_manager/mod.rs +++ b/veilid-core/src/network_manager/mod.rs @@ -150,6 +150,7 @@ struct NetworkManagerUnlockedInner { storage_manager: StorageManager, protected_store: ProtectedStore, table_store: TableStore, + #[cfg(feature="unstable-blockstore")] block_store: BlockStore, crypto: Crypto, // Accessors @@ -181,6 +182,7 @@ impl NetworkManager { storage_manager: StorageManager, protected_store: ProtectedStore, table_store: TableStore, + #[cfg(feature="unstable-blockstore")] block_store: BlockStore, crypto: Crypto, ) -> NetworkManagerUnlockedInner { @@ -189,6 +191,7 @@ impl NetworkManager { storage_manager, protected_store, table_store, + #[cfg(feature="unstable-blockstore")] block_store, crypto, routing_table: RwLock::new(None), @@ -204,6 +207,7 @@ impl NetworkManager { storage_manager: StorageManager, protected_store: ProtectedStore, table_store: TableStore, + #[cfg(feature="unstable-blockstore")] block_store: BlockStore, crypto: Crypto, ) -> Self { @@ -214,6 +218,7 @@ impl NetworkManager { storage_manager, protected_store, table_store, + #[cfg(feature="unstable-blockstore")] block_store, crypto, )), @@ -241,6 +246,7 @@ impl NetworkManager { pub fn table_store(&self) -> TableStore { self.unlocked_inner.table_store.clone() } + #[cfg(feature="unstable-blockstore")] pub fn block_store(&self) -> BlockStore { self.unlocked_inner.block_store.clone() } diff --git a/veilid-core/src/routing_table/tests/test_serialize.rs b/veilid-core/src/routing_table/tests/test_serialize.rs index 14e07930..b3609745 100644 --- a/veilid-core/src/routing_table/tests/test_serialize.rs +++ b/veilid-core/src/routing_table/tests/test_serialize.rs @@ -2,6 +2,7 @@ use crate::*; fn fake_routing_table() -> routing_table::RoutingTable { let veilid_config = VeilidConfig::new(); + #[cfg(feature = "unstable-blockstore")] let block_store = BlockStore::new(veilid_config.clone()); let protected_store = ProtectedStore::new(veilid_config.clone()); let table_store = TableStore::new(veilid_config.clone(), protected_store.clone()); @@ -11,6 +12,7 @@ fn fake_routing_table() -> routing_table::RoutingTable { crypto.clone(), protected_store.clone(), table_store.clone(), + #[cfg(feature = "unstable-blockstore")] block_store.clone(), ); let network_manager = network_manager::NetworkManager::new( @@ -18,6 +20,7 @@ fn fake_routing_table() -> routing_table::RoutingTable { storage_manager, protected_store.clone(), table_store.clone(), + #[cfg(feature = "unstable-blockstore")] block_store.clone(), crypto.clone(), ); diff --git a/veilid-core/src/rpc_processor/coders/operations/answer.rs b/veilid-core/src/rpc_processor/coders/operations/answer.rs index c1aeb4c4..356d1ba5 100644 --- a/veilid-core/src/rpc_processor/coders/operations/answer.rs +++ b/veilid-core/src/rpc_processor/coders/operations/answer.rs @@ -37,7 +37,9 @@ pub enum RPCAnswerDetail { GetValueA(RPCOperationGetValueA), SetValueA(RPCOperationSetValueA), WatchValueA(RPCOperationWatchValueA), + #[cfg(feature = "unstable-blockstore")] SupplyBlockA(RPCOperationSupplyBlockA), + #[cfg(feature = "unstable-blockstore")] FindBlockA(RPCOperationFindBlockA), #[cfg(feature = "unstable-tunnels")] StartTunnelA(RPCOperationStartTunnelA), @@ -56,7 +58,9 @@ impl RPCAnswerDetail { RPCAnswerDetail::GetValueA(_) => "GetValueA", RPCAnswerDetail::SetValueA(_) => "SetValueA", RPCAnswerDetail::WatchValueA(_) => "WatchValueA", + #[cfg(feature = "unstable-blockstore")] RPCAnswerDetail::SupplyBlockA(_) => "SupplyBlockA", + #[cfg(feature = "unstable-blockstore")] RPCAnswerDetail::FindBlockA(_) => "FindBlockA", #[cfg(feature = "unstable-tunnels")] RPCAnswerDetail::StartTunnelA(_) => "StartTunnelA", @@ -74,7 +78,9 @@ impl RPCAnswerDetail { RPCAnswerDetail::GetValueA(r) => r.validate(validate_context), RPCAnswerDetail::SetValueA(r) => r.validate(validate_context), RPCAnswerDetail::WatchValueA(r) => r.validate(validate_context), + #[cfg(feature = "unstable-blockstore")] RPCAnswerDetail::SupplyBlockA(r) => r.validate(validate_context), + #[cfg(feature = "unstable-blockstore")] RPCAnswerDetail::FindBlockA(r) => r.validate(validate_context), #[cfg(feature = "unstable-tunnels")] RPCAnswerDetail::StartTunnelA(r) => r.validate(validate_context), @@ -119,11 +125,13 @@ impl RPCAnswerDetail { let out = RPCOperationWatchValueA::decode(&op_reader)?; RPCAnswerDetail::WatchValueA(out) } + #[cfg(feature = "unstable-blockstore")] veilid_capnp::answer::detail::SupplyBlockA(r) => { let op_reader = r.map_err(RPCError::protocol)?; let out = RPCOperationSupplyBlockA::decode(&op_reader)?; RPCAnswerDetail::SupplyBlockA(out) } + #[cfg(feature = "unstable-blockstore")] veilid_capnp::answer::detail::FindBlockA(r) => { let op_reader = r.map_err(RPCError::protocol)?; let out = RPCOperationFindBlockA::decode(&op_reader)?; @@ -163,9 +171,11 @@ impl RPCAnswerDetail { RPCAnswerDetail::WatchValueA(d) => { d.encode(&mut builder.reborrow().init_watch_value_a()) } + #[cfg(feature = "unstable-blockstore")] RPCAnswerDetail::SupplyBlockA(d) => { d.encode(&mut builder.reborrow().init_supply_block_a()) } + #[cfg(feature = "unstable-blockstore")] RPCAnswerDetail::FindBlockA(d) => d.encode(&mut builder.reborrow().init_find_block_a()), #[cfg(feature = "unstable-tunnels")] RPCAnswerDetail::StartTunnelA(d) => { diff --git a/veilid-core/src/rpc_processor/coders/operations/mod.rs b/veilid-core/src/rpc_processor/coders/operations/mod.rs index db390b9f..72f4c520 100644 --- a/veilid-core/src/rpc_processor/coders/operations/mod.rs +++ b/veilid-core/src/rpc_processor/coders/operations/mod.rs @@ -2,7 +2,6 @@ mod answer; mod operation; mod operation_app_call; mod operation_app_message; -mod operation_find_block; mod operation_find_node; mod operation_get_value; mod operation_return_receipt; @@ -10,7 +9,7 @@ mod operation_route; mod operation_set_value; mod operation_signal; mod operation_status; -mod operation_supply_block; + mod operation_validate_dial_info; mod operation_value_changed; mod operation_watch_value; @@ -18,6 +17,11 @@ mod question; mod respond_to; mod statement; +#[cfg(feature = "unstable-blockstore")] +mod operation_find_block; +#[cfg(feature = "unstable-blockstore")] +mod operation_supply_block; + #[cfg(feature = "unstable-tunnels")] mod operation_cancel_tunnel; #[cfg(feature = "unstable-tunnels")] @@ -29,7 +33,6 @@ pub use answer::*; pub use operation::*; pub use operation_app_call::*; pub use operation_app_message::*; -pub use operation_find_block::*; pub use operation_find_node::*; pub use operation_get_value::*; pub use operation_return_receipt::*; @@ -37,7 +40,6 @@ pub use operation_route::*; pub use operation_set_value::*; pub use operation_signal::*; pub use operation_status::*; -pub use operation_supply_block::*; pub use operation_validate_dial_info::*; pub use operation_value_changed::*; pub use operation_watch_value::*; @@ -45,6 +47,11 @@ pub use question::*; pub use respond_to::*; pub use statement::*; +#[cfg(feature = "unstable-blockstore")] +pub use operation_find_block::*; +#[cfg(feature = "unstable-blockstore")] +pub use operation_supply_block::*; + #[cfg(feature = "unstable-tunnels")] pub use operation_cancel_tunnel::*; #[cfg(feature = "unstable-tunnels")] diff --git a/veilid-core/src/rpc_processor/coders/operations/question.rs b/veilid-core/src/rpc_processor/coders/operations/question.rs index 03debcb5..12591220 100644 --- a/veilid-core/src/rpc_processor/coders/operations/question.rs +++ b/veilid-core/src/rpc_processor/coders/operations/question.rs @@ -49,7 +49,9 @@ pub enum RPCQuestionDetail { GetValueQ(RPCOperationGetValueQ), SetValueQ(RPCOperationSetValueQ), WatchValueQ(RPCOperationWatchValueQ), + #[cfg(feature = "unstable-blockstore")] SupplyBlockQ(RPCOperationSupplyBlockQ), + #[cfg(feature = "unstable-blockstore")] FindBlockQ(RPCOperationFindBlockQ), #[cfg(feature = "unstable-tunnels")] StartTunnelQ(RPCOperationStartTunnelQ), @@ -68,7 +70,9 @@ impl RPCQuestionDetail { RPCQuestionDetail::GetValueQ(_) => "GetValueQ", RPCQuestionDetail::SetValueQ(_) => "SetValueQ", RPCQuestionDetail::WatchValueQ(_) => "WatchValueQ", + #[cfg(feature = "unstable-blockstore")] RPCQuestionDetail::SupplyBlockQ(_) => "SupplyBlockQ", + #[cfg(feature = "unstable-blockstore")] RPCQuestionDetail::FindBlockQ(_) => "FindBlockQ", #[cfg(feature = "unstable-tunnels")] RPCQuestionDetail::StartTunnelQ(_) => "StartTunnelQ", @@ -86,7 +90,9 @@ impl RPCQuestionDetail { RPCQuestionDetail::GetValueQ(r) => r.validate(validate_context), RPCQuestionDetail::SetValueQ(r) => r.validate(validate_context), RPCQuestionDetail::WatchValueQ(r) => r.validate(validate_context), + #[cfg(feature = "unstable-blockstore")] RPCQuestionDetail::SupplyBlockQ(r) => r.validate(validate_context), + #[cfg(feature = "unstable-blockstore")] RPCQuestionDetail::FindBlockQ(r) => r.validate(validate_context), #[cfg(feature = "unstable-tunnels")] RPCQuestionDetail::StartTunnelQ(r) => r.validate(validate_context), @@ -132,11 +138,13 @@ impl RPCQuestionDetail { let out = RPCOperationWatchValueQ::decode(&op_reader)?; RPCQuestionDetail::WatchValueQ(out) } + #[cfg(feature = "unstable-blockstore")] veilid_capnp::question::detail::SupplyBlockQ(r) => { let op_reader = r.map_err(RPCError::protocol)?; let out = RPCOperationSupplyBlockQ::decode(&op_reader)?; RPCQuestionDetail::SupplyBlockQ(out) } + #[cfg(feature = "unstable-blockstore")] veilid_capnp::question::detail::FindBlockQ(r) => { let op_reader = r.map_err(RPCError::protocol)?; let out = RPCOperationFindBlockQ::decode(&op_reader)?; @@ -176,9 +184,11 @@ impl RPCQuestionDetail { RPCQuestionDetail::WatchValueQ(d) => { d.encode(&mut builder.reborrow().init_watch_value_q()) } + #[cfg(feature = "unstable-blockstore")] RPCQuestionDetail::SupplyBlockQ(d) => { d.encode(&mut builder.reborrow().init_supply_block_q()) } + #[cfg(feature = "unstable-blockstore")] RPCQuestionDetail::FindBlockQ(d) => { d.encode(&mut builder.reborrow().init_find_block_q()) } diff --git a/veilid-core/src/rpc_processor/mod.rs b/veilid-core/src/rpc_processor/mod.rs index d76d31fe..ba24517e 100644 --- a/veilid-core/src/rpc_processor/mod.rs +++ b/veilid-core/src/rpc_processor/mod.rs @@ -5,7 +5,6 @@ mod operation_waiter; mod rpc_app_call; mod rpc_app_message; mod rpc_error; -mod rpc_find_block; mod rpc_find_node; mod rpc_get_value; mod rpc_return_receipt; @@ -13,11 +12,15 @@ mod rpc_route; mod rpc_set_value; mod rpc_signal; mod rpc_status; -mod rpc_supply_block; mod rpc_validate_dial_info; mod rpc_value_changed; mod rpc_watch_value; +#[cfg(feature = "unstable-blockstore")] +mod rpc_find_block; +#[cfg(feature = "unstable-blockstore")] +mod rpc_supply_block; + #[cfg(feature = "unstable-tunnels")] mod rpc_cancel_tunnel; #[cfg(feature = "unstable-tunnels")] @@ -1412,7 +1415,9 @@ impl RPCProcessor { RPCQuestionDetail::GetValueQ(_) => self.process_get_value_q(msg).await, RPCQuestionDetail::SetValueQ(_) => self.process_set_value_q(msg).await, RPCQuestionDetail::WatchValueQ(_) => self.process_watch_value_q(msg).await, + #[cfg(feature = "unstable-blockstore")] RPCQuestionDetail::SupplyBlockQ(_) => self.process_supply_block_q(msg).await, + #[cfg(feature = "unstable-blockstore")] RPCQuestionDetail::FindBlockQ(_) => self.process_find_block_q(msg).await, #[cfg(feature = "unstable-tunnels")] RPCQuestionDetail::StartTunnelQ(_) => self.process_start_tunnel_q(msg).await, diff --git a/veilid-core/src/storage_manager/mod.rs b/veilid-core/src/storage_manager/mod.rs index 96f772d2..42f30d94 100644 --- a/veilid-core/src/storage_manager/mod.rs +++ b/veilid-core/src/storage_manager/mod.rs @@ -30,6 +30,7 @@ struct StorageManagerUnlockedInner { crypto: Crypto, protected_store: ProtectedStore, table_store: TableStore, + #[cfg(feature = "unstable-blockstore")] block_store: BlockStore, // Background processes @@ -48,13 +49,14 @@ impl StorageManager { crypto: Crypto, protected_store: ProtectedStore, table_store: TableStore, - block_store: BlockStore, + #[cfg(feature = "unstable-blockstore")] block_store: BlockStore, ) -> StorageManagerUnlockedInner { StorageManagerUnlockedInner { config, crypto, protected_store, table_store, + #[cfg(feature = "unstable-blockstore")] block_store, flush_record_stores_task: TickTask::new(FLUSH_RECORD_STORES_INTERVAL_SECS), } @@ -68,13 +70,14 @@ impl StorageManager { crypto: Crypto, protected_store: ProtectedStore, table_store: TableStore, - block_store: BlockStore, + #[cfg(feature = "unstable-blockstore")] block_store: BlockStore, ) -> StorageManager { let unlocked_inner = Arc::new(Self::new_unlocked_inner( config, crypto, protected_store, table_store, + #[cfg(feature = "unstable-blockstore")] block_store, )); let this = StorageManager { diff --git a/veilid-core/src/veilid_api/api.rs b/veilid-core/src/veilid_api/api.rs index 59a9f2db..33d4ed91 100644 --- a/veilid-core/src/veilid_api/api.rs +++ b/veilid-core/src/veilid_api/api.rs @@ -70,6 +70,7 @@ impl VeilidAPI { } Err(VeilidAPIError::not_initialized()) } + #[cfg(feature = "unstable-blockstore")] pub fn block_store(&self) -> VeilidAPIResult { let inner = self.inner.lock(); if let Some(context) = &inner.context { diff --git a/veilid-core/src/veilid_api/json_api/crypto_system.rs b/veilid-core/src/veilid_api/json_api/crypto_system.rs index 73f08923..7f7e9aef 100644 --- a/veilid-core/src/veilid_api/json_api/crypto_system.rs +++ b/veilid-core/src/veilid_api/json_api/crypto_system.rs @@ -18,49 +18,210 @@ pub struct CryptoSystemResponse { #[serde(tag = "cs_op")] pub enum CryptoSystemRequestOp { Release, - CachedDh, - ComputeDh, - RandomBytes, + CachedDh { + #[schemars(with = "String")] + key: PublicKey, + #[schemars(with = "String")] + secret: SecretKey, + }, + ComputeDh { + #[schemars(with = "String")] + key: PublicKey, + #[schemars(with = "String")] + secret: SecretKey, + }, + RandomBytes { + len: u32, + }, DefaultSaltLength, - HashPassword, - VerifyPassword, - DeriveSharedSecret, + HashPassword { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + password: Vec, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + salt: Vec, + }, + VerifyPassword { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + password: Vec, + password_hash: String, + }, + DeriveSharedSecret { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + password: Vec, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + salt: Vec, + }, RandomNonce, RandomSharedSecret, GenerateKeyPair, - GenerateHash, - ValidateKeyPair, - ValidateHash, - Distance, - Sign, - Verify, + GenerateHash { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + data: Vec, + }, + ValidateKeyPair { + #[schemars(with = "String")] + key: PublicKey, + #[schemars(with = "String")] + secret: SecretKey, + }, + ValidateHash { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + data: Vec, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + hash_digest: Vec, + }, + Distance { + #[schemars(with = "String")] + key1: CryptoKey, + #[schemars(with = "String")] + key2: CryptoKey, + }, + Sign { + #[schemars(with = "String")] + key: PublicKey, + #[schemars(with = "String")] + secret: SecretKey, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + data: Vec, + }, + Verify { + #[schemars(with = "String")] + key: PublicKey, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + data: Vec, + #[schemars(with = "String")] + secret: Signature, + }, AeadOverhead, - DecryptAead, - EncryptAead, - CryptNoAuth, + DecryptAead { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + body: Vec, + #[schemars(with = "String")] + nonce: Nonce, + #[schemars(with = "String")] + shared_secret: SharedSecret, + #[serde(with = "opt_json_as_base64")] + #[schemars(with = "Option")] + associated_data: Option>, + }, + EncryptAead { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + body: Vec, + #[schemars(with = "String")] + nonce: Nonce, + #[schemars(with = "String")] + shared_secret: SharedSecret, + #[serde(with = "opt_json_as_base64")] + #[schemars(with = "Option")] + associated_data: Option>, + }, + CryptNoAuth { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + body: Vec, + #[schemars(with = "String")] + nonce: Nonce, + #[schemars(with = "String")] + shared_secret: SharedSecret, + }, } #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(tag = "cs_op")] pub enum CryptoSystemResponseOp { Release, - CachedDh, - ComputeDh, - RandomBytes, - DefaultSaltLength, - HashPassword, - VerifyPassword, - DeriveSharedSecret, - RandomNonce, - RandomSharedSecret, - GenerateKeyPair, - GenerateHash, - ValidateKeyPair, - ValidateHash, - Distance, - Sign, - Verify, - AeadOverhead, - DecryptAead, - EncryptAead, - CryptNoAuth, + CachedDh { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithString, + }, + ComputeDh { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithString, + }, + RandomBytes { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithVecU8, + }, + DefaultSaltLength { + value: u32, + }, + HashPassword { + #[serde(flatten)] + result: ApiResult, + }, + VerifyPassword { + #[serde(flatten)] + result: ApiResult, + }, + DeriveSharedSecret { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithString, + }, + RandomNonce { + #[schemars(with = "String")] + value: Nonce, + }, + RandomSharedSecret { + #[schemars(with = "String")] + value: SharedSecret, + }, + GenerateKeyPair { + #[schemars(with = "String")] + value: KeyPair, + }, + GenerateHash { + #[schemars(with = "String")] + value: HashDigest, + }, + ValidateKeyPair { + value: bool, + }, + ValidateHash { + value: bool, + }, + Distance { + #[schemars(with = "String")] + value: CryptoKeyDistance, + }, + Sign { + #[schemars(with = "String")] + value: Signature, + }, + Verify { + #[serde(flatten)] + result: ApiResult<()>, + }, + AeadOverhead { + value: u32, + }, + DecryptAead { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithVecU8, + }, + EncryptAead { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithVecU8, + }, + CryptNoAuth { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithVecU8, + }, } diff --git a/veilid-core/src/veilid_api/json_api/mod.rs b/veilid-core/src/veilid_api/json_api/mod.rs index 5fb4b07b..9fab5eae 100644 --- a/veilid-core/src/veilid_api/json_api/mod.rs +++ b/veilid-core/src/veilid_api/json_api/mod.rs @@ -11,7 +11,8 @@ pub use crypto_system::*; #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct Request { - /// Operation Id (pairs with Response) + /// Operation Id (pairs with Response, or empty if unidirectional) + #[serde(default)] id: String, /// The request operation variant #[serde(flatten)] @@ -20,7 +21,8 @@ pub struct Request { #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct Response { - /// Operation Id (pairs with Request) + /// Operation Id (pairs with Request, or empty if unidirectional) + #[serde(default)] id: String, /// The response operation variant #[serde(flatten)] @@ -36,17 +38,14 @@ pub enum RequestOp { NewPrivateRoute, NewCustomPrivateRoute { #[schemars(with = "Vec")] - crypto_kinds: Vec, + kinds: Vec, #[serde(default)] stability: Stability, #[serde(default)] sequencing: Sequencing, }, ImportRemotePrivateRoute { - #[serde( - serialize_with = "json_as_base64::serialize", - deserialize_with = "json_as_base64::deserialize" - )] + #[serde(with = "json_as_base64")] #[schemars(with = "String")] blob: Vec, }, @@ -57,10 +56,7 @@ pub enum RequestOp { AppCallReply { #[schemars(with = "String")] call_id: OperationId, - #[serde( - serialize_with = "json_as_base64::serialize", - deserialize_with = "json_as_base64::deserialize" - )] + #[serde(with = "json_as_base64")] #[schemars(with = "String")] message: Vec, }, @@ -79,27 +75,21 @@ pub enum RequestOp { // Crypto GetCryptoSystem { #[schemars(with = "String")] - crypto_kind: CryptoKind, + kind: CryptoKind, }, BestCryptoSystem, CryptoSystem(CryptoSystemRequest), VerifySignatures { #[schemars(with = "Vec")] node_ids: Vec, - #[serde( - serialize_with = "json_as_base64::serialize", - deserialize_with = "json_as_base64::deserialize" - )] + #[serde(with = "json_as_base64")] #[schemars(with = "String")] data: Vec, #[schemars(with = "Vec")] signatures: Vec, }, GenerateSignatures { - #[serde( - serialize_with = "json_as_base64::serialize", - deserialize_with = "json_as_base64::deserialize" - )] + #[serde(with = "json_as_base64")] #[schemars(with = "String")] data: Vec, #[schemars(with = "Vec")] @@ -107,7 +97,7 @@ pub enum RequestOp { }, GenerateKeyPair { #[schemars(with = "String")] - crypto_kind: CryptoKind, + kind: CryptoKind, }, // Misc Now, @@ -122,10 +112,7 @@ pub enum RequestOp { pub struct NewPrivateRouteResult { #[schemars(with = "String")] route_id: RouteId, - #[serde( - serialize_with = "json_as_base64::serialize", - deserialize_with = "json_as_base64::deserialize" - )] + #[serde(with = "json_as_base64")] #[schemars(with = "String")] blob: Vec, } @@ -133,6 +120,9 @@ pub struct NewPrivateRouteResult { #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(tag = "op")] pub enum ResponseOp { + Update { + value: VeilidUpdate, + }, GetState { #[serde(flatten)] result: ApiResult, @@ -153,28 +143,74 @@ pub enum ResponseOp { #[serde(flatten)] result: ApiResult, }, - ImportRemotePrivateRoute, - ReleasePrivateRoute, - AppCallReply, + ImportRemotePrivateRoute { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithString, + }, + ReleasePrivateRoute { + #[serde(flatten)] + result: ApiResult<()>, + }, + AppCallReply { + #[serde(flatten)] + result: ApiResult<()>, + }, // Routing Context - NewRoutingContext, + NewRoutingContext { + value: String, + }, RoutingContext(RoutingContextResponse), // TableDb - OpenTableDb, - DeleteTableDb, + OpenTableDb { + #[serde(flatten)] + result: ApiResult, + }, + DeleteTableDb { + #[serde(flatten)] + result: ApiResult, + }, TableDb(TableDbResponse), // Crypto - GetCryptoSystem, - BestCryptoSystem, + GetCryptoSystem { + #[serde(flatten)] + result: ApiResult, + }, + BestCryptoSystem { + value: String, + }, CryptoSystem(CryptoSystemResponse), - VerifySignatures, - GenerateSignatures, - GenerateKeyPair, + VerifySignatures { + #[serde(flatten)] + #[schemars(with = "ApiResult>")] + result: ApiResultWithVecString, + }, + GenerateSignatures { + #[serde(flatten)] + #[schemars(with = "ApiResult>")] + result: ApiResultWithVecString, + }, + GenerateKeyPair { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithString, + }, // Misc - Now, - Debug, - VeilidVersionString, - VeilidVersion, + Now { + #[schemars(with = "String")] + value: Timestamp, + }, + Debug { + value: String, + }, + VeilidVersionString { + value: String, + }, + VeilidVersion { + major: u32, + minor: u32, + patch: u32, + }, } #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] @@ -187,6 +223,49 @@ where Err { error: VeilidAPIError }, } +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[serde(untagged)] +pub enum ApiResultWithString +where + T: Clone + fmt::Debug, +{ + Ok { + #[schemars(with = "String")] + value: T, + }, + Err { + error: VeilidAPIError, + }, +} + +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[serde(untagged)] +pub enum ApiResultWithVecU8 { + Ok { + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + value: Vec, + }, + Err { + error: VeilidAPIError, + }, +} + +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +#[serde(untagged)] +pub enum ApiResultWithVecString +where + T: Clone + fmt::Debug, +{ + Ok { + #[schemars(with = "Vec")] + value: T, + }, + Err { + error: VeilidAPIError, + }, +} + pub fn emit_schemas(out: &mut HashMap) { let schema_request = schema_for!(Request); let schema_response = schema_for!(Response); diff --git a/veilid-core/src/veilid_api/json_api/routing_context.rs b/veilid-core/src/veilid_api/json_api/routing_context.rs index c4a9405c..9376d81f 100644 --- a/veilid-core/src/veilid_api/json_api/routing_context.rs +++ b/veilid-core/src/veilid_api/json_api/routing_context.rs @@ -19,34 +19,122 @@ pub struct RoutingContextResponse { pub enum RoutingContextRequestOp { Release, WithPrivacy, - WithCustomPrivacy, - WithSequencing, - AppCall, - AppMessage, - CreateDhtRecord, - OpenDhtRecord, - CloseDhtRecord, - DeleteDhtRecord, - GetDhtValue, - SetDhtValue, - WatchDhtValues, - CancelDhtWatch, + WithCustomPrivacy { + stability: Stability, + }, + WithSequencing { + sequencing: Sequencing, + }, + AppCall { + target: String, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + request: Vec, + }, + AppMessage { + target: String, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + message: Vec, + }, + CreateDhtRecord { + #[schemars(with = "String")] + kind: CryptoKind, + schema: DHTSchema, + }, + OpenDhtRecord { + #[schemars(with = "String")] + key: TypedKey, + #[schemars(with = "Option")] + writer: Option, + }, + CloseDhtRecord { + #[schemars(with = "String")] + key: TypedKey, + }, + DeleteDhtRecord { + #[schemars(with = "String")] + key: TypedKey, + }, + GetDhtValue { + #[schemars(with = "String")] + key: TypedKey, + subkey: ValueSubkey, + force_refresh: bool, + }, + SetDhtValue { + #[schemars(with = "String")] + key: TypedKey, + subkey: ValueSubkey, + #[serde(with = "json_as_base64")] + #[schemars(with = "String")] + data: Vec, + }, + WatchDhtValues { + #[schemars(with = "String")] + key: TypedKey, + subkeys: ValueSubkeyRangeSet, + expiration: Timestamp, + count: u32, + }, + CancelDhtWatch { + #[schemars(with = "String")] + key: TypedKey, + subkeys: ValueSubkeyRangeSet, + }, } #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(tag = "rc_op")] pub enum RoutingContextResponseOp { Release, - WithPrivacy, - WithCustomPrivacy, - WithSequencing, - AppCall, - AppMessage, - CreateDhtRecord, - OpenDhtRecord, - CloseDhtRecord, - DeleteDhtRecord, - GetDhtValue, - SetDhtValue, - WatchDhtValues, - CancelDhtWatch, + WithPrivacy { + value: String, + }, + WithCustomPrivacy { + value: String, + }, + WithSequencing { + value: String, + }, + AppCall { + #[serde(flatten)] + #[schemars(with = "ApiResult")] + result: ApiResultWithVecU8, + }, + AppMessage { + #[serde(flatten)] + result: ApiResult<()>, + }, + CreateDhtRecord { + #[serde(flatten)] + result: ApiResult, + }, + OpenDhtRecord { + #[serde(flatten)] + result: ApiResult, + }, + CloseDhtRecord { + #[serde(flatten)] + result: ApiResult<()>, + }, + DeleteDhtRecord { + #[serde(flatten)] + result: ApiResult<()>, + }, + GetDhtValue { + #[serde(flatten)] + result: ApiResult>, + }, + SetDhtValue { + #[serde(flatten)] + result: ApiResult>, + }, + WatchDhtValues { + #[serde(flatten)] + result: ApiResult, + }, + CancelDhtWatch { + #[serde(flatten)] + result: ApiResult, + }, } diff --git a/veilid-core/src/veilid_api/mod.rs b/veilid-core/src/veilid_api/mod.rs index 50afee6f..9f05e56d 100644 --- a/veilid-core/src/veilid_api/mod.rs +++ b/veilid-core/src/veilid_api/mod.rs @@ -22,6 +22,7 @@ pub use alloc::string::ToString; pub use attachment_manager::AttachmentManager; pub use core::str::FromStr; pub use crypto::*; +#[cfg(feature = "unstable-blockstore")] pub use intf::BlockStore; pub use intf::ProtectedStore; pub use network_manager::NetworkManager; diff --git a/veilid-core/src/veilid_api/routing_context.rs b/veilid-core/src/veilid_api/routing_context.rs index 952a42b7..e73e54ca 100644 --- a/veilid-core/src/veilid_api/routing_context.rs +++ b/veilid-core/src/veilid_api/routing_context.rs @@ -290,10 +290,12 @@ impl RoutingContext { /////////////////////////////////// /// Block Store + #[cfg(feature = "unstable-blockstore")] pub async fn find_block(&self, _block_id: PublicKey) -> VeilidAPIResult> { panic!("unimplemented"); } + #[cfg(feature = "unstable-blockstore")] pub async fn supply_block(&self, _block_id: PublicKey) -> VeilidAPIResult { panic!("unimplemented"); } diff --git a/veilid-core/src/veilid_api/serialize_helpers/serialize_json.rs b/veilid-core/src/veilid_api/serialize_helpers/serialize_json.rs index 5e98624c..afdab167 100644 --- a/veilid-core/src/veilid_api/serialize_helpers/serialize_json.rs +++ b/veilid-core/src/veilid_api/serialize_helpers/serialize_json.rs @@ -55,6 +55,27 @@ pub mod json_as_base64 { } } +pub mod opt_json_as_base64 { + use data_encoding::BASE64URL_NOPAD; + use serde::{Deserialize, Deserializer, Serialize, Serializer}; + + pub fn serialize(v: &Option>, s: S) -> Result { + let base64 = v.as_ref().map(|x| BASE64URL_NOPAD.encode(&x)); + Option::::serialize(&base64, s) + } + + pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result>, D::Error> { + let base64 = Option::::deserialize(d)?; + base64 + .map(|x| { + BASE64URL_NOPAD + .decode(x.as_bytes()) + .map_err(|e| serde::de::Error::custom(e)) + }) + .transpose() + } +} + pub mod json_as_string { use std::fmt::Display; use std::str::FromStr; diff --git a/veilid-core/src/veilid_api/types/aligned_u64.rs b/veilid-core/src/veilid_api/types/aligned_u64.rs index 11ee022d..566bac84 100644 --- a/veilid-core/src/veilid_api/types/aligned_u64.rs +++ b/veilid-core/src/veilid_api/types/aligned_u64.rs @@ -24,10 +24,7 @@ use super::*; #[archive_attr(repr(C, align(8)), derive(CheckBytes))] #[serde(transparent)] pub struct AlignedU64( - #[serde( - serialize_with = "json_as_string::serialize", - deserialize_with = "json_as_string::deserialize" - )] + #[serde(with = "json_as_string")] #[schemars(with = "String")] u64, ); diff --git a/veilid-core/src/veilid_api/types/app_message_call.rs b/veilid-core/src/veilid_api/types/app_message_call.rs index 2f3c77be..fb33ae50 100644 --- a/veilid-core/src/veilid_api/types/app_message_call.rs +++ b/veilid-core/src/veilid_api/types/app_message_call.rs @@ -16,18 +16,12 @@ use super::*; #[archive_attr(repr(C), derive(CheckBytes))] pub struct VeilidAppMessage { /// Some(sender) if the message was sent directly, None if received via a private/safety route - #[serde( - serialize_with = "opt_json_as_string::serialize", - deserialize_with = "opt_json_as_string::deserialize" - )] + #[serde(with = "opt_json_as_string")] #[schemars(with = "Option")] sender: Option, /// The content of the message to deliver to the application - #[serde( - serialize_with = "json_as_base64::serialize", - deserialize_with = "json_as_base64::deserialize" - )] + #[serde(with = "json_as_base64")] #[schemars(with = "String")] message: Vec, } @@ -61,26 +55,17 @@ impl VeilidAppMessage { #[archive_attr(repr(C), derive(CheckBytes))] pub struct VeilidAppCall { /// Some(sender) if the request was sent directly, None if received via a private/safety route - #[serde( - serialize_with = "opt_json_as_string::serialize", - deserialize_with = "opt_json_as_string::deserialize" - )] + #[serde(with = "opt_json_as_string")] #[schemars(with = "Option")] sender: Option, /// The content of the request to deliver to the application - #[serde( - serialize_with = "json_as_base64::serialize", - deserialize_with = "json_as_base64::deserialize" - )] + #[serde(with = "json_as_base64")] #[schemars(with = "String")] message: Vec, /// The id to reply to - #[serde( - serialize_with = "json_as_string::serialize", - deserialize_with = "json_as_string::deserialize" - )] + #[serde(with = "json_as_string")] #[schemars(with = "String")] id: OperationId, } diff --git a/veilid-core/src/veilid_api/types/dht/value_data.rs b/veilid-core/src/veilid_api/types/dht/value_data.rs index eae2e2c4..c57fc2e5 100644 --- a/veilid-core/src/veilid_api/types/dht/value_data.rs +++ b/veilid-core/src/veilid_api/types/dht/value_data.rs @@ -21,10 +21,7 @@ pub struct ValueData { seq: ValueSeqNum, /// The contents of a DHT Record - #[serde( - serialize_with = "json_as_base64::serialize", - deserialize_with = "json_as_base64::deserialize" - )] + #[serde(with = "json_as_base64")] #[schemars(with = "String")] data: Vec, diff --git a/veilid-core/src/veilid_api/types/dht/value_subkey_range_set.rs b/veilid-core/src/veilid_api/types/dht/value_subkey_range_set.rs index 955c9d01..567081f6 100644 --- a/veilid-core/src/veilid_api/types/dht/value_subkey_range_set.rs +++ b/veilid-core/src/veilid_api/types/dht/value_subkey_range_set.rs @@ -18,12 +18,10 @@ use range_set_blaze::*; JsonSchema, )] #[archive_attr(repr(C), derive(CheckBytes))] +#[serde(transparent)] pub struct ValueSubkeyRangeSet { #[with(RkyvRangeSetBlaze)] - #[serde( - serialize_with = "serialize_range_set_blaze::serialize", - deserialize_with = "serialize_range_set_blaze::deserialize" - )] + #[serde(with = "serialize_range_set_blaze")] #[schemars(with = "Vec<(u32,u32)>")] data: RangeSetBlaze, }