104 lines
2.4 KiB
Rust
Raw Normal View History

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};
///, WasmLoggerBuilder};
use fluence;
use fluence::marine;
use fluence::WasmLoggerBuilder;
2021-03-08 11:49:48 -06:00
use serde::Deserialize;
use serde_json;
use std::path::{Path, PathBuf};
2021-03-08 11:49:48 -06:00
use std::sync::atomic::{AtomicBool, Ordering};
use crate::auth::is_owner;
use crate::crud::create_table;
2021-03-08 11:49:48 -06:00
const DB_PATH: &str = "/tmp/fluence_service_db.sqlite";
2021-03-08 11:49:48 -06:00
mod auth;
mod crud;
2021-03-08 11:49:48 -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()
}
#[marine]
#[derive(Debug)]
pub struct InitResult {
pub success: bool,
pub err_msg: String,
2021-03-08 11:49:48 -06:00
}
#[fmarinece]
2021-03-09 13:34:20 -06:00
pub fn init_service() -> InitResult {
if !is_owner() {
return InitResult {
success: false,
err_msg: "Not authorized to use this service".into(),
};
}
2021-03-08 11:49:48 -06:00
if INIT.load(Ordering::Relaxed) {
return InitResult {
success: false,
err_msg: "Service already initiated".into(),
};
}
2021-03-08 11:49:48 -06:00
let conn = get_connection();
let res = create_table(&conn);
if res.is_err() {
return InitResult {
success: false,
err_msg: "Failure to create tables".into(),
};
}
2021-03-09 13:34:20 -06:00
// TODO: implement rollbacks
2021-03-08 11:49:48 -06:00
INIT.store(true, Ordering::Relaxed);
InitResult {
success: true,
err_msg: "".into(),
}
2021-03-08 11:49:48 -06:00
}
#[marine]
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"];
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
}