forked from cruzbit/cruzbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor_test.go
84 lines (80 loc) · 3.07 KB
/
processor_test.go
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
// Copyright 2019 cruzbit developers
// Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
package cruzbit
import "testing"
func TestBlockCreationReward(t *testing.T) {
var maxHalvings int64 = 64
var previous int64 = INITIAL_COINBASE_REWARD * 2
var halvings int64
for halvings = 0; halvings < maxHalvings; halvings++ {
var height int64 = halvings * BLOCKS_UNTIL_REWARD_HALVING
reward := BlockCreationReward(height)
if reward > INITIAL_COINBASE_REWARD {
t.Fatalf("Reward %d at height %d greater than initial reward %d",
reward, height, INITIAL_COINBASE_REWARD)
}
if reward != previous/2 {
t.Fatalf("Reward %d at height %d not equal to half previous period reward",
reward, height)
}
previous = reward
}
if BlockCreationReward(maxHalvings*BLOCKS_UNTIL_REWARD_HALVING) != 0 {
t.Fatalf("Expected 0 reward by %d halving", maxHalvings)
}
}
func TestComputeMaxTransactionsPerBlock(t *testing.T) {
var maxDoublings int64 = 64
var doublings int64
previous := INITIAL_MAX_TRANSACTIONS_PER_BLOCK / 2
// verify the max is always doubling as expected
for doublings = 0; doublings < maxDoublings; doublings++ {
var height int64 = doublings * BLOCKS_UNTIL_TRANSACTIONS_PER_BLOCK_DOUBLING
max := computeMaxTransactionsPerBlock(height)
if max < INITIAL_MAX_TRANSACTIONS_PER_BLOCK {
t.Fatalf("Max %d at height %d less than initial", max, height)
}
expect := previous * 2
if expect > MAX_TRANSACTIONS_PER_BLOCK {
expect = MAX_TRANSACTIONS_PER_BLOCK
}
if max != expect {
t.Fatalf("Max %d at height %d not equal to expected max %d",
max, height, expect)
}
if doublings > 0 {
var previous2 int = max
// walk back over the previous period and make sure:
// 1) the max is never greater than this period's first max
// 2) the max is always <= the previous as we walk back
for height -= 1; height >= (doublings-1)*BLOCKS_UNTIL_TRANSACTIONS_PER_BLOCK_DOUBLING; height-- {
max2 := computeMaxTransactionsPerBlock(height)
if max2 > max {
t.Fatalf("Max %d at height %d is greater than next period's first max %d",
max2, height, max)
}
if max2 > previous2 {
t.Fatalf("Max %d at height %d is greater than previous max %d at height %d",
max2, height, previous2, height+1)
}
previous2 = max2
}
}
previous = max
}
max := computeMaxTransactionsPerBlock(MAX_TRANSACTIONS_PER_BLOCK_EXCEEDED_AT_HEIGHT)
if max != MAX_TRANSACTIONS_PER_BLOCK {
t.Fatalf("Expected %d at height %d, found %d",
MAX_TRANSACTIONS_PER_BLOCK, MAX_TRANSACTIONS_PER_BLOCK_EXCEEDED_AT_HEIGHT, max)
}
max = computeMaxTransactionsPerBlock(MAX_TRANSACTIONS_PER_BLOCK_EXCEEDED_AT_HEIGHT + 1)
if max != MAX_TRANSACTIONS_PER_BLOCK {
t.Fatalf("Expected %d at height %d, found",
MAX_TRANSACTIONS_PER_BLOCK, MAX_TRANSACTIONS_PER_BLOCK_EXCEEDED_AT_HEIGHT+1)
}
max = computeMaxTransactionsPerBlock(MAX_TRANSACTIONS_PER_BLOCK_EXCEEDED_AT_HEIGHT - 1)
if max >= MAX_TRANSACTIONS_PER_BLOCK {
t.Fatalf("Expected less than max at height %d, found %d",
MAX_TRANSACTIONS_PER_BLOCK_EXCEEDED_AT_HEIGHT-1, max)
}
}