mirror of
https://gitlab.com/veilid/veilid.git
synced 2025-09-27 03:51:02 -04:00
safety by default
This commit is contained in:
parent
88389a1b78
commit
ee375ad430
22 changed files with 1406 additions and 1593 deletions
|
@ -239,9 +239,10 @@ abstract class VeilidRoutingContext {
|
|||
void close();
|
||||
|
||||
// Modifiers
|
||||
VeilidRoutingContext withPrivacy();
|
||||
VeilidRoutingContext withCustomPrivacy(SafetySelection safetySelection);
|
||||
VeilidRoutingContext withDefaultSafety();
|
||||
VeilidRoutingContext withSafety(SafetySelection safetySelection);
|
||||
VeilidRoutingContext withSequencing(Sequencing sequencing);
|
||||
Future<SafetySelection> safety();
|
||||
|
||||
// App call/message
|
||||
Future<Uint8List> appCall(String target, Uint8List request);
|
||||
|
|
|
@ -45,12 +45,15 @@ typedef _DetachDart = void Function(int);
|
|||
typedef _RoutingContextDart = void Function(int);
|
||||
// fn release_routing_context(id: u32)
|
||||
typedef _ReleaseRoutingContextDart = int Function(int);
|
||||
// fn routing_context_with_privacy(id: u32) -> u32
|
||||
typedef _RoutingContextWithPrivacyDart = int Function(int);
|
||||
// fn routing_context_with_custom_privacy(id: u32, stability: FfiStr)
|
||||
typedef _RoutingContextWithCustomPrivacyDart = int Function(int, Pointer<Utf8>);
|
||||
// fn routing_context_with_default_safety(id: u32) -> u32
|
||||
typedef _RoutingContextWithDefaultSafetyDart = int Function(int);
|
||||
// fn routing_context_with_safety(id: u32, stability: FfiStr)
|
||||
typedef _RoutingContextWithSafetyDart = int Function(int, Pointer<Utf8>);
|
||||
// fn routing_context_with_sequencing(id: u32, sequencing: FfiStr)
|
||||
typedef _RoutingContextWithSequencingDart = int Function(int, Pointer<Utf8>);
|
||||
// fn routing_context_safety(port: i64,
|
||||
// id: u32)
|
||||
typedef _RoutingContextSafetyDart = void Function(int, int);
|
||||
// fn routing_context_app_call(port: i64,
|
||||
// id: u32, target: FfiStr, request: FfiStr)
|
||||
typedef _RoutingContextAppCallDart = void Function(
|
||||
|
@ -525,16 +528,16 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext {
|
|||
}
|
||||
|
||||
@override
|
||||
VeilidRoutingContextFFI withPrivacy() {
|
||||
VeilidRoutingContextFFI withDefaultSafety() {
|
||||
_ctx.ensureValid();
|
||||
final newId = _ctx.ffi._routingContextWithPrivacy(_ctx.id!);
|
||||
final newId = _ctx.ffi._routingContextWithDefaultSafety(_ctx.id!);
|
||||
return VeilidRoutingContextFFI._(_Ctx(newId, _ctx.ffi));
|
||||
}
|
||||
|
||||
@override
|
||||
VeilidRoutingContextFFI withCustomPrivacy(SafetySelection safetySelection) {
|
||||
VeilidRoutingContextFFI withSafety(SafetySelection safetySelection) {
|
||||
_ctx.ensureValid();
|
||||
final newId = _ctx.ffi._routingContextWithCustomPrivacy(
|
||||
final newId = _ctx.ffi._routingContextWithSafety(
|
||||
_ctx.id!, jsonEncode(safetySelection).toNativeUtf8());
|
||||
return VeilidRoutingContextFFI._(_Ctx(newId, _ctx.ffi));
|
||||
}
|
||||
|
@ -547,6 +550,17 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext {
|
|||
return VeilidRoutingContextFFI._(_Ctx(newId, _ctx.ffi));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<SafetySelection> safety() async {
|
||||
_ctx.ensureValid();
|
||||
final recvPort = ReceivePort('routing_context_safety');
|
||||
final sendPort = recvPort.sendPort;
|
||||
_ctx.ffi._routingContextSafety(sendPort.nativePort, _ctx.id!);
|
||||
final out = await processFutureJson<SafetySelection>(
|
||||
SafetySelection.fromJson, recvPort.first);
|
||||
return out;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> appCall(String target, Uint8List request) async {
|
||||
_ctx.ensureValid();
|
||||
|
@ -1175,17 +1189,20 @@ class VeilidFFI extends Veilid {
|
|||
'routing_context'),
|
||||
_releaseRoutingContext = dylib.lookupFunction<Int32 Function(Uint32),
|
||||
_ReleaseRoutingContextDart>('release_routing_context'),
|
||||
_routingContextWithPrivacy = dylib.lookupFunction<
|
||||
Uint32 Function(Uint32),
|
||||
_RoutingContextWithPrivacyDart>('routing_context_with_privacy'),
|
||||
_routingContextWithCustomPrivacy = dylib.lookupFunction<
|
||||
_routingContextWithDefaultSafety = dylib.lookupFunction<
|
||||
Uint32 Function(Uint32), _RoutingContextWithDefaultSafetyDart>(
|
||||
'routing_context_with_default_safety'),
|
||||
_routingContextWithSafety = dylib.lookupFunction<
|
||||
Uint32 Function(Uint32, Pointer<Utf8>),
|
||||
_RoutingContextWithCustomPrivacyDart>(
|
||||
_RoutingContextWithSafetyDart>(
|
||||
'routing_context_with_custom_privacy'),
|
||||
_routingContextWithSequencing = dylib.lookupFunction<
|
||||
Uint32 Function(Uint32, Pointer<Utf8>),
|
||||
_RoutingContextWithSequencingDart>(
|
||||
'routing_context_with_sequencing'),
|
||||
_routingContextSafety = dylib.lookupFunction<
|
||||
Void Function(Int64, Uint32),
|
||||
_RoutingContextSafetyDart>('routing_context_safety'),
|
||||
_routingContextAppCall = dylib.lookupFunction<
|
||||
Void Function(Int64, Uint32, Pointer<Utf8>, Pointer<Utf8>),
|
||||
_RoutingContextAppCallDart>('routing_context_app_call'),
|
||||
|
@ -1383,9 +1400,10 @@ class VeilidFFI extends Veilid {
|
|||
|
||||
final _RoutingContextDart _routingContext;
|
||||
final _ReleaseRoutingContextDart _releaseRoutingContext;
|
||||
final _RoutingContextWithPrivacyDart _routingContextWithPrivacy;
|
||||
final _RoutingContextWithCustomPrivacyDart _routingContextWithCustomPrivacy;
|
||||
final _RoutingContextWithDefaultSafetyDart _routingContextWithDefaultSafety;
|
||||
final _RoutingContextWithSafetyDart _routingContextWithSafety;
|
||||
final _RoutingContextWithSequencingDart _routingContextWithSequencing;
|
||||
final _RoutingContextSafetyDart _routingContextSafety;
|
||||
final _RoutingContextAppCallDart _routingContextAppCall;
|
||||
final _RoutingContextAppMessageDart _routingContextAppMessage;
|
||||
final _RoutingContextCreateDHTRecordDart _routingContextCreateDHTRecord;
|
||||
|
|
|
@ -68,20 +68,18 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
|
|||
}
|
||||
|
||||
@override
|
||||
VeilidRoutingContextJS withPrivacy() {
|
||||
VeilidRoutingContextJS withDefaultSafety() {
|
||||
final id = _ctx.requireId();
|
||||
final int newId =
|
||||
js_util.callMethod(wasm, 'routing_context_with_privacy', [id]);
|
||||
js_util.callMethod(wasm, 'routing_context_with_default_safety', [id]);
|
||||
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
|
||||
}
|
||||
|
||||
@override
|
||||
VeilidRoutingContextJS withCustomPrivacy(SafetySelection safetySelection) {
|
||||
VeilidRoutingContextJS withSafety(SafetySelection safetySelection) {
|
||||
final id = _ctx.requireId();
|
||||
final newId = js_util.callMethod<int>(
|
||||
wasm,
|
||||
'routing_context_with_custom_privacy',
|
||||
[id, jsonEncode(safetySelection)]);
|
||||
wasm, 'routing_context_with_safety', [id, jsonEncode(safetySelection)]);
|
||||
|
||||
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
|
||||
}
|
||||
|
@ -94,6 +92,15 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
|
|||
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<SafetySelection> safety() async {
|
||||
final id = _ctx.requireId();
|
||||
return SafetySelection.fromJson(jsonDecode(await _wrapApiPromise(
|
||||
js_util.callMethod(wasm, 'routing_context_safety', [
|
||||
id,
|
||||
]))));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> appCall(String target, Uint8List request) async {
|
||||
final id = _ctx.requireId();
|
||||
|
|
|
@ -407,7 +407,7 @@ fn add_routing_context(
|
|||
pub extern "C" fn routing_context(port: i64) {
|
||||
DartIsolateWrapper::new(port).spawn_result(async move {
|
||||
let veilid_api = get_veilid_api().await?;
|
||||
let routing_context = veilid_api.routing_context();
|
||||
let routing_context = veilid_api.routing_context()?;
|
||||
let mut rc = ROUTING_CONTEXTS.lock();
|
||||
let new_id = add_routing_context(&mut rc, routing_context);
|
||||
APIResult::Ok(new_id)
|
||||
|
@ -424,12 +424,12 @@ pub extern "C" fn release_routing_context(id: u32) -> i32 {
|
|||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn routing_context_with_privacy(id: u32) -> u32 {
|
||||
pub extern "C" fn routing_context_with_default_safety(id: u32) -> u32 {
|
||||
let mut rc = ROUTING_CONTEXTS.lock();
|
||||
let Some(routing_context) = rc.get(&id) else {
|
||||
return 0;
|
||||
};
|
||||
let Ok(routing_context) = routing_context.clone().with_privacy() else {
|
||||
let Ok(routing_context) = routing_context.clone().with_default_safety() else {
|
||||
return 0;
|
||||
};
|
||||
|
||||
|
@ -437,7 +437,7 @@ pub extern "C" fn routing_context_with_privacy(id: u32) -> u32 {
|
|||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn routing_context_with_custom_privacy(id: u32, safety_selection: FfiStr) -> u32 {
|
||||
pub extern "C" fn routing_context_with_safety(id: u32, safety_selection: FfiStr) -> u32 {
|
||||
let safety_selection: veilid_core::SafetySelection =
|
||||
veilid_core::deserialize_opt_json(safety_selection.into_opt_string()).unwrap();
|
||||
|
||||
|
@ -445,10 +445,7 @@ pub extern "C" fn routing_context_with_custom_privacy(id: u32, safety_selection:
|
|||
let Some(routing_context) = rc.get(&id) else {
|
||||
return 0;
|
||||
};
|
||||
let Ok(routing_context) = routing_context
|
||||
.clone()
|
||||
.with_custom_privacy(safety_selection)
|
||||
else {
|
||||
let Ok(routing_context) = routing_context.clone().with_safety(safety_selection) else {
|
||||
return 0;
|
||||
};
|
||||
|
||||
|
@ -469,6 +466,23 @@ pub extern "C" fn routing_context_with_sequencing(id: u32, sequencing: FfiStr) -
|
|||
add_routing_context(&mut rc, routing_context)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn routing_context_safety(port: i64, id: u32) {
|
||||
DartIsolateWrapper::new(port).spawn_result_json(async move {
|
||||
let routing_context = {
|
||||
let rc = ROUTING_CONTEXTS.lock();
|
||||
let Some(routing_context) = rc.get(&id) else {
|
||||
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument(
|
||||
"routing_context_app_call",
|
||||
"id",
|
||||
id,
|
||||
));
|
||||
};
|
||||
routing_context.clone()
|
||||
};
|
||||
APIResult::Ok(routing_context.safety())
|
||||
});
|
||||
}
|
||||
#[no_mangle]
|
||||
pub extern "C" fn routing_context_app_call(port: i64, id: u32, target: FfiStr, request: FfiStr) {
|
||||
let target_string: String = target.into_opt_string().unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue