registry/src/main.rs

137 lines
4.5 KiB
Rust
Raw Normal View History

2021-05-11 18:32:37 +03:00
#![feature(try_trait)]
2021-04-27 16:53:01 +03:00
/*
* Copyright 2021 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mod results;
2021-05-11 18:32:37 +03:00
mod tests;
mod impls;
2021-04-27 16:53:01 +03:00
2021-05-31 13:30:23 +03:00
use crate::results::{Key, GetKeyMetadataResult, DhtResult, GetValuesResult, Record, RepublishValuesResult, ClearExpiredResult, EvictStaleResult, MergeResult};
use crate::impls::{create_keys_table, create_values_table, register_key_impl, get_key_metadata_impl, republish_key_impl, put_value_impl, get_values_impl, republish_values_impl, clear_expired_impl, evict_stale_impl, merge_impl, renew_host_value_impl, clear_host_value_impl};
2021-04-27 16:53:01 +03:00
2021-05-11 18:32:37 +03:00
use fluence::marine;
2021-04-26 10:45:28 +03:00
use fluence::module_manifest;
#[macro_use]
extern crate fstrings;
module_manifest!();
2021-05-11 18:32:37 +03:00
pub static KEYS_TABLE_NAME: &str = "dht_keys";
pub static VALUES_TABLE_NAME: &str = "dht_values";
2021-04-26 10:45:28 +03:00
pub static DB_PATH: &str = "/tmp/dht.db";
pub static STALE_VALUE_AGE: u64 = 60 * 60;
pub static EXPIRED_VALUE_AGE: u64 = 24 * 60 * 60;
2021-05-31 13:30:23 +03:00
pub static EXPIRED_HOST_VALUE_AGE: u64 = 10 * EXPIRED_VALUE_AGE;
pub static VALUES_LIMIT: usize = 20;
2021-04-26 10:45:28 +03:00
2021-05-11 18:32:37 +03:00
pub static TRUSTED_TIMESTAMP_SERVICE_ID: &str = "peer";
pub static TRUSTED_TIMESTAMP_FUNCTION_NAME: &str = "timestamp_sec";
2021-04-26 10:45:28 +03:00
fn main() {
2021-05-11 18:32:37 +03:00
create_keys_table();
create_values_table();
2021-04-26 10:45:28 +03:00
}
2021-05-11 18:32:37 +03:00
// KEYS
#[marine]
2021-05-31 13:30:23 +03:00
pub fn register_key(key: String, current_timestamp: u64, pin: bool, weight: u32) -> DhtResult {
register_key_impl(key, current_timestamp, pin, weight).into()
2021-04-26 10:45:28 +03:00
}
2021-05-11 18:32:37 +03:00
#[marine]
2021-05-17 15:35:57 +03:00
pub fn get_key_metadata(key: String, current_timestamp: u64) -> GetKeyMetadataResult {
get_key_metadata_impl(key, current_timestamp).into()
2021-04-26 10:45:28 +03:00
}
2021-05-11 18:32:37 +03:00
#[marine]
2021-05-31 13:30:23 +03:00
pub fn republish_key(key: Key, current_timestamp: u64) -> DhtResult {
2021-05-11 18:32:37 +03:00
republish_key_impl(key, current_timestamp).into()
2021-04-28 09:50:06 +03:00
}
2021-05-11 18:32:37 +03:00
// VALUES
#[marine]
2021-05-31 13:30:23 +03:00
pub fn put_value(key: String, value: String, current_timestamp: u64, relay_id: Vec<String>, service_id: Vec<String>, weight: u32) -> DhtResult {
put_value_impl(key, value, current_timestamp, relay_id, service_id, weight, false).into()
}
2021-05-11 18:58:06 +03:00
2021-05-26 23:58:46 +03:00
#[marine]
2021-05-31 13:30:23 +03:00
pub fn put_value_relay(key: String, value: String, current_timestamp: u64, relay_id: String, weight: u32) -> DhtResult {
put_value_impl(key, value, current_timestamp, vec![relay_id], vec![], weight, false).into()
}
#[marine]
pub fn put_host_value(key: String, value: String, current_timestamp: u64, relay_id: Vec<String>, service_id: Vec<String>, weight: u32) -> DhtResult {
put_value_impl(key, value, current_timestamp, relay_id, service_id, weight, true).into()
}
#[marine]
pub fn put_host_value_relay(key: String, value: String, current_timestamp: u64, relay_id: String, weight: u32) -> DhtResult {
put_value_impl(key, value, current_timestamp, vec![relay_id], vec![], weight, true).into()
2021-05-26 23:58:46 +03:00
}
#[marine]
pub fn get_values(key: String, current_timestamp: u64) -> GetValuesResult {
get_values_impl(key, current_timestamp).into()
}
2021-05-11 18:32:37 +03:00
#[marine]
2021-05-27 20:12:12 +03:00
pub fn republish_values(key: String, records: Vec<Record>, current_timestamp: u64) -> RepublishValuesResult {
republish_values_impl(key, records, current_timestamp).into()
}
2021-05-11 18:32:37 +03:00
2021-05-31 13:30:23 +03:00
#[marine]
pub fn renew_host_value(key: String, current_timestamp: u64) -> DhtResult {
renew_host_value_impl(key, current_timestamp).into()
}
#[marine]
pub fn clear_host_value(key: String, current_timestamp: u64) -> DhtResult {
clear_host_value_impl(key, current_timestamp).into()
}
2021-05-11 18:32:37 +03:00
// BOTH
#[marine]
pub fn clear_expired(current_timestamp: u64) -> ClearExpiredResult {
clear_expired_impl(current_timestamp).into()
}
2021-05-16 23:06:07 +03:00
2021-05-17 15:35:57 +03:00
#[marine]
pub fn evict_stale(current_timestamp: u64) -> EvictStaleResult {
evict_stale_impl(current_timestamp).into()
}
2021-05-26 23:58:46 +03:00
#[marine]
2021-05-27 12:45:43 +03:00
pub fn merge(records: Vec<Vec<Record>>) -> MergeResult {
merge_impl(records.into_iter().flatten().collect()).into()
2021-05-26 23:58:46 +03:00
}
#[marine]
2021-05-27 12:45:43 +03:00
pub fn merge_two(a: Vec<Record>, b: Vec<Record>) -> MergeResult {
2021-05-26 23:58:46 +03:00
merge_impl(a.into_iter().chain(b.into_iter()).collect()).into()
}
2021-05-27 19:39:52 +03:00
2021-05-27 20:12:12 +03:00
#[marine]
pub fn merge_hack_get_values(records: Vec<GetValuesResult>) -> MergeResult {
merge_impl(
records
.into_iter()
.filter(|elem| elem.success)
.map(|elem| elem.result)
.flatten()
.collect()
).into()
2021-05-27 19:39:52 +03:00
}