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-13 19:04:11 +03:00
|
|
|
use crate::results::{Key, GetKeyMetadataResult, RegisterKeyResult, RepublishKeyResult, PutValueResult, GetValuesResult, Record, RepublishValuesResult, ClearExpiredResult};
|
|
|
|
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};
|
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 * 1000;
|
|
|
|
pub static EXPIRED_VALUE_AGE: u64 = 24 * 60 * 60 * 1000;
|
|
|
|
|
2021-05-11 18:32:37 +03:00
|
|
|
pub static TRUSTED_TIMESTAMP_SERVICE_ID: &str = "peer";
|
|
|
|
pub static TRUSTED_TIMESTAMP_FUNCTION_NAME: &str = "timestamp_ms";
|
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]
|
|
|
|
pub fn get_key_metadata(key: String) -> GetKeyMetadataResult {
|
|
|
|
get_key_metadata_impl(key).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]
|
|
|
|
pub fn put_value(key: String, value: String, current_timestamp: u64, relay_id: Vec<String>) -> PutValueResult {
|
|
|
|
put_value_impl(key, value, current_timestamp, relay_id).into()
|
|
|
|
}
|
2021-05-11 18:58:06 +03:00
|
|
|
|
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]
|
|
|
|
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
|
|
|
|
|
|
|
// 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-11 18:32:37 +03:00
|
|
|
//
|
2021-05-11 18:58:06 +03:00
|
|
|
// #[marine]
|
2021-05-11 18:32:37 +03:00
|
|
|
// pub fn evict_stale(current_timestamp: u64) -> GetStaleResult {
|
|
|
|
// evict_stale_impl(current_timestamp).into()
|
|
|
|
// }
|