forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockchainparam_test.go
149 lines (123 loc) · 4.67 KB
/
blockchainparam_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package miner
import (
"github.com/julwil/bazo-miner/crypto"
"github.com/julwil/bazo-miner/protocol"
"testing"
)
//Testing whether target calculation responds to block rollbacks
func TestTargetHistory(t *testing.T) {
cleanAndPrepare()
activeParameters.DiffInterval = 5
activeParameters.BlockInterval = 5
//Build 5 blocks, this results in a targets update and a targetTime update
//with timerange.first = 0 because of the genesis block
var blocks []*protocol.Block
var tmpBlock *protocol.Block
tmpBlock = new(protocol.Block)
for cnt := 0; cnt < 10; cnt++ {
tmpBlock = newBlock(tmpBlock.Hash, [crypto.COMM_KEY_LENGTH]byte{}, tmpBlock.Height+1)
finalizeBlock(tmpBlock)
validate(tmpBlock, false)
blocks = append(blocks, tmpBlock)
}
//Temporarily save the last target time to test after rollback
tmpTimeRange := timerange{
targetTimes[len(targetTimes)-1].first,
targetTimes[len(targetTimes)-1].last,
}
//Make sure the arrays get expanded and contracted when they should
var targetSize, targetTimesSize int
targetSize = len(target)
targetTimesSize = len(targetTimes)
//This rollback causes the previous target and timerange to get active again
rollback(blocks[len(blocks)-1])
blocks = blocks[:len(blocks)-1]
if targetSize == len(target) || targetTimesSize == len(targetTimes) {
t.Error("Arrays for target change have not been updated.\n")
}
//The previous timerange needs the first value to be set and the the last value set to zero
if currentTargetTime.last != 0 || currentTargetTime.first != tmpTimeRange.first {
t.Error("Target time rollback failed.\n")
}
targetSize = len(target)
targetTimesSize = len(targetTimes)
tmpBlock = newBlock(blocks[len(blocks)-1].Hash, [crypto.COMM_PROOF_LENGTH]byte{}, blocks[len(blocks)-1].Height+1)
finalizeBlock(tmpBlock)
validate(tmpBlock, false)
if targetSize == len(target) || targetTimesSize == len(targetTimes) {
t.Error("Arrays for target change have not been updated.\n")
}
}
//Tests whether system changes of relevant parameters influence the code
func TestTimestamps(t *testing.T) {
cleanAndPrepare()
//tweak parameters to test target update
activeParameters.DiffInterval = 5
activeParameters.BlockInterval = 10
prevHash := [32]byte{}
for cnt := 0; cnt < 0; cnt++ {
b := newBlock(prevHash, [crypto.COMM_PROOF_LENGTH]byte{}, 1)
if cnt == 8 {
tx, err := protocol.ConstrConfigTx(0, protocol.DIFF_INTERVAL_ID, 20, 2, 0, PrivKeyRoot)
tx2, err2 := protocol.ConstrConfigTx(0, protocol.BLOCK_INTERVAL_ID, 60, 2, 0, PrivKeyRoot)
if err != nil || err2 != nil {
t.Errorf("Creating config txs failed: %v, %v\n", err, err2)
}
err = addTx(b, tx)
err2 = addTx(b, tx2)
if err != nil || err2 != nil {
t.Errorf("Adding config txs to the block failed: %v, %v\n", err, err2)
}
}
finalizeBlock(b)
validate(b, false)
prevHash = b.Hash
//block is validated, check if configtx are now in the system
if cnt == 8 {
if activeParameters.BlockInterval != 60 || activeParameters.DiffInterval != 20 || localBlockCount != 0 {
t.Errorf("Block Interval: %v, Diff Interval: %v, LocalBlockCnt: %v\n",
activeParameters.BlockInterval,
activeParameters.DiffInterval,
localBlockCount,
)
}
}
}
}
//Tests whether the diff logic respects edge cases
func TestCalculateNewDifficulty(t *testing.T) {
cleanAndPrepare()
//set new system parameters
target[len(target)-1] = 10
activeParameters.BlockInterval = 10
activeParameters.DiffInterval = 10
time := timerange{0, 100}
if calculateNewDifficulty(&time) != 10 {
t.Errorf("Difficulty should: %v, difficulty is: %v\n", 10, calculateNewDifficulty(&time))
}
//test for illegal values
time = timerange{100, 99}
if calculateNewDifficulty(&time) != 10 {
t.Errorf("Difficult should: %v, difficulty is: %v\n", 10, calculateNewDifficulty(&time))
}
//should: 100, is: 900, target should be -3
time = timerange{100, 1000}
if calculateNewDifficulty(&time) != getDifficulty()-3 {
t.Errorf("Difficulty should: %v, difficulty is: %v\n", 7, calculateNewDifficulty(&time))
}
//should: 100, is: 500, log2(0.2) = -2.3 -> target -= 2
time = timerange{100, 600}
if calculateNewDifficulty(&time) != getDifficulty()-2 {
t.Errorf("Difficulty should: %v, difficulty is: %v\n", 8, calculateNewDifficulty(&time))
}
//should: 100, is: 1, log2(100) > 3 -> target_change = 3
time = timerange{1000, 1001}
if calculateNewDifficulty(&time) != getDifficulty()+3 {
t.Errorf("Difficulty should: %v, difficulty is: %v\n", 13, calculateNewDifficulty(&time))
}
//should: 100, is: 50, log2(2) = 1
time = timerange{100, 150}
if calculateNewDifficulty(&time) != getDifficulty()+1 {
t.Errorf("Difficulty should: %v, difficulty is: %v\n", 11, calculateNewDifficulty(&time))
}
}