Skip to content

Commit bf9212c

Browse files
authored
feat:the proof of the Pois algorithm is partially completed (#446)
1 parent 28feb97 commit bf9212c

19 files changed

Lines changed: 2710 additions & 129 deletions

File tree

Cargo.lock

Lines changed: 86 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ spin = { version = "0.9", default-features = false, features = [
394394
static_assertions = "1.1.0"
395395
subxt = { git = "https://github.com/CESSProject/subxt", branch = "polkadot-stable2412", default-features = false }
396396
syn = "2.0"
397+
sysinfo = { version = "0.34.2" }
397398
tempfile = "3.10"
398399
thiserror = "1.0"
399400
threadpool = "1.8.1"

crates/ces-pois/Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name = "ces-pois"
33
version = "0.4.5"
44
edition = "2021"
55

6+
[features]
7+
default = []
8+
use-sysinfo = []
9+
610
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
711

812
[dependencies]
@@ -19,4 +23,9 @@ prost = { workspace = true }
1923
rand = { workspace = true }
2024
rsa = { workspace = true, features = ["std"] }
2125
serde = { workspace = true, features = ["derive"] }
22-
sha2 = { workspace = true }
26+
serde_json = { workspace = true, features = ["std"] }
27+
sha2 = { workspace = true }
28+
tokio = { workspace = true, features = ["full"] }
29+
sysinfo = { workspace = true, optional = true }
30+
async-trait = { workspace = true }
31+
byteorder = { workspace = true }
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use super::multi_level_acc::{AccData, DEFAULT_BACKUP_NAME, DEFAULT_NAME};
2+
use crate::util;
3+
use anyhow::{Context, Result};
4+
use std::fs::{self, File};
5+
use std::io::Read;
6+
use std::path::Path;
7+
8+
pub fn save_acc_data(dir: &str, index: i64, elems: Vec<Vec<u8>>, wits: Vec<Vec<u8>>) -> Result<()> {
9+
let data = AccData { values: elems, wits };
10+
11+
let jbytes = serde_json::to_vec(&data)?;
12+
13+
let fpath = Path::new(dir).join(format!("{}-{}", DEFAULT_NAME, index));
14+
15+
util::save_file(&fpath, &jbytes)
16+
}
17+
18+
pub fn read_acc_data(dir: &str, index: i64) -> Result<AccData> {
19+
let fpath = format!("{}/{}-{}", dir, DEFAULT_NAME, index);
20+
read_data(&fpath)
21+
}
22+
pub fn read_backup(dir: &str, index: i64) -> Result<AccData> {
23+
let fpath = format!("{}/{}-{}", dir, DEFAULT_BACKUP_NAME, index);
24+
read_data(&fpath)
25+
}
26+
27+
pub fn read_data(fpath: &str) -> Result<AccData> {
28+
let mut file = File::open(fpath).context("read element data error")?;
29+
let mut data = Vec::new();
30+
file.read_to_end(&mut data).context("read element data error")?;
31+
serde_json::from_slice(&data).context("read element data error")
32+
}
33+
34+
// deleteAccData delete from the given index
35+
pub fn delete_acc_data(dir: &str, last: i32) -> Result<()> {
36+
let fs = fs::read_dir(dir).context("delete element data error")?;
37+
for entry in fs {
38+
let entry = entry.context("delete element data error")?;
39+
let path = entry.path();
40+
if let Some(file_name) = path.file_name() {
41+
if let Some(file_name_str) = file_name.to_str() {
42+
if let Some(index_str) = file_name_str.rsplit('-').next() {
43+
if let Ok(index) = index_str.parse::<i32>() {
44+
if index <= last {
45+
fs::remove_file(path).context("delete element data error")?;
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+
Ok(())
53+
}
54+
55+
pub fn clean_backup(dir: &str, index: i64) -> Result<()> {
56+
let backup = format!("{}/{}-{}", dir, DEFAULT_BACKUP_NAME, index);
57+
fs::remove_file(backup).context("clean backup error")
58+
}
59+
60+
pub fn backup_acc_data(dir: &str, index: i64) -> Result<()> {
61+
let fpath = format!("{}/{}-{}", dir, DEFAULT_NAME, index);
62+
let backup = format!("{}/{}-{}", dir, DEFAULT_BACKUP_NAME, index);
63+
util::copy_file(&fpath, &backup).context("backup element data error")
64+
}
65+
66+
pub fn backup_acc_data_for_chall(src: &str, des: &str, index: i64) -> Result<()> {
67+
let fpath = Path::new(src).join(format!("{}-{}", DEFAULT_NAME, index));
68+
let backup = Path::new(des).join(format!("{}-{}", DEFAULT_NAME, index));
69+
util::copy_file(fpath.to_str().unwrap(), backup.to_str().unwrap())
70+
.context("backup acc data for challenge error")?;
71+
Ok(())
72+
}
73+
74+
pub fn recovery_acc_data(dir: &str, index: i64) -> Result<()> {
75+
let backup = Path::join(Path::new(dir), &format!("{}-{}", DEFAULT_BACKUP_NAME, index));
76+
let fpath = Path::join(Path::new(dir), &format!("{}-{}", DEFAULT_NAME, index));
77+
78+
if !backup.exists() {
79+
return Ok(());
80+
}
81+
82+
fs::rename(&backup, &fpath).context("recovery acc data error")?;
83+
Ok(())
84+
}

0 commit comments

Comments
 (0)