2021-03-08 11:49:48 -06: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use fce_sqlite_connector;
|
|
|
|
use fce_sqlite_connector::{Connection, State, Value};
|
2021-06-13 15:46:35 -05:00
|
|
|
///, WasmLoggerBuilder};
|
|
|
|
use fluence;
|
|
|
|
use fluence::marine;
|
|
|
|
use fluence::WasmLoggerBuilder;
|
2021-03-08 11:49:48 -06:00
|
|
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde_json;
|
2021-06-13 15:46:35 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-03-08 11:49:48 -06:00
|
|
|
|
2021-03-08 17:35:11 -06:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
|
|
use crate::auth::is_owner;
|
2021-06-13 15:46:35 -05:00
|
|
|
use crate::crud::create_table;
|
2021-03-08 11:49:48 -06:00
|
|
|
|
2021-06-13 15:46:35 -05:00
|
|
|
const DB_PATH: &str = "/tmp/fluence_service_db.sqlite";
|
2021-03-08 11:49:48 -06:00
|
|
|
|
|
|
|
mod auth;
|
2021-06-13 15:46:35 -05:00
|
|
|
mod crud;
|
2021-03-08 11:49:48 -06:00
|
|
|
|
2021-03-08 17:35:11 -06:00
|
|
|
pub static INIT: AtomicBool = AtomicBool::new(false);
|
2021-03-08 11:49:48 -06:00
|
|
|
|
2021-03-09 13:34:20 -06:00
|
|
|
fn main() {}
|
|
|
|
|
2021-03-08 11:49:48 -06:00
|
|
|
fn get_connection() -> Connection {
|
|
|
|
Connection::open(DB_PATH).unwrap()
|
|
|
|
}
|
|
|
|
|
2021-06-13 15:46:35 -05:00
|
|
|
#[marine]
|
2021-03-08 17:35:11 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InitResult {
|
|
|
|
pub success: bool,
|
|
|
|
pub err_msg: String,
|
2021-03-08 11:49:48 -06:00
|
|
|
}
|
|
|
|
|
2021-06-13 16:12:46 -05:00
|
|
|
#[marine]
|
2021-03-09 13:34:20 -06:00
|
|
|
pub fn init_service() -> InitResult {
|
|
|
|
if !is_owner() {
|
2021-06-13 15:46:35 -05:00
|
|
|
return InitResult {
|
|
|
|
success: false,
|
|
|
|
err_msg: "Not authorized to use this service".into(),
|
|
|
|
};
|
2021-03-08 17:35:11 -06:00
|
|
|
}
|
2021-03-08 11:49:48 -06:00
|
|
|
|
2021-03-08 17:35:11 -06:00
|
|
|
if INIT.load(Ordering::Relaxed) {
|
2021-06-13 15:46:35 -05:00
|
|
|
return InitResult {
|
|
|
|
success: false,
|
|
|
|
err_msg: "Service already initiated".into(),
|
|
|
|
};
|
2021-03-08 17:35:11 -06:00
|
|
|
}
|
2021-03-08 11:49:48 -06:00
|
|
|
|
2021-03-08 17:35:11 -06:00
|
|
|
let conn = get_connection();
|
|
|
|
let res = create_table(&conn);
|
|
|
|
if res.is_err() {
|
2021-06-13 15:46:35 -05:00
|
|
|
return InitResult {
|
|
|
|
success: false,
|
|
|
|
err_msg: "Failure to create tables".into(),
|
|
|
|
};
|
2021-03-08 17:35:11 -06:00
|
|
|
}
|
2021-03-09 13:34:20 -06:00
|
|
|
// TODO: implement rollbacks
|
2021-03-08 11:49:48 -06:00
|
|
|
|
2021-03-08 17:35:11 -06:00
|
|
|
INIT.store(true, Ordering::Relaxed);
|
2021-06-13 15:46:35 -05:00
|
|
|
InitResult {
|
|
|
|
success: true,
|
|
|
|
err_msg: "".into(),
|
|
|
|
}
|
2021-03-08 11:49:48 -06:00
|
|
|
}
|
|
|
|
|
2021-06-13 15:46:35 -05:00
|
|
|
#[marine]
|
2021-03-08 17:35:11 -06:00
|
|
|
pub fn owner_nuclear_reset() -> bool {
|
|
|
|
if !is_owner() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT.store(false, Ordering::Relaxed);
|
|
|
|
|
|
|
|
let conn = get_connection();
|
2021-03-09 13:34:20 -06:00
|
|
|
let t_names = vec!["reward_blocks"];
|
2021-03-08 17:35:11 -06:00
|
|
|
for t_name in t_names {
|
|
|
|
let stmt = format!("delete from {}", t_name);
|
|
|
|
let mut del_cur = conn.prepare(&stmt).unwrap().cursor();
|
|
|
|
del_cur.next().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
2021-03-08 11:49:48 -06:00
|
|
|
}
|