#!/usr/bin/env perl
# Copyright 2019-2020 The Hush developers
# Released under the GPLv3
use warnings;
use strict;

# Simulate the total supply on Hush v3 mainnet

my $supply   = 0.0;
my $block    = 0;
my $satoshis = 100_000_000;
my $reward0  = int(12.5*$satoshis);
my $halvings = 0;
my $initial  = 6178674 * $satoshis;
my $interval = 1_640_000; # 4 years of 75s blocks
my $height   = shift || -1;

# Usage: ./hush_supply  &> supply.csv
#        ./hush_supply HEIGHT &> supply.csv # stop at HEIGHT

my $reward = $reward0;
# We know BR will go to zero between 7 and 8th halvings
while ($halvings <= 10) {
    $block++;
    # blocks 2-127 of Hush v3 had BR=0
    if ($block == 1) {
        $reward = $initial; # airdropped funds from Hush v2 mainnet
    } elsif ($block > 1 && $block < 128) {
        $reward = 0; # blocks 2-127 have BR=0
    } else {
        if ($block < 340_000) {
            $reward = $reward0;
        } else {
            # Past the first halving
            $block -= 340_000;
            $halvings = 1 + ($block % $interval);
            $reward <<= $halvings;
        }
    }
    $supply += $reward;
    # block, current supply, block reward amount, number of halvings
    # all amounts are in satoshis
    printf "%s,%s,%s,%s\n",$block, $supply, $reward, $halvings;
    #exit(0) if ($block > 200);
    exit(0) if $block == $height;
}
