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.
|
|
|
|
*/
|
|
|
|
|
2021-06-17 19:26:55 +03:00
|
|
|
use marine_rs_sdk::marine;
|
|
|
|
use marine_rs_sdk::module_manifest;
|
2021-04-26 10:45:28 +03:00
|
|
|
|
2021-11-24 17:57:38 +03:00
|
|
|
use crate::config::{create_config, load_config, write_config};
|
|
|
|
use crate::impls::{
|
|
|
|
clear_expired_impl, clear_host_value_impl, create_keys_table, create_values_table,
|
|
|
|
evict_stale_impl, get_key_metadata_impl, get_values_impl, merge_impl,
|
|
|
|
propagate_host_value_impl, put_value_impl, register_key_impl, renew_host_value_impl,
|
|
|
|
republish_key_impl, republish_values_impl,
|
|
|
|
};
|
|
|
|
use crate::results::{
|
|
|
|
ClearExpiredResult, DhtResult, EvictStaleResult, GetKeyMetadataResult, GetValuesResult, Key,
|
|
|
|
MergeResult, PutHostValueResult, Record, RepublishValuesResult,
|
|
|
|
};
|
|
|
|
|
|
|
|
mod config;
|
|
|
|
mod defaults;
|
|
|
|
mod error;
|
|
|
|
mod impls;
|
|
|
|
mod results;
|
|
|
|
mod tests;
|
2021-06-03 14:32:11 +03:00
|
|
|
|
2021-04-26 10:45:28 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate fstrings;
|
|
|
|
|
|
|
|
module_manifest!();
|
|
|
|
|
|
|
|
fn main() {
|
2021-05-11 18:32:37 +03:00
|
|
|
create_keys_table();
|
|
|
|
create_values_table();
|
2021-06-03 14:32:11 +03:00
|
|
|
create_config();
|
2021-04-26 10:45:28 +03:00
|
|
|
}
|
|
|
|
|
2021-05-11 18:32:37 +03:00
|
|
|
// KEYS
|
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn register_key(key: String, current_timestamp_sec: u64, pin: bool, weight: u32) -> DhtResult {
|
|
|
|
register_key_impl(key, current_timestamp_sec, pin, weight).into()
|
2021-04-26 10:45:28 +03:00
|
|
|
}
|
|
|
|
|
2021-05-11 18:32:37 +03:00
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn get_key_metadata(key: String, current_timestamp_sec: u64) -> GetKeyMetadataResult {
|
|
|
|
get_key_metadata_impl(key, current_timestamp_sec).into()
|
2021-04-26 10:45:28 +03:00
|
|
|
}
|
|
|
|
|
2021-05-11 18:32:37 +03:00
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn republish_key(key: Key, current_timestamp_sec: u64) -> DhtResult {
|
|
|
|
republish_key_impl(key, current_timestamp_sec).into()
|
2021-04-28 09:50:06 +03:00
|
|
|
}
|
|
|
|
|
2021-05-11 18:32:37 +03:00
|
|
|
// VALUES
|
2021-05-13 15:33:45 +03:00
|
|
|
#[marine]
|
2021-11-24 17:57:38 +03:00
|
|
|
pub fn put_value(
|
|
|
|
key: String,
|
|
|
|
value: String,
|
|
|
|
current_timestamp_sec: u64,
|
|
|
|
relay_id: Vec<String>,
|
|
|
|
service_id: Vec<String>,
|
|
|
|
weight: u32,
|
|
|
|
) -> DhtResult {
|
|
|
|
put_value_impl(
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
current_timestamp_sec,
|
|
|
|
relay_id,
|
|
|
|
service_id,
|
|
|
|
weight,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.map(|_| ())
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[marine]
|
|
|
|
pub fn put_host_value(
|
|
|
|
key: String,
|
|
|
|
value: String,
|
|
|
|
current_timestamp_sec: u64,
|
|
|
|
relay_id: Vec<String>,
|
|
|
|
service_id: Vec<String>,
|
|
|
|
weight: u32,
|
|
|
|
) -> PutHostValueResult {
|
|
|
|
let mut result: PutHostValueResult = put_value_impl(
|
|
|
|
key.clone(),
|
|
|
|
value,
|
|
|
|
current_timestamp_sec,
|
|
|
|
relay_id,
|
|
|
|
service_id,
|
|
|
|
weight,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.into();
|
|
|
|
|
|
|
|
// key is needed to be passed to propagate_host_value
|
2021-06-08 14:29:15 +03:00
|
|
|
result.key = key;
|
2021-05-31 13:30:23 +03:00
|
|
|
|
2021-06-08 14:29:15 +03:00
|
|
|
result
|
2021-05-31 13:30:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[marine]
|
2021-11-24 17:57:38 +03:00
|
|
|
pub fn propagate_host_value(
|
|
|
|
set_host_value: PutHostValueResult,
|
|
|
|
current_timestamp_sec: u64,
|
|
|
|
weight: u32,
|
|
|
|
) -> DhtResult {
|
2021-06-08 14:29:15 +03:00
|
|
|
propagate_host_value_impl(set_host_value, current_timestamp_sec, weight).into()
|
2021-05-26 23:58:46 +03:00
|
|
|
}
|
2021-05-13 15:33:45 +03:00
|
|
|
|
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn get_values(key: String, current_timestamp_sec: u64) -> GetValuesResult {
|
|
|
|
get_values_impl(key, current_timestamp_sec).into()
|
2021-05-13 15:33:45 +03:00
|
|
|
}
|
2021-05-11 18:32:37 +03:00
|
|
|
|
2021-05-13 19:04:11 +03:00
|
|
|
#[marine]
|
2021-11-24 17:57:38 +03:00
|
|
|
pub fn republish_values(
|
|
|
|
key: String,
|
|
|
|
records: Vec<Record>,
|
|
|
|
current_timestamp_sec: u64,
|
|
|
|
) -> RepublishValuesResult {
|
2021-06-03 14:56:45 +03:00
|
|
|
republish_values_impl(key, records, current_timestamp_sec).into()
|
2021-05-13 19:04:11 +03:00
|
|
|
}
|
2021-05-11 18:32:37 +03:00
|
|
|
|
2021-05-31 13:30:23 +03:00
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn renew_host_value(key: String, current_timestamp_sec: u64) -> DhtResult {
|
|
|
|
renew_host_value_impl(key, current_timestamp_sec).into()
|
2021-05-31 13:30:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn clear_host_value(key: String, current_timestamp_sec: u64) -> DhtResult {
|
|
|
|
clear_host_value_impl(key, current_timestamp_sec).into()
|
2021-05-31 13:30:23 +03:00
|
|
|
}
|
|
|
|
|
2021-05-11 18:32:37 +03:00
|
|
|
// BOTH
|
2021-05-13 19:04:11 +03:00
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn clear_expired(current_timestamp_sec: u64) -> ClearExpiredResult {
|
|
|
|
clear_expired_impl(current_timestamp_sec).into()
|
2021-05-13 19:04:11 +03:00
|
|
|
}
|
2021-05-16 23:06:07 +03:00
|
|
|
|
2021-05-17 15:35:57 +03:00
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn evict_stale(current_timestamp_sec: u64) -> EvictStaleResult {
|
|
|
|
evict_stale_impl(current_timestamp_sec).into()
|
2021-05-17 15:35:57 +03:00
|
|
|
}
|
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()
|
2021-11-24 17:57:38 +03:00
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
.into()
|
2021-06-03 14:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn set_expired_timeout(timeout_sec: u64) {
|
2021-06-03 14:32:11 +03:00
|
|
|
let mut config = load_config();
|
2021-06-03 14:56:45 +03:00
|
|
|
config.expired_timeout = timeout_sec;
|
2021-06-03 14:32:11 +03:00
|
|
|
write_config(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn set_host_expired_timeout(timeout_sec: u64) {
|
2021-06-03 14:32:11 +03:00
|
|
|
let mut config = load_config();
|
2021-06-03 14:56:45 +03:00
|
|
|
config.host_expired_timeout = timeout_sec;
|
2021-06-03 14:32:11 +03:00
|
|
|
write_config(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[marine]
|
2021-06-03 14:56:45 +03:00
|
|
|
pub fn set_stale_timeout(timeout_sec: u64) {
|
2021-06-03 14:32:11 +03:00
|
|
|
let mut config = load_config();
|
2021-06-03 14:56:45 +03:00
|
|
|
config.stale_timeout = timeout_sec;
|
2021-06-03 14:32:11 +03:00
|
|
|
write_config(config);
|
2021-06-03 14:39:15 +03:00
|
|
|
}
|