This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_reader.py
More file actions
158 lines (135 loc) · 5.96 KB
/
test_reader.py
File metadata and controls
158 lines (135 loc) · 5.96 KB
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
150
151
152
153
154
155
156
157
158
import json
from statistics_reader.block import Block
from statistics_reader.blockchain_reader import BlockchainReader
from .mock_adapter import MockAdapter
def setup_with(blocks):
adapter = MockAdapter(hashrates=[3] * 20, is_minings=[1] * 20, new_blocks=blocks)
return adapter, BlockchainReader('some_name', 'some_chain', adapter)
def test_avg_time_initial():
blocks = [[Block(3, ['a', 'a', 'a'], 1101, 3), Block(3, ['a', 'a', 'a'], 1101, 3),
Block(3, ['a', 'a', 'a'], 1101, 3)], []]
adapter, reader = setup_with(blocks)
reader._store_averages()
assert 0 == reader.data.time
adapter.next_epoch()
reader._store_averages()
assert 0 == reader.data.time
def test_avg_time_1():
blocks = [[Block(3, ['a', 'a', 'a'], 5, 3)],
[Block(3, ['a', 'a', 'a'], 10, 3), Block(3, ['a', 'a', 'a'], 20, 3),
Block(3, ['a', 'a', 'a'], 33, 3)],
[]]
adapter, reader = setup_with(blocks)
reader._store_averages()
assert 0 == reader.data.time
adapter.next_epoch()
reader._store_averages()
assert (33 - 5) / 3 == reader.data.time
adapter.next_epoch()
reader._store_averages()
assert (33 - 5) / 3 == reader.data.time
def test_avg_time_with_genesis():
blocks = [[Block(3, ['a', 'a', 'a'], 0, 3),
Block(3, ['a', 'a', 'a'], 5, 3)],
[Block(3, ['a', 'a', 'a'], 10, 3), Block(3, ['a', 'a', 'a'], 20, 3),
Block(3, ['a', 'a', 'a'], 33, 3)],
[]]
adapter, reader = setup_with(blocks)
reader._store_averages()
assert 0 == reader.data.time
adapter.next_epoch()
reader._store_averages()
assert (33 - 5) / 3 == reader.data.time
adapter.next_epoch()
reader._store_averages()
def test_avg_time_with_genesis_long_wait():
blocks = [[], [Block(3, ['a', 'a', 'a'], 0, 3)], [], [], [Block(3, ['a', 'a', 'a'], 5, 3)],
[Block(3, ['a', 'a', 'a'], 10, 3), Block(3, ['a', 'a', 'a'], 20, 3),
Block(3, ['a', 'a', 'a'], 33, 3)],
[]]
adapter, reader = setup_with(blocks)
wanted_results = [0, 0, 0, 0, 0, (33-5)/3, (33-5)/3]
for result in wanted_results:
reader._store_averages()
assert result == reader.data.time
adapter.next_epoch()
def test_avg_time_multiple_empty_epochs():
blocks = [[Block(3, ['a', 'a', 'a'], 5, 3)],
[Block(3, ['a', 'a', 'a'], 10, 3), Block(3, ['a', 'a', 'a'], 20, 3),
Block(3, ['a', 'a', 'a'], 33, 3)],
[], [], [],
[Block(3, ['a', 'a', 'a'], 66, 3)], []]
adapter, reader = setup_with(blocks)
wanted_results = [0, (33 - 5) / 3, (33 - 5) / 3, (33 - 5) / 3, (33 - 5) / 3,
(66 - 33) / 1, (66 - 33) / 1]
for result in wanted_results:
reader._store_averages()
assert result == reader.data.time
adapter.next_epoch()
def test_avg_difficulty_multiple_empty_epochs():
blocks = [[Block(3.6, ['a', 'a', 'a'], 5, 3)],
[Block(3.9, ['a', 'a', 'a'], 10, 3), Block(3, ['a', 'a', 'a'], 20, 3),
Block(6, ['a', 'a', 'a'], 33, 3)],
[], [], [],
[Block(1, ['a', 'a', 'a'], 66, 3)], []]
adapter, reader = setup_with(blocks)
wanted_results = [3.6, (3.9 + 3 + 6) / 3, (3.9 + 3 + 6) / 3, (3.9 + 3 + 6) / 3,
(3.9 + 3 + 6) / 3, 1]
for result in wanted_results:
reader._store_averages()
assert result == reader.data.difficulty
adapter.next_epoch()
def test_avg_difficulty_initial_empty_epochs():
blocks = [[], [Block(3.6, ['a', 'a', 'a'], 5, 3)],
[Block(3.9, ['a', 'a', 'a'], 10, 3), Block(3, ['a', 'a', 'a'], 20, 3),
Block(6, ['a', 'a', 'a'], 33, 3)],
[], [], [],
[Block(1, ['a', 'a', 'a'], 66, 3)], []]
adapter, reader = setup_with(blocks)
wanted_results = [0, 3.6, (3.9 + 3 + 6) / 3, (3.9 + 3 + 6) / 3, (3.9 + 3 + 6) / 3,
(3.9 + 3 + 6) / 3, 1]
for result in wanted_results:
reader._store_averages()
assert result == reader.data.difficulty
adapter.next_epoch()
def test_avg_size_initial_empty_epochs():
blocks = [[], [Block(3.6, ['a', 'a', 'a'], 5, 3)],
[Block(3.9, ['a', 'a', 'a'], 10, 10), Block(3, ['a', 'a', 'a'], 20, 11),
Block(6, ['a', 'a', 'a'], 33, 20)],
[], [], [],
[Block(1, ['a', 'a', 'a'], 66, 4)], []]
adapter, reader = setup_with(blocks)
wanted_results = [0, 3, (10 + 11 + 20) / 3, (10 + 11 + 20) / 3, (10 + 11 + 20) / 3,
(10 + 11 + 20) / 3, 4, 4]
for result in wanted_results:
reader._store_averages()
assert result == reader.data.block_size
adapter.next_epoch()
def test_avg_transactions_initial_empty_epochs():
blocks = [[], [Block(3.6, ['a', 'a', 'a'], 5, 3)],
[Block(3.9, ['a', 'a'], 10, 10), Block(3, ['a', 'a', 'a', 'a'], 20, 11),
Block(6, ['a', 'a', 'a'], 33, 20)], [Block(1, ['a', 'a', 'a'], 66, 4)],
[], [], [],
[Block(1, [], 66, 4)], []]
adapter, reader = setup_with(blocks)
wanted_results = [0, 3, ((2 + 4 + 3) / 3), 3, 3, 3, 3, 0, 0]
for result in wanted_results:
reader._store_averages()
assert result == reader.data.transactions
adapter.next_epoch()
def test_json():
blocks = [[], [Block(3.6, ['a', 'a', 'a'], 5, 3)],
[Block(3.9, ['a', 'a'], 10, 10), Block(3, ['a', 'a', 'a', 'a'], 20, 11),
Block(6, ['a', 'a', 'a'], 33, 20)], [Block(1, ['a', 'a', 'a'], 66, 4)],
[], [], [],
[Block(1, [], 66, 4)], []]
adapter, reader = setup_with(blocks)
data = reader.read_dict_data()
assert type(data) == dict
assert type(json.dumps(data)) == str
assert type(data['hashrate']) == int
adapter.next_epoch()
data = reader.read_dict_data()
assert type(data) == dict
assert type(json.dumps(data)) == str
assert type(data['chainName']) == str