Skip to content

Commit e65934b

Browse files
committed
minor cleanup of mining block example
1 parent 097d38a commit e65934b

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

examples/create_and_mine_block.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,30 @@ def main():
233233

234234

235235
setup("mainnet")
236-
# mock requirements
237-
from_txid = "0000000000000000000000000000000000000000000000000000000000000000"
236+
237+
# mock tx1
238+
tx1 = Transaction()
239+
tx1 = tx1.from_raw(tx_details["hex"])
240+
241+
# tx1's wtxid (different from txid)
242+
wtxid = tx1.get_wtxid();
243+
print("\nWtx id :", wtxid)
244+
245+
# address to send the block rewards
238246
to_addr = "bc1pvh7n6s375348q5zjfrde38nnq2lmhhtyaeqe8hv6t8mf398smeyqnug47s"
239247
to_addr = P2trAddress(to_addr)
240248

241-
# First create a coinbase transaction which is the first transaction of the block
249+
# create a coinbase transaction which is the first transaction of the block
250+
# it generates new coins so it does not spent an existing UTXO
251+
from_txid = "0000000000000000000000000000000000000000000000000000000000000000"
252+
242253
# The witness reserved value is a 32-byte value that is reserved for future use.
243254
# and the coinbase transaction must have it in its input's witness
244255
witness_reserved_value = (
245256
"0000000000000000000000000000000000000000000000000000000000000000"
246257
)
247258

248259
# Constructing the coinbase transaction
249-
tx1 = Transaction()
250-
tx1 = tx1.from_raw(tx_details["hex"])
251260
txinp = TxInput(
252261
txid=from_txid,
253262
txout_index=0,
@@ -258,17 +267,14 @@ def main():
258267
# learn more: https://learnmeabitcoin.com/technical/upgrades/segregated-witness/
259268
witness_stack = [witness_reserved_value]
260269

261-
# wtxid is different from txid
262-
wtxid = tx1.get_wtxid();
263-
print("Wtx id :", wtxid)
264270

265271
# Coinbase wtxid must be set to all zeros to avoid circular reference
266272
# Learn more: https://learnmeabitcoin.com/technical/transaction/wtxid/
267273
coinbase_wtxid = "0" * 64
268274

269275
witness_root_hash = calculate_witness_root_hash([coinbase_wtxid, wtxid])
270276
witness_root_hash = witness_root_hash.hex()
271-
print("Witness root hash ", witness_root_hash)
277+
print("\nWitness root hash ", witness_root_hash)
272278

273279
# Calculating the witness commitment hash (double SHA256 of witness_root_hash and witness_reserved_value)
274280
witness_commitment_hash = calculate_witness_commitment(
@@ -293,45 +299,50 @@ def main():
293299
# Learn more: https://learnmeabitcoin.com/technical/mining/block-reward/
294300
txout1 = TxOutput(to_satoshis(3.125+0.01), to_addr.to_script_pub_key())
295301

296-
# The commitment script is described above
297-
# and is added to the second output of the coinbase transaction
298-
witness_commitment_script = Script(["OP_RETURN", "aa21a9ed"+witness_commitment_hash]);
299-
print("witness commitment script : ", witness_commitment_script)
302+
# The commitment script is an OP_RETURN which includes:
303+
# a commitment header: to differentiate that OP_RETURN from others, and
304+
# the commitment hash: Double-SHA256(witness root hash|witness reserved value)
305+
witness_commitment_script = Script(["OP_RETURN", "aa21a9ed" + witness_commitment_hash]);
306+
print("\nWitness commitment script : ", witness_commitment_script)
307+
300308
txout2 = TxOutput(to_satoshis(0), witness_commitment_script)
301309
coinbase_tx = Transaction(
302310
[txinp],
303311
[txout1, txout2],
304312
has_segwit=True,
305313
witnesses=[TxWitnessInput(witness_stack)],
306314
)
315+
307316
# Example difficulty target
308317
# learn more: https://learnmeabitcoin.com/technical/mining/target/
309318
# Note, this is even higher then the genesis block example
310319
# to prevent the mining process from running for a long time
311320
difficulty_target = (
312321
"0000ffff00000000000000000000000000000000000000000000000000000000"
313322
)
323+
314324
# Creating a block that includes the coinbase transaction and tx1.
315325
block = create_block(coinbase_tx, tx1)
316326

317-
print("block header: ", block.header)
327+
print("\nBlock header: ", block.header)
328+
318329
# getting the block header bytes
319330
# Note: only the header is hashed and compared to the difficulty in mining
320331
serialized_header = block.header.serialize_header()
321-
print("Serialized header : ", serialized_header)
332+
print("\nSerialized header : ", serialized_header)
322333

323334
# mining the block using mine_block returns the nonce
324335
# nonce is the value in the header that can be changed iteratively
325336
# to get a hash that is less than the target
326337
nonce, mined_hash = mine_block(serialized_header, difficulty_target)
327-
print("NONCE: ", nonce)
338+
print("\nNONCE: ", nonce)
328339

329340
# Replace the last 4 bytes of the header with the current nonce in little-endian format.
330341
serialized_header = serialized_header[:-4] + nonce.to_bytes(4, byteorder="little")
331342

332-
print("hex of serialized header ", serialized_header.hex())
333-
print("coinbase transaction :", coinbase_tx.to_hex())
334-
print("Block: ", block)
343+
print("\nHex of serialized header ", serialized_header.hex())
344+
print("\nCoinbase transaction :", coinbase_tx.to_hex())
345+
print("\nBlock: ", block)
335346

336347

337348
if __name__ == "__main__":

0 commit comments

Comments
 (0)