refactor create dht value

This commit is contained in:
Christien Rioux 2023-07-08 22:50:44 -04:00
parent 9f9feeb325
commit 19f384ab33
13 changed files with 53 additions and 40 deletions

View File

@ -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] [<count>] [in|out]
test <route>
record list <local|remote>
purge <local|remote> [bytes]
create <cryptokind> <dhtschema> <safety>
create <dhtschema> [<cryptokind> [<safety>]]
set <key>[+<safety>] <subkey> <writer> <data>
get <key>[+<safety>] <subkey> [force]
delete <key>

View File

@ -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,
),
}
}

View File

@ -38,9 +38,9 @@ pub enum RoutingContextRequestOp {
message: Vec<u8>,
},
CreateDhtRecord {
#[schemars(with = "String")]
kind: CryptoKind,
schema: DHTSchema,
#[schemars(with = "Option<String>")]
kind: Option<CryptoKind>,
},
OpenDhtRecord {
#[schemars(with = "String")]

View File

@ -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<CryptoKind>,
) -> VeilidAPIResult<DHTRecordDescriptor> {
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)

View File

@ -234,8 +234,8 @@ abstract class VeilidRoutingContext {
Future<void> appMessage(String target, Uint8List message);
// DHT Operations
Future<DHTRecordDescriptor> createDHTRecord(
CryptoKind kind, DHTSchema schema);
Future<DHTRecordDescriptor> createDHTRecord(DHTSchema schema,
{CryptoKind kind = 0});
Future<DHTRecordDescriptor> openDHTRecord(TypedKey key, KeyPair? writer);
Future<void> closeDHTRecord(TypedKey key);
Future<void> deleteDHTRecord(TypedKey key);

View File

@ -78,9 +78,9 @@ typedef _RoutingContextAppMessageDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>);
// fn routing_context_create_dht_record(port: i64, id: u32, kind: u32, schema: FfiStr)
typedef _RoutingContextCreateDHTRecordC = Void Function(
Int64, Uint32, Uint32, Pointer<Utf8>);
Int64, Uint32, Pointer<Utf8>, Uint32);
typedef _RoutingContextCreateDHTRecordDart = void Function(
int, int, int, Pointer<Utf8>);
int, int, Pointer<Utf8>, int);
// fn routing_context_open_dht_record(port: i64, id: u32, key: FfiStr, writer: FfiStr)
typedef _RoutingContextOpenDHTRecordC = Void Function(
Int64, Uint32, Pointer<Utf8>, Pointer<Utf8>);
@ -634,13 +634,13 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext {
}
@override
Future<DHTRecordDescriptor> createDHTRecord(
CryptoKind kind, DHTSchema schema) async {
Future<DHTRecordDescriptor> 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;

View File

@ -78,11 +78,11 @@ class VeilidRoutingContextJS implements VeilidRoutingContext {
}
@override
Future<DHTRecordDescriptor> createDHTRecord(
CryptoKind kind, DHTSchema schema) async {
Future<DHTRecordDescriptor> 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

View File

@ -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)
});
}

View File

@ -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()

View File

@ -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

View File

@ -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(

View File

@ -303,13 +303,15 @@
{
"type": "object",
"required": [
"kind",
"rc_op",
"schema"
],
"properties": {
"kind": {
"type": "string"
"type": [
"string",
"null"
]
},
"rc_op": {
"type": "string",

View File

@ -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 {