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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use crate::{
    core::{BlockNumber, Capacity, CapacityResult, EpochNumber},
    packed,
    prelude::*,
    U256,
};
use ckb_rational::RationalU256;
use std::cmp::Ordering;
use std::fmt;
use std::num::ParseIntError;
use std::str::FromStr;

/// TODO(doc): @quake
#[derive(Clone, PartialEq, Default, Debug)]
pub struct BlockExt {
    /// TODO(doc): @quake
    pub received_at: u64,
    /// TODO(doc): @quake
    pub total_difficulty: U256,
    /// TODO(doc): @quake
    pub total_uncles_count: u64,
    /// TODO(doc): @quake
    pub verified: Option<bool>,
    /// TODO(doc): @quake
    pub txs_fees: Vec<Capacity>,
}

/// TODO(doc): @quake
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct TransactionInfo {
    /// TODO(doc): @quake
    // Block hash
    pub block_hash: packed::Byte32,
    /// TODO(doc): @quake
    pub block_number: BlockNumber,
    /// TODO(doc): @quake
    pub block_epoch: EpochNumberWithFraction,
    /// TODO(doc): @quake
    // Index in the block
    pub index: usize,
}

impl TransactionInfo {
    /// TODO(doc): @quake
    pub fn key(&self) -> packed::TransactionKey {
        packed::TransactionKey::new_builder()
            .block_hash(self.block_hash.clone())
            .index(self.index.pack())
            .build()
    }

    /// TODO(doc): @quake
    pub fn new(
        block_number: BlockNumber,
        block_epoch: EpochNumberWithFraction,
        block_hash: packed::Byte32,
        index: usize,
    ) -> Self {
        TransactionInfo {
            block_number,
            block_epoch,
            block_hash,
            index,
        }
    }

    /// TODO(doc): @quake
    pub fn is_cellbase(&self) -> bool {
        self.index == 0
    }

    /// TODO(doc): @quake
    pub fn is_genesis(&self) -> bool {
        self.block_number == 0
    }
}

/// TODO(doc): @quake
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct EpochExt {
    pub(crate) number: EpochNumber,
    pub(crate) base_block_reward: Capacity,
    pub(crate) remainder_reward: Capacity,
    pub(crate) previous_epoch_hash_rate: U256,
    pub(crate) last_block_hash_in_previous_epoch: packed::Byte32,
    pub(crate) start_number: BlockNumber,
    pub(crate) length: BlockNumber,
    pub(crate) compact_target: u32,
}

#[derive(Clone, Debug)]
pub struct EpochExtBuilder(pub(crate) EpochExt);

impl EpochExt {
    //
    // Simple Getters
    //

    /// TODO(doc): @quake
    pub fn number(&self) -> EpochNumber {
        self.number
    }

    /// TODO(doc): @quake
    pub fn primary_reward(&self) -> Capacity {
        Capacity::shannons(
            self.base_block_reward.as_u64() * self.length + self.remainder_reward.as_u64(),
        )
    }
    /// TODO(doc): @quake
    pub fn base_block_reward(&self) -> &Capacity {
        &self.base_block_reward
    }

    /// TODO(doc): @quake
    pub fn remainder_reward(&self) -> &Capacity {
        &self.remainder_reward
    }

    /// TODO(doc): @quake
    pub fn previous_epoch_hash_rate(&self) -> &U256 {
        &self.previous_epoch_hash_rate
    }

    /// TODO(doc): @quake
    pub fn last_block_hash_in_previous_epoch(&self) -> packed::Byte32 {
        self.last_block_hash_in_previous_epoch.clone()
    }

    /// TODO(doc): @quake
    pub fn start_number(&self) -> BlockNumber {
        self.start_number
    }

    /// TODO(doc): @quake
    pub fn length(&self) -> BlockNumber {
        self.length
    }

    /// TODO(doc): @quake
    pub fn compact_target(&self) -> u32 {
        self.compact_target
    }

    //
    // Simple Setters
    //

    /// TODO(doc): @quake
    pub fn set_number(&mut self, number: BlockNumber) {
        self.number = number;
    }

