Skip to content

Commit

Permalink
fix: include merkle_root in natural order
Browse files Browse the repository at this point in the history
  • Loading branch information
15IITian committed Apr 27, 2024
1 parent 9794b9b commit b0113d3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
37 changes: 31 additions & 6 deletions Block.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from unittest import TestCase
import Transacttions
from Helper import *
import time



GENESIS_BLOCK = bytes.fromhex('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c')
TESTNET_GENESIS_BLOCK = bytes.fromhex('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff001d1aa4ae18')
Expand Down Expand Up @@ -32,8 +35,8 @@ def serialize_blockheader(self):
result = int_to_little_endian(self.version, 4)
# prev_block - 32 bytes, little endian
result += self.prev_block[::-1]
# merkle_root - 32 bytes, little endian
result += self.merkle_root[::-1]
# merkle_root - 32 bytes, little endian -> require it in natural order in grader
result += self.merkle_root
# timestamp - 4 bytes, little endian
result += int_to_little_endian(self.timestamp, 4)
# bits - 4 bytes
Expand Down Expand Up @@ -88,9 +91,8 @@ def get_mined_tx_data(cls, tx_files):
@classmethod
def create_block(cls, tx_files):
# we assume that the tx_files are in sorted in fee_rate

version = 803823616
timestamp = 1714064564
timestamp = int(time.time())

prev_block= "000000000000000000030ed9d2b8643c4c1d8519f34fbecaf150136e20ee183f"
prev_block_in_bytes= bytes.fromhex(prev_block)
Expand Down Expand Up @@ -151,10 +153,32 @@ def create_block(cls, tx_files):
# now print the required block details in the output.txt

# print the serliased block header
print(valid_block.serialize_blockheader())
serliased= valid_block.serialize_blockheader()
print(serliased)







# print("merkle root : \n {}".format(valid_block.merkle_root.hex()))
# print(serliased)
# serliased_in_bytes= bytes.fromhex(serliased)


# desired_slice = serliased_in_bytes[36:68].hex()
# print(desired_slice)
# print(desired_slice == valid_block.merkle_root.hex())
# print("{} {} {} {} {} {} ".format(valid_block.version,(valid_block.prev_block).hex(),(valid_block.merkle_root).hex(),valid_block.timestamp,(valid_block.bits).hex(),valid_block.nonce))

# print("block hash: {}".format(valid_block.hash()))






# serliased coinbase transaction
print(serliased_coinbase_tx)

Expand Down Expand Up @@ -205,4 +229,5 @@ def get_valid_nonce(self,target):
# block_header= "0060e92f3f18ee206e1350f1cabe4ff319851d4c3c64b8d2d90e030000000000000000005f73d58ab5be2407065a26a290750250c1ebebc46a280e6f2b1c5032b3397c2ab48c2a66ffff001f00000000"
# target ="0000ffff00000000000000000000000000000000000000000000000000000000"

# print(Block.check_pow(block_header,target))
# print(Block.check_pow(block_header,target))

14 changes: 9 additions & 5 deletions Transacttions.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def get_serliased_coinbase_tx(cls,fees_collected,tx_files):
witness_commitment = Tx.get_witness_commitment(tx_files)
amount_2 = 0
script_pubkey_2 = "6a24aa21a9ed" + witness_commitment


witness_reserved_value = "0000000000000000000000000000000000000000000000000000000000000000"

Expand Down Expand Up @@ -494,7 +494,13 @@ def get_serliased_coinbase_tx(cls,fees_collected,tx_files):

@classmethod
def get_witness_commitment(cls, tx_files):
wit_hashes = []

# Note -> for calculating witness_root -> we also require witness_txid of coinbase tx -> which is set to all zeroes
# As witness_commitment will be inside the tx -> so this value is used in order to prvent circular reference

# wit_hashes will contain hashes in bytes
coinbase_wtxid= "0000000000000000000000000000000000000000000000000000000000000000"
wit_hashes = [bytes.fromhex(coinbase_wtxid)]

for tx in tx_files:
wit_hashes.append(hash256(Tx.serialize(tx)))
Expand Down Expand Up @@ -637,6 +643,4 @@ def categorize_sequence(sequence):


return result




Binary file modified __pycache__/Block.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/Transacttions.cpython-311.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions output.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
0060e92f3f18ee206e1350f1cabe4ff319851d4c3c64b8d2d90e03000000000000000000d40eb0df1c5945f0059bfb300ab3d2f2064ad229f15aaac08d9f7c6896d0a395b48c2a66ffff001f627f0100
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff11030cd3bf0c004f4345414e2e58595a002effffffff02e412bf13000000001976a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac0000000000000000266a24aa21a9ed07528e9068d79f4fa737ce2b8cdea09f1b9c3c7a588948ff29e9d29d9a9cb2ef0120000000000000000000000000000000000000000000000000000000000000000000000000
0060e92f3f18ee206e1350f1cabe4ff319851d4c3c64b8d2d90e0300000000000000000095a3d096687c9f8dc0aa5af129d24a06f2d2b30a30fb9b05f045591cdfb00ed42e0f2d66ffff001f00e40100
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff11030cd3bf0c004f4345414e2e58595a002effffffff02e412bf13000000001976a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac0000000000000000266a24aa21a9ed68d3d720d695be2c9d1e2f33c3d9d1042bdec1ce26b563afdc471db6a02a12520120000000000000000000000000000000000000000000000000000000000000000000000000
dcd522b3588c7adb0418454539e1a929fff936f211e5a20383fdcbc3ad8751b9
423c61f1ec58b9400a373e1052c26debdd5d55eb0057900c3dcf41f54762486c
141655b0e4239480afbdc12874e642ffeb94da72536ee4b8bace750820b26a06
Expand Down
20 changes: 20 additions & 0 deletions trial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const WITNESS_RESERVED_VALUE = Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000000',
'hex',
)

// print(WITNESS_RESERVED_VALUE)
console.log(typeof WITNESS_RESERVED_VALUE)
console.log(WITNESS_RESERVED_VALUE.toString('hex'));


const str = WITNESS_RESERVED_VALUE.toString('hex');
let count = 0;

for (let i = 0; i < str.length; i++) {
if (str[i] === '0') {
count++;
}
}

console.log("Number of zeros:", count);

0 comments on commit b0113d3

Please sign in to comment.