From f1ed3f1e5603e2d592bac8114e820c8e86701a4a Mon Sep 17 00:00:00 2001 From: librelois <elois@ifee.fr> Date: Wed, 3 Jun 2020 18:11:46 +0200 Subject: [PATCH] cpp wot --- app/lib/blockchain/DuniterBlockchain.ts | 2 +- app/lib/system/directory.ts | 32 +++---- .../crawler/lib/sync/v2/GlobalIndexStream.ts | 2 +- neon/native/src/wot/read_from_file.rs | 87 +------------------ test/neon/test_wot.ts | 15 ---- 5 files changed, 18 insertions(+), 120 deletions(-) diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts index 05dc9136b..9ab3e5efa 100644 --- a/app/lib/blockchain/DuniterBlockchain.ts +++ b/app/lib/blockchain/DuniterBlockchain.ts @@ -329,7 +329,7 @@ export class DuniterBlockchain { // Save wot file if (!dal.fs.isMemoryOnly()) { - let wotbFilepath = await Directory.getWotbFilePath(dal.rootPath); + const wotbFilepath = Directory.getWotbFilePath(dal.rootPath); dal.wotb.writeInFile(wotbFilepath); } diff --git a/app/lib/system/directory.ts b/app/lib/system/directory.ts index 9c364b5c5..b8fec8b43 100644 --- a/app/lib/system/directory.ts +++ b/app/lib/system/directory.ts @@ -19,8 +19,6 @@ import { Wot, WotBuilder } from "../../../neon/lib"; import { FileDALParams } from "../dal/fileDAL"; import { cliprogram } from "../common-libs/programOptions"; import { LevelDBDriver } from "../dal/drivers/LevelDBDriver"; -import { LevelUp } from "levelup"; -import { AbstractLevelDOWN } from "abstract-leveldown"; const opts = cliprogram; const qfs = require("q-io/fs"); @@ -196,22 +194,15 @@ export const Directory = { return params; }, - getWotbFilePathSync: (home: string): string => { + getWotbFilePath: (home: string): string => { let datas_dir = path.join(home, Directory.DATA_DIR); - let wotbFilePath = path.join(datas_dir, Directory.NEW_WOTB_FILE); - let existsFile = fs.existsSync(wotbFilePath); + const wotbFilePath = path.join(datas_dir, Directory.NEW_WOTB_FILE); + const existsFile = fs.existsSync(wotbFilePath); if (!existsFile) { - wotbFilePath = path.join(home, Directory.OLD_WOTB_FILE); - } - return wotbFilePath; - }, - - getWotbFilePath: async (home: string): Promise<string> => { - let datas_dir = path.join(home, Directory.DATA_DIR); - let wotbFilePath = path.join(datas_dir, Directory.NEW_WOTB_FILE); - let existsFile = qfs.exists(wotbFilePath); - if (!existsFile) { - wotbFilePath = path.join(home, Directory.OLD_WOTB_FILE); + const oldWotbFilePath = path.join(home, Directory.OLD_WOTB_FILE); + if (fs.existsSync(oldWotbFilePath)) { + throw "This upgrade requires resetting the data and resynchronization (duniter reset data && duniter sync)."; + } } return wotbFilePath; }, @@ -233,8 +224,13 @@ export const Directory = { // File DB const sqlitePath = path.join(home, Directory.DUNITER_DB_NAME + ".db"); dbf = () => new SQLiteDriver(sqlitePath); - let wotbFilePath = await Directory.getWotbFilePath(home); - wotbf = () => WotBuilder.fromFile(wotbFilePath); + try { + const wotbFilePath = Directory.getWotbFilePath(home); + wotbf = () => WotBuilder.fromFile(wotbFilePath); + } catch (e) { + console.log(e); + throw e; + } } return { home: params.home, diff --git a/app/modules/crawler/lib/sync/v2/GlobalIndexStream.ts b/app/modules/crawler/lib/sync/v2/GlobalIndexStream.ts index ceb33b67f..694e52069 100644 --- a/app/modules/crawler/lib/sync/v2/GlobalIndexStream.ts +++ b/app/modules/crawler/lib/sync/v2/GlobalIndexStream.ts @@ -105,7 +105,7 @@ export class GlobalIndexStream extends Duplex { this.wotbMem = dal.wotb; if (!this.memoryOnly) { - this.wotbFilePath = Directory.getWotbFilePathSync(dal.rootPath); + this.wotbFilePath = Directory.getWotbFilePath(dal.rootPath); } const nbBlocksToDownload = Math.max(0, to - localNumber); diff --git a/neon/native/src/wot/read_from_file.rs b/neon/native/src/wot/read_from_file.rs index b9d2a7581..ca082fdef 100644 --- a/neon/native/src/wot/read_from_file.rs +++ b/neon/native/src/wot/read_from_file.rs @@ -13,12 +13,11 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -use dubp_wot::data::{rusty::RustyWebOfTrust, WebOfTrust, WotId}; +use dubp_wot::data::rusty::RustyWebOfTrust; use flate2::read::ZlibDecoder; use std::convert::TryFrom; use std::fs::File; use std::io::prelude::*; -use std::io::BufReader; use std::path::{Path, PathBuf}; pub(crate) fn wot_from_file(file_path_str: String) -> Result<RustyWebOfTrust, String> { @@ -32,23 +31,10 @@ pub(crate) fn wot_from_file(file_path_str: String) -> Result<RustyWebOfTrust, St Ok(bincode::deserialize::<RustyWebOfTrust>(&bytes).map_err(|e| format!("{}", e))?) } } else { - let bytes = read_bytes_from_file(file_path.as_path()).map_err(|e| format!("{}", e))?; - from_cpp_wot(&bytes) + Err("invalid wot file format.".to_owned()) } } -/// Read bytes from file -fn read_bytes_from_file(file_path: &Path) -> Result<Vec<u8>, std::io::Error> { - let file = File::open(file_path)?; - - let mut buf_reader = BufReader::new(file); - - let mut decompressed_bytes = Vec::new(); - buf_reader.read_to_end(&mut decompressed_bytes)?; - - Ok(decompressed_bytes) -} - /// Read and decompress bytes from file fn read_and_decompress_bytes_from_file(file_path: &Path) -> Result<Vec<u8>, std::io::Error> { if !file_path.exists() { @@ -68,72 +54,3 @@ fn read_and_decompress_bytes_from_file(file_path: &Path) -> Result<Vec<u8>, std: Ok(vec![]) } } - -fn from_cpp_wot(bytes: &[u8]) -> Result<RustyWebOfTrust, String> { - if bytes.len() < 8 { - return Err("wot file is corrupted".to_owned()); - } - - let mut buffer = [0u8; 4]; - let mut cursor = 0; - - // Read max_links and create empty wot - buffer.copy_from_slice(&bytes[cursor..cursor + 4]); - cursor += 4; - let max_links = u32::from_le_bytes(buffer); - let mut wot = RustyWebOfTrust::new(max_links as usize); - - // Read nodes count - buffer.copy_from_slice(&bytes[cursor..cursor + 4]); - cursor += 4; - let nodes_count = u32::from_le_bytes(buffer); - - for _ in 0..nodes_count { - let wot_id = wot.add_node(); - - // Read enabled - let enabled = bytes[cursor]; - cursor += 1; - if enabled == 0 { - wot.set_enabled(wot_id, false); - } - - // Read certs_count - buffer.copy_from_slice(&bytes[cursor..cursor + 4]); - cursor += 4; - let certs_count = u32::from_le_bytes(buffer); - - // Read certs - for _ in 0..certs_count { - buffer.copy_from_slice(&bytes[cursor..cursor + 4]); - cursor += 4; - let cert_source = WotId(u32::from_le_bytes(buffer) as usize); - wot.add_link(cert_source, wot_id); - } - } - - Ok(wot) -} - -#[cfg(test)] -mod tests { - - use super::*; - use dubp_wot::data::HasLinkResult; - - #[test] - fn test_from_cpp_wot() -> Result<(), std::io::Error> { - let bytes = read_bytes_from_file(PathBuf::from("tests/wotb.bin").as_path())?; - - let wot = from_cpp_wot(&bytes).expect("fail to read cpp wot"); - - assert_eq!(wot.get_max_link(), 100); - assert_eq!(wot.size(), 3394); - assert_eq!( - wot.has_link(WotId(33), WotId(35)), - HasLinkResult::Link(true), - ); - - Ok(()) - } -} diff --git a/test/neon/test_wot.ts b/test/neon/test_wot.ts index dbaf0f0e0..84eb07b8c 100644 --- a/test/neon/test_wot.ts +++ b/test/neon/test_wot.ts @@ -500,19 +500,4 @@ const __OK__ = false; assert.equal(wot.getSentries(FROM_3_LINKS_SENTRIES).length, 48); }); })); - - describe('tests cpp wot', newInstance((wot) => { - - before(() => { - wot = WotBuilder.fromFile(CPP_FILE); - }); - - it('should have 100 max links', function() { - assert.equal(wot.getMaxCert(), 100) - }); - - it('should have 3394 nodes', function() { - assert.equal(wot.getWoTSize(), 3394) - }); - })); }); -- GitLab