    /// TODO(doc): @quake
    pub fn set_base_block_reward(&mut self, base_block_reward: Capacity) {
        self.base_block_reward = base_block_reward;
    }

    /// TODO(doc): @quake
    pub fn set_remainder_reward(&mut self, remainder_reward: Capacity) {
        self.remainder_reward = remainder_reward;
    }

    /// TODO(doc): @quake
    pub fn set_previous_epoch_hash_rate(&mut self, previous_epoch_hash_rate: U256) {
        self.previous_epoch_hash_rate = previous_epoch_hash_rate;
    }

    /// TODO(doc): @quake
    pub fn set_last_block_hash_in_previous_epoch(
        &mut self,
        last_block_hash_in_previous_epoch: packed::Byte32,
    ) {
        self.last_block_hash_in_previous_epoch = last_block_hash_in_previous_epoch;
    }

    /// TODO(doc): @quake
    pub fn set_start_number(&mut self, start_number: BlockNumber) {
        self.start_number = start_number;
    }

    /// TODO(doc): @quake
    pub fn set_length(&mut self, length: BlockNumber) {
        self.length = length;
    }

    /// TODO(doc): @quake
    pub fn set_primary_reward(&mut self, primary_reward: Capacity) {
        let primary_reward_u64 = primary_reward.as_u64();
        self.base_block_reward = Capacity::shannons(primary_reward_u64 / self.length);
        self.remainder_reward = Capacity::shannons(primary_reward_u64 % self.length);
    }

    /// TODO(doc): @quake
    pub fn set_compact_target(&mut self, compact_target: u32) {
        self.compact_target = compact_target;
    }

    //
    // Normal Methods
    //

    /// TODO(doc): @quake
    pub fn new_builder() -> EpochExtBuilder {
        EpochExtBuilder(EpochExt::default())
    }

    /// TODO(doc): @quake
    pub fn into_builder(self) -> EpochExtBuilder {
        EpochExtBuilder(self)
    }

    /// TODO(doc): @quake
    pub fn is_genesis(&self) -> bool {
        0 == self.number
    }

    /// TODO(doc): @quake
    pub fn block_reward(&self, number: BlockNumber) -> CapacityResult<Capacity> {
        if number >= self.start_number()
            && number < self.start_number() + self.remainder_reward.as_u64()
        {
            self.base_block_reward.safe_add(Capacity::one())
        } else {
            Ok(self.base_block_reward)
        }
    }

    /// TODO(doc): @quake
    pub fn number_with_fraction(&self, number: BlockNumber) -> EpochNumberWithFraction {
        debug_assert!(
            number >= self.start_number() && number < self.start_number() + self.length()
        );
        EpochNumberWithFraction::new(self.number(), number - self.start_number(), self.length())
    }

    // We name this issuance since it covers multiple parts: block reward,
    // NervosDAO issuance as well as treasury part.
    /// TODO(doc): @quake
    pub fn secondary_block_issuance(
        &self,
        block_number: BlockNumber,
        secondary_epoch_issuance: Capacity,
    ) -> CapacityResult<Capacity> {
        let mut g2 = Capacity::shannons(secondary_epoch_issuance.as_u64() / self.length());
        let remainder = secondary_epoch_issuance.as_u64() % self.length();
        if block_number >= self.start_number() && block_number < self.start_number() + remainder {
            g2 = g2.safe_add(Capacity::one())?;
        }
        Ok(g2)
    }
}

impl EpochExtBuilder {
    //
    // Simple Setters
    //

    pub fn number(mut self, number: BlockNumber) -> Self {
        self.0.set_number(number);
        self
    }

    pub fn base_block_reward(mut self, base_block_reward: Capacity) -> Self {
        self.0.set_base_block_reward(base_block_reward);
        self
    }

    pub fn remainder_reward(mut self, remainder_reward: Capacity) -> Self {
        self.0.set_remainder_reward(remainder_reward);
        self
    }

