Most of server is finished.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
|
||||
|
||||
use std::{ops::Not, path::Path, u64, u128};
|
||||
use std::{path::Path, u64};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
|
||||
@@ -4,12 +4,8 @@ use redb::{
|
||||
Database, ReadableDatabase, ReadableTable, ReadableTableMetadata, TableDefinition
|
||||
};
|
||||
// use std::collections::HashMap;
|
||||
use std::{ops::Not, path::Path, u64, u128};
|
||||
use std::{path::Path, u64, u128};
|
||||
|
||||
//tables defenition
|
||||
const DB_FILE: &str = "./data";
|
||||
|
||||
const COMPLETED_RANGES: TableDefinition<u128, u128> = TableDefinition::new("completed_ranges");
|
||||
|
||||
// const RANGE_STATUS: MultimapTableDefinition<bool, u128> =
|
||||
// MultimapTableDefinition::new("range_status");
|
||||
@@ -183,15 +179,15 @@ impl RangeRecord {
|
||||
}
|
||||
}
|
||||
|
||||
fn tweaks_left(&self) -> u128 {
|
||||
let t_left = (self.tweak_end - self.tweak_current) + 1;
|
||||
if t_left == 1 && self.key_committed_progress == u128::MAX {
|
||||
0u128
|
||||
}
|
||||
else {
|
||||
t_left
|
||||
}
|
||||
}
|
||||
// fn tweaks_left(&self) -> u128 {
|
||||
// let t_left = (self.tweak_end - self.tweak_current) + 1;
|
||||
// if t_left == 1 && self.key_committed_progress == u128::MAX {
|
||||
// 0u128
|
||||
// }
|
||||
// else {
|
||||
// t_left
|
||||
// }
|
||||
// }
|
||||
|
||||
fn completed_ratio(&self) -> f64 {
|
||||
let t_left = self.tweak_end - self.tweak_current;
|
||||
@@ -268,15 +264,15 @@ struct RangesTable<'a> {
|
||||
}
|
||||
|
||||
impl<'a> RangesTable<'a> {
|
||||
fn new(
|
||||
ranges: redb::Table<'a, u16, (u128, u128, u128, u128)>,
|
||||
ranges_to_use: redb::Table<'a, u16, ()>,
|
||||
) -> Self {
|
||||
RangesTable {
|
||||
ranges: ranges,
|
||||
ranges_to_use: ranges_to_use,
|
||||
}
|
||||
}
|
||||
// fn new(
|
||||
// ranges: redb::Table<'a, u16, (u128, u128, u128, u128)>,
|
||||
// ranges_to_use: redb::Table<'a, u16, ()>,
|
||||
// ) -> Self {
|
||||
// RangesTable {
|
||||
// ranges: ranges,
|
||||
// ranges_to_use: ranges_to_use,
|
||||
// }
|
||||
// }
|
||||
|
||||
fn open_rw(trx: &'a redb::WriteTransaction) -> Result<Self, redb::Error> {
|
||||
Ok(RangesTable {
|
||||
@@ -333,16 +329,16 @@ impl<'a> RangesTable<'a> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compute_progress(&self) -> Result<f64, redb::StorageError> {
|
||||
let mut mean_prog: f64 = 0.0;
|
||||
for range_res in self.ranges.iter()? {
|
||||
let ag = range_res?;
|
||||
let range = RangeRecord::from_value(ag.0.value(), &ag.1.value());
|
||||
mean_prog += range.completed_ratio();
|
||||
}
|
||||
// fn compute_progress(&self) -> Result<f64, redb::StorageError> {
|
||||
// let mut mean_prog: f64 = 0.0;
|
||||
// for range_res in self.ranges.iter()? {
|
||||
// let ag = range_res?;
|
||||
// let range = RangeRecord::from_value(ag.0.value(), &ag.1.value());
|
||||
// mean_prog += range.completed_ratio();
|
||||
// }
|
||||
|
||||
Ok(mean_prog / 65536.0f64)
|
||||
}
|
||||
// Ok(mean_prog / 65536.0f64)
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -452,6 +448,7 @@ pub struct FoundKey {
|
||||
pub enc_key: u128,
|
||||
pub timestump: u64
|
||||
}
|
||||
|
||||
pub fn db_get_found_keys(db: &Database) -> Result<Vec<FoundKey>, redb::Error> {
|
||||
let trx: redb::ReadTransaction = db.begin_read()?;
|
||||
let mut res:Vec<FoundKey> = Vec::new();
|
||||
@@ -477,10 +474,10 @@ pub fn db_put_found_key(db: &Database, tw_k: u128, en_k: u128) -> Result<u64, re
|
||||
let trx: redb::WriteTransaction = db.begin_write()?;
|
||||
let new_keys_count = {
|
||||
let mut found_keys_t = trx.open_table(FOUND_KEYS_TABLE)?;
|
||||
found_keys_t.insert((tw_k,en_k), get_timestump());
|
||||
found_keys_t.insert((tw_k,en_k), get_timestump())?;
|
||||
found_keys_t.len()?
|
||||
};
|
||||
trx.commit();
|
||||
trx.commit()?;
|
||||
|
||||
return Ok(new_keys_count);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// i: [u32; 2],
|
||||
// }
|
||||
|
||||
use prost::bytes::BufMut;
|
||||
|
||||
fn u128_to_u32arr(a: u128) -> [u32;4] {
|
||||
let mut res = [0u32;4];
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
use std::ops::DerefMut;
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::{path::PathBuf};
|
||||
|
||||
|
||||
use redb;
|
||||
use clap::Parser;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::{Arc};
|
||||
use tokio::sync::RwLock as AsyncRwLock;
|
||||
use tokio::time::{interval, Duration};
|
||||
|
||||
use tonic::{Request, Response, Status, transport::Server};
|
||||
|
||||
@@ -20,7 +17,7 @@ pub mod nyash_proto {
|
||||
|
||||
use nyash_proto::nyash_luks_server::{NyashLuks, NyashLuksServer};
|
||||
use nyash_proto::{
|
||||
CommitReply, KeyData, ProgressReply, ProgressRequest, WorkCommit, WorkData, WorkReply, WorkRequest, work_commit,
|
||||
CommitReply, ProgressReply, ProgressRequest, WorkCommit, WorkData, WorkReply, WorkRequest, work_commit,
|
||||
work_reply,
|
||||
};
|
||||
|
||||
@@ -43,27 +40,6 @@ fn work_rec_to_work_data(work_rec: database::JobRecord) -> WorkData {
|
||||
}
|
||||
}
|
||||
|
||||
// Shared state
|
||||
// #[derive(Clone, Debug)]
|
||||
// pub struct ServiceState {
|
||||
// pub db: Arc<redb::Database>,
|
||||
// pub key_found: bool,
|
||||
// pub progress: f64,
|
||||
// }
|
||||
|
||||
// Background task to update state
|
||||
// async fn update_service_state(state: Arc<AsyncRwLock<ServiceState>>) {
|
||||
// loop {
|
||||
// tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
||||
// let mut guard = state.write().await;
|
||||
// match database::db_get_progress(&guard.db) {
|
||||
// Ok(p) => {
|
||||
// guard.progress = p;
|
||||
// },
|
||||
// Err(_) => println!("Error getting progress from DB")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// Background task to update progress from DB
|
||||
async fn update_service_progress(progress: Arc<AsyncRwLock<f64>>, db: Arc<redb::Database>) {
|
||||
@@ -91,6 +67,12 @@ pub struct NyashService {
|
||||
impl NyashLuks for NyashService {
|
||||
async fn request_work(&self, request: Request<WorkRequest>) -> Result<Response<WorkReply>, Status> {
|
||||
println!("Got a request: {:?}", request);
|
||||
|
||||
|
||||
if *self.key_found.read().await == true {
|
||||
return Ok(Response::new(WorkReply { result: Some(work_reply::Result::NoWork(true)) }));
|
||||
}
|
||||
|
||||
let pref_work_size: u64 = request.into_inner().pref_work_size;
|
||||
|
||||
match database::db_create_job(&self.db, pref_work_size) {
|
||||
@@ -196,7 +178,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if found_keys.len() >0 {
|
||||
println!("Key already found!");
|
||||
for key in found_keys {
|
||||
println!("Key found: {:?}", key);
|
||||
println!("Key found! Timestump: {}, Tweak key: {}, Enc key: {}",key.timestump, key.tweak_key, key.enc_key);
|
||||
}
|
||||
println!("Exit!");
|
||||
return Ok(());
|
||||
|
||||
Reference in New Issue
Block a user