1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::err::{Error, Result};
use num_bigint::BigUint;
use serde::Deserialize;
use serde::Serialize;
use std::cmp::{Eq, PartialEq};
use std::ops::Deref;
use std::ops::Neg;
use std::ops::{Add, Sub};
use std::str::FromStr;
use web3::types::H160;
#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Debug, Serialize, Deserialize, Hash)]
pub struct Did(H160);
#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize, Hash)]
pub struct BiasId {
bias: Did,
did: Did,
}
impl BiasId {
pub fn new(bias: &Did, id: &Did) -> BiasId {
BiasId {
bias: *bias,
did: *id - *bias,
}
}
pub fn to_did(bid: &BiasId) -> Did {
bid.did + bid.bias
}
pub fn pos(&self) -> Did {
self.did
}
}
impl PartialOrd for BiasId {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
if other.bias != self.bias {
let did: Did = other.into();
let bid = BiasId::new(&self.bias, &did);
self.did.partial_cmp(&bid.did)
} else {
self.did.partial_cmp(&other.did)
}
}
}
impl Ord for BiasId {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
if other.bias != self.bias {
let did: Did = other.into();
let bid = BiasId::new(&self.bias, &did);
self.did.cmp(&bid.did)
} else {
self.did.cmp(&other.did)
}
}
}
impl From<BiasId> for Did {
fn from(id: BiasId) -> Did {
BiasId::to_did(&id)
}
}
impl From<&BiasId> for Did {
fn from(id: &BiasId) -> Did {
BiasId::to_did(id)
}
}
impl Did {
pub fn in_range(&self, id: &Self, a: &Self, b: &Self) -> bool {
*self - *id > *a - *id && *b - *id > *self - *id
}
}
pub trait SortRing {
fn sort(&mut self, id: Did);
}
impl SortRing for Vec<Did> {
fn sort(&mut self, id: Did) {
self.sort_by(|a, b| {
let (da, db) = (*a - id, *b - id);
(da).partial_cmp(&db).unwrap()
});
}
}
impl Deref for Did {
type Target = H160;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Did> for H160 {
fn from(a: Did) -> Self {
a.0.to_owned()
}
}
impl From<Did> for BigUint {
fn from(did: Did) -> BigUint {
BigUint::from_bytes_be(did.as_bytes())
}
}
impl From<BigUint> for Did {
fn from(a: BigUint) -> Self {
let ff = a % (BigUint::from(2u16).pow(160));
let mut va: Vec<u8> = ff.to_bytes_be();
let mut res = vec![0u8; 20 - va.len()];
res.append(&mut va);
assert_eq!(res.len(), 20, "{:?}", res);
Self(H160::from_slice(&res))
}
}
impl From<H160> for Did {
fn from(addr: H160) -> Self {
Self(addr)
}
}
impl FromStr for Did {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
Ok(Self(H160::from_str(s).map_err(|_| Error::BadCHexInCache)?))
}
}
impl Neg for Did {
type Output = Self;
fn neg(self) -> Self {
let ret = BigUint::from(2u16).pow(160) - BigUint::from(self);
ret.into()
}
}
impl Add for Did {
type Output = Self;
fn add(self, rhs: Self) -> Self {
((BigUint::from(self) + BigUint::from(rhs)) % (BigUint::from(2u16).pow(160))).into()
}
}
impl Sub for Did {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
self + (-rhs)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
#[test]
fn test_did() {
let a = Did::from_str("0x11E807fcc88dD319270493fB2e822e388Fe36ab0").unwrap();
let b = Did::from_str("0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E").unwrap();
let c = Did::from_str("0xc0ffee254729296a45a3885639AC7E10F9d54979").unwrap();
assert!(c > b && b > a);
}
#[test]
fn test_finate_ring_neg() {
let zero = Did::from_str("0x0000000000000000000000000000000000000000").unwrap();
let a = Did::from_str("0x11E807fcc88dD319270493fB2e822e388Fe36ab0").unwrap();
assert_eq!(-a + a, zero);
assert_eq!(-(-a), a);
}
#[test]
fn test_sort() {
let a = Did::from_str("0xaaE807fcc88dD319270493fB2e822e388Fe36ab0").unwrap();
let b = Did::from_str("0xbb9999cf1046e68e36E1aA2E0E07105eDDD1f08E").unwrap();
let c = Did::from_str("0xccffee254729296a45a3885639AC7E10F9d54979").unwrap();
let d = Did::from_str("0xdddfee254729296a45a3885639AC7E10F9d54979").unwrap();
let mut v = vec![c, b, a, d];
v.sort(a);
assert_eq!(v, vec![a, b, c, d]);
v.sort(b);
assert_eq!(v, vec![b, c, d, a]);
v.sort(c);
assert_eq!(v, vec![c, d, a, b]);
v.sort(d);
assert_eq!(v, vec![d, a, b, c]);
}
}