From 19f384ab3379efcfc608a580ca2071b53a9f4765 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sat, 8 Jul 2023 22:50:44 -0400 Subject: [PATCH] refactor create dht value --- veilid-core/src/veilid_api/debug.rs | 24 ++++++++++--------- .../src/veilid_api/json_api/process.rs | 4 ++-- .../veilid_api/json_api/routing_context.rs | 4 ++-- veilid-core/src/veilid_api/routing_context.rs | 3 ++- veilid-flutter/lib/routing_context.dart | 4 ++-- veilid-flutter/lib/veilid_ffi.dart | 10 ++++---- veilid-flutter/lib/veilid_js.dart | 6 ++--- veilid-flutter/rust/src/dart_ffi.rs | 10 +++++--- veilid-python/tests/test_dht.py | 10 ++++---- veilid-python/veilid/api.py | 2 +- veilid-python/veilid/json_api.py | 2 +- veilid-python/veilid/schema/Request.json | 6 +++-- veilid-wasm/src/lib.rs | 8 +++++-- 13 files changed, 53 insertions(+), 40 deletions(-) diff --git a/veilid-core/src/veilid_api/debug.rs b/veilid-core/src/veilid_api/debug.rs index ebe633d5..eacc3d85 100644 --- a/veilid-core/src/veilid_api/debug.rs +++ b/veilid-core/src/veilid_api/debug.rs @@ -1068,22 +1068,24 @@ impl VeilidAPI { let routing_table = netman.routing_table(); let crypto = self.crypto()?; - let csv = get_debug_argument_at( - &args, - 1, - "debug_record_create", - "kind", - get_crypto_system_version(crypto.clone()), - ) - .unwrap_or_else(|_| crypto.best()); let schema = get_debug_argument_at( &args, - 2, + 1, "debug_record_create", "dht_schema", get_dht_schema, ) .unwrap_or_else(|_| DHTSchema::dflt(1)); + + let csv = get_debug_argument_at( + &args, + 2, + "debug_record_create", + "kind", + get_crypto_system_version(crypto.clone()), + ) + .unwrap_or_else(|_| crypto.best()); + let ss = get_debug_argument_at( &args, 3, @@ -1106,7 +1108,7 @@ impl VeilidAPI { }; // Do a record get - let record = match rc.create_dht_record(csv.kind(), schema).await { + let record = match rc.create_dht_record(schema, Some(csv.kind())).await { Err(e) => return Ok(format!("Can't open DHT record: {}", e)), Ok(v) => v, }; @@ -1348,7 +1350,7 @@ route allocate [ord|*ord] [rel] [] [in|out] test record list purge [bytes] - create + create [ []] set [+] get [+] [force] delete diff --git a/veilid-core/src/veilid_api/json_api/process.rs b/veilid-core/src/veilid_api/json_api/process.rs index 018dc276..8e1264df 100644 --- a/veilid-core/src/veilid_api/json_api/process.rs +++ b/veilid-core/src/veilid_api/json_api/process.rs @@ -277,10 +277,10 @@ impl JsonRequestProcessor { ), } } - RoutingContextRequestOp::CreateDhtRecord { kind, schema } => { + RoutingContextRequestOp::CreateDhtRecord { schema, kind } => { RoutingContextResponseOp::CreateDhtRecord { result: to_json_api_result( - routing_context.create_dht_record(kind, schema).await, + routing_context.create_dht_record(schema, kind).await, ), } } 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 8ecfff32..d589bdbc 100644 --- a/veilid-core/src/veilid_api/json_api/routing_context.rs +++ b/veilid-core/src/veilid_api/json_api/routing_context.rs @@ -38,9 +38,9 @@ pub enum RoutingContextRequestOp { message: Vec, }, CreateDhtRecord { - #[schemars(with = "String")] - kind: CryptoKind, schema: DHTSchema, + #[schemars(with = "Option")] + kind: Option, }, OpenDhtRecord { #[schemars(with = "String")] diff --git a/veilid-core/src/veilid_api/routing_context.rs b/veilid-core/src/veilid_api/routing_context.rs index 47b30271..16afc60a 100644 --- a/veilid-core/src/veilid_api/routing_context.rs +++ b/veilid-core/src/veilid_api/routing_context.rs @@ -195,9 +195,10 @@ impl RoutingContext { /// Returns the newly allocated DHT record's key if successful. The records is considered 'open' after the create operation succeeds. pub async fn create_dht_record( &self, - kind: CryptoKind, schema: DHTSchema, + kind: Option, ) -> VeilidAPIResult { + let kind = kind.unwrap_or(best_crypto_kind()); let storage_manager = self.api.storage_manager()?; storage_manager .create_record(kind, schema, self.unlocked_inner.safety_selection) diff --git a/veilid-flutter/lib/routing_context.dart b/veilid-flutter/lib/routing_context.dart index 43a4c16f..fdd7fd1e 100644 --- a/veilid-flutter/lib/routing_context.dart +++ b/veilid-flutter/lib/routing_context.dart @@ -234,8 +234,8 @@ abstract class VeilidRoutingContext { Future appMessage(String target, Uint8List message); // DHT Operations - Future createDHTRecord( - CryptoKind kind, DHTSchema schema); + Future createDHTRecord(DHTSchema schema, + {CryptoKind kind = 0}); Future openDHTRecord(TypedKey key, KeyPair? writer); Future closeDHTRecord(TypedKey key); Future deleteDHTRecord(TypedKey key); diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index f544f8dc..f3fe37c7 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -78,9 +78,9 @@ typedef _RoutingContextAppMessageDart = void Function( int, int, Pointer, Pointer); // fn routing_context_create_dht_record(port: i64, id: u32, kind: u32, schema: FfiStr) typedef _RoutingContextCreateDHTRecordC = Void Function( - Int64, Uint32, Uint32, Pointer); + Int64, Uint32, Pointer, Uint32); typedef _RoutingContextCreateDHTRecordDart = void Function( - int, int, int, Pointer); + int, int, Pointer, int); // fn routing_context_open_dht_record(port: i64, id: u32, key: FfiStr, writer: FfiStr) typedef _RoutingContextOpenDHTRecordC = Void Function( Int64, Uint32, Pointer, Pointer); @@ -634,13 +634,13 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext { } @override - Future createDHTRecord( - CryptoKind kind, DHTSchema schema) async { + Future createDHTRecord(DHTSchema schema, + {CryptoKind kind = 0}) async { final nativeSchema = jsonEncode(schema).toNativeUtf8(); final recvPort = ReceivePort("routing_context_create_dht_record"); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextCreateDHTRecord( - sendPort.nativePort, _ctx.id, kind, nativeSchema); + sendPort.nativePort, _ctx.id, nativeSchema, kind); final dhtRecordDescriptor = await processFutureJson(DHTRecordDescriptor.fromJson, recvPort.first); return dhtRecordDescriptor; diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index 32bde035..c9f1ebf3 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -78,11 +78,11 @@ class VeilidRoutingContextJS implements VeilidRoutingContext { } @override - Future createDHTRecord( - CryptoKind kind, DHTSchema schema) async { + Future createDHTRecord(DHTSchema schema, + {CryptoKind kind = 0}) async { return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util .callMethod(wasm, "routing_context_create_dht_record", - [_ctx.id, kind, jsonEncode(schema)])))); + [_ctx.id, jsonEncode(schema), kind])))); } @override diff --git a/veilid-flutter/rust/src/dart_ffi.rs b/veilid-flutter/rust/src/dart_ffi.rs index 79419af3..c86e9fb5 100644 --- a/veilid-flutter/rust/src/dart_ffi.rs +++ b/veilid-flutter/rust/src/dart_ffi.rs @@ -492,8 +492,12 @@ pub extern "C" fn routing_context_app_message(port: i64, id: u32, target: FfiStr } #[no_mangle] -pub extern "C" fn routing_context_create_dht_record(port: i64, id: u32, kind: u32, schema: FfiStr) { - let crypto_kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind); +pub extern "C" fn routing_context_create_dht_record(port: i64, id: u32, schema: FfiStr, kind: u32) { + let crypto_kind = if kind == 0 { + None + } else { + Some(veilid_core::FourCC::from(kind)) + }; let schema: veilid_core::DHTSchema = veilid_core::deserialize_opt_json(schema.into_opt_string()).unwrap(); DartIsolateWrapper::new(port).spawn_result_json(async move { @@ -505,7 +509,7 @@ pub extern "C" fn routing_context_create_dht_record(port: i64, id: u32, kind: u3 routing_context.clone() }; - let dht_record_descriptor = routing_context.create_dht_record(crypto_kind, schema).await?; + let dht_record_descriptor = routing_context.create_dht_record(schema, crypto_kind).await?; APIResult::Ok(dht_record_descriptor) }); } diff --git a/veilid-python/tests/test_dht.py b/veilid-python/tests/test_dht.py index 79dc9bd1..8faae302 100644 --- a/veilid-python/tests/test_dht.py +++ b/veilid-python/tests/test_dht.py @@ -42,7 +42,7 @@ async def test_delete_dht_record_nonexistent(api_connection: veilid.VeilidAPI): async def test_create_delete_dht_record_simple(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() async with rc: - rec = await rc.create_dht_record(veilid.CryptoKind.CRYPTO_KIND_VLD0, veilid.DHTSchema.dflt(1)) + rec = await rc.create_dht_record(veilid.DHTSchema.dflt(1), veilid.CryptoKind.CRYPTO_KIND_VLD0) await rc.close_dht_record(rec.key) await rc.delete_dht_record(rec.key) @@ -50,7 +50,7 @@ async def test_create_delete_dht_record_simple(api_connection: veilid.VeilidAPI) async def test_get_dht_value_nonexistent(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() async with rc: - rec = await rc.create_dht_record(veilid.CryptoKind.CRYPTO_KIND_VLD0, veilid.DHTSchema.dflt(1)) + rec = await rc.create_dht_record(veilid.DHTSchema.dflt(1)) assert await rc.get_dht_value(rec.key, 0, False) == None await rc.close_dht_record(rec.key) await rc.delete_dht_record(rec.key) @@ -59,7 +59,7 @@ async def test_get_dht_value_nonexistent(api_connection: veilid.VeilidAPI): async def test_set_get_dht_value(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() async with rc: - rec = await rc.create_dht_record(veilid.CryptoKind.CRYPTO_KIND_VLD0, veilid.DHTSchema.dflt(2)) + rec = await rc.create_dht_record(veilid.DHTSchema.dflt(2)) vd = await rc.set_dht_value(rec.key, 0, b"BLAH BLAH BLAH") assert vd != None @@ -87,13 +87,13 @@ async def test_set_get_dht_value(api_connection: veilid.VeilidAPI): async def test_open_writer_dht_value(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() async with rc: - rec = await rc.create_dht_record(veilid.CryptoKind.CRYPTO_KIND_VLD0, veilid.DHTSchema.dflt(2)) + rec = await rc.create_dht_record(veilid.DHTSchema.dflt(2)) key = rec.key owner = rec.owner secret = rec.owner_secret print(f"key:{key}") - cs = await api_connection.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0) + cs = await api_connection.get_crypto_system(rec.key.kind()) async with cs: assert await cs.validate_key_pair(owner, secret) other_keypair = await cs.generate_key_pair() diff --git a/veilid-python/veilid/api.py b/veilid-python/veilid/api.py index 4a17804f..b8d824cf 100644 --- a/veilid-python/veilid/api.py +++ b/veilid-python/veilid/api.py @@ -46,7 +46,7 @@ class RoutingContext(ABC): @abstractmethod async def create_dht_record( - self, kind: types.CryptoKind, schema: types.DHTSchema + self, schema: types.DHTSchema, kind: Optional[types.CryptoKind] = None ) -> types.DHTRecordDescriptor: pass diff --git a/veilid-python/veilid/json_api.py b/veilid-python/veilid/json_api.py index 8138cf57..6b1dd72f 100644 --- a/veilid-python/veilid/json_api.py +++ b/veilid-python/veilid/json_api.py @@ -514,7 +514,7 @@ class _JsonRoutingContext(RoutingContext): ) async def create_dht_record( - self, kind: CryptoKind, schema: DHTSchema + self, schema: DHTSchema, kind: Optional[CryptoKind] = None ) -> DHTRecordDescriptor: return DHTRecordDescriptor.from_json( raise_api_result( diff --git a/veilid-python/veilid/schema/Request.json b/veilid-python/veilid/schema/Request.json index d087d1cc..9d78ceac 100644 --- a/veilid-python/veilid/schema/Request.json +++ b/veilid-python/veilid/schema/Request.json @@ -303,13 +303,15 @@ { "type": "object", "required": [ - "kind", "rc_op", "schema" ], "properties": { "kind": { - "type": "string" + "type": [ + "string", + "null" + ] }, "rc_op": { "type": "string", diff --git a/veilid-wasm/src/lib.rs b/veilid-wasm/src/lib.rs index 80455917..48e4f7d0 100644 --- a/veilid-wasm/src/lib.rs +++ b/veilid-wasm/src/lib.rs @@ -425,8 +425,12 @@ pub fn routing_context_app_message(id: u32, target: String, message: String) -> } #[wasm_bindgen()] -pub fn routing_context_create_dht_record(id: u32, kind: u32, schema: String) -> Promise { - let crypto_kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind); +pub fn routing_context_create_dht_record(id: u32, schema: String, kind: u32) -> Promise { + let crypto_kind = if kind == 0 { + None + } else { + Some(veilid_core::FourCC::from(kind)) + }; let schema: veilid_core::DHTSchema = veilid_core::deserialize_json(&schema).unwrap(); wrap_api_future_json(async move {