Commit 3f5c6e74 authored by Éloïs's avatar Éloïs

Merge branch 'fix/wotb-upgrade-need-reset-data' into 'dev'

[fix] wotb upgrade: force to reset data

See merge request nodes/typescript/duniter!1318
parents 13c29d24 f1ed3f1e
......@@ -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);
}
......
......@@ -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,
......
......@@ -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);
......
......@@ -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(())
}
}
......@@ -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)
});
}));
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment