diff --git a/Block.py b/Block.py index ddcdd57b..bf1bedd0 100644 --- a/Block.py +++ b/Block.py @@ -82,7 +82,11 @@ def get_mined_tx_data(cls, tx_files): # sort the tx_included list tx_included = sorted(tx_included, key=lambda x: x['fee_rate'],reverse=True) - + print("tx_count: {}".format(tx_count)) + print("fee_collected: {}".format(fee_collected)) + print("weight_stored: {}".format(weight_stored)) + + return (tx_count,tx_included,fee_collected,weight_stored) @@ -171,38 +175,13 @@ def create_block(cls, tx_files): 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) # print all txids for tx_id in tx_ids_list: - print(tx_id) - - + print(tx_id) - @@ -217,9 +196,6 @@ def check_pow(cls,serliased_block_header,target,nonce): proof = little_endian_to_int(h256) # return whether this integer is less than the target target= int.from_bytes(bytes.fromhex(target),"big") - - - # print("{} : {} \n".format(nonce,proof.to_bytes(32,"big").hex()) ) return proof < target def get_valid_nonce(self,target): @@ -233,15 +209,6 @@ def get_valid_nonce(self,target): else: print("shit-man") break - # if flag: - # print("required nonce: {}".format(self.nonce)) - return self - -# block_header= "0060e92f3f18ee206e1350f1cabe4ff319851d4c3c64b8d2d90e030000000000000000005f73d58ab5be2407065a26a290750250c1ebebc46a280e6f2b1c5032b3397c2ab48c2a66ffff001f00000000" -# target ="0000ffff00000000000000000000000000000000000000000000000000000000" - -# print(Block.check_pow(block_header,target)) - diff --git a/Helper.py b/Helper.py index 61fa9c13..296ee3ed 100755 --- a/Helper.py +++ b/Helper.py @@ -168,19 +168,11 @@ def merkle_root(hashes): def HASH160(s): - #sha256 followed by ripemd160 - # return hashlib.new('ripemd160', hashlib.sha256(s).digest()).digest() return ripemd160(s) -a= 5 -# print(HASH160(a.to_bytes(4,"big"))) + -# print(ripemd160(a.to_bytes(4,"big")).hex()) -# h = RIPEMD160.new() -# h.update(b'Hello') -# print h.hexdigest() -# print(HASH160(a.to_bytes(4,"big"))) \ No newline at end of file diff --git a/Script.py b/Script.py index 9d117d85..5d48bdb4 100644 --- a/Script.py +++ b/Script.py @@ -12,46 +12,7 @@ from Helper import * -script_types= { - - "p2pkh":{ - "scriptpubkey":["OP_DUP","OP_HASH160","OP_PUSHBYTES_20","<20_BYTE_HASH>","OP_EQUALVERIFY","OP_CHECKSIG"], - "script_sig":["",""] - }, - "p2sh":{ - "scriptpubkey":["OP_HASH160", "OP_PUSHBYTES_20", "<20_BYTE_HASH>", "OP_EQUAL"], - "script_sig":"" ## can be anything depending on redeem script but will contain redeem script - - }, - "bare-multisig":{ - "scriptpubkey":["OP_PUSHNUM_M","N(PushBytes + PubKeys)", "OP_PUSHNUM_N", "OP_CHECKMULTISIG"], - "script_sig":[] - }, - - "v0_p2wpkh":{ - "scriptpubkey":["OP_0", "OP_PUSHBYTES_20","<20_BYTE_HASH>"], - "script_sig":"" - }, - "v0_p2wsh":{ - "scriptpubkey":["OP_0" "OP_PUSHBYTES_32" "<32_Byte_Hash>"], - "script_sig":"" - }, - "v1_p2tr":{ - "scriptpubkey":["OP_PUSHNUM_1", "OP_PUSHBYTES_32", "<32_BYTE_HASH>"], - "script_sig":"" - }, - "v0_p2sh_p2wpkh":{ - "scriptpubkey":["OP_HASH160", "OP_PUSHBYTES_20", "<20_BYTE_HASH>", "OP_EQUAL"], - "script_sig":["OP_PUSHBYTES_22","<20 BYTE REDEEM_SCRIPT HASH>"], - "witness":[] - }, - "v0_p2sh_p2wsh":{ - "scriptpubkey":["OP_HASH160", "OP_PUSHBYTES_20", "<20_BYTE_HASH>", "OP_EQUAL"], - "script_sig":["OP_PUSHBYTES_34","<20 BYTE REDEEM_SCRIPT HASH>"], - "witness":[] - } - -} + class Script: def __init__(self,cmds=None): if cmds is None: diff --git a/Test.ipynb b/Test.ipynb deleted file mode 100644 index 5db1b87e..00000000 --- a/Test.ipynb +++ /dev/null @@ -1,195 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'Helper'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[1], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mWork\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mHelper\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mWork\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mTransacttions\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[1;32m 3\u001b[0m all_files\u001b[38;5;241m=\u001b[39m list_all_tx()\n", - "File \u001b[0;32m~/code-challenge-2024-15IITian/Work/Transacttions.py:4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# import Script\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;66;03m# from Script import *\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mWork\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mHelper\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mWork\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mScript\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m \n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# constants related to Sequence values\u001b[39;00m\n\u001b[1;32m 9\u001b[0m SEQUENCE_MAX\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m4294967295\u001b[39m\n", - "File \u001b[0;32m~/code-challenge-2024-15IITian/Work/Script.py:11\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m module_path \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m sys\u001b[38;5;241m.\u001b[39mpath:\n\u001b[1;32m 8\u001b[0m sys\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mappend(module_path)\n\u001b[0;32m---> 11\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mWork\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mOpcodes\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mWork\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mHelper\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;66;03m# \"OP_DUP OP_HASH160 OP_PUSHBYTES_20 6085312a9c500ff9cc35b571b0a1e5efb7fb9f16 OP_EQUALVERIFY OP_CHECKSIG\",\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;66;03m# \"scriptpubkey\":[\"OP_HASH160\", \"OP_PUSHBYTES_20\",\"<20_BYTE_HASH>\", \"OP_EQUAL\"],\u001b[39;00m\n\u001b[1;32m 16\u001b[0m \u001b[38;5;66;03m# \"script_sig\":[]\u001b[39;00m\n", - "File \u001b[0;32m~/code-challenge-2024-15IITian/Work/Opcodes.py:7\u001b[0m\n\u001b[1;32m 4\u001b[0m sys\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mappend(module_path)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;66;03m# from Work.Helper import *\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mHelper\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mlogging\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m getLogger\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msecp256k1\u001b[39;00m\n", - "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'Helper'" - ] - } - ], - "source": [ - "from Work.Helper import *\n", - "from Work.Transacttions import *\n", - "all_files= list_all_tx()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'Tx' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m new_files\u001b[38;5;241m=\u001b[39m\u001b[43mTx\u001b[49m\u001b[38;5;241m.\u001b[39mmodified_tx_files(all_files\u001b[38;5;241m=\u001b[39mall_files)\n", - "\u001b[0;31mNameError\u001b[0m: name 'Tx' is not defined" - ] - } - ], - "source": [ - "new_files=Tx.modified_tx_files(all_files=all_files)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "144\n" - ] - } - ], - "source": [ - "a= \"3045022100c233c3a8a510e03ad18b0a24694ef00c78101bfd5ac075b8c1037952ce26e91e02205aa5f8f88f29bb4ad5808ebc12abfd26bd791256f367b04c6d955f01f28a772401\"\n", - "print(len(a))" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "prev_block = \"000000000000000000030ed9d2b8643c4c1d8519f34fbecaf150136e20ee183f\"\n", - "target = \"0000ffff00000000000000000000000000000000000000000000000000000000\"\n", - "\n", - "int.from_bytes(bytes.fromhex(prev_block) ,\"big\") < int.from_bytes(bytes.fromhex(target))" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3999680\n" - ] - } - ], - "source": [ - "print(4000000 - 320)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "803823616\n" - ] - } - ], - "source": [ - "a= \"2fe96000\"\n", - "print(int.from_bytes(bytes.fromhex(a),\"big\"))" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "29284b2c\n", - "2c4b2829\n" - ] - } - ], - "source": [ - "a= 690506540\n", - "print(a.to_bytes(4,\"big\").hex())\n", - "print(a.to_bytes(4,\"little\").hex())" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4294967295" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "2 **32 -1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.6" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/__pycache__/Block.cpython-311.pyc b/__pycache__/Block.cpython-311.pyc index a04350f6..6a0a64c5 100644 Binary files a/__pycache__/Block.cpython-311.pyc and b/__pycache__/Block.cpython-311.pyc differ diff --git a/main.py b/main.py index 86084ca9..2effd493 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,6 @@ from Transacttions import * from Block import * all_files = list_all_tx() -# print(len(all_files)) # modify the tx files which contains some additional fields which are important new_files= Tx.modified_tx_files(all_files) @@ -28,33 +27,8 @@ # now segregate the transactions which can be included in our block i.e do not have any locktime constraints (valid_tx_unlocked,_,zeroes)= Tx.get_unlocked_tx(valid_tx_list) -# print("valid_tx: {}".format(valid_tx)) -# print("valid_tx_unlocked: {}".format(len(valid_tx_unlocked))) -# print("zeroes: {}".format(zeroes)) - - - - - - + (tx_count,tx_included,fee_collected,weight_stored) =Block.get_mined_tx_data(valid_tx_unlocked) -# print("tx_count:{}".format(tx_count)) -# print("tx_included: {}".format(tx_included)) -# print("fee_collected:{}".format(fee_collected)) -# print("weight_stored:{}".format(weight_stored)) -# for tx in new_files: -# print(tx["fee_rate"]) - -# print(new_files[0]) - - # now create a block Block.create_block(tx_included) - -# tx_hashes_list= [] - -# for tx in valid_tx_list: -# tx_hash = Transacttions.Tx.get_tx_hash(tx) -# tx_hashes_list.append(tx_hash) -# print(merkle_root(tx_hashes_list)) \ No newline at end of file diff --git a/output.txt b/output.txt index 39ec36ca..c802ac98 100644 --- a/output.txt +++ b/output.txt @@ -1,4 +1,10 @@ -0060e92f3f18ee206e1350f1cabe4ff319851d4c3c64b8d2d90e03000000000000000000664d84331a42280c640a257b7e2223c2774445a448668a242a075add51187f5719232d66ffff001f87110000 +tx_count: 4606 +fee_collected: 18789316 +weight_stored: 3999485 +tx_count: 4606 +fee_collected: 18789316 +weight_stored: 3999485 +0060e92f3f18ee206e1350f1cabe4ff319851d4c3c64b8d2d90e03000000000000000000664d84331a42280c640a257b7e2223c2774445a448668a242a075add51187f5768242d66ffff001fd7bc0100 010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff11030cd3bf0c004f4345414e2e58595a002effffffff02e412bf13000000001976a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac0000000000000000266a24aa21a9ed68d3d720d695be2c9d1e2f33c3d9d1042bdec1ce26b563afdc471db6a02a12520120000000000000000000000000000000000000000000000000000000000000000000000000 96cb0dce25d42069df3ad24623a1d0c303e32b30412821f25cb7f6619d2e66c5 dcd522b3588c7adb0418454539e1a929fff936f211e5a20383fdcbc3ad8751b9