Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Eigentrust
- struct Peer;
- type Score = f64;
- static INITIAL_TRUST_PEERS: HashMap<Peer, Score> = todo!();
- static PEERS: Vec<Peer> = todo!();
- /// Initial trust
- fn p(j: Peer) -> Score {
- INITIAL_TRUST_PEERS.get(j).unwrap_or(0)
- }
- /// Trust in some time (should be called only for local trust)
- fn s(i: Peer, j: Peer) -> Score {
- if success(i, j) + failed(i, j) > 0 {
- success(i, j) / (success(i, j) + failed(i, j))
- } else {
- p(j)
- }
- }
- /// Normalized local trust (broadcasted also)
- fn c(i: Peer, j: Peer) -> Score {
- s(i, j) / PEERS.iter().map(|m| s(i, m)).sum()
- }
- /// Intial trust coefficient. [0; 1)
- ///
- /// Increasing it will increase influence of initial trust on current trust
- const A: f64 = todo!();
- /// Weighted trust mixed with trust from other peers (first iteration)
- fn t(iter: i32, i: Peer, k: Peer) -> Score {
- let init = |j| (1.0 - A) * c(i, j) * c(j, k) + A * p(k);
- let step = |j| (1.0 - A) * t(iter - 1, i, j) * t(iter - 1, j, k) + A * p(k);
- let map = if iter == 0 { init } else { step };
- PEERS.iter().map(map).sum()
- }
- /// Weighted trust mixed with trust from other peers (step)
- fn t_i(i: Peer, k: Peer) -> Score {
- // t_0 here is previous iteration
- PEERS
- .iter()
- .map()
- .sum()
- }
- // Eigentrust++
- struct Peer;
- type Score = f64;
- static INITIAL_TRUST_PEERS: HashMap<Peer, Score> = todo!();
- static PEERS: Vec<Peer> = todo!();
- /// Initial trust
- fn p(j: Peer) -> Score {
- INITIAL_TRUST_PEERS.get(j).unwrap_or(0)
- }
- /// Trust in some time (should be called only for local trust)
- fn s(i: Peer, j: Peer) -> Score {
- if success(i, j) + failed(i, j) > 0 {
- success(i, j) / (success(i, j) + failed(i, j))
- } else {
- p(j)
- }
- }
- /// Normalized local trust (broadcasted also)
- fn c(i: Peer, j: Peer) -> Score {
- s(i, j) / PEERS.iter().map(|m| s(i, m)).sum()
- }
- /// Similarity of feedback of 2 nodes
- ///
- /// `common` -- set of nodes with which both peers interacted
- fn sim(u: Peer, v: Peer) -> Score {
- if common(u, v).len() == 0 {
- 0
- } else {
- 1 - (
- common(u, v)
- .iter()
- .map(|w| s(u, w) * s(v, w) * s(u, w) * s(v, w))
- /
- common(u, v).len()
- ).sqrt()
- }
- }
- /// Feedback credibility
- fn f(i: Peer, j: Peer) -> Score {
- let all_sim = NODES.iter().map(|m| sim(i, m)).sum();
- if all_sim > 0.0 { sim(i, j) / all_sim } else { 0.0 }
- }
- /// Feedback credibility
- fn f(i: Peer, j: Peer) -> Score {
- let all_sim = NODES.iter().map(|m| sim(i, m)).sum();
- if all_sim > 0.0 { sim(i, j) / all_sim } else { 0.0 }
- }
- /// Feedback credibility
- fn l(i: Peer, j: Peer) -> Score {
- let all_l = NODES.iter().map(|m| f(i, m) * c(i, m)).sum();
- if all_l > 0.0 { f(i, j) * c(i, j) / all_l } else { 0.0 }
- }
- /// Intial trust coefficient. [0; 1)
- ///
- /// Increasing it will increase influence of initial trust on current trust
- const A: f64 = todo!();
- /// Weighted final trust
- fn t(i: i32) -> Vec<Score> {
- let p = PEERS.iter().map(p).collect();
- if i == 0 {
- p
- } else {
- let L: Vec<Vec<Score>> = PEERS
- .iter()
- .map(|i| PEERS.iter().map(|j| l(i, j)).collect())
- .collect();
- (1.0 - A) * L.transpose() * t(i-1) + A * p
- }
- }
- // Hijiri
- struct Peer;
- type Score = f64;
- static INITIAL_TRUST_PEERS: HashMap<Peer, Score> = todo!();
- static PEERS: Vec<Peer> = todo!();
- /// Initial trust
- fn p(j: Peer) -> Score {
- INITIAL_TRUST_PEERS.get(j).unwrap_or(0)
- }
- /// Ordinary ping metric.
- ///
- /// 1. Take P as ping time
- /// 2. log(MinPingTime) - log(P) / (-log(MinPingTime) * 2)
- fn network_locality(i: Peer, j: Peer) -> Score;
- /// Computational test.
- ///
- /// May be calculated with something like:
- /// 1. sending transaction with instruction count equal to N
- /// 2. taking time to respond with data of transaction time as T and P as ping time
- /// 3. (T - P) / N (Should be normalized by some amount of predefined computation score maximum)
- fn comp(i: Peer, j: Peer) -> Score;
- /// Version test.
- ///
- /// Send all types of messages to peer and determine version of peer
- fn version(i: Peer, j: Peer) -> Score;
- fn data_consistency(i: Peer, j: Peer) -> Score;
- fn success_rate(i: Peer, j: Peer) -> Score {
- if success(i, j) + failed(i, j) > 0 {
- success(i, j) / (success(i, j) + failed(i, j))
- } else {
- 0.0
- }
- }
- /// Trust in some time (should be called only for local trust)
- fn s(i: Peer, j: Peer) -> Score {
- if interacted(i, j) {
- w_network * network_locality(i, j) +
- w_comp * comp(i, j) +
- w_version * version(i, j) +
- w_data * data_consistency(i, j) +
- w_success * success_rate(i, j)
- } else {
- p(j)
- }
- }
- /// Normalized local trust (broadcasted also)
- fn c(i: Peer, j: Peer) -> Score {
- s(i, j) / PEERS.iter().map(|m| s(i, m)).sum()
- }
- /// Similarity of feedback of 2 nodes
- ///
- /// `common` -- set of nodes with which both peers interacted
- fn sim(u: Peer, v: Peer) -> Score {
- if common(u, v).len() == 0 {
- 0
- } else {
- 1 - (
- common(u, v)
- .iter()
- .map(|w| s(u, w) * s(v, w) * s(u, w) * s(v, w))
- /
- common(u, v).len()
- ).sqrt()
- }
- }
- /// Feedback credibility
- fn f(i: Peer, j: Peer) -> Score {
- let all_sim = NODES.iter().map(|m| sim(i, m)).sum();
- if all_sim > 0.0 { sim(i, j) / all_sim } else { 0.0 }
- }
- /// Feedback credibility
- fn f(i: Peer, j: Peer) -> Score {
- let all_sim = NODES.iter().map(|m| sim(i, m)).sum();
- if all_sim > 0.0 { sim(i, j) / all_sim } else { 0.0 }
- }
- /// Feedback credibility
- fn l(i: Peer, j: Peer) -> Score {
- let all_l = NODES.iter().map(|m| f(i, m) * c(i, m)).sum();
- if all_l > 0.0 { f(i, j) * c(i, j) / all_l } else { 0.0 }
- }
- /// Intial trust coefficient. [0; 1)
- ///
- /// Increasing it will increase influence of initial trust on current trust
- const A: f64 = todo!();
- /// Weighted final trust
- fn t(i: i32) -> Vec<Score> {
- let p = PEERS.iter().map(p).collect();
- if i == 0 {
- p
- } else {
- let L: Vec<Vec<Score>> = PEERS
- .iter()
- .map(|i| PEERS.iter().map(|j| l(i, j)).collect())
- .collect();
- (1.0 - A) * L.transpose() * t(i-1) + A * p
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement