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
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the snarkOS library.

// The snarkOS library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The snarkOS library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.

pub const DIFFICULTY_BOMB_DIVISOR: f64 = 1024f64;

/// Linear difficulty recalculation using time elapsed.
pub fn naive_retarget(
    block_timestamp: i64,
    parent_timestamp: i64,
    target_block_time: i64,
    parent_difficulty: u64,
) -> u64 {
    let time_elapsed = block_timestamp - parent_timestamp;
    if time_elapsed == target_block_time || time_elapsed == 0 {
        parent_difficulty
    } else {
        let parent_diff = parent_difficulty as f64;
        let mut x: f64;

        // (target_block_time - time_elapsed) / target_block_time
        x = (target_block_time - time_elapsed) as f64;
        x /= target_block_time as f64;

        // parent_diff - ((target_block_time - block_time) / target_block_time * parent_diff)
        x *= parent_diff;
        x = parent_diff - x;

        println!("old difficulty        {:#x}", parent_difficulty);
        println!("new difficulty        {:#x}", x as u64);

        x as u64
    }
}

/// Bitcoin difficulty retarget algorithm.
pub fn bitcoin_retarget(
    block_timestamp: i64,
    parent_timestamp: i64,
    target_block_time: i64,
    parent_difficulty: u64,
) -> u64 {
    let mut time_elapsed = block_timestamp - parent_timestamp;

    // Limit difficulty adjustment by factor of 2
    if time_elapsed < target_block_time / 2 {
        time_elapsed = target_block_time / 2
    } else if time_elapsed > target_block_time * 2 {
        time_elapsed = target_block_time * 2
    }

    let mut x: u64;
    x = match parent_difficulty.checked_mul(time_elapsed as u64) {
        Some(x) => x,
        None => u64::max_value(),
    };

    x /= target_block_time as u64;

    x
}

/// Custom difficulty retarget algorithm.
pub fn custom_retarget(
    _block_timestamp: i64,
    _parent_timestamp: i64,
    _target_block_time: i64,
    _parent_difficulty: u64,
) -> u64 {
    unimplemented!()
}

/// Ethereum difficulty retarget algorithm.
pub fn ethereum_retarget(block_timestamp: i64, parent_timestamp: i64, parent_difficulty: u64) -> u64 {
    let parent_diff = parent_difficulty as f64;
    let mut x: f64;
    let y: f64;

    // 1 - (block_timestamp - parent_timestamp) // 10
    x = (block_timestamp - parent_timestamp) as f64;
    x /= 10f64;
    x = 1f64 - x;

    // max (1 - (block_timestamp - parent_timestamp) // 10, -99))
    x = f64::max(x, -99f64);

    // (parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99))
    y = parent_diff / DIFFICULTY_BOMB_DIVISOR;
    x *= y;
    x += parent_diff;

    println!("old difficulty        {:#x}", parent_difficulty);
    println!("new difficulty        {:#x}", x as u64);

    x as u64
}