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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use std::fmt;
use std::ops::{Add, AddAssign, Div, Sub, SubAssign};

use num_bigint::BigUint;

use beserial::{Deserialize, DeserializeWithLength, ReadBytesExt, Serialize, SerializeWithLength, SerializingError, WriteBytesExt};
use fixed_unsigned::types::FixedUnsigned10;
use hash::Argon2dHash;
use primitives::policy;

/// Compact Target (represented internally as `u32`)
#[derive(Default, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Serialize, Deserialize)]
pub struct TargetCompact(u32);

impl fmt::Debug for TargetCompact {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "TargetCompact{{{:04x}: shift={}, value={:06x}}}", self.0, self.0 >> 24, self.0 & 0x00FF_FFFF)
    }
}

// Target as 32 byte array
create_typed_array!(Target, u8, 32);

/// Difficulty as FixedUnsigned with 10 decimal places
#[derive(Default, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)]
pub struct Difficulty(FixedUnsigned10);

/// Convert `TargetCompact` to `u32`
impl From<TargetCompact> for u32 {
    fn from(t: TargetCompact) -> Self { t.0 }
}

/// Convert `u32` to `TargetCompact`
impl From<u32> for TargetCompact {
    fn from(u: u32) -> Self { TargetCompact(u) }
}

/// Convert `TargetCompact` to `Target`
impl From<TargetCompact> for Target {
    fn from(target_compact: TargetCompact) -> Self {
        let mut val = [0u8; 32];
        let shift_bytes = (target_compact.0 >> 24).saturating_sub(3) as usize;
        val[32 - shift_bytes - 1] = (target_compact.0 & 0xff) as u8;
        val[32 - shift_bytes - 2] = ((target_compact.0 >> 8) & 0xff) as u8;
        val[32 - shift_bytes - 3] = ((target_compact.0 >> 16) & 0xff) as u8;
        Target(val)
    }
}

/// Convert `Target` to `TargetCompact`
///
/// NOTE: This just delegates to `From<&Target> for TargetCompact`
impl From<Target> for TargetCompact {
    fn from(target: Target) -> Self {
        TargetCompact::from(&target)
    }
}

/// Convert `&Target` to `TargetCompact`
impl<'a> From<&'a Target> for TargetCompact {
    fn from(target: &'a Target) -> Self {
        // Determine where the first byte in target is.
        let mut first_byte = 0;
        for i in 0..target.0.len() {
            if target.0[i] > 0 {
                first_byte = i;
                break;
            }
        }

        // The significant is signed and therefore can't be > 0x80. If it is, we take the byte before it as first byte
        if target.0[first_byte] >= 0x80 && first_byte <= 29 {
            first_byte -= 1;
        }

        let shift_bytes = 32 - first_byte;
        let start_byte = first_byte.min(29);
        TargetCompact(((shift_bytes as u32) << 24)
            | (u32::from(target.0[start_byte]) << 16)
            | (u32::from(target.0[start_byte + 1]) << 8)
            | u32::from(target.0[start_byte + 2]))
    }
}

/// Convert `Argon2dHash` to `Target`
impl<'a> From<&'a Argon2dHash> for Target {
    fn from(hash: &'a Argon2dHash) -> Self {
        Target::from(hash.as_bytes())
    }
}

/// Convert `BigUint` to `Target`
impl From<BigUint> for Target {
    fn from(num: BigUint) -> Self {
        // Convert `BigUint` to big-endian byte represetation, this must have length <= 32
        let bytes = num.to_bytes_be();
        let byte_len = bytes.len();
        assert!(byte_len <= 32, "Cannot convert BigDecimal to Target - out of bounds");

        // Fill up front of array (most-siginificant bytes) with zeros
        let mut target_data = [0u8; 32];
        for i in 0..byte_len {
            target_data[32 - byte_len + i] = bytes[i];
        }

        Target(target_data)
    }
}

/// Convert `Target` to `BigUint`
impl From<Target> for BigUint {
    fn from(target: Target) -> BigUint {
        let target_data: [u8; 32] = target.into();
        BigUint::from_bytes_be(&target_data)
    }
}

/// Convert `Target` to `FixedUnsigned10`
impl From<Target> for FixedUnsigned10 {
    fn from(target: Target) -> FixedUnsigned10 {
        FixedUnsigned10::from(BigUint::from(target))
    }
}

/// Convert `TargetCompact` to `Difficulty`
impl From<TargetCompact> for Difficulty {
    fn from(target_compact: TargetCompact) -> Self {
        Target::from(target_compact).into()
    }
}

/// Convert `Target` to `Difficulty`
impl From<Target> for Difficulty {
    fn from(target: Target) -> Self {
        Difficulty(&*policy::BLOCK_TARGET_MAX / &FixedUnsigned10::from(target))
    }
}

/// Convert `FixedUnsigned10` to `Target`
impl From<FixedUnsigned10> for Target {
    fn from(fixed: FixedUnsigned10) -> Target {
        Target::from(fixed.into_biguint())
    }
}

/// XXX For testing only
impl From<Difficulty> for TargetCompact {
    fn from(difficulty: Difficulty) -> Self {
        Target::from(difficulty).into()
    }
}

/// XXX For testing only
impl From<Difficulty> for Target {
    fn from(difficulty: Difficulty) -> Self {
        Target::from(&*policy::BLOCK_TARGET_MAX / &FixedUnsigned10::from(difficulty))
    }
}

