network fixes

This commit is contained in:
Christien Rioux 2023-07-19 10:07:51 -04:00
parent 0fb49bf715
commit f65400a1ce
16 changed files with 309 additions and 129 deletions

1
Cargo.lock generated
View File

@ -5872,7 +5872,6 @@ dependencies = [
"oslog", "oslog",
"paranoid-android", "paranoid-android",
"parking_lot 0.11.2", "parking_lot 0.11.2",
"parking_lot 0.12.1",
"rand 0.7.3", "rand 0.7.3",
"range-set-blaze", "range-set-blaze",
"rust-fsm", "rust-fsm",

View File

@ -80,7 +80,7 @@ core:
set_value_count: 5 set_value_count: 5
set_value_fanout: 4 set_value_fanout: 4
min_peer_count: 20 min_peer_count: 20
min_peer_refresh_time_ms: 2000 min_peer_refresh_time_ms: 60000
validate_dial_info_receipt_time_ms: 2000 validate_dial_info_receipt_time_ms: 2000
local_subkey_cache_size: 128 local_subkey_cache_size: 128
local_max_subkey_cache_memory_mb: 256 local_max_subkey_cache_memory_mb: 256

View File

@ -247,7 +247,7 @@ dht:
set_value_count: 5 set_value_count: 5
set_value_fanout: 4 set_value_fanout: 4
min_peer_count: 20 min_peer_count: 20
min_peer_refresh_time_ms: 2000 min_peer_refresh_time_ms: 60000
validate_dial_info_receipt_time_ms: 2000 validate_dial_info_receipt_time_ms: 2000
local_subkey_cache_size: 128 local_subkey_cache_size: 128
local_max_subkey_cache_memory_mb: 256 local_max_subkey_cache_memory_mb: 256

View File

@ -261,7 +261,7 @@ impl BucketEntryInner {
// See if we have an existing signed_node_info to update or not // See if we have an existing signed_node_info to update or not
let mut node_info_changed = false; let mut node_info_changed = false;
if let Some(current_sni) = opt_current_sni { if let Some(current_sni) = opt_current_sni {
// Always allow overwriting invalid/unsigned node // Always allow overwriting unsigned node (bootstrap)
if current_sni.has_any_signature() { if current_sni.has_any_signature() {
// If the timestamp hasn't changed or is less, ignore this update // If the timestamp hasn't changed or is less, ignore this update
if signed_node_info.timestamp() <= current_sni.timestamp() { if signed_node_info.timestamp() <= current_sni.timestamp() {
@ -424,7 +424,7 @@ impl BucketEntryInner {
} }
// Removes a connection descriptor in this entry's table of last connections // Removes a connection descriptor in this entry's table of last connections
pub fn clear_last_connection(&mut self, last_connection: ConnectionDescriptor) { pub fn clear_last_connection(&mut self, last_connection: ConnectionDescriptor) {
let key = self.descriptor_to_key(last_connection); let key = self.descriptor_to_key(last_connection);
self.last_connections self.last_connections
.remove(&key); .remove(&key);

View File

@ -208,7 +208,7 @@ impl RoutingTable {
rolling_transfers_task: TickTask::new(ROLLING_TRANSFERS_INTERVAL_SECS), rolling_transfers_task: TickTask::new(ROLLING_TRANSFERS_INTERVAL_SECS),
kick_buckets_task: TickTask::new(1), kick_buckets_task: TickTask::new(1),
bootstrap_task: TickTask::new(1), bootstrap_task: TickTask::new(1),
peer_minimum_refresh_task: TickTask::new_ms(c.network.dht.min_peer_refresh_time_ms), peer_minimum_refresh_task: TickTask::new(1),
ping_validator_task: TickTask::new(1), ping_validator_task: TickTask::new(1),
relay_management_task: TickTask::new(RELAY_MANAGEMENT_INTERVAL_SECS), relay_management_task: TickTask::new(RELAY_MANAGEMENT_INTERVAL_SECS),
private_route_management_task: TickTask::new(PRIVATE_ROUTE_MANAGEMENT_INTERVAL_SECS), private_route_management_task: TickTask::new(PRIVATE_ROUTE_MANAGEMENT_INTERVAL_SECS),

View File

@ -19,12 +19,18 @@ impl RoutingTable {
// Get counts by crypto kind // Get counts by crypto kind
let entry_count = self.inner.read().cached_entry_counts(); let entry_count = self.inner.read().cached_entry_counts();
let min_peer_count = self.with_config(|c| c.network.dht.min_peer_count as usize); let (min_peer_count, min_peer_refresh_time_ms) = self.with_config(|c| {
(
c.network.dht.min_peer_count as usize,
c.network.dht.min_peer_refresh_time_ms,
)
});
// For the PublicInternet routing domain, get list of all peers we know about // For the PublicInternet routing domain, get list of all peers we know about
// even the unreliable ones, and ask them to find nodes close to our node too // even the unreliable ones, and ask them to find nodes close to our node too
let mut ord = FuturesOrdered::new(); let mut ord = FuturesOrdered::new();
let cur_ts = get_timestamp();
for crypto_kind in VALID_CRYPTO_KINDS { for crypto_kind in VALID_CRYPTO_KINDS {
// Do we need to peer minimum refresh this crypto kind? // Do we need to peer minimum refresh this crypto kind?
@ -37,16 +43,26 @@ impl RoutingTable {
} }
let routing_table = self.clone(); let routing_table = self.clone();
let mut filters = VecDeque::new(); let mut filters = VecDeque::new();
let filter = Box::new( let filter = Box::new(
move |_rti: &RoutingTableInner, opt_entry: Option<Arc<BucketEntry>>| { move |rti: &RoutingTableInner, opt_entry: Option<Arc<BucketEntry>>| {
// Keep only the entries that contain the crypto kind we're looking for let entry = opt_entry.unwrap().clone();
if let Some(entry) = opt_entry { entry.with(rti, |_rti, e| {
entry.with_inner(|e| e.crypto_kinds().contains(&crypto_kind)) // Keep only the entries that contain the crypto kind we're looking for
} else { let compatible_crypto = e.crypto_kinds().contains(&crypto_kind);
VALID_CRYPTO_KINDS.contains(&crypto_kind) if !compatible_crypto {
} return false;
}
// Keep only the entries we haven't talked to in the min_peer_refresh_time
if let Some(last_q_ts) = e.peer_stats().rpc_stats.last_question_ts {
if cur_ts.saturating_sub(last_q_ts.as_u64())
< (min_peer_refresh_time_ms as u64 * 1_000u64)
{
return false;
}
}
true
})
}, },
) as RoutingTableEntryFilter; ) as RoutingTableEntryFilter;
filters.push_front(filter); filters.push_front(filter);

View File

@ -324,10 +324,13 @@ impl RPCProcessor {
let timeout_us = TimestampDuration::new(ms_to_us(c.network.rpc.timeout_ms)); let timeout_us = TimestampDuration::new(ms_to_us(c.network.rpc.timeout_ms));
let max_route_hop_count = c.network.rpc.max_route_hop_count as usize; let max_route_hop_count = c.network.rpc.max_route_hop_count as usize;
if concurrency == 0 { if concurrency == 0 {
concurrency = get_concurrency() / 2; concurrency = get_concurrency();
if concurrency == 0 { if concurrency == 0 {
concurrency = 1; concurrency = 1;
} }
// Default RPC concurrency is the number of CPUs * 16 rpc workers per core, as a single worker takes about 1% CPU when relaying and 16% is reasonable for baseline plus relay
concurrency *= 16;
} }
let validate_dial_info_receipt_time_ms = c.network.dht.validate_dial_info_receipt_time_ms; let validate_dial_info_receipt_time_ms = c.network.dht.validate_dial_info_receipt_time_ms;

View File

@ -217,7 +217,7 @@ fn config_callback(key: String) -> ConfigCallbackReturn {
"network.dht.set_value_count" => Ok(Box::new(5u32)), "network.dht.set_value_count" => Ok(Box::new(5u32)),
"network.dht.set_value_fanout" => Ok(Box::new(4u32)), "network.dht.set_value_fanout" => Ok(Box::new(4u32)),
"network.dht.min_peer_count" => Ok(Box::new(20u32)), "network.dht.min_peer_count" => Ok(Box::new(20u32)),
"network.dht.min_peer_refresh_time_ms" => Ok(Box::new(2_000u32)), "network.dht.min_peer_refresh_time_ms" => Ok(Box::new(60_000u32)),
"network.dht.validate_dial_info_receipt_time_ms" => Ok(Box::new(5_000u32)), "network.dht.validate_dial_info_receipt_time_ms" => Ok(Box::new(5_000u32)),
"network.dht.local_subkey_cache_size" => Ok(Box::new(128u32)), "network.dht.local_subkey_cache_size" => Ok(Box::new(128u32)),
"network.dht.local_max_subkey_cache_memory_mb" => Ok(Box::new(256u32)), "network.dht.local_max_subkey_cache_memory_mb" => Ok(Box::new(256u32)),
@ -345,7 +345,7 @@ pub async fn test_config() {
assert_eq!(inner.network.dht.set_value_count, 5u32); assert_eq!(inner.network.dht.set_value_count, 5u32);
assert_eq!(inner.network.dht.set_value_fanout, 4u32); assert_eq!(inner.network.dht.set_value_fanout, 4u32);
assert_eq!(inner.network.dht.min_peer_count, 20u32); assert_eq!(inner.network.dht.min_peer_count, 20u32);
assert_eq!(inner.network.dht.min_peer_refresh_time_ms, 2_000u32); assert_eq!(inner.network.dht.min_peer_refresh_time_ms, 60_000u32);
assert_eq!( assert_eq!(
inner.network.dht.validate_dial_info_receipt_time_ms, inner.network.dht.validate_dial_info_receipt_time_ms,
5_000u32 5_000u32

View File

@ -118,7 +118,7 @@ Future<VeilidConfig> getDefaultVeilidConfig(String programName) async {
setValueCount: 20, setValueCount: 20,
setValueFanout: 5, setValueFanout: 5,
minPeerCount: 20, minPeerCount: 20,
minPeerRefreshTimeMs: 2000, minPeerRefreshTimeMs: 60000,
validateDialInfoReceiptTimeMs: 2000, validateDialInfoReceiptTimeMs: 2000,
localSubkeyCacheSize: getLocalSubkeyCacheSize(), localSubkeyCacheSize: getLocalSubkeyCacheSize(),
localMaxSubkeyCacheMemoryMb: getLocalMaxSubkeyCacheMemoryMb(), localMaxSubkeyCacheMemoryMb: getLocalMaxSubkeyCacheMemoryMb(),

View File

@ -226,6 +226,8 @@ class RouteBlob with _$RouteBlob {
/// VeilidRoutingContext /// VeilidRoutingContext
abstract class VeilidRoutingContext { abstract class VeilidRoutingContext {
void close();
// Modifiers // Modifiers
VeilidRoutingContext withPrivacy(); VeilidRoutingContext withPrivacy();
VeilidRoutingContext withCustomPrivacy(SafetySelection safetySelection); VeilidRoutingContext withCustomPrivacy(SafetySelection safetySelection);

View File

@ -176,10 +176,18 @@ abstract class VeilidCryptoSystem {
Future<HashDigest> generateHash(Uint8List data); Future<HashDigest> generateHash(Uint8List data);
//Future<HashDigest> generateHashReader(Stream<List<int>> reader); //Future<HashDigest> generateHashReader(Stream<List<int>> reader);
Future<bool> validateKeyPair(PublicKey key, SecretKey secret); Future<bool> validateKeyPair(PublicKey key, SecretKey secret);
Future<bool> validateKeyPairWithKeyPair(KeyPair keyPair) {
return validateKeyPair(keyPair.key, keyPair.secret);
}
Future<bool> validateHash(Uint8List data, HashDigest hash); Future<bool> validateHash(Uint8List data, HashDigest hash);
//Future<bool> validateHashReader(Stream<List<int>> reader, HashDigest hash); //Future<bool> validateHashReader(Stream<List<int>> reader, HashDigest hash);
Future<CryptoKeyDistance> distance(CryptoKey key1, CryptoKey key2); Future<CryptoKeyDistance> distance(CryptoKey key1, CryptoKey key2);
Future<Signature> sign(PublicKey key, SecretKey secret, Uint8List data); Future<Signature> sign(PublicKey key, SecretKey secret, Uint8List data);
Future<Signature> signWithKeyPair(KeyPair keyPair, Uint8List data) {
return sign(keyPair.key, keyPair.secret, data);
}
Future<void> verify(PublicKey key, Uint8List data, Signature signature); Future<void> verify(PublicKey key, Uint8List data, Signature signature);
Future<int> aeadOverhead(); Future<int> aeadOverhead();
Future<Uint8List> decryptAead(Uint8List body, Nonce nonce, Future<Uint8List> decryptAead(Uint8List body, Nonce nonce,

View File

@ -536,7 +536,7 @@ Stream<T> processStreamJson<T>(
case messageStreamItemJson: case messageStreamItemJson:
{ {
if (list[1] == null) { if (list[1] == null) {
throw VeilidAPIExceptionInternal( throw const VeilidAPIExceptionInternal(
"Null MESSAGE_STREAM_ITEM_JSON value"); "Null MESSAGE_STREAM_ITEM_JSON value");
} }
var ret = jsonDecode(list[1] as String); var ret = jsonDecode(list[1] as String);
@ -573,49 +573,70 @@ Stream<T> processStreamJson<T>(
} }
class _Ctx { class _Ctx {
final int id; int? id;
final VeilidFFI ffi; final VeilidFFI ffi;
_Ctx(this.id, this.ffi); _Ctx(int this.id, this.ffi);
void ensureValid() {
if (id == null) {
throw VeilidAPIExceptionNotInitialized();
}
}
void close() {
if (id != null) {
ffi._releaseRoutingContext(id!);
id = null;
}
}
} }
// FFI implementation of VeilidRoutingContext // FFI implementation of VeilidRoutingContext
class VeilidRoutingContextFFI implements VeilidRoutingContext { class VeilidRoutingContextFFI extends VeilidRoutingContext {
final _Ctx _ctx; final _Ctx _ctx;
static final Finalizer<_Ctx> _finalizer = static final Finalizer<_Ctx> _finalizer = Finalizer((ctx) => ctx.close());
Finalizer((ctx) => ctx.ffi._releaseRoutingContext(ctx.id));
VeilidRoutingContextFFI._(this._ctx) { VeilidRoutingContextFFI._(this._ctx) {
_finalizer.attach(this, _ctx, detach: this); _finalizer.attach(this, _ctx, detach: this);
} }
@override
void close() {
_ctx.close();
}
@override @override
VeilidRoutingContextFFI withPrivacy() { VeilidRoutingContextFFI withPrivacy() {
final newId = _ctx.ffi._routingContextWithPrivacy(_ctx.id); _ctx.ensureValid();
final newId = _ctx.ffi._routingContextWithPrivacy(_ctx.id!);
return VeilidRoutingContextFFI._(_Ctx(newId, _ctx.ffi)); return VeilidRoutingContextFFI._(_Ctx(newId, _ctx.ffi));
} }
@override @override
VeilidRoutingContextFFI withCustomPrivacy(SafetySelection safetySelection) { VeilidRoutingContextFFI withCustomPrivacy(SafetySelection safetySelection) {
_ctx.ensureValid();
final newId = _ctx.ffi._routingContextWithCustomPrivacy( final newId = _ctx.ffi._routingContextWithCustomPrivacy(
_ctx.id, jsonEncode(safetySelection).toNativeUtf8()); _ctx.id!, jsonEncode(safetySelection).toNativeUtf8());
return VeilidRoutingContextFFI._(_Ctx(newId, _ctx.ffi)); return VeilidRoutingContextFFI._(_Ctx(newId, _ctx.ffi));
} }
@override @override
VeilidRoutingContextFFI withSequencing(Sequencing sequencing) { VeilidRoutingContextFFI withSequencing(Sequencing sequencing) {
_ctx.ensureValid();
final newId = _ctx.ffi._routingContextWithSequencing( final newId = _ctx.ffi._routingContextWithSequencing(
_ctx.id, jsonEncode(sequencing).toNativeUtf8()); _ctx.id!, jsonEncode(sequencing).toNativeUtf8());
return VeilidRoutingContextFFI._(_Ctx(newId, _ctx.ffi)); return VeilidRoutingContextFFI._(_Ctx(newId, _ctx.ffi));
} }
@override @override
Future<Uint8List> appCall(String target, Uint8List request) async { Future<Uint8List> appCall(String target, Uint8List request) async {
_ctx.ensureValid();
var nativeEncodedTarget = target.toNativeUtf8(); var nativeEncodedTarget = target.toNativeUtf8();
var nativeEncodedRequest = base64UrlNoPadEncode(request).toNativeUtf8(); var nativeEncodedRequest = base64UrlNoPadEncode(request).toNativeUtf8();
final recvPort = ReceivePort("routing_context_app_call"); final recvPort = ReceivePort("routing_context_app_call");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextAppCall(sendPort.nativePort, _ctx.id, _ctx.ffi._routingContextAppCall(sendPort.nativePort, _ctx.id!,
nativeEncodedTarget, nativeEncodedRequest); nativeEncodedTarget, nativeEncodedRequest);
final out = await processFuturePlain(recvPort.first); final out = await processFuturePlain(recvPort.first);
return base64UrlNoPadDecode(out); return base64UrlNoPadDecode(out);
@ -623,12 +644,13 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext {
@override @override
Future<void> appMessage(String target, Uint8List message) { Future<void> appMessage(String target, Uint8List message) {
_ctx.ensureValid();
final nativeEncodedTarget = target.toNativeUtf8(); final nativeEncodedTarget = target.toNativeUtf8();
final nativeEncodedMessage = base64UrlNoPadEncode(message).toNativeUtf8(); final nativeEncodedMessage = base64UrlNoPadEncode(message).toNativeUtf8();
final recvPort = ReceivePort("routing_context_app_message"); final recvPort = ReceivePort("routing_context_app_message");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextAppMessage(sendPort.nativePort, _ctx.id, _ctx.ffi._routingContextAppMessage(sendPort.nativePort, _ctx.id!,
nativeEncodedTarget, nativeEncodedMessage); nativeEncodedTarget, nativeEncodedMessage);
return processFutureVoid(recvPort.first); return processFutureVoid(recvPort.first);
} }
@ -636,11 +658,12 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext {
@override @override
Future<DHTRecordDescriptor> createDHTRecord(DHTSchema schema, Future<DHTRecordDescriptor> createDHTRecord(DHTSchema schema,
{CryptoKind kind = 0}) async { {CryptoKind kind = 0}) async {
_ctx.ensureValid();
final nativeSchema = jsonEncode(schema).toNativeUtf8(); final nativeSchema = jsonEncode(schema).toNativeUtf8();
final recvPort = ReceivePort("routing_context_create_dht_record"); final recvPort = ReceivePort("routing_context_create_dht_record");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextCreateDHTRecord( _ctx.ffi._routingContextCreateDHTRecord(
sendPort.nativePort, _ctx.id, nativeSchema, kind); sendPort.nativePort, _ctx.id!, nativeSchema, kind);
final dhtRecordDescriptor = final dhtRecordDescriptor =
await processFutureJson(DHTRecordDescriptor.fromJson, recvPort.first); await processFutureJson(DHTRecordDescriptor.fromJson, recvPort.first);
return dhtRecordDescriptor; return dhtRecordDescriptor;
@ -649,13 +672,14 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext {
@override @override
Future<DHTRecordDescriptor> openDHTRecord( Future<DHTRecordDescriptor> openDHTRecord(
TypedKey key, KeyPair? writer) async { TypedKey key, KeyPair? writer) async {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeKey = jsonEncode(key).toNativeUtf8();
final nativeWriter = final nativeWriter =
writer != null ? jsonEncode(key).toNativeUtf8() : nullptr; writer != null ? jsonEncode(key).toNativeUtf8() : nullptr;
final recvPort = ReceivePort("routing_context_open_dht_record"); final recvPort = ReceivePort("routing_context_open_dht_record");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextOpenDHTRecord( _ctx.ffi._routingContextOpenDHTRecord(
sendPort.nativePort, _ctx.id, nativeKey, nativeWriter); sendPort.nativePort, _ctx.id!, nativeKey, nativeWriter);
final dhtRecordDescriptor = final dhtRecordDescriptor =
await processFutureJson(DHTRecordDescriptor.fromJson, recvPort.first); await processFutureJson(DHTRecordDescriptor.fromJson, recvPort.first);
return dhtRecordDescriptor; return dhtRecordDescriptor;
@ -663,32 +687,35 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext {
@override @override
Future<void> closeDHTRecord(TypedKey key) { Future<void> closeDHTRecord(TypedKey key) {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeKey = jsonEncode(key).toNativeUtf8();
final recvPort = ReceivePort("routing_context_close_dht_record"); final recvPort = ReceivePort("routing_context_close_dht_record");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi _ctx.ffi._routingContextCloseDHTRecord(
._routingContextCloseDHTRecord(sendPort.nativePort, _ctx.id, nativeKey); sendPort.nativePort, _ctx.id!, nativeKey);
return processFutureVoid(recvPort.first); return processFutureVoid(recvPort.first);
} }
@override @override
Future<void> deleteDHTRecord(TypedKey key) { Future<void> deleteDHTRecord(TypedKey key) {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeKey = jsonEncode(key).toNativeUtf8();
final recvPort = ReceivePort("routing_context_delete_dht_record"); final recvPort = ReceivePort("routing_context_delete_dht_record");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextDeleteDHTRecord( _ctx.ffi._routingContextDeleteDHTRecord(
sendPort.nativePort, _ctx.id, nativeKey); sendPort.nativePort, _ctx.id!, nativeKey);
return processFutureVoid(recvPort.first); return processFutureVoid(recvPort.first);
} }
@override @override
Future<ValueData?> getDHTValue( Future<ValueData?> getDHTValue(
TypedKey key, int subkey, bool forceRefresh) async { TypedKey key, int subkey, bool forceRefresh) async {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeKey = jsonEncode(key).toNativeUtf8();
final recvPort = ReceivePort("routing_context_get_dht_value"); final recvPort = ReceivePort("routing_context_get_dht_value");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextGetDHTValue( _ctx.ffi._routingContextGetDHTValue(
sendPort.nativePort, _ctx.id, nativeKey, subkey, forceRefresh); sendPort.nativePort, _ctx.id!, nativeKey, subkey, forceRefresh);
final valueData = await processFutureJson( final valueData = await processFutureJson(
optFromJson(ValueData.fromJson), recvPort.first); optFromJson(ValueData.fromJson), recvPort.first);
return valueData; return valueData;
@ -697,13 +724,14 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext {
@override @override
Future<ValueData?> setDHTValue( Future<ValueData?> setDHTValue(
TypedKey key, int subkey, Uint8List data) async { TypedKey key, int subkey, Uint8List data) async {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeKey = jsonEncode(key).toNativeUtf8();
final nativeData = base64UrlNoPadEncode(data).toNativeUtf8(); final nativeData = base64UrlNoPadEncode(data).toNativeUtf8();
final recvPort = ReceivePort("routing_context_set_dht_value"); final recvPort = ReceivePort("routing_context_set_dht_value");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextSetDHTValue( _ctx.ffi._routingContextSetDHTValue(
sendPort.nativePort, _ctx.id, nativeKey, subkey, nativeData); sendPort.nativePort, _ctx.id!, nativeKey, subkey, nativeData);
final valueData = await processFutureJson( final valueData = await processFutureJson(
optFromJson(ValueData.fromJson), recvPort.first); optFromJson(ValueData.fromJson), recvPort.first);
return valueData; return valueData;
@ -712,13 +740,14 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext {
@override @override
Future<Timestamp> watchDHTValues(TypedKey key, List<ValueSubkeyRange> subkeys, Future<Timestamp> watchDHTValues(TypedKey key, List<ValueSubkeyRange> subkeys,
Timestamp expiration, int count) async { Timestamp expiration, int count) async {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeKey = jsonEncode(key).toNativeUtf8();
final nativeSubkeys = jsonEncode(subkeys).toNativeUtf8(); final nativeSubkeys = jsonEncode(subkeys).toNativeUtf8();
final nativeExpiration = expiration.value.toInt(); final nativeExpiration = expiration.value.toInt();
final recvPort = ReceivePort("routing_context_watch_dht_values"); final recvPort = ReceivePort("routing_context_watch_dht_values");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextWatchDHTValues(sendPort.nativePort, _ctx.id, _ctx.ffi._routingContextWatchDHTValues(sendPort.nativePort, _ctx.id!,
nativeKey, nativeSubkeys, nativeExpiration, count); nativeKey, nativeSubkeys, nativeExpiration, count);
final actualExpiration = Timestamp( final actualExpiration = Timestamp(
value: BigInt.from(await processFuturePlain<int>(recvPort.first))); value: BigInt.from(await processFuturePlain<int>(recvPort.first)));
@ -728,60 +757,82 @@ class VeilidRoutingContextFFI implements VeilidRoutingContext {
@override @override
Future<bool> cancelDHTWatch( Future<bool> cancelDHTWatch(
TypedKey key, List<ValueSubkeyRange> subkeys) async { TypedKey key, List<ValueSubkeyRange> subkeys) async {
_ctx.ensureValid();
final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeKey = jsonEncode(key).toNativeUtf8();
final nativeSubkeys = jsonEncode(subkeys).toNativeUtf8(); final nativeSubkeys = jsonEncode(subkeys).toNativeUtf8();
final recvPort = ReceivePort("routing_context_cancel_dht_watch"); final recvPort = ReceivePort("routing_context_cancel_dht_watch");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_ctx.ffi._routingContextCancelDHTWatch( _ctx.ffi._routingContextCancelDHTWatch(
sendPort.nativePort, _ctx.id, nativeKey, nativeSubkeys); sendPort.nativePort, _ctx.id!, nativeKey, nativeSubkeys);
final cancelled = await processFuturePlain<bool>(recvPort.first); final cancelled = await processFuturePlain<bool>(recvPort.first);
return cancelled; return cancelled;
} }
} }
class _TDBT { class _TDBT {
final int id; int? id;
VeilidTableDBFFI tdbffi; final VeilidTableDBFFI tdbffi;
VeilidFFI ffi; final VeilidFFI ffi;
_TDBT(this.id, this.tdbffi, this.ffi); _TDBT(int this.id, this.tdbffi, this.ffi);
void ensureValid() {
if (id == null) {
throw VeilidAPIExceptionNotInitialized();
}
}
void close() {
if (id != null) {
ffi._releaseTableDbTransaction(id!);
id = null;
}
}
} }
// FFI implementation of VeilidTableDBTransaction // FFI implementation of VeilidTableDBTransaction
class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction {
final _TDBT _tdbt; final _TDBT _tdbt;
static final Finalizer<_TDBT> _finalizer = static final Finalizer<_TDBT> _finalizer = Finalizer((tdbt) => tdbt.close());
Finalizer((tdbt) => tdbt.ffi._releaseTableDbTransaction(tdbt.id));
VeilidTableDBTransactionFFI._(this._tdbt) { VeilidTableDBTransactionFFI._(this._tdbt) {
_finalizer.attach(this, _tdbt, detach: this); _finalizer.attach(this, _tdbt, detach: this);
} }
@override @override
Future<void> commit() { bool isDone() {
return _tdbt.id == null;
}
@override
Future<void> commit() async {
_tdbt.ensureValid();
final recvPort = ReceivePort("veilid_table_db_transaction_commit"); final recvPort = ReceivePort("veilid_table_db_transaction_commit");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_tdbt.ffi._tableDbTransactionCommit( _tdbt.ffi._tableDbTransactionCommit(
sendPort.nativePort, sendPort.nativePort,
_tdbt.id, _tdbt.id!,
); );
return processFutureVoid(recvPort.first); await processFutureVoid(recvPort.first);
_tdbt.close();
} }
@override @override
Future<void> rollback() { Future<void> rollback() async {
_tdbt.ensureValid();
final recvPort = ReceivePort("veilid_table_db_transaction_rollback"); final recvPort = ReceivePort("veilid_table_db_transaction_rollback");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_tdbt.ffi._tableDbTransactionRollback( _tdbt.ffi._tableDbTransactionRollback(
sendPort.nativePort, sendPort.nativePort,
_tdbt.id, _tdbt.id!,
); );
return processFutureVoid(recvPort.first); await processFutureVoid(recvPort.first);
_tdbt.close();
} }
@override @override
Future<void> store(int col, Uint8List key, Uint8List value) { Future<void> store(int col, Uint8List key, Uint8List value) {
_tdbt.ensureValid();
final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8();
final nativeEncodedValue = base64UrlNoPadEncode(value).toNativeUtf8(); final nativeEncodedValue = base64UrlNoPadEncode(value).toNativeUtf8();
@ -789,7 +840,7 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction {
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_tdbt.ffi._tableDbTransactionStore( _tdbt.ffi._tableDbTransactionStore(
sendPort.nativePort, sendPort.nativePort,
_tdbt.id, _tdbt.id!,
col, col,
nativeEncodedKey, nativeEncodedKey,
nativeEncodedValue, nativeEncodedValue,
@ -799,13 +850,14 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction {
@override @override
Future<void> delete(int col, Uint8List key) { Future<void> delete(int col, Uint8List key) {
_tdbt.ensureValid();
final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8();
final recvPort = ReceivePort("veilid_table_db_transaction_delete"); final recvPort = ReceivePort("veilid_table_db_transaction_delete");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_tdbt.ffi._tableDbTransactionDelete( _tdbt.ffi._tableDbTransactionDelete(
sendPort.nativePort, sendPort.nativePort,
_tdbt.id, _tdbt.id!,
col, col,
nativeEncodedKey, nativeEncodedKey,
); );
@ -814,32 +866,51 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction {
} }
class _TDB { class _TDB {
final int id; int? id;
VeilidFFI ffi; final VeilidFFI ffi;
_TDB(this.id, this.ffi); _TDB(int this.id, this.ffi);
void ensureValid() {
if (id == null) {
throw VeilidAPIExceptionNotInitialized();
}
}
void close() {
if (id != null) {
ffi._releaseTableDb(id!);
id = null;
}
}
} }
// FFI implementation of VeilidTableDB // FFI implementation of VeilidTableDB
class VeilidTableDBFFI extends VeilidTableDB { class VeilidTableDBFFI extends VeilidTableDB {
final _TDB _tdb; final _TDB _tdb;
static final Finalizer<_TDB> _finalizer = static final Finalizer<_TDB> _finalizer = Finalizer((tdb) => tdb.close());
Finalizer((tdb) => tdb.ffi._releaseTableDb(tdb.id));
VeilidTableDBFFI._(this._tdb) { VeilidTableDBFFI._(this._tdb) {
_finalizer.attach(this, _tdb, detach: this); _finalizer.attach(this, _tdb, detach: this);
} }
@override
void close() {
_tdb.close();
}
@override @override
int getColumnCount() { int getColumnCount() {
return _tdb.ffi._tableDbGetColumnCount(_tdb.id); _tdb.ensureValid();
return _tdb.ffi._tableDbGetColumnCount(_tdb.id!);
} }
@override @override
Future<List<Uint8List>> getKeys(int col) { Future<List<Uint8List>> getKeys(int col) {
_tdb.ensureValid();
final recvPort = ReceivePort("veilid_table_db_get_keys"); final recvPort = ReceivePort("veilid_table_db_get_keys");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_tdb.ffi._tableDbGetKeys(sendPort.nativePort, _tdb.id, col); _tdb.ffi._tableDbGetKeys(sendPort.nativePort, _tdb.id!, col);
return processFutureJson( return processFutureJson(
jsonListConstructor<Uint8List>(base64UrlNoPadDecodeDynamic), jsonListConstructor<Uint8List>(base64UrlNoPadDecodeDynamic),
@ -848,12 +919,16 @@ class VeilidTableDBFFI extends VeilidTableDB {
@override @override
VeilidTableDBTransaction transact() { VeilidTableDBTransaction transact() {
final id = _tdb.ffi._tableDbTransact(_tdb.id); _tdb.ensureValid();
final id = _tdb.ffi._tableDbTransact(_tdb.id!);
return VeilidTableDBTransactionFFI._(_TDBT(id, this, _tdb.ffi)); return VeilidTableDBTransactionFFI._(_TDBT(id, this, _tdb.ffi));
} }
@override @override
Future<void> store(int col, Uint8List key, Uint8List value) { Future<void> store(int col, Uint8List key, Uint8List value) {
_tdb.ensureValid();
final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8();
final nativeEncodedValue = base64UrlNoPadEncode(value).toNativeUtf8(); final nativeEncodedValue = base64UrlNoPadEncode(value).toNativeUtf8();
@ -861,7 +936,7 @@ class VeilidTableDBFFI extends VeilidTableDB {
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_tdb.ffi._tableDbStore( _tdb.ffi._tableDbStore(
sendPort.nativePort, sendPort.nativePort,
_tdb.id, _tdb.id!,
col, col,
nativeEncodedKey, nativeEncodedKey,
nativeEncodedValue, nativeEncodedValue,
@ -871,13 +946,14 @@ class VeilidTableDBFFI extends VeilidTableDB {
@override @override
Future<Uint8List?> load(int col, Uint8List key) async { Future<Uint8List?> load(int col, Uint8List key) async {
_tdb.ensureValid();
final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8();
final recvPort = ReceivePort("veilid_table_db_load"); final recvPort = ReceivePort("veilid_table_db_load");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_tdb.ffi._tableDbLoad( _tdb.ffi._tableDbLoad(
sendPort.nativePort, sendPort.nativePort,
_tdb.id, _tdb.id!,
col, col,
nativeEncodedKey, nativeEncodedKey,
); );
@ -890,13 +966,14 @@ class VeilidTableDBFFI extends VeilidTableDB {
@override @override
Future<Uint8List?> delete(int col, Uint8List key) async { Future<Uint8List?> delete(int col, Uint8List key) async {
_tdb.ensureValid();
final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8();
final recvPort = ReceivePort("veilid_table_db_delete"); final recvPort = ReceivePort("veilid_table_db_delete");
final sendPort = recvPort.sendPort; final sendPort = recvPort.sendPort;
_tdb.ffi._tableDbDelete( _tdb.ffi._tableDbDelete(
sendPort.nativePort, sendPort.nativePort,
_tdb.id, _tdb.id!,
col, col,
nativeEncodedKey, nativeEncodedKey,
); );
@ -909,7 +986,7 @@ class VeilidTableDBFFI extends VeilidTableDB {
} }
// FFI implementation of VeilidCryptoSystem // FFI implementation of VeilidCryptoSystem
class VeilidCryptoSystemFFI implements VeilidCryptoSystem { class VeilidCryptoSystemFFI extends VeilidCryptoSystem {
final CryptoKind _kind; final CryptoKind _kind;
final VeilidFFI _ffi; final VeilidFFI _ffi;
@ -1154,7 +1231,7 @@ class VeilidCryptoSystemFFI implements VeilidCryptoSystem {
} }
// FFI implementation of high level Veilid API // FFI implementation of high level Veilid API
class VeilidFFI implements Veilid { class VeilidFFI extends Veilid {
// veilid_core shared library // veilid_core shared library
final DynamicLibrary _dylib; final DynamicLibrary _dylib;

View File

@ -22,75 +22,98 @@ Future<T> _wrapApiPromise<T>(Object p) {
} }
class _Ctx { class _Ctx {
final int id; int? id;
final VeilidJS js; final VeilidJS js;
_Ctx(this.id, this.js); _Ctx(int this.id, this.js);
void ensureValid() {
if (id == null) {
throw VeilidAPIExceptionNotInitialized();
}
}
void close() {
if (id != null) {
js_util.callMethod(wasm, "release_routing_context", [id!]);
id = null;
}
}
} }
// JS implementation of VeilidRoutingContext // JS implementation of VeilidRoutingContext
class VeilidRoutingContextJS implements VeilidRoutingContext { class VeilidRoutingContextJS extends VeilidRoutingContext {
final _Ctx _ctx; final _Ctx _ctx;
static final Finalizer<_Ctx> _finalizer = Finalizer( static final Finalizer<_Ctx> _finalizer = Finalizer((ctx) => ctx.close());
(ctx) => js_util.callMethod(wasm, "release_routing_context", [ctx.id]));
VeilidRoutingContextJS._(this._ctx) { VeilidRoutingContextJS._(this._ctx) {
_finalizer.attach(this, _ctx, detach: this); _finalizer.attach(this, _ctx, detach: this);
} }
@override
void close() {
_ctx.close();
}
@override @override
VeilidRoutingContextJS withPrivacy() { VeilidRoutingContextJS withPrivacy() {
_ctx.ensureValid();
int newId = int newId =
js_util.callMethod(wasm, "routing_context_with_privacy", [_ctx.id]); js_util.callMethod(wasm, "routing_context_with_privacy", [_ctx.id!]);
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
} }
@override @override
VeilidRoutingContextJS withCustomPrivacy(SafetySelection safetySelection) { VeilidRoutingContextJS withCustomPrivacy(SafetySelection safetySelection) {
_ctx.ensureValid();
final newId = js_util.callMethod( final newId = js_util.callMethod(
wasm, wasm,
"routing_context_with_custom_privacy", "routing_context_with_custom_privacy",
[_ctx.id, jsonEncode(safetySelection)]); [_ctx.id!, jsonEncode(safetySelection)]);
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
} }
@override @override
VeilidRoutingContextJS withSequencing(Sequencing sequencing) { VeilidRoutingContextJS withSequencing(Sequencing sequencing) {
_ctx.ensureValid();
final newId = js_util.callMethod(wasm, "routing_context_with_sequencing", final newId = js_util.callMethod(wasm, "routing_context_with_sequencing",
[_ctx.id, jsonEncode(sequencing)]); [_ctx.id!, jsonEncode(sequencing)]);
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
} }
@override @override
Future<Uint8List> appCall(String target, Uint8List request) async { Future<Uint8List> appCall(String target, Uint8List request) async {
_ctx.ensureValid();
var encodedRequest = base64UrlNoPadEncode(request); var encodedRequest = base64UrlNoPadEncode(request);
return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod( return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod(
wasm, "routing_context_app_call", [_ctx.id, target, encodedRequest]))); wasm, "routing_context_app_call", [_ctx.id!, target, encodedRequest])));
} }
@override @override
Future<void> appMessage(String target, Uint8List message) { Future<void> appMessage(String target, Uint8List message) {
_ctx.ensureValid();
var encodedMessage = base64UrlNoPadEncode(message); var encodedMessage = base64UrlNoPadEncode(message);
return _wrapApiPromise(js_util.callMethod(wasm, return _wrapApiPromise(js_util.callMethod(wasm,
"routing_context_app_message", [_ctx.id, target, encodedMessage])); "routing_context_app_message", [_ctx.id!, target, encodedMessage]));
} }
@override @override
Future<DHTRecordDescriptor> createDHTRecord(DHTSchema schema, Future<DHTRecordDescriptor> createDHTRecord(DHTSchema schema,
{CryptoKind kind = 0}) async { {CryptoKind kind = 0}) async {
_ctx.ensureValid();
return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util
.callMethod(wasm, "routing_context_create_dht_record", .callMethod(wasm, "routing_context_create_dht_record",
[_ctx.id, jsonEncode(schema), kind])))); [_ctx.id!, jsonEncode(schema), kind]))));
} }
@override @override
Future<DHTRecordDescriptor> openDHTRecord( Future<DHTRecordDescriptor> openDHTRecord(
TypedKey key, KeyPair? writer) async { TypedKey key, KeyPair? writer) async {
_ctx.ensureValid();
return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util
.callMethod(wasm, "routing_context_open_dht_record", [ .callMethod(wasm, "routing_context_open_dht_record", [
_ctx.id, _ctx.id!,
jsonEncode(key), jsonEncode(key),
writer != null ? jsonEncode(writer) : null writer != null ? jsonEncode(writer) : null
])))); ]))));
@ -98,42 +121,47 @@ class VeilidRoutingContextJS implements VeilidRoutingContext {
@override @override
Future<void> closeDHTRecord(TypedKey key) { Future<void> closeDHTRecord(TypedKey key) {
_ctx.ensureValid();
return _wrapApiPromise(js_util.callMethod( return _wrapApiPromise(js_util.callMethod(
wasm, "routing_context_close_dht_record", [_ctx.id, jsonEncode(key)])); wasm, "routing_context_close_dht_record", [_ctx.id!, jsonEncode(key)]));
} }
@override @override
Future<void> deleteDHTRecord(TypedKey key) { Future<void> deleteDHTRecord(TypedKey key) {
return _wrapApiPromise(js_util.callMethod( _ctx.ensureValid();
wasm, "routing_context_delete_dht_record", [_ctx.id, jsonEncode(key)])); return _wrapApiPromise(js_util.callMethod(wasm,
"routing_context_delete_dht_record", [_ctx.id!, jsonEncode(key)]));
} }
@override @override
Future<ValueData?> getDHTValue( Future<ValueData?> getDHTValue(
TypedKey key, int subkey, bool forceRefresh) async { TypedKey key, int subkey, bool forceRefresh) async {
_ctx.ensureValid();
final opt = await _wrapApiPromise(js_util.callMethod( final opt = await _wrapApiPromise(js_util.callMethod(
wasm, wasm,
"routing_context_get_dht_value", "routing_context_get_dht_value",
[_ctx.id, jsonEncode(key), subkey, forceRefresh])); [_ctx.id!, jsonEncode(key), subkey, forceRefresh]));
return opt == null ? null : ValueData.fromJson(jsonDecode(opt)); return opt == null ? null : ValueData.fromJson(jsonDecode(opt));
} }
@override @override
Future<ValueData?> setDHTValue( Future<ValueData?> setDHTValue(
TypedKey key, int subkey, Uint8List data) async { TypedKey key, int subkey, Uint8List data) async {
_ctx.ensureValid();
final opt = await _wrapApiPromise(js_util.callMethod( final opt = await _wrapApiPromise(js_util.callMethod(
wasm, wasm,
"routing_context_set_dht_value", "routing_context_set_dht_value",
[_ctx.id, jsonEncode(key), subkey, base64UrlNoPadEncode(data)])); [_ctx.id!, jsonEncode(key), subkey, base64UrlNoPadEncode(data)]));
return opt == null ? null : ValueData.fromJson(jsonDecode(opt)); return opt == null ? null : ValueData.fromJson(jsonDecode(opt));
} }
@override @override
Future<Timestamp> watchDHTValues(TypedKey key, List<ValueSubkeyRange> subkeys, Future<Timestamp> watchDHTValues(TypedKey key, List<ValueSubkeyRange> subkeys,
Timestamp expiration, int count) async { Timestamp expiration, int count) async {
_ctx.ensureValid();
final ts = await _wrapApiPromise(js_util.callMethod( final ts = await _wrapApiPromise(js_util.callMethod(
wasm, "routing_context_watch_dht_values", [ wasm, "routing_context_watch_dht_values", [
_ctx.id, _ctx.id!,
jsonEncode(key), jsonEncode(key),
jsonEncode(subkeys), jsonEncode(subkeys),
expiration.toString(), expiration.toString(),
@ -144,15 +172,16 @@ class VeilidRoutingContextJS implements VeilidRoutingContext {
@override @override
Future<bool> cancelDHTWatch(TypedKey key, List<ValueSubkeyRange> subkeys) { Future<bool> cancelDHTWatch(TypedKey key, List<ValueSubkeyRange> subkeys) {
_ctx.ensureValid();
return _wrapApiPromise(js_util.callMethod( return _wrapApiPromise(js_util.callMethod(
wasm, wasm,
"routing_context_cancel_dht_watch", "routing_context_cancel_dht_watch",
[_ctx.id, jsonEncode(key), jsonEncode(subkeys)])); [_ctx.id!, jsonEncode(key), jsonEncode(subkeys)]));
} }
} }
// JS implementation of VeilidCryptoSystem // JS implementation of VeilidCryptoSystem
class VeilidCryptoSystemJS implements VeilidCryptoSystem { class VeilidCryptoSystemJS extends VeilidCryptoSystem {
final CryptoKind _kind; final CryptoKind _kind;
final VeilidJS _js; final VeilidJS _js;
@ -327,105 +356,146 @@ class VeilidCryptoSystemJS implements VeilidCryptoSystem {
} }
class _TDBT { class _TDBT {
final int id; int? id;
VeilidTableDBJS tdbjs; final VeilidTableDBJS tdbjs;
VeilidJS js; final VeilidJS js;
_TDBT(this.id, this.tdbjs, this.js); _TDBT(this.id, this.tdbjs, this.js);
void ensureValid() {
if (id == null) {
throw VeilidAPIExceptionNotInitialized();
}
}
void close() {
if (id != null) {
js_util.callMethod(wasm, "release_table_db_transaction", [id!]);
id = null;
}
}
} }
// JS implementation of VeilidTableDBTransaction // JS implementation of VeilidTableDBTransaction
class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { class VeilidTableDBTransactionJS extends VeilidTableDBTransaction {
final _TDBT _tdbt; final _TDBT _tdbt;
static final Finalizer<_TDBT> _finalizer = Finalizer((tdbt) => static final Finalizer<_TDBT> _finalizer = Finalizer((tdbt) => tdbt.close());
js_util.callMethod(wasm, "release_table_db_transaction", [tdbt.id]));
VeilidTableDBTransactionJS._(this._tdbt) { VeilidTableDBTransactionJS._(this._tdbt) {
_finalizer.attach(this, _tdbt, detach: this); _finalizer.attach(this, _tdbt, detach: this);
} }
@override @override
Future<void> commit() { bool isDone() {
return _wrapApiPromise( return _tdbt.id == null;
js_util.callMethod(wasm, "table_db_transaction_commit", [_tdbt.id]));
} }
@override @override
Future<void> rollback() { Future<void> commit() async {
return _wrapApiPromise( _tdbt.ensureValid();
js_util.callMethod(wasm, "table_db_transaction_rollback", [_tdbt.id])); await _wrapApiPromise(
js_util.callMethod(wasm, "table_db_transaction_commit", [_tdbt.id!]));
_tdbt.close();
} }
@override @override
Future<void> store(int col, Uint8List key, Uint8List value) { Future<void> rollback() async {
_tdbt.ensureValid();
await _wrapApiPromise(
js_util.callMethod(wasm, "table_db_transaction_rollback", [_tdbt.id!]));
_tdbt.close();
}
@override
Future<void> store(int col, Uint8List key, Uint8List value) async {
_tdbt.ensureValid();
final encodedKey = base64UrlNoPadEncode(key); final encodedKey = base64UrlNoPadEncode(key);
final encodedValue = base64UrlNoPadEncode(value); final encodedValue = base64UrlNoPadEncode(value);
return _wrapApiPromise(js_util.callMethod( await _wrapApiPromise(js_util.callMethod(wasm, "table_db_transaction_store",
wasm, [_tdbt.id!, col, encodedKey, encodedValue]));
"table_db_transaction_store",
[_tdbt.id, col, encodedKey, encodedValue]));
} }
@override @override
Future<void> delete(int col, Uint8List key) { Future<void> delete(int col, Uint8List key) async {
_tdbt.ensureValid();
final encodedKey = base64UrlNoPadEncode(key); final encodedKey = base64UrlNoPadEncode(key);
return _wrapApiPromise(js_util.callMethod( await _wrapApiPromise(js_util.callMethod(
wasm, "table_db_transaction_delete", [_tdbt.id, col, encodedKey])); wasm, "table_db_transaction_delete", [_tdbt.id!, col, encodedKey]));
} }
} }
class _TDB { class _TDB {
final int id; int? id;
VeilidJS js; final VeilidJS js;
_TDB(this.id, this.js); _TDB(int this.id, this.js);
void ensureValid() {
if (id == null) {
throw VeilidAPIExceptionNotInitialized();
}
}
void close() {
if (id != null) {
js_util.callMethod(wasm, "release_table_db", [id!]);
id = null;
}
}
} }
// JS implementation of VeilidTableDB // JS implementation of VeilidTableDB
class VeilidTableDBJS extends VeilidTableDB { class VeilidTableDBJS extends VeilidTableDB {
final _TDB _tdb; final _TDB _tdb;
static final Finalizer<_TDB> _finalizer = Finalizer( static final Finalizer<_TDB> _finalizer = Finalizer((tdb) => tdb.close());
(tdb) => js_util.callMethod(wasm, "release_table_db", [tdb.id]));
VeilidTableDBJS._(this._tdb) { VeilidTableDBJS._(this._tdb) {
_finalizer.attach(this, _tdb, detach: this); _finalizer.attach(this, _tdb, detach: this);
} }
@override
void close() {
_tdb.close();
}
@override @override
int getColumnCount() { int getColumnCount() {
return js_util.callMethod(wasm, "table_db_get_column_count", [_tdb.id]); _tdb.ensureValid();
return js_util.callMethod(wasm, "table_db_get_column_count", [_tdb.id!]);
} }
@override @override
Future<List<Uint8List>> getKeys(int col) async { Future<List<Uint8List>> getKeys(int col) async {
_tdb.ensureValid();
return jsonListConstructor(base64UrlNoPadDecodeDynamic)(jsonDecode( return jsonListConstructor(base64UrlNoPadDecodeDynamic)(jsonDecode(
await js_util.callMethod(wasm, "table_db_get_keys", [_tdb.id, col]))); await js_util.callMethod(wasm, "table_db_get_keys", [_tdb.id!, col])));
} }
@override @override
VeilidTableDBTransaction transact() { VeilidTableDBTransaction transact() {
final id = js_util.callMethod(wasm, "table_db_transact", [_tdb.id]); _tdb.ensureValid();
final id = js_util.callMethod(wasm, "table_db_transact", [_tdb.id!]);
return VeilidTableDBTransactionJS._(_TDBT(id, this, _tdb.js)); return VeilidTableDBTransactionJS._(_TDBT(id, this, _tdb.js));
} }
@override @override
Future<void> store(int col, Uint8List key, Uint8List value) { Future<void> store(int col, Uint8List key, Uint8List value) {
_tdb.ensureValid();
final encodedKey = base64UrlNoPadEncode(key); final encodedKey = base64UrlNoPadEncode(key);
final encodedValue = base64UrlNoPadEncode(value); final encodedValue = base64UrlNoPadEncode(value);
return _wrapApiPromise(js_util.callMethod( return _wrapApiPromise(js_util.callMethod(
wasm, "table_db_store", [_tdb.id, col, encodedKey, encodedValue])); wasm, "table_db_store", [_tdb.id!, col, encodedKey, encodedValue]));
} }
@override @override
Future<Uint8List?> load(int col, Uint8List key) async { Future<Uint8List?> load(int col, Uint8List key) async {
_tdb.ensureValid();
final encodedKey = base64UrlNoPadEncode(key); final encodedKey = base64UrlNoPadEncode(key);
String? out = await _wrapApiPromise( String? out = await _wrapApiPromise(
js_util.callMethod(wasm, "table_db_load", [_tdb.id, col, encodedKey])); js_util.callMethod(wasm, "table_db_load", [_tdb.id!, col, encodedKey]));
if (out == null) { if (out == null) {
return null; return null;
} }
@ -434,16 +504,17 @@ class VeilidTableDBJS extends VeilidTableDB {
@override @override
Future<Uint8List?> delete(int col, Uint8List key) { Future<Uint8List?> delete(int col, Uint8List key) {
_tdb.ensureValid();
final encodedKey = base64UrlNoPadEncode(key); final encodedKey = base64UrlNoPadEncode(key);
return _wrapApiPromise(js_util return _wrapApiPromise(js_util
.callMethod(wasm, "table_db_delete", [_tdb.id, col, encodedKey])); .callMethod(wasm, "table_db_delete", [_tdb.id!, col, encodedKey]));
} }
} }
// JS implementation of high level Veilid API // JS implementation of high level Veilid API
class VeilidJS implements Veilid { class VeilidJS extends Veilid {
@override @override
void initializeVeilidCore(Map<String, dynamic> platformConfigJson) { void initializeVeilidCore(Map<String, dynamic> platformConfigJson) {
var platformConfigJsonString = jsonEncode(platformConfigJson); var platformConfigJsonString = jsonEncode(platformConfigJson);

View File

@ -5,6 +5,7 @@ import 'dart:convert';
///////////////////////////////////// /////////////////////////////////////
/// VeilidTableDB /// VeilidTableDB
abstract class VeilidTableDBTransaction { abstract class VeilidTableDBTransaction {
bool isDone();
Future<void> commit(); Future<void> commit();
Future<void> rollback(); Future<void> rollback();
Future<void> store(int col, Uint8List key, Uint8List value); Future<void> store(int col, Uint8List key, Uint8List value);
@ -24,6 +25,7 @@ abstract class VeilidTableDBTransaction {
} }
abstract class VeilidTableDB { abstract class VeilidTableDB {
void close();
int getColumnCount(); int getColumnCount();
Future<List<Uint8List>> getKeys(int col); Future<List<Uint8List>> getKeys(int col);
VeilidTableDBTransaction transact(); VeilidTableDBTransaction transact();

View File

@ -126,19 +126,21 @@ fn main() -> EyreResult<()> {
} }
// --- Normal Startup --- // --- Normal Startup ---
let orig_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
// invoke the default handler and exit the process
orig_hook(panic_info);
let backtrace = backtrace::Backtrace::new();
eprintln!("Backtrace:\n{:?}", backtrace);
eprintln!("exiting!");
std::process::exit(1);
}));
let panic_on_shutdown = matches.occurrences_of("panic") != 0; let panic_on_shutdown = matches.occurrences_of("panic") != 0;
ctrlc::set_handler(move || { ctrlc::set_handler(move || {
if panic_on_shutdown { if panic_on_shutdown {
let orig_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
// invoke the default handler and exit the process
orig_hook(panic_info);
let backtrace = backtrace::Backtrace::new();
eprintln!("Backtrace:\n{:?}", backtrace);
std::process::exit(1);
}));
panic!("panic requested"); panic!("panic requested");
} else { } else {
shutdown(); shutdown();

View File

@ -102,7 +102,7 @@ core:
set_value_count: 5 set_value_count: 5
set_value_fanout: 4 set_value_fanout: 4
min_peer_count: 20 min_peer_count: 20
min_peer_refresh_time_ms: 2000 min_peer_refresh_time_ms: 60000
validate_dial_info_receipt_time_ms: 2000 validate_dial_info_receipt_time_ms: 2000
local_subkey_cache_size: 128 local_subkey_cache_size: 128
local_max_subkey_cache_memory_mb: 256 local_max_subkey_cache_memory_mb: 256
@ -1620,7 +1620,7 @@ mod tests {
assert_eq!(s.core.network.dht.set_value_count, 5u32); assert_eq!(s.core.network.dht.set_value_count, 5u32);
assert_eq!(s.core.network.dht.set_value_fanout, 4u32); assert_eq!(s.core.network.dht.set_value_fanout, 4u32);
assert_eq!(s.core.network.dht.min_peer_count, 20u32); assert_eq!(s.core.network.dht.min_peer_count, 20u32);
assert_eq!(s.core.network.dht.min_peer_refresh_time_ms, 2_000u32); assert_eq!(s.core.network.dht.min_peer_refresh_time_ms, 60_000u32);
assert_eq!( assert_eq!(
s.core.network.dht.validate_dial_info_receipt_time_ms, s.core.network.dht.validate_dial_info_receipt_time_ms,
2_000u32 2_000u32