    pub fn previous_epoch_hash_rate(mut self, previous_epoch_hash_rate: U256) -> Self {
        self.0
            .set_previous_epoch_hash_rate(previous_epoch_hash_rate);
        self
    }

    pub fn last_block_hash_in_previous_epoch(
        mut self,
        last_block_hash_in_previous_epoch: packed::Byte32,
    ) -> Self {
        self.0
            .set_last_block_hash_in_previous_epoch(last_block_hash_in_previous_epoch);
        self
    }

    pub fn start_number(mut self, start_number: BlockNumber) -> Self {
        self.0.set_start_number(start_number);
        self
    }

    pub fn length(mut self, length: BlockNumber) -> Self {
        self.0.set_length(length);
        self
    }

    pub fn compact_target(mut self, compact_target: u32) -> Self {
        self.0.set_compact_target(compact_target);
        self
    }

    //
    // Normal Methods
    //
    // The `new` methods are unnecessary. Creating `EpochExtBuilder` from `EpochExt`, it's enough.

    pub fn build(self) -> EpochExt {
        self.0
    }
}

/// Represents an epoch number with a fraction unit, it can be
/// used to accurately represent the position for a block within
/// an epoch.
// Don't derive `Default` trait:
// - If we set default inner value to 0, it would panic when call `to_rational()`
// - But when uses it as an increment, "length == 0" is allowed, it's a valid default value.
// So, use `new()` or `new_unchecked()` to construct the instance depends on the context.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct EpochNumberWithFraction(u64);

impl fmt::Display for EpochNumberWithFraction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            write!(f, "{}({}/{})", self.number(), self.index(), self.length())
        } else {
            write!(
                f,
                "Epoch {{ number: {}, index: {}, length: {} }}",
                self.number(),
                self.index(),
                self.length()
            )
        }
    }
}

impl FromStr for EpochNumberWithFraction {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let v = u64::from_str(s)?;
        Ok(EpochNumberWithFraction(v))
    }
}

impl PartialOrd for EpochNumberWithFraction {
    fn partial_cmp(&self, other: &EpochNumberWithFraction) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for EpochNumberWithFraction {
    fn cmp(&self, other: &EpochNumberWithFraction) -> Ordering {
        match self.number().cmp(&other.number()) {
            ord @ Ordering::Less | ord @ Ordering::Greater => ord,
            _ => {
                let a = self.index() * other.length();
                let b = other.index() * self.length();
                a.cmp(&b)
            }
        }
    }
}

impl EpochNumberWithFraction {
    /// TODO(doc): @quake
    pub const NUMBER_OFFSET: usize = 0;
    /// TODO(doc): @quake
    pub const NUMBER_BITS: usize = 24;
    /// TODO(doc): @quake
    pub const NUMBER_MAXIMUM_VALUE: u64 = (1u64 << Self::NUMBER_BITS);
    /// TODO(doc): @quake
    pub const NUMBER_MASK: u64 = (Self::NUMBER_MAXIMUM_VALUE - 1);
    /// TODO(doc): @quake
    pub const INDEX_OFFSET: usize = Self::NUMBER_BITS;
    /// TODO(doc): @quake
    pub const INDEX_BITS: usize = 16;
    /// TODO(doc): @quake
    pub const INDEX_MAXIMUM_VALUE: u64 = (1u64 << Self::INDEX_BITS);
    /// TODO(doc): @quake
    pub const INDEX_MASK: u64 = (Self::INDEX_MAXIMUM_VALUE - 1);
    /// TODO(doc): @quake
    pub const LENGTH_OFFSET: usize = Self::NUMBER_BITS + Self::INDEX_BITS;
    /// TODO(doc): @quake
    pub const LENGTH_BITS: usize = 16;
    /// TODO(doc): @quake
    pub const LENGTH_MAXIMUM_VALUE: u64 = (1u64 << Self::LENGTH_BITS);
    /// TODO(doc): @quake
    pub const LENGTH_MASK: u64 = (Self::LENGTH_MAXIMUM_VALUE - 1);

