github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/emission/emission_test.go (about)

     1  // Copyright 2017-2018 DERO Project. All rights reserved.
     2  // Use of this source code in any form is governed by RESEARCH license.
     3  // license can be found in the LICENSE file.
     4  // GPG: 0F39 E425 8C65 3947 702A  8234 08B2 0360 A03A 9DE8
     5  //
     6  //
     7  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
     8  // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     9  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    10  // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    11  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    12  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    13  // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    14  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
    15  // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    16  
    17  package emission
    18  
    19  //import "encoding/hex"
    20  //import "bytes"
    21  import "testing"
    22  
    23  func Test_Emission_Rewards(t *testing.T) {
    24  
    25  	bl_median_size := uint64(0)
    26  	bl_current_size := uint64(0)
    27  	already_generated_coins := uint64(0)
    28  	hard_fork_version := uint64(6)
    29  	fee := uint64(0)
    30  	if GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee) != uint64(1000000000000) {
    31  		t.Error("Block reward failed for genesis block\n")
    32  	}
    33  
    34  	already_generated_coins = uint64(1000000000000)
    35  
    36  	if GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee) != uint64(2000000000000000000) {
    37  		t.Error("Block reward failed for pre-mine\n")
    38  	}
    39  
    40  	already_generated_coins += uint64(2000000000000000000)
    41  
    42  	calculated := GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee)
    43  	expected := uint64(31369672915858)
    44  	if calculated != expected {
    45  		t.Errorf("Block reward failed for 2nd block estm %d calculated %d\n", expected, calculated)
    46  	}
    47  
    48  	already_generated_coins += uint64(31369672915858)
    49  
    50  	// calculated block reward for 3rd block
    51  	calculated = GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee)
    52  	expected = uint64(31369613082955)
    53  
    54  	if calculated != expected {
    55  		t.Errorf("Block reward failed for 3rd block estm %d calculated %d\n", expected, calculated)
    56  	}
    57  
    58  	// 4 consecutive blocks were filled so test them
    59  	// tests if the block size is greater than median block ( penalty )
    60  	// test from block
    61  	bl_median_size = 300000
    62  	bl_current_size = 301174
    63  	already_generated_coins = 3160947266765937256
    64  	calculated = GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee)
    65  	expected = uint64(29154897151057)
    66  	if calculated != expected {
    67  		t.Errorf("Block reward failed for 38383 block estm %d calculated %d\n", expected, calculated)
    68  	}
    69  
    70  	bl_median_size = 300000
    71  	bl_current_size = 301237
    72  	already_generated_coins = 3160976421663088313
    73  	calculated = GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee)
    74  	expected = uint64(29154792337976)
    75  	if calculated != expected {
    76  		t.Errorf("Block reward failed for 38384 block estm %d calculated %d\n", expected, calculated)
    77  	}
    78  
    79  	bl_median_size = 300000
    80  	bl_current_size = 301745
    81  	already_generated_coins = 3161005576455426289
    82  	calculated = GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee)
    83  	expected = uint64(29154245997471)
    84  	if calculated != expected {
    85  		t.Errorf("Block reward failed for 38385 block estm %d calculated %d\n", expected, calculated)
    86  	}
    87  
    88  	bl_median_size = 300000
    89  	bl_current_size = 302406
    90  	already_generated_coins = 3161034730701423760
    91  	calculated = GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee)
    92  	expected = uint64(29153301544320)
    93  	if calculated != expected {
    94  		t.Errorf("Block reward failed for 38386 block estm %d calculated %d \n", expected, calculated)
    95  	}
    96  
    97  	// TODO test trickle emission
    98  	bl_median_size = 0
    99  	bl_current_size = 0
   100  	already_generated_coins = 0xffffffffffffffff
   101  	calculated = GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee)
   102  	expected = uint64(600000000000)
   103  	if calculated != expected {
   104  		t.Errorf("Block reward failed for trickle block estm %d calculated %d \n", expected, calculated)
   105  	}
   106  
   107  }
   108  
   109  func Test_Emission_Panic_For_Huge_Block(t *testing.T) {
   110  
   111  	defer func() {
   112  		if r := recover(); r == nil {
   113  			t.Fatalf("Emission did not panic on huge blocks")
   114  		}
   115  	}()
   116  
   117  	bl_median_size := uint64(9)
   118  	bl_current_size := uint64(7 * 100 * 1024) // default median size is 300KB
   119  	already_generated_coins := uint64(3161034730701423760)
   120  	hard_fork_version := uint64(6)
   121  	fee := uint64(0)
   122  	expected := GetBlockReward(bl_median_size, bl_current_size, already_generated_coins, hard_fork_version, fee) // this should panic
   123  
   124  	if expected >= 0 {
   125  		t.Errorf("We should never land here %d\n", expected)
   126  	}
   127  
   128  }