diff --git a/lib/index.js b/lib/index.js index d096f0c2a862b31ae5f1714d8fde1d7b153f69f6..7b577e2e8df35b50587f9b8cb66da368281bea7a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,17 +5,17 @@ const path = require('path'); module.exports = { - /*newFileInstance: (filePath) => { + newFileInstance: (filePath, sigStock) => { const instance = Object.create(WotB) if (process.platform == 'win32') { let bufferedPath = new Buffer(filePath,'ascii'); - instance.init(wotb.newFileInstanceWin32(bufferedPath, bufferedPath.length)) + instance.init(addon.new_file_instance_win_32(bufferedPath, bufferedPath.length, sigStock)) } else { - instance.init(wotb.new_file_instance(filePath)); + instance.init(addon.new_file_instance(filePath, sigStock)); } instance.setFilePath(filePath) return instance - },*/ + }, newMemoryInstance: (sigStock) => { const instance = Object.create(WotB) @@ -44,6 +44,10 @@ const WotB = { this.filePath = filePath }, + write: function() { + return addon.write_instance_in_file(this.instanceID, this.filePath); + }, + memCopy: function() { const copy = Object.create(WotB) copy.instanceID = addon.mem_copy(this.instanceID); diff --git a/native/src/lib.rs b/native/src/lib.rs index 52005fb97d7111d6291f9022e7e700873c19f97b..c5caa3cc303fb793791aca43aac9de0f30639b6b 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -8,6 +8,7 @@ use duniter_wotb::data::rusty::RustyWebOfTrust; use duniter_wotb::data::{HasLinkResult, NewLinkResult, NodeId, RemLinkResult, WebOfTrust}; use duniter_wotb::operations::distance::{DistanceCalculator, RustyDistanceCalculator, WotDistance, WotDistanceParameters}; +use duniter_wotb::operations::file::{BinaryFileFormater, FileFormater}; use duniter_wotb::operations::path::{PathFinder, RustyPathFinder}; use neon::js::{JsArray, JsBoolean, JsInteger, JsNumber, JsObject, JsString, Object}; use neon::vm::{Call, JsResult}; @@ -16,6 +17,7 @@ use std::ops::{Deref, DerefMut}; static mut WOT_INSTANCES: Option<HashMap<usize, Box<RustyWebOfTrust>>> = None; static DISTANCE_CALCULATOR: RustyDistanceCalculator = RustyDistanceCalculator {}; +static FILE_FORMATER: BinaryFileFormater = BinaryFileFormater {}; static PATH_FINDER: RustyPathFinder = RustyPathFinder {}; fn get_wots() -> &'static mut HashMap<usize, Box<RustyWebOfTrust>> { @@ -77,6 +79,35 @@ fn threading_hint(call: Call) -> JsResult<JsNumber> { Ok(JsNumber::new(call.scope, num_cpus::get() as f64)) } +fn new_file_instance(call: Call) -> JsResult<JsInteger> { + let scope = call.scope; + let file_path = try!(try!(call.arguments.require(scope, 0)).check::<JsString>()).value(); + let max_links = try!(try!(call.arguments.require(scope, 1)).check::<JsInteger>()).value(); + let (wot, _blockstamp) = FILE_FORMATER + .from_file(&file_path, max_links as usize) + .unwrap(); + let wots = get_wots(); + let mut instance_id = 0; + while wots.contains_key(&instance_id) { + instance_id += 1; + } + wots.insert(instance_id, Box::new(wot)); + Ok(JsInteger::new(scope, instance_id as i32)) +} + +fn write_instance_in_file(call: Call) -> JsResult<JsBoolean> { + let scope = call.scope; + let instance_id = + try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value() as usize; + let file_path = try!(try!(call.arguments.require(scope, 1)).check::<JsString>()).value(); + let wot = get_wot(instance_id as usize); + let header: Vec<u8> = Vec::with_capacity(0); + match FILE_FORMATER.to_file(wot, &header, &file_path) { + Ok(_) => Ok(JsBoolean::new(scope, true)), + Err(e) => panic!("Fatal error : fail to write wot in file : {:?}", e), + } +} + fn new_memory_instance(call: Call) -> JsResult<JsInteger> { let scope = call.scope; let max_links = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value(); @@ -388,6 +419,8 @@ fn remove_wot(call: Call) -> JsResult<JsBoolean> { register_module!(m, { m.export("hello", hello)?; m.export("threadingHint", threading_hint)?; + m.export("new_file_instance", new_file_instance)?; + m.export("write_instance_in_file", write_instance_in_file)?; m.export("new_memory_instance", new_memory_instance)?; m.export("mem_copy", mem_copy)?; m.export("get_max_links", get_max_links)?; diff --git a/tests/g1_genesis.bin b/tests/g1_genesis.bin new file mode 100644 index 0000000000000000000000000000000000000000..e7838df9c9efb58d10cbbb7f8a3ba1a2a4407db5 Binary files /dev/null and b/tests/g1_genesis.bin differ diff --git a/tests/test.js b/tests/test.js index bcdf8fc9f8e98d94208a59a2be34a762f3506cc5..d23bc15ff094a79c8da21a14b6e3ea50e23859de 100644 --- a/tests/test.js +++ b/tests/test.js @@ -6,7 +6,7 @@ var fs = require('fs'); var path = require('path'); var should = require('should'); -const FILE = path.join(__dirname, 'test.wot'); +const FILE = path.join(__dirname, 'g1_genesis.bin'); const X_PERCENT = 1.0; const _100_PERCENT = 1.0; const MAX_DISTANCE_1 = 1; @@ -19,29 +19,19 @@ const FROM_2_LINKS_SENTRIES = 2; const FROM_3_LINKS_SENTRIES = 3; const __OUTDISTANCED__ = true; const __OK__ = false; -const MEMORY_MODE = true; -const FILE_MODE = false; -testSuite("MEMORY", MEMORY_MODE); +testSuite(); -function testSuite(title, mode) { +function testSuite() { function newInstance(launchTests) { return () => { - if (mode === FILE_MODE) { - let wot = addon.newFileInstance(FILE); - launchTests(wot, () => { - wot.clear(); - wot.setMaxCert(3); - }); - } else { - let wot = addon.newMemoryInstance(3); - launchTests(wot, () => {}); - } + let wot = addon.newMemoryInstance(3); + launchTests(wot); } } - describe(title, () => { - describe('Basic operations', newInstance((wot, cleanInstance) => { + describe("wotb-rs binding tests", () => { + describe('Basic operations', newInstance((wot) => { it('should have an instanceID zero', function() { assert.equal(wot.instanceID, 0) @@ -264,14 +254,11 @@ function testSuite(title, mode) { should.equal(wot2.instanceID, 0); wot2.clear() }); - - after(cleanInstance); })); - describe('Building a larger WoT', newInstance((wot, cleanInstance) => { + describe('Building a larger WoT', newInstance((wot) => { before(() => { - cleanInstance(); /** * We build WoT: * @@ -483,6 +470,46 @@ function testSuite(title, mode) { should.equal(wot.isOutdistanced(2, FROM_3_LINKS_SENTRIES, MAX_DISTANCE_5, 0.01), __OK__); }); }); + + describe('testing write in file', () => { + it('should can write in file', function() { + wot.setFilePath("test.bin"); + assert.equal(wot.write(), true) + }); + }); + })); + + describe('tests g1 genesis wot', newInstance((wot) => { + + before(() => { + wot.clear(); + wot = addon.newFileInstance(FILE, 100); + }); + + it('should have an instanceID zero', function() { + assert.equal(wot.instanceID, 0) + }); + + it('should have 100 max links', function() { + assert.equal(wot.getMaxCert(), 100) + }); + + it('should have a wot size of 59', function() { + should.equal(wot.getWoTSize(), 59); + }); + + it('should have only enabled members', function() { + should.equal(wot.getEnabled().length, 59); + should.equal(wot.getDisabled().length, 0); + }); + + it('should have 48 sentries', function() { + should.equal(wot.getSentries(FROM_3_LINKS_SENTRIES).length, 48); + }); + + after(() => { + wot.clear(); + }); })); }); } \ No newline at end of file