registry/service/src/main.rs

123 lines
3.2 KiB
Rust
Raw Normal View History

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};
2022-02-24 16:37:58 +03:00
use crate::results::{ClearExpiredResult, EvictStaleResult};
use crate::storage_impl::get_storage;
use crate::tetraplets_checkers::check_timestamp_tetraplets;
2021-11-24 17:57:38 +03:00
mod config;
mod defaults;
mod error;
mod key;
mod key_api;
mod key_storage_impl;
2022-02-24 16:37:58 +03:00
mod misc;
mod record;
mod record_api;
mod record_storage_impl;
2021-11-24 17:57:38 +03:00
mod results;
2022-02-24 16:37:58 +03:00
mod storage_impl;
2021-11-24 17:57:38 +03:00
mod tests;
2022-02-24 16:37:58 +03:00
mod tetraplets_checkers;
2021-06-03 14:32:11 +03:00
2021-04-26 10:45:28 +03:00
#[macro_use]
extern crate fstrings;
/*
2022-07-20 15:23:43 +04:00
_initialize function that calls __wasm_call_ctors is required to mitigade memory leak
that is described in https://github.com/WebAssembly/wasi-libc/issues/298
2022-07-20 15:23:43 +04:00
In short, without this code rust wraps every export function
with __wasm_call_ctors/__wasm_call_dtors calls. This causes memory leaks. When compiler sees
an explicit call to __wasm_call_ctors in _initialize function, it disables export wrapping.
2022-07-20 15:23:43 +04:00
TODO: remove when updating to marine-rs-sdk with fix
*/
extern "C" {
pub fn __wasm_call_ctors();
}
#[no_mangle]
fn _initialize() {
unsafe {
__wasm_call_ctors();
}
}
//------------------------------
2021-04-26 10:45:28 +03:00
module_manifest!();
2022-02-24 16:37:58 +03:00
pub fn wrapped_try<F, T>(func: F) -> T
where
F: FnOnce() -> T,
{
func()
}
2021-05-11 18:32:37 +03:00
2022-02-24 16:37:58 +03:00
// TODO: ship tg results as crate, remove duplication
2021-05-31 13:30:23 +03:00
#[marine]
2022-02-24 16:37:58 +03:00
pub struct WeightResult {
pub success: bool,
pub weight: u32,
pub peer_id: String,
pub error: String,
2021-05-31 13:30:23 +03:00
}
2022-02-24 16:37:58 +03:00
fn main() {
_initialize(); // As __wasm_call_ctors still does necessary work, we call it at the start of the module
2022-02-24 16:37:58 +03:00
let storage = get_storage().unwrap();
storage.create_key_tables();
2022-02-24 16:37:58 +03:00
storage.create_values_table();
create_config();
2021-05-31 13:30:23 +03:00
}
#[marine]
2021-06-03 14:56:45 +03:00
pub fn clear_expired(current_timestamp_sec: u64) -> ClearExpiredResult {
2022-02-24 16:37:58 +03:00
wrapped_try(|| {
let call_parameters = marine_rs_sdk::get_call_parameters();
check_timestamp_tetraplets(&call_parameters, 0)?;
get_storage()?.clear_expired(current_timestamp_sec)
})
.into()
}
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 {
2022-02-24 16:37:58 +03:00
wrapped_try(|| {
let call_parameters = marine_rs_sdk::get_call_parameters();
check_timestamp_tetraplets(&call_parameters, 0)?;
get_storage()?.evict_stale(current_timestamp_sec)
})
2021-11-24 17:57:38 +03:00
.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_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
}