    /// TODO(doc): @quake
    pub fn new(number: u64, index: u64, length: u64) -> EpochNumberWithFraction {
        debug_assert!(number < Self::NUMBER_MAXIMUM_VALUE);
        debug_assert!(index < Self::INDEX_MAXIMUM_VALUE);
        debug_assert!(length < Self::LENGTH_MAXIMUM_VALUE);
        debug_assert!(length > 0);
        Self::new_unchecked(number, index, length)
    }

    /// TODO(doc): @quake
    pub const fn new_unchecked(number: u64, index: u64, length: u64) -> Self {
        EpochNumberWithFraction(
            (length << Self::LENGTH_OFFSET)
                | (index << Self::INDEX_OFFSET)
                | (number << Self::NUMBER_OFFSET),
        )
    }

    /// TODO(doc): @quake
    pub fn number(self) -> EpochNumber {
        (self.0 >> Self::NUMBER_OFFSET) & Self::NUMBER_MASK
    }

    /// TODO(doc): @quake
    pub fn index(self) -> u64 {
        (self.0 >> Self::INDEX_OFFSET) & Self::INDEX_MASK
    }

    /// TODO(doc): @quake
    pub fn length(self) -> u64 {
        (self.0 >> Self::LENGTH_OFFSET) & Self::LENGTH_MASK
    }

    /// TODO(doc): @quake
    pub const fn full_value(self) -> u64 {
        self.0
    }

    /// Estimate the floor limit of epoch number after N blocks.
    ///
    /// Since we couldn't know the length of next epoch before reach the next epoch,
    /// this function could only return `self.number()` or `self.number()+1`.
    pub fn minimum_epoch_number_after_n_blocks(self, n: BlockNumber) -> EpochNumber {
        let number = self.number();
        let length = self.length();
        let index = self.index();
        if index + n >= length {
            number + 1
        } else {
            number
        }
    }

    /// TODO(doc): @quake
    // One caveat here, is that if the user specifies a zero epoch length either
    // deliberately, or by accident, calling to_rational() after that might
    // result in a division by zero panic. To prevent that, this method would
    // automatically rewrite the value to epoch index 0 with epoch length to
    // prevent panics
    pub fn from_full_value(value: u64) -> Self {
        Self::from_full_value_unchecked(value).normalize()
    }

    /// Converts from an unsigned 64 bits number without checks.
    ///
    /// # Notice
    ///
    /// The `EpochNumberWithFraction` constructed by this method has a potential risk that when
    /// call `self.to_rational()` may lead to a panic if the user specifies a zero epoch length.
    pub fn from_full_value_unchecked(value: u64) -> Self {
        Self(value)
    }

    /// Prevents leading to a panic if the `EpochNumberWithFraction` is constructed without checks.
    pub fn normalize(self) -> Self {
        if self.length() == 0 {
            Self::new(self.number(), 0, 1)
        } else {
            self
        }
    }

    /// Converts the epoch to an unsigned 256 bits rational.
    ///
    /// # Panics
    ///
    /// Only genesis epoch's length could be zero, otherwise causes a division-by-zero panic.
    pub fn to_rational(self) -> RationalU256 {
        if self.0 == 0 {
            RationalU256::zero()
        } else {
            RationalU256::new(self.index().into(), self.length().into()) + U256::from(self.number())
        }
    }

    /// Check if current value is another value's successor.
    pub fn is_successor_of(self, predecessor: Self) -> bool {
        if predecessor.index() + 1 == predecessor.length() {
            self.number() == predecessor.number() + 1 && self.index() == 0
        } else {
            self.number() == predecessor.number()
                && self.index() == predecessor.index() + 1
                && self.length() == predecessor.length()
        }
    }

    /// Check the data format.
    ///
    /// The epoch length should be greater than zero.
    /// The epoch index should be less than the epoch length.
    pub fn is_well_formed(self) -> bool {
        self.length() > 0 && self.length() > self.index()
    }

    /// Check the data format as an increment.
    ///
    /// The epoch index should be less than the epoch length or both of them are zero.
    pub fn is_well_formed_increment(self) -> bool {
        self.length() > self.index() || (self.length() == 0 && self.index() == 0)
    }
}