#!/usr/bin/env perl
# Copyright (c) 2016-2021 The Hush developers
# Released under the GPLv3
use warnings;
use strict;


my $supply   = 0.0;
my $block    = 0; # Block 0 in Hush Smart chains is the BTC genesis block
my $puposhis = 100_000_000;
my $subsidy0 = 1_250_000_000;
my $halvings = 0;
my $initial  = 6178674 * $puposhis;
my $interval = 1_680_000; # ~4 years of 75s blocks
my $stop     = shift || -1;
my $totalfr  = 0; # total paid out to FR address


if ($stop eq 'help' or $stop =~ m/-h/) {
    die <<HELP;
# Simulate the total supply on Hush v3 mainnet
# Block Reward: Total Coinbase In Block
# Subsidy     : Coinbase Earned by Miner
# FR          : Founders Reward (10%)
# Block Reward = Subsidy + FR
Usage: ./hush_supply  &> supply.csv
       ./hush_supply HEIGHT &> supply.csv # stop at HEIGHT
# This will generate CSV in the form of:
# block, supply, reward, subsidy, fr, totalfr, halvings
HELP
}


printf "# block, supply, reward, subsidy, fr, totalfr, halvings\n";

# Block Reward Amounts in puposhis
# The non-integral amounts cannot be represented exactly
# 12.5 * 100000000          = 1250000000
# 12.5 * 100000000 / 2      = 625000000
# 12.5 * 100000000 / 4      = 312500000
# 12.5 * 100000000 / 8      = 156250000
# 12.5 * 100000000 / 16     = 78125000
# 12.5 * 100000000 / 32     = 39062500
# 12.5 * 100000000 / 64     = 19531250
# 12.5 * 100000000 / 128    = 9765625
# 12.5 * 100000000 / 256    = 4882812.5
# 12.5 * 100000000 / 512    = 2441406.25
# 12.5 * 100000000 / 1024   = 1220703.125
# 12.5 * 100000000 / 2048   = 610351.5625
# 12.5 * 100000000 / 4096   = 305175.78125
# 12.5 * 100000000 / 8192   = 152587.890625
# 12.5 * 100000000 / 16384  = 76293.9453125
# 12.5 * 100000000 / 32768  = 38146.97265625
# 12.5 * 100000000 / 65536  = 19073.486328125

# Hush Halving Heights and Block Rewards
# 1,12500000000,340000
# 2,312500000,2020000
# 3,156250000,3700000
# 4,78125000,5380000
# 5,39062500,7060000
# 6,19531250,8740000
# 7,9765625,10420000
# 8,4882812,12100000
# 9,2441406,13780000
# 10,1220703,15460000
# 11,610351,17140000
# 12,305175,18820000
# 13,152587,20500000
# 14,76293,22180000
# 15,38146,23860000
# 16,19073,25540000
# 17,9536,27220000
# 18,4768,28900000
# 19,2384,30580000
# 20,1192,32260000
# 21,596,33940000
# 22,298,35620000
# 23,149,37300000
# 24,74,38980000
# 25,37,40660000
# 26,18,42340000
# 27,9,44020000
# 28,4,45700000
# 29,2,47380000
# 30,1,49060000
# 31,0,50740000


sub hush_block_reward
{
    my $reward   = 0;
    my $height   = shift;
    my $halvings = 0;

    if  ($height     >= 50740000)    {
        $reward       = 0;
        $halvings     = 31;
    } elsif ($height >= 49060000)    {
        $reward       = 1;
        $halvings     = 30;
    } elsif ($height >= 47380000)    {
        $reward       = 1;
        $halvings     = 29;
    } elsif ($height >= 45700000)    {
        $reward       = 2;
        $halvings     = 28;
    } elsif ($height >= 44020000)    {
        $reward       = 4;
        $halvings     = 27;
    } elsif ($height >= 42340000)    {
        $reward       = 9;
        $halvings     = 26;
    } elsif ($height >= 40660000)    {
        $reward       = 18;
        $halvings     = 25;
    } elsif ($height >= 38980000)    {
        $reward       = 37;
        $halvings     = 24;
    } elsif ($height >= 37380000)    {
        $reward       = 74;
        $halvings     = 23;
    } elsif ($height >= 35620000)    {
        $reward       = 149;
        $halvings     = 22;
    } elsif ($height >= 33940000)    {
        $reward       = 298;
        $halvings     = 21;
    } elsif ($height >= 32260001)    {
        $reward       = 596;
        $halvings     = 20;
    } elsif ($height >= 30580000)    {
        $reward       = 1192;
        $halvings     = 19;
    } elsif ($height >= 28900000)    {
        $reward       = 2384;
        $halvings     = 18;
    } elsif ($height >= 27220000)    {
        $reward       = 4768;
        $halvings     = 17;
    } elsif ($height >= 25540000)    {
        $reward       = 9536;
        $halvings     = 16;
    } elsif ($height >= 23860000)    {
        $reward       = 19073; # 0.486328125 deviation
        $halvings     = 15;
    } elsif ($height >= 22180000)    {
        $reward       = 38146; # 0.97265625 deviation
        $halvings     = 14;
    } elsif ($height >= 20500000)    {
        $reward       = 76293; # 0.9453125 deviation
        $halvings     = 13;
    } elsif ($height >= 18820000)    {
        $reward       = 152587; #  0.890625 deviation
        $halvings     = 12;
    } elsif ($height >= 17140000)    {
        $reward       = 305175; # 0.78125sat deviation
        $halvings     = 11;
    } elsif ($height >= 15460000)    {
        $reward       = 610351; # 0.5625sat deviation
        $halvings     = 10;
    } elsif ($height >= 13780000)    {
        $reward       = 1220703; # 0.125sat deviation
        $halvings     = 9
    } elsif ($height >= 12100000) {
        $reward       = 2441406; # 0.25sat deviation
        $halvings     = 8
    } elsif ($height >= 10420000) {
        $reward       = 4882812; # 0.5sat deviation
        $halvings     = 7;
    } elsif ($height >= 8740000)  {
        $reward       = 9765625; # last exact reward
        $halvings     = 6;
    } elsif ($height >= 7060000)  {
        $reward       = 19531250; # 0.1953125 HUSH
        $halvings     = 5;
    } elsif ($height >= 5380000)  {
        $reward       = 39062500; # 0.390625 HUSH
        $halvings     = 4;
    } elsif ($height >= 3700000) {
        $reward       = 78125000;  # 0.78125 HUSH
        $halvings     = 3;
    } elsif ($height >= 2020000) {
        $reward       = 156250000; # 1.5625 HUSH
        $halvings     = 2;
    } elsif ($height >= 340000) {
        $reward       = 312500000; # 3.125 HUSH
        $halvings     = 1;
    } elsif ($height >= 128) {
        $reward = 1250000000; # 12.5 HUSH
    }

    return ($reward,$halvings);
}

# Block reward is 0 at the 31st halving
while ($halvings <= 30) {
    $block++;
    my ($reward,$halvings) = hush_block_reward($block);
    my $fr                 = int($reward /  10);
    my $subsidy            = $reward - $fr;
    
    if($block == 1) {
        # initial airdrop of funds from HUSH v2 network @ Block 500000
        $reward = $initial;
        $subsidy= $reward;
        $fr     = 0;
    }
    $supply    += $reward;
    $totalfr   += $fr;

    # all values in puposhis
    # block, current supply, block reward amount, fr, totalfr, number of halvings
    printf "%d,%d,%d,%d,%d,%d,%d\n", $block, $supply, $reward, $subsidy, $fr, $totalfr, $halvings;
    exit(0) if $block == $stop;
    exit(0) if ($block > 128 && $reward == 0);
    exit(-1) if ($supply >= 21_000_000*$puposhis);
}
