-
Notifications
You must be signed in to change notification settings - Fork 661
/
answers.py
300 lines (226 loc) · 5.99 KB
/
answers.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
'''
# tag::exercise12[]
==== Exercise 12
Calculate the new bits given the first and last blocks of this 2,016-block difficulty adjustment period:
* Block 471744:
+
```
000000203471101bbda3fe307664b3283a9ef0e97d9a38a7eacd88000000000000000000
10c8aba8479bbaa5e0848152fd3c2289ca50e1c3e58c9a4faaafbdf5803c5448ddb84559
7e8b0118e43a81d3
```
* Block 473759:
+
```
02000020f1472d9db4b563c35f97c428ac903f23b7fc055d1cfc26000000000000000000
b3f449fcbe1bc4cfbcb8283a0d2c037f961a3fdf2b8bedc144973735eea707e126425859
7e8b0118e5f00474
```
# end::exercise12[]
# tag::answer12[]
>>> from io import BytesIO
>>> from block import Block
>>> from helper import TWO_WEEKS
>>> from helper import target_to_bits
>>> block1_hex = '000000203471101bbda3fe307664b3283a9ef0e97d9a38a7eacd88000000\
00000000000010c8aba8479bbaa5e0848152fd3c2289ca50e1c3e58c9a4faaafbdf5803c5448dd\
b845597e8b0118e43a81d3'
>>> block2_hex = '02000020f1472d9db4b563c35f97c428ac903f23b7fc055d1cfc26000000\
000000000000b3f449fcbe1bc4cfbcb8283a0d2c037f961a3fdf2b8bedc144973735eea707e126\
4258597e8b0118e5f00474'
>>> last_block = Block.parse(BytesIO(bytes.fromhex(block1_hex)))
>>> first_block = Block.parse(BytesIO(bytes.fromhex(block2_hex)))
>>> time_differential = last_block.timestamp - first_block.timestamp
>>> if time_differential > TWO_WEEKS * 4:
... time_differential = TWO_WEEKS * 4
>>> if time_differential < TWO_WEEKS // 4:
... time_differential = TWO_WEEKS // 4
>>> new_target = last_block.target() * time_differential // TWO_WEEKS
>>> new_bits = target_to_bits(new_target)
>>> print(new_bits.hex())
80df6217
# end::answer12[]
'''
from unittest import TestCase
import helper
from block import Block
from helper import (
hash256,
int_to_little_endian,
little_endian_to_int,
target_to_bits,
TWO_WEEKS,
)
from tx import Tx
'''
# tag::exercise1[]
==== Exercise 1
Write the `is_coinbase` method of the `Tx` class.
# end::exercise1[]
'''
# tag::answer1[]
def is_coinbase(self):
if len(self.tx_ins) != 1:
return False
first_input = self.tx_ins[0]
if first_input.prev_tx != b'\x00' * 32:
return False
if first_input.prev_index != 0xffffffff:
return False
return True
# end::answer1[]
'''
# tag::exercise2[]
==== Exercise 2
Write the `coinbase_height` method for the `Tx` class.
# end::exercise2[]
'''
# tag::answer2[]
def coinbase_height(self):
if not self.is_coinbase():
return None
element = self.tx_ins[0].script_sig.cmds[0]
return little_endian_to_int(element)
# end::answer2[]
'''
# tag::exercise3[]
==== Exercise 3
Write the `parse` method for `Block`.
# end::exercise3[]
'''
# tag::answer3[]
@classmethod
def parse(cls, s):
version = little_endian_to_int(s.read(4))
prev_block = s.read(32)[::-1]
merkle_root = s.read(32)[::-1]
timestamp = little_endian_to_int(s.read(4))
bits = s.read(4)
nonce = s.read(4)
return cls(version, prev_block, merkle_root, timestamp, bits, nonce)
# end::answer3[]
'''
# tag::exercise4[]
==== Exercise 4
Write the `serialize` method for `Block`.
# end::exercise4[]
'''
# tag::answer4[]
def serialize(self):
result = int_to_little_endian(self.version, 4)
result += self.prev_block[::-1]
result += self.merkle_root[::-1]
result += int_to_little_endian(self.timestamp, 4)
result += self.bits
result += self.nonce
return result
# end::answer4[]
'''
# tag::exercise5[]
==== Exercise 5
Write the `hash` method for `Block`.
# end::exercise5[]
'''
# tag::answer5[]
def hash(self):
s = self.serialize()
sha = hash256(s)
return sha[::-1]
# end::answer5[]
'''
# tag::exercise6[]
==== Exercise 6
Write the `bip9` method for the `Block` class.
# end::exercise6[]
'''
# tag::answer6[]
def bip9(self):
return self.version >> 29 == 0b001
# end::answer6[]
'''
# tag::exercise7[]
==== Exercise 7
Write the `bip91` method for the `Block` class.
# end::exercise7[]
'''
# tag::answer7[]
def bip91(self):
return self.version >> 4 & 1 == 1
# end::answer7[]
'''
# tag::exercise8[]
==== Exercise 8
Write the `bip141` method for the `Block` class.
# end::exercise8[]
'''
# tag::answer8[]
def bip141(self):
return self.version >> 1 & 1 == 1
# end::answer8[]
'''
# tag::exercise9[]
==== Exercise 9
Write the `bits_to_target` function in _helper.py_.
# end::exercise9[]
'''
# tag::answer9[]
def bits_to_target(bits):
exponent = bits[-1]
coefficient = little_endian_to_int(bits[:-1])
return coefficient * 256**(exponent - 3)
# end::answer9[]
def target(self):
return bits_to_target(self.bits)
'''
# tag::exercise10[]
==== Exercise 10
Write the `difficulty` method for `Block`.
# end::exercise10[]
'''
# tag::answer10[]
def difficulty(self):
lowest = 0xffff * 256**(0x1d - 3)
return lowest / self.target()
# end::answer10[]
'''
# tag::exercise11[]
==== Exercise 11
Write the `check_pow` method for `Block`.
# end::exercise11[]
'''
# tag::answer11[]
def check_pow(self):
sha = hash256(self.serialize())
proof = little_endian_to_int(sha)
return proof < self.target()
# end::answer11[]
'''
# tag::exercise13[]
==== Exercise 13
Write the `calculate_new_bits` function in _helper.py_.
# end::exercise13[]
'''
# tag::answer13[]
def calculate_new_bits(previous_bits, time_differential):
if time_differential > TWO_WEEKS * 4:
time_differential = TWO_WEEKS * 4
if time_differential < TWO_WEEKS // 4:
time_differential = TWO_WEEKS // 4
new_target = bits_to_target(previous_bits) * time_differential // TWO_WEEKS
return target_to_bits(new_target)
# end::answer13[]
class ChapterTest(TestCase):
def test_apply(self):
Tx.is_coinbase = is_coinbase
Tx.coinbase_height = coinbase_height
Block.parse = parse
Block.serialize = serialize
Block.hash = hash
Block.bip9 = bip9
Block.bip91 = bip91
Block.bip141 = bip141
Block.difficulty = difficulty
Block.check_pow = check_pow
Block.target = target
helper.calculate_new_bits = calculate_new_bits
helper.bits_to_target = bits_to_target