Mise à jour de GitLab prévue ce dimanche 25 octobre 2020 à partir de 9h00 CEST | GitLab upgrade planned this Sunday 25rd of October 2020 from 9:00 AM CEST

Commit 4b9dde65 authored by Éloïs's avatar Éloïs

[enh] add all wotb methods and all js tests (except io file)

parent 57556590
Pipeline #1953 passed with stage
in 3 minutes
......@@ -63,25 +63,19 @@ const WotB = {
},
getEnabled: function() {
var wot_size = addon.get_wot_size(this.instanceID);
var enabled_array = new Array();
for (let i = 0; i < wot_size; i++) {
if (addon.is_enabled(this.instanceID, i)) {
enabled_array.push(i);
}
}
return enabled_array;
return addon.get_enabled(this.instanceID);
},
getDisabled: function() {
var wot_size = addon.get_wot_size(this.instanceID);
var enabled_array = new Array();
for (let i = 0; i < wot_size; i++) {
if (!addon.is_enabled(this.instanceID, i)) {
enabled_array.push(i);
}
}
return enabled_array;
return addon.get_disabled(this.instanceID);
},
getSentries: function(sentry_requirement) {
return addon.get_sentries(this.instanceID, sentry_requirement);
},
getNonSentries: function(sentry_requirement) {
return addon.get_non_sentries(this.instanceID, sentry_requirement);
},
existsLink: function(source, target) {
......@@ -118,6 +112,24 @@ const WotB = {
return distance_result.outdistanced;
},
detailedDistance: function(node, sentry_requirement, step_max, x_percent) {
var distance_result = addon.compute_distance(this.instanceID, node, sentry_requirement, step_max, x_percent);
return {
nbReached: distance_result.reached,
nbSuccess: distance_result.success,
nbSentries: distance_result.sentries,
isOutdistanced: distance_result.outdistanced
};
},
detailedDistanceV2: function(node, sentry_requirement, step_max, x_percent) {
return addon.compute_distance(this.instanceID, node, sentry_requirement, step_max, x_percent);
},
getPaths: function(from, to, step_max) {
return addon.find_paths(this.instanceID, from, to, step_max);
},
clear: function() {
return addon.remove_wot(this.instanceID);
}
......
......@@ -76,7 +76,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "duniter-wotb"
version = "0.7.1"
version = "0.8.0-a0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bincode 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -352,7 +352,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "wotb-rs"
version = "0.1.0"
dependencies = [
"duniter-wotb 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"duniter-wotb 0.8.0-a0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"neon 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)",
"neon-build 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -369,7 +369,7 @@ dependencies = [
"checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150"
"checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9"
"checksum cslice 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "697c714f50560202b1f4e2e09cd50a421881c83e9025db75d15f276616f04f40"
"checksum duniter-wotb 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "834b85bd66552e19e45245c3fe2f96bd350fa11adad679902ae6a127ed9f4dc1"
"checksum duniter-wotb 0.8.0-a0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "873a948b6755405ec2da9f25b1266fcfaf45f9ef043b08e01eab4a6f91e9e6ad"
"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
......
......@@ -15,4 +15,4 @@ neon-build = "0.1.22"
[dependencies]
neon = "0.1.22"
num_cpus = "1.8.0"
duniter-wotb = "0.7.1"
duniter-wotb = "0.8.0-a0.5"
......@@ -4,15 +4,19 @@ extern crate neon;
extern crate duniter_wotb;
extern crate num_cpus;
use duniter_wotb::rusty::RustyWebOfTrust;
use duniter_wotb::{HasLinkResult, NewLinkResult, NodeId, RemLinkResult, WebOfTrust, WotDistance,
WotDistanceParameters};
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::path::{PathFinder, RustyPathFinder};
use neon::js::{JsArray, JsBoolean, JsInteger, JsNumber, JsObject, JsString, Object};
use neon::vm::{Call, JsResult};
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
static mut WOT_INSTANCES: Option<HashMap<usize, Box<RustyWebOfTrust>>> = None;
static DISTANCE_CALCULATOR: RustyDistanceCalculator = RustyDistanceCalculator {};
static PATH_FINDER: RustyPathFinder = RustyPathFinder {};
fn get_wots() -> &'static mut HashMap<usize, Box<RustyWebOfTrust>> {
unsafe {
......@@ -82,9 +86,13 @@ fn new_memory_instance(call: Call) -> JsResult<JsInteger> {
fn mem_copy(call: Call) -> JsResult<JsInteger> {
let scope = call.scope;
let instance_id = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value() as usize;
let instance_id =
try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value() as usize;
let wots = get_wots();
let wot_copy = wots.get(&instance_id).expect("This instance don't exist !").deref().clone();
let wot_copy = wots.get(&instance_id)
.expect("This instance don't exist !")
.deref()
.clone();
let mut new_instance_id = 0;
while wots.contains_key(&new_instance_id) {
new_instance_id += 1;
......@@ -123,18 +131,18 @@ fn is_enabled(call: Call) -> JsResult<JsBoolean> {
}
}
fn get_enabled(call: Call) -> JsResult<JsObject> {
fn get_enabled(call: Call) -> JsResult<JsArray> {
let scope = call.scope;
let instance_id = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value();
let enabled = get_mut_wot(instance_id as usize).get_enabled();
let mut js_array = JsObject::new(scope);
let mut js_array = JsArray::new(scope, enabled.len() as u32);
let mut index: u32 = 0;
for node_id in enabled {
let _bool = JsObject::set(
let _bool = try!(JsArray::set(
*js_array.deref_mut(),
index,
JsInteger::new(scope, node_id.0 as i32),
);
));
index += 1;
}
Ok(js_array)
......@@ -147,11 +155,50 @@ fn get_disabled(call: Call) -> JsResult<JsArray> {
let js_array = JsArray::new(scope, disabled.len() as u32);
let mut index: u32 = 0;
for node_id in disabled {
let _bool = JsArray::set(
let _bool = try!(JsArray::set(
*js_array.deref(),
index,
JsInteger::new(scope, node_id.0 as i32),
));
index += 1;
}
Ok(js_array)
}
fn get_sentries(call: Call) -> JsResult<JsArray> {
let scope = call.scope;
let instance_id = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value();
let sentry_requirement =
try!(try!(call.arguments.require(scope, 1)).check::<JsInteger>()).value();
let sentries = get_mut_wot(instance_id as usize).get_sentries(sentry_requirement as usize);
let js_array = JsArray::new(scope, sentries.len() as u32);
let mut index: u32 = 0;
for node_id in sentries {
let _bool = try!(JsArray::set(
*js_array.deref(),
index,
JsInteger::new(scope, node_id.0 as i32),
));
index += 1;
}
Ok(js_array)
}
fn get_non_sentries(call: Call) -> JsResult<JsArray> {
let scope = call.scope;
let instance_id = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value();
let sentry_requirement =
try!(try!(call.arguments.require(scope, 1)).check::<JsInteger>()).value();
let non_sentries =
get_mut_wot(instance_id as usize).get_non_sentries(sentry_requirement as usize);
let js_array = JsArray::new(scope, non_sentries.len() as u32);
let mut index: u32 = 0;
for node_id in non_sentries {
let _bool = try!(JsArray::set(
*js_array.deref(),
index,
JsInteger::new(scope, node_id.0 as i32),
);
));
index += 1;
}
Ok(js_array)
......@@ -252,6 +299,7 @@ fn compute_distance(call: Call) -> JsResult<JsObject> {
step_max,
x_percent,
};
let wot = get_wot(instance_id as usize);
let WotDistance {
sentries,
success,
......@@ -259,43 +307,71 @@ fn compute_distance(call: Call) -> JsResult<JsObject> {
reached,
reached_at_border,
outdistanced,
} = get_wot(instance_id as usize)
.compute_distance(distance_params)
} = DISTANCE_CALCULATOR
.compute_distance(wot, distance_params)
.unwrap();
let js_object = JsObject::new(scope);
let _bool = JsObject::set(
let _bool = try!(JsObject::set(
*js_object.deref(),
"sentries",
JsInteger::new(scope, sentries as i32),
);
let _bool = JsObject::set(
));
let _bool = try!(JsObject::set(
*js_object.deref(),
"success",
JsInteger::new(scope, success as i32),
);
let _bool = JsObject::set(
));
let _bool = try!(JsObject::set(
*js_object.deref(),
"success_at_border",
JsInteger::new(scope, success_at_border as i32),
);
let _bool = JsObject::set(
));
let _bool = try!(JsObject::set(
*js_object.deref(),
"reached",
JsInteger::new(scope, reached as i32),
);
let _bool = JsObject::set(
));
let _bool = try!(JsObject::set(
*js_object.deref(),
"reached_at_border",
JsInteger::new(scope, reached_at_border as i32),
);
let _bool = JsObject::set(
));
let _bool = try!(JsObject::set(
*js_object.deref(),
"outdistanced",
JsBoolean::new(scope, outdistanced),
);
));
Ok(js_object)
}
fn find_paths(call: Call) -> JsResult<JsArray> {
let scope = call.scope;
let instance_id =
try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value() as usize;
let from = try!(try!(call.arguments.require(scope, 1)).check::<JsInteger>()).value() as u32;
let to = try!(try!(call.arguments.require(scope, 2)).check::<JsInteger>()).value() as u32;
let step_max = try!(try!(call.arguments.require(scope, 3)).check::<JsInteger>()).value() as u32;
let wot = get_wot(instance_id as usize);
let paths = PATH_FINDER.find_paths(wot, NodeId(from as usize), NodeId(to as usize), step_max);
let mut i: u32 = 0;
let js_paths = JsArray::new(scope, paths.len() as u32);
for path in paths {
let mut j: u32 = 0;
let js_path = JsArray::new(scope, path.len() as u32);
for node_id in path {
let _bool = try!(JsArray::set(
*js_path.deref(),
j,
JsInteger::new(scope, node_id.0 as i32),
));
j += 1;
}
let _bool = try!(JsArray::set(*js_paths.deref(), i, js_path,));
i += 1;
}
Ok(js_paths)
}
fn remove_wot(call: Call) -> JsResult<JsBoolean> {
let scope = call.scope;
let instance_id =
......@@ -319,6 +395,8 @@ register_module!(m, {
m.export("is_enabled", is_enabled)?;
m.export("get_enabled", get_enabled)?;
m.export("get_disabled", get_disabled)?;
m.export("get_sentries", get_sentries)?;
m.export("get_non_sentries", get_non_sentries)?;
m.export("exist_link", exist_link)?;
m.export("set_max_links", set_max_links)?;
m.export("add_node", add_node)?;
......@@ -327,6 +405,7 @@ register_module!(m, {
m.export("add_link", add_link)?;
m.export("rem_link", rem_link)?;
m.export("compute_distance", compute_distance)?;
m.export("find_paths", find_paths)?;
m.export("remove_wot", remove_wot)?;
Ok(())
});
......@@ -5,7 +5,7 @@
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "https://git.duniter.org/nodes/typescript/wotb-rust"
"url": "https://git.duniter.org/nodes/typescript/wotb-rs"
},
"author": "librelois <elois@ifee.fr>",
"license": "AGPL-3.0",
......
This diff is collapsed.
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