forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerate_genesis_block.py
373 lines (300 loc) · 9.66 KB
/
generate_genesis_block.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import csv
from pathlib import Path
import time
import hashlib
from dataclasses import dataclass
from typing import List
def sha256(b: bytes) -> bytes:
return hashlib.sha256(b).digest()
def sha256d(b: bytes) -> bytes:
return sha256(sha256(b))
def sha256t(b: bytes) -> bytes:
return sha256(sha256d(b))
def encode_int(i, nbytes, encoding='little'):
""" encode integer i into nbytes bytes using a given byte ordering """
return i.to_bytes(nbytes, encoding)
def encode_varint(i):
""" encode a (possibly but rarely large) integer into bytes with a super simple compression scheme """
if i < 0xfd:
return bytes([i])
elif i < 0x10000:
return b'\xfd' + encode_int(i, 2)
elif i < 0x100000000:
return b'\xfe' + encode_int(i, 4)
elif i < 0x10000000000000000:
return b'\xff' + encode_int(i, 8)
else:
raise ValueError("integer too large: %d" % (i,))
@dataclass
class OpCode:
data: int
def __int__(self):
return self.data
OP_PUSHDATA1 = OpCode(0x4c)
OP_PUSHDATA2 = OpCode(0x4d)
OP_PUSHDATA4 = OpCode(0x4e)
OP_1 = OpCode(0x51)
OP_CHECKSIG = OpCode(0xac)
@dataclass
class CScriptNum:
@staticmethod
def serialize(n: int) -> bytes:
if n == 0:
return bytes()
result = bytes()
neg = n < 0
absval = abs(n)
while absval:
result += bytes([absval & 0xff])
absval >>= 8
if result[-1] & 0x80:
result += bytes([0x80 if neg else 0])
elif neg:
result[-1] |= 0x80
return result
@dataclass()
class CScript:
cmds: bytes = bytes()
def __add__(self, other):
if isinstance(other, OpCode):
assert 0 <= other.data <= 0xff
self.cmds += bytes([other.data])
return self
if isinstance(other, bytes) or isinstance(other, bytearray):
if len(other) < int(OP_PUSHDATA1):
self.cmds += encode_int(len(other), 1)
elif len(other) <= 0xff:
self.cmds += bytes([int(OP_PUSHDATA1)])
self.cmds += encode_int(len(other), 1)
elif len(other) <= 0xffff:
self.cmds += bytes([int(OP_PUSHDATA2)])
self.cmds += encode_int(len(other), 2)
else:
self.cmds += bytes([int(OP_PUSHDATA4)])
self.cmds += encode_int(len(other), 4)
self.cmds += other
return self
if isinstance(other, int):
if other == -1 or (1 <= other <= 16):
self.cmds += bytes([other + (int(OP_1) - 1)])
elif other == 0:
self.cmds += bytes([0])
else:
return self + CScriptNum.serialize(other)
return self
if isinstance(other, CScript):
self.cmds += other.cmds
return self
raise Exception("Bad type: {}".format(other))
def __iadd__(self, other):
v = self + other
self.cmds = v.cmds
return self
def encode(self) -> bytes:
return self.cmds
@dataclass
class CTxIn:
scriptSig: CScript
nSequence: int = 0xffffffff
prevtx: bytes = b'\x00' * 32
previndex: int = 0xffffffff
def encode(self):
out = []
out += [self.prevtx[::-1]] # little endian vs big endian encodings... sigh
out += [encode_int(self.previndex, 4)]
e = self.scriptSig.encode()
out += [encode_varint(len(e))]
out += [e]
out += [encode_int(self.nSequence, 4)]
return b''.join(out)
@dataclass
class CTxOut:
scriptPubKey: CScript
amount: int = 0xffffffff
def encode(self):
out = []
out += [encode_int(self.amount, 8)]
e = self.scriptPubKey.encode()
out += [encode_varint(len(e))]
out += [e]
return b''.join(out)
@dataclass
class Tx:
inputs: List[CTxIn]
outputs: List[CTxOut]
locktime: int = 0
version: int = 1
def encode(self, sig_index=-1) -> bytes:
"""
Encode this transaction as bytes.
If sig_index is given then return the modified transaction
encoding of this tx with respect to the single input index.
This result then constitutes the "message" that gets signed
by the aspiring transactor of this input.
"""
out = []
# encode metadata
out += [encode_int(self.version, 4)]
# encode inputs
out += [encode_varint(len(self.inputs))]
# we are just serializing a fully formed transaction
out += [tx_in.encode() for tx_in in self.inputs]
assert sig_index == -1, sig_index
# encode outputs
out += [encode_varint(len(self.outputs))]
out += [tx_out.encode() for tx_out in self.outputs]
# encode... other metadata
out += [encode_int(self.locktime, 4)]
out += [encode_int(1, 4) if sig_index != -1 else b''] # 1 = SIGHASH_ALL
return b''.join(out)
def tx_id(self) -> bytes:
return sha256d(self.encode())
def script_with_prefix(nbits) -> CScript:
c = CScript() + nbits
if nbits <= 0xff:
return c + CScriptNum.serialize(1)
if nbits <= 0xffff:
return c + CScriptNum.serialize(2)
if nbits <= 0xffffff:
return c + CScriptNum.serialize(3)
return c + CScriptNum.serialize(4)
def make_header(
version: int,
prev_block: bytes,
merkle_root: bytes,
timestamp: int,
nbits: int,
nonce: int
) -> bytearray:
header = bytearray()
header += encode_int(version, 4, 'little')
header += prev_block
header += merkle_root
header += encode_int(timestamp, 4, 'little')
header += encode_int(nbits, 4, 'little')
header += encode_int(nonce, 4, 'little')
assert len(header) == 80, len(header)
return header
def set_header_nonce(header: bytearray, nonce: int) -> bytearray:
n = encode_int(nonce, 4, 'little')
header[-4:] = n
return header
def set_header_timestamp(header: bytearray, ts: int) -> bytearray:
n = encode_int(ts, 4, 'little')
start = -4 - 4 - 4
end = start + 4
header[start:end] = n
return header
def decode_target(bits):
# http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html
# https://gist.github.com/shirriff/cd5c66da6ba21a96bb26#file-mine-py
# https://gist.github.com/shirriff/cd5c66da6ba21a96bb26#file-mine-py
# https://en.bitcoin.it/wiki/Difficulty
exp = bits >> 24
mant = bits & 0xffffff
target_hexstr = '%064x' % (mant * (1 << (8 * (exp - 3))))
return target_hexstr
def decode_target_int(nbits):
return int(decode_target(nbits), 16)
def get_block_hash(header: bytes) -> bytes:
return sha256d(header)[::-1]
def get_block_hash_int(header: bytes) -> int:
return int(get_block_hash(header).hex(), 16)
def read_balances(file: Path):
assert file.exists()
with open(file, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for script, satoshis, _ in reader:
try:
bytes.fromhex(script)
except:
raise Exception("Looks like {} is not a script".format(script))
yield CTxOut(CScript() + script, satoshis)
def generate_genesis_block(
nTime: int,
nBits: int,
nVersion: int,
pszTimestamp: str = "VeriBlock",
txouts: List[CTxOut] = []
):
assert nTime < int(time.time()) - 10000
assert nBits <= 0x207fffff
assert len(txouts) > 0
PREV = b'\x00' * 32
# create input
scriptSig = CScript() + script_with_prefix(nBits) + pszTimestamp.encode('ascii')
assert 2 <= len(scriptSig.cmd) <= 100
# create coinbase tx
tx = Tx(
version=nVersion,
inputs=[CTxIn(scriptSig=scriptSig)],
outputs=txouts
)
merkleroot = tx.tx_id()
TIME = nTime
while True:
nonce = 0
header = make_header(
nVersion,
PREV,
merkleroot,
TIME,
nBits,
nonce
)
before = time.time()
target = decode_target_int(nBits)
while get_block_hash_int(header) >= target:
nonce += 1
if nonce > 0xffffffff:
nonce = 0
TIME += 1
print("Increasing timestamp {}".format(TIME))
set_header_timestamp(header, TIME)
set_header_nonce(header, nonce)
after = time.time()
if get_block_hash_int(header) < target:
print(f'''Found:
Nonce: {nonce}
Header: {header.hex()}
Hash: {get_block_hash(header).hex()}
MerkleRoot: {merkleroot[::-1].hex()}
Tx: {tx.encode().hex()}
Took: {after-before}
''')
return
else:
print("Can not find a solution...")
def main():
pszTimestamp = "VeriBlock Bitcoin Reference Implementation, Sept 13, 2021"
timestamp = 1631200000
out = CTxOut(
scriptPubKey=CScript() + OpCode(0x00) + bytes.fromhex("3dd5f2f667315cc98e669deb88d3dfe831aa2cea"),
amount=5 * 10**8
)
print("Regtest")
generate_genesis_block(
nTime=timestamp,
pszTimestamp=pszTimestamp,
nBits=0x207fffff,
nVersion=1,
txouts=[out]
)
print("Testnet")
generate_genesis_block(
nTime=timestamp,
pszTimestamp=pszTimestamp,
nBits=0x1d07ffff,
nVersion=1,
txouts=[out]
)
print("Mainnet")
generate_genesis_block(
nTime=timestamp,
pszTimestamp=pszTimestamp,
nBits=0x1d00ffff,
nVersion=1,
txouts=[out]
)
if __name__ == "__main__":
main()