/// Converts a `i32` to a `Difficulty`
///
/// NOTE: This is just to please the old test gods, because the tests were implemented with
///       positive `i32`s.
///
/// XXX For testing only
impl From<i32> for Difficulty {
    fn from(x: i32) -> Difficulty {
        warn!("Conversion from `i32` to `Difficulty`. This will panic on negative numbers! Do not use!");
        if x < 0 {
            panic!("Can't convert a `i32` into a `Difficulty`!");
        }
        Difficulty(FixedUnsigned10::from(x as u32))
    }
}

/// Convert a `FixedUnsigned10` (10 decimal places) to `Difficulty`
impl From<FixedUnsigned10> for Difficulty {
    fn from(fixed: FixedUnsigned10) -> Self {
        Difficulty(fixed)
    }
}

impl From<Difficulty> for FixedUnsigned10 {
    fn from(difficulty: Difficulty) -> Self { difficulty.0 }
}

/// XXX Only for debugging - or is it?
impl From<u64> for Difficulty {
    fn from(x: u64) -> Difficulty {
        Difficulty(FixedUnsigned10::from(x))
    }
}

/// Convert `u32` to `Difficulty`
impl From<u32> for Difficulty {
    fn from(x: u32) -> Difficulty {
        Difficulty(FixedUnsigned10::from(x))
    }
}

/// Add a `u32` onto a `Difficulty`
///
/// This is used in `Blockchain::get_next_target` to compute the `delta_total_difficulty`:
impl AddAssign<u32> for Difficulty {
    fn add_assign(&mut self, rhs: u32) {
        *self += Difficulty(FixedUnsigned10::from(rhs));
    }
}

/// Divide `Difficulty` by `u32`
///
/// This is used in `Blockchain::get_next_target`
///
/// NOTE: I chose to implement exactly those traits with these types to abstract away how
///       `Difficulty` works from the `Blockchain`
impl Div<u32> for Difficulty {
    type Output = Difficulty;

    fn div(self, rhs: u32) -> <Self as Div<u32>>::Output {
        Difficulty(self.0 / FixedUnsigned10::from(rhs))
    }
}

impl Add<Difficulty> for Difficulty {
    type Output = Difficulty;

    fn add(self, rhs: Difficulty) -> Difficulty {
        Difficulty(self.0 + rhs.0)
    }
}

impl<'a, 'b> Add<&'b Difficulty> for &'a Difficulty {
    type Output = Difficulty;

    fn add(self, rhs: &'b Difficulty) -> Difficulty {
        Difficulty(&self.0 + &rhs.0)
    }
}

impl AddAssign<Difficulty> for Difficulty {
    fn add_assign(&mut self, rhs: Difficulty) {
        *self = Difficulty(&self.0 + &rhs.0);
    }
}

impl Sub<Difficulty> for Difficulty {
    type Output = Difficulty;

    fn sub(self, rhs: Difficulty) -> Difficulty {
        Difficulty(self.0 - rhs.0)
    }
}

impl<'a, 'b> Sub<&'b Difficulty> for &'a Difficulty {
    type Output = Difficulty;

    fn sub(self, rhs: &'b Difficulty) -> Difficulty {
        Difficulty(&self.0 - &rhs.0)
    }
}

impl SubAssign<Difficulty> for Difficulty {
    fn sub_assign(&mut self, rhs: Difficulty) {
        *self = Difficulty(&self.0 - &rhs.0);
    }
}

impl Serialize for Difficulty {
    fn serialize<W: WriteBytesExt>(&self, writer: &mut W) -> Result<usize, SerializingError> {
        SerializeWithLength::serialize::<u8, W>(&self.0.to_bytes_be(), writer)
    }

    fn serialized_size(&self) -> usize {
        self.0.bytes() + 1 // 1 byte for length
    }
}

impl Deserialize for Difficulty {
    fn deserialize<R: ReadBytesExt>(reader: &mut R) -> Result<Self, SerializingError> {
        let bytes: Vec<u8> = DeserializeWithLength::deserialize::<u8, R>(reader)?;
        Ok(Difficulty(FixedUnsigned10::from_bytes_be(bytes.as_slice())))
    }
}

impl fmt::Display for Difficulty {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        return write!(f, "{}", self.0);
    }
}

impl Target {
    pub fn is_met_by(&self, hash: &Argon2dHash) -> bool {
        let reached = Target::from(hash);
        reached < *self
    }

    pub fn get_depth(&self) -> u8 {
        // Compute: 240 - ceil(log2(self))

        // Find first non-zero byte.
        let len = self.0.len();
        let mut first_byte = 0;
        for i in 0..len {
            if self.0[i] > 0 {
                first_byte = i;
                break;
            }
        }

        // Find last non-zero byte.
        let mut last_byte = 0;
        for i in 0..len - first_byte {
            let idx = len - i - 1;
            if self.0[idx] > 0 {
                last_byte = idx;
                break;
            }
        }

        let leading_zeros = self.0[first_byte].leading_zeros();
        let mut exp = 8 - leading_zeros + (len - first_byte - 1) as u32 * 8;

        if first_byte == last_byte && self.0[first_byte].trailing_zeros() + leading_zeros == 7 {
            exp -= 1;
        }

        240u8.saturating_sub(exp as u8)
    }
}