Advertisement
Guest User

Untitled

a guest
Mar 13th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 6.42 KB | None | 0 0
  1. // Eigentrust
  2. struct Peer;
  3. type Score = f64;
  4.  
  5. static INITIAL_TRUST_PEERS: HashMap<Peer, Score> = todo!();
  6. static PEERS: Vec<Peer> = todo!();
  7.  
  8. /// Initial trust
  9. fn p(j: Peer) -> Score {
  10.     INITIAL_TRUST_PEERS.get(j).unwrap_or(0)
  11. }
  12.  
  13. /// Trust in some time (should be called only for local trust)
  14. fn s(i: Peer, j: Peer) -> Score {
  15.     if success(i, j) + failed(i, j) > 0 {
  16.         success(i, j) / (success(i, j) + failed(i, j))
  17.     } else {
  18.         p(j)
  19.     }
  20. }
  21.  
  22. /// Normalized local trust (broadcasted also)
  23. fn c(i: Peer, j: Peer) -> Score {
  24.     s(i, j) / PEERS.iter().map(|m| s(i, m)).sum()
  25. }
  26.  
  27. /// Intial trust coefficient. [0; 1)
  28. ///
  29. /// Increasing it will increase influence of initial trust on current trust
  30. const A: f64 = todo!();
  31.  
  32. /// Weighted trust mixed with trust from other peers (first iteration)
  33. fn t(iter: i32, i: Peer, k: Peer) -> Score {
  34.     let init = |j| (1.0 - A) * c(i, j) * c(j, k) + A * p(k);
  35.     let step = |j| (1.0 - A) * t(iter - 1, i, j) * t(iter - 1, j, k) + A * p(k);
  36.     let map = if iter == 0 { init } else { step };
  37.  
  38.     PEERS.iter().map(map).sum()
  39. }
  40.  
  41. /// Weighted trust mixed with trust from other peers (step)
  42. fn t_i(i: Peer, k: Peer) -> Score {
  43.     // t_0 here is previous iteration
  44.     PEERS
  45.         .iter()
  46.         .map()
  47.         .sum()
  48. }
  49.  
  50. // Eigentrust++
  51. struct Peer;
  52. type Score = f64;
  53.  
  54. static INITIAL_TRUST_PEERS: HashMap<Peer, Score> = todo!();
  55. static PEERS: Vec<Peer> = todo!();
  56.  
  57. /// Initial trust
  58. fn p(j: Peer) -> Score {
  59.     INITIAL_TRUST_PEERS.get(j).unwrap_or(0)
  60. }
  61.  
  62. /// Trust in some time (should be called only for local trust)
  63. fn s(i: Peer, j: Peer) -> Score {
  64.     if success(i, j) + failed(i, j) > 0 {
  65.         success(i, j) / (success(i, j) + failed(i, j))
  66.     } else {
  67.         p(j)
  68.     }
  69. }
  70.  
  71. /// Normalized local trust (broadcasted also)
  72. fn c(i: Peer, j: Peer) -> Score {
  73.     s(i, j) / PEERS.iter().map(|m| s(i, m)).sum()
  74. }
  75.  
  76. /// Similarity of feedback of 2 nodes
  77. ///
  78. /// `common` -- set of nodes with which both peers interacted
  79. fn sim(u: Peer, v: Peer) -> Score {
  80.     if common(u, v).len() == 0 {
  81.         0
  82.     } else {
  83.         1 - (
  84.             common(u, v)
  85.                 .iter()
  86.                 .map(|w| s(u, w) * s(v, w) * s(u, w) * s(v, w))
  87.                 /
  88.                 common(u, v).len()
  89.         ).sqrt()
  90.     }
  91. }
  92.  
  93. /// Feedback credibility
  94. fn f(i: Peer, j: Peer) -> Score {
  95.     let all_sim = NODES.iter().map(|m| sim(i, m)).sum();
  96.     if all_sim > 0.0 { sim(i, j) / all_sim } else { 0.0 }
  97. }
  98.  
  99. /// Feedback credibility
  100. fn f(i: Peer, j: Peer) -> Score {
  101.     let all_sim = NODES.iter().map(|m| sim(i, m)).sum();
  102.     if all_sim > 0.0 { sim(i, j) / all_sim } else { 0.0 }
  103. }
  104.  
  105. /// Feedback credibility
  106. fn l(i: Peer, j: Peer) -> Score {
  107.     let all_l = NODES.iter().map(|m| f(i, m) * c(i, m)).sum();
  108.     if all_l > 0.0 { f(i, j) * c(i, j) / all_l } else { 0.0 }
  109. }
  110.  
  111. /// Intial trust coefficient. [0; 1)
  112. ///
  113. /// Increasing it will increase influence of initial trust on current trust
  114. const A: f64 = todo!();
  115.  
  116. /// Weighted final trust
  117. fn t(i: i32) -> Vec<Score> {
  118.     let p = PEERS.iter().map(p).collect();
  119.  
  120.     if i == 0 {
  121.         p
  122.     } else {
  123.         let L: Vec<Vec<Score>> = PEERS
  124.             .iter()
  125.             .map(|i| PEERS.iter().map(|j| l(i, j)).collect())
  126.             .collect();
  127.  
  128.         (1.0 - A) * L.transpose() * t(i-1) + A * p
  129.     }
  130. }
  131.  
  132. // Hijiri
  133. struct Peer;
  134. type Score = f64;
  135.  
  136. static INITIAL_TRUST_PEERS: HashMap<Peer, Score> = todo!();
  137. static PEERS: Vec<Peer> = todo!();
  138.  
  139. /// Initial trust
  140. fn p(j: Peer) -> Score {
  141.     INITIAL_TRUST_PEERS.get(j).unwrap_or(0)
  142. }
  143.  
  144. /// Ordinary ping metric.
  145. ///
  146. /// 1. Take P as ping time
  147. /// 2. log(MinPingTime) - log(P) / (-log(MinPingTime) * 2)
  148. fn network_locality(i: Peer, j: Peer) -> Score;
  149.  
  150. /// Computational test.
  151. ///
  152. /// May be calculated with something like:
  153. /// 1. sending transaction with instruction count equal to N
  154. /// 2. taking time to respond with data of transaction time as T and P as ping time
  155. /// 3. (T - P) / N (Should be normalized by some amount of predefined computation score maximum)
  156. fn comp(i: Peer, j: Peer) -> Score;
  157.  
  158. /// Version test.
  159. ///
  160. /// Send all types of messages to peer and determine version of peer
  161. fn version(i: Peer, j: Peer) -> Score;
  162.  
  163. fn data_consistency(i: Peer, j: Peer) -> Score;
  164.  
  165. fn success_rate(i: Peer, j: Peer) -> Score {
  166.     if success(i, j) + failed(i, j) > 0 {
  167.         success(i, j) / (success(i, j) + failed(i, j))
  168.     } else {
  169.         0.0
  170.     }
  171. }
  172.  
  173. /// Trust in some time (should be called only for local trust)
  174. fn s(i: Peer, j: Peer) -> Score {
  175.     if interacted(i, j) {
  176.         w_network * network_locality(i, j) +
  177.             w_comp * comp(i, j) +
  178.             w_version * version(i, j) +
  179.             w_data * data_consistency(i, j) +
  180.             w_success * success_rate(i, j)
  181.     } else {
  182.         p(j)
  183.     }
  184. }
  185.  
  186. /// Normalized local trust (broadcasted also)
  187. fn c(i: Peer, j: Peer) -> Score {
  188.     s(i, j) / PEERS.iter().map(|m| s(i, m)).sum()
  189. }
  190.  
  191. /// Similarity of feedback of 2 nodes
  192. ///
  193. /// `common` -- set of nodes with which both peers interacted
  194. fn sim(u: Peer, v: Peer) -> Score {
  195.     if common(u, v).len() == 0 {
  196.         0
  197.     } else {
  198.         1 - (
  199.             common(u, v)
  200.                 .iter()
  201.                 .map(|w| s(u, w) * s(v, w) * s(u, w) * s(v, w))
  202.                 /
  203.                 common(u, v).len()
  204.         ).sqrt()
  205.     }
  206. }
  207.  
  208. /// Feedback credibility
  209. fn f(i: Peer, j: Peer) -> Score {
  210.     let all_sim = NODES.iter().map(|m| sim(i, m)).sum();
  211.     if all_sim > 0.0 { sim(i, j) / all_sim } else { 0.0 }
  212. }
  213.  
  214. /// Feedback credibility
  215. fn f(i: Peer, j: Peer) -> Score {
  216.     let all_sim = NODES.iter().map(|m| sim(i, m)).sum();
  217.     if all_sim > 0.0 { sim(i, j) / all_sim } else { 0.0 }
  218. }
  219.  
  220. /// Feedback credibility
  221. fn l(i: Peer, j: Peer) -> Score {
  222.     let all_l = NODES.iter().map(|m| f(i, m) * c(i, m)).sum();
  223.     if all_l > 0.0 { f(i, j) * c(i, j) / all_l } else { 0.0 }
  224. }
  225.  
  226. /// Intial trust coefficient. [0; 1)
  227. ///
  228. /// Increasing it will increase influence of initial trust on current trust
  229. const A: f64 = todo!();
  230.  
  231. /// Weighted final trust
  232. fn t(i: i32) -> Vec<Score> {
  233.     let p = PEERS.iter().map(p).collect();
  234.  
  235.     if i == 0 {
  236.         p
  237.     } else {
  238.         let L: Vec<Vec<Score>> = PEERS
  239.             .iter()
  240.             .map(|i| PEERS.iter().map(|j| l(i, j)).collect())
  241.             .collect();
  242.  
  243.         (1.0 - A) * L.transpose() * t(i-1) + A * p
  244.     }
  245. }
  246.  
  247.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement