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-27 20:06:47 +03:00
|
|
|
use crate::results::{Key, GetKeyMetadataResult, RegisterKeyResult, RepublishKeyResult, PutValueResult, GetValuesResult, Record, RepublishValuesResult, ClearExpiredResult, EvictStaleResult, MergeResult, RecordsStruct};
|
2021-05-26 23:58:46 +03:00
|
|
|
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};
|
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";
|
2021-05-19 01:10:41 +03:00
|
|
|
pub static STALE_VALUE_AGE: u64 = 60 * 60;
|
|
|
|
pub static EXPIRED_VALUE_AGE: u64 = 24 * 60 * 60;
|
2021-04-26 10:45:28 +03:00
|
|
|
|
2021-05-11 18:32:37 +03:00
|
|
|
pub static TRUSTED_TIMESTAMP_SERVICE_ID: &str = "peer";
|
2021-05-19 01:10:41 +03:00
|
|
|
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]
|
|
|
|
pub fn register_key(key: String, current_timestamp: u64) -> RegisterKeyResult {
|
|
|
|
register_key_impl(key, current_timestamp).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]
|
|
|
|
pub fn republish_key(key: Key, current_timestamp: u64) -> RepublishKeyResult {
|
|
|
|
republish_key_impl(key, current_timestamp).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-05-18 17:33:32 +03:00
|
|
|
pub fn put_value(key: String, value: String, current_timestamp: u64, relay_id: Vec<String>, service_id: Vec<String>) -> PutValueResult {
|
|
|
|
put_value_impl(key, value, current_timestamp, relay_id, service_id).into()
|
2021-05-13 15:33:45 +03:00
|
|
|
}
|
2021-05-11 18:58:06 +03:00
|
|
|
|
2021-05-26 23:58:46 +03:00
|
|
|
#[marine]
|
|
|
|
pub fn put_value_relay(key: String, value: String, current_timestamp: u64, relay_id: String) -> PutValueResult {
|
|
|
|
put_value_impl(key, value, current_timestamp, vec![relay_id], vec![]).into()
|
|
|
|
}
|
2021-05-13 15:33:45 +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
|
|
|
|
2021-05-13 19:04:11 +03:00
|
|
|
#[marine]
|
2021-05-27 20:12:12 +03:00
|
|
|
pub fn republish_values(key: String, records: Vec<Record>, current_timestamp: u64) -> RepublishValuesResult {
|
2021-05-13 19:04:11 +03:00
|
|
|
republish_values_impl(key, records, current_timestamp).into()
|
|
|
|
}
|
2021-05-11 18:32:37 +03:00
|
|
|
|
|
|
|
// BOTH
|
2021-05-13 19:04:11 +03:00
|
|
|
#[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
|
|
|
|
|
|
|
#[marine]
|
2021-05-27 19:52:08 +03:00
|
|
|
pub fn merge_hack(records: Vec<Vec<Record>>, hack: String) -> MergeResult {
|
2021-05-27 20:12:12 +03:00
|
|
|
print!("{}", hack);
|
2021-05-27 19:39:52 +03:00
|
|
|
merge(records)
|
|
|
|
}
|
2021-05-27 20:12:12 +03:00
|
|
|
|
2021-05-27 19:39:52 +03:00
|
|
|
#[marine]
|
|
|
|
pub fn merge_wrapped(records: Vec<Vec<Vec<Record>>>) -> MergeResult {
|
|
|
|
merge_impl(records.into_iter().flatten().flatten().collect()).into()
|
2021-05-27 20:06:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[marine]
|
|
|
|
pub fn merge_hack_struct(records: RecordsStruct) -> MergeResult {
|
|
|
|
merge_impl(records.records).into()
|
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
|
|
|
}
|