Skip to content

Commit

Permalink
fix: make some additional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
15IITian committed Apr 30, 2024
1 parent 4d2a943 commit 39046a4
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 75 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"locktime",
"prevout",
"RIPEMD",
"secp",
Expand Down
3 changes: 0 additions & 3 deletions Block.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@



GENESIS_BLOCK = bytes.fromhex('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c')
TESTNET_GENESIS_BLOCK = bytes.fromhex('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4adae5494dffff001d1aa4ae18')
LOWEST_BITS = bytes.fromhex('ffff001d')

MAX_BLOCK_SIZE= 4000000
BLOCK_HEADER_SIZE= 320
Expand Down
4 changes: 2 additions & 2 deletions Opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@



# this function will give the funciton object from the string
# this function will give the function object from the string
def get_function_by_name(func_name):
# Check if the function exists in the global namespace
if func_name in globals() and callable(globals()[func_name]):
Expand All @@ -284,7 +284,7 @@ def get_function_by_name(func_name):
return None


all_opcodes_used : { 'OP_ELSE', 'OP_ENDIF'}
# all_opcodes_used : { 'OP_ELSE', 'OP_ENDIF'}



Expand Down
3 changes: 1 addition & 2 deletions Transacttions.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ def verify(cls,file):
if not Tx.verify_input(file,i):
return False

# verify the signatures provided.
return True


Expand Down Expand Up @@ -539,7 +538,7 @@ def get_unlocked_tx(cls,tx_files):
if unlock_flag:
tx_unlocked.append(file)

return (tx_unlocked,tx_locked,zeroes)
return tx_unlocked



Expand Down
Binary file modified __pycache__/Block.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/Helper.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/Opcodes.cpython-311.pyc
Binary file not shown.
Binary file removed __pycache__/RIPEMD160.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/Transacttions.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/ecc.cpython-311.pyc
Binary file not shown.
62 changes: 0 additions & 62 deletions ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,65 +329,3 @@ def parse(cls, signature_bin):
raise SyntaxError("Signature too long")
return cls(r, s)



# class PrivateKey:

# def __init__(self, secret):
# self.secret = secret
# self.point = secret * G

# def hex(self):
# return '{:x}'.format(self.secret).zfill(64)

# def sign(self, z):
# k = self.deterministic_k(z)
# # r is the x coordinate of the resulting point k*G
# r = (k * G).x.num
# # remember 1/k = pow(k, N-2, N)
# k_inv = pow(k, N - 2, N)
# # s = (z+r*secret) / k
# s = (z + r * self.secret) * k_inv % N
# if s > N / 2:
# s = N - s
# # return an instance of Signature:
# # Signature(r, s)
# return Signature(r, s)

# def deterministic_k(self, z):
# k = b'\x00' * 32
# v = b'\x01' * 32
# if z > N:
# z -= N
# z_bytes = z.to_bytes(32, 'big')
# secret_bytes = self.secret.to_bytes(32, 'big')
# s256 = hashlib.sha256
# k = hmac.new(k, v + b'\x00' + secret_bytes + z_bytes, s256).digest()
# v = hmac.new(k, v, s256).digest()
# k = hmac.new(k, v + b'\x01' + secret_bytes + z_bytes, s256).digest()
# v = hmac.new(k, v, s256).digest()
# while True:
# v = hmac.new(k, v, s256).digest()
# candidate = int.from_bytes(v, 'big')
# if candidate >= 1 and candidate < N:
# return candidate
# k = hmac.new(k, v + b'\x00', s256).digest()
# v = hmac.new(k, v, s256).digest()

# def wif(self, compressed=True, testnet=False):
# # convert the secret from integer to a 32-bytes in big endian using num.to_bytes(32, 'big')
# secret_bytes = self.secret.to_bytes(32, 'big')
# # prepend b'\xef' on testnet, b'\x80' on mainnet
# if testnet:
# prefix = b'\xef'
# else:
# prefix = b'\x80'
# # append b'\x01' if compressed
# if compressed:
# suffix = b'\x01'
# else:
# suffix = b''
# # encode_base58_checksum the whole thing
# return encode_base58_checksum(prefix + secret_bytes + suffix)


Binary file added image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 13 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,33 @@
new_files= sorted(new_files, key=lambda x: x['fee_rate'],reverse=True)


invalid_tx = 0
valid_tx = 0

valid_tx_list=[]
invalid_tx_list = []
for file in new_files:
if Tx.verify(file):
valid_tx+=1
valid_tx_list.append(file)

else:
invalid_tx+=1
invalid_tx_list.append(file)


# 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)
valid_tx_unlocked= Tx.get_unlocked_tx(valid_tx_list)




(tx_count,tx_included,fee_collected,weight_stored) =Block.get_mined_tx_data(valid_tx_unlocked)





# now create a block




Block.create_block(tx_included)
2 changes: 1 addition & 1 deletion output.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0060e92f3f18ee206e1350f1cabe4ff319851d4c3c64b8d2d90e03000000000000000000664d84331a42280c640a257b7e2223c2774445a448668a242a075add51187f57252f2f66ffff001fc8060000
0060e92f3f18ee206e1350f1cabe4ff319851d4c3c64b8d2d90e03000000000000000000664d84331a42280c640a257b7e2223c2774445a448668a242a075add51187f57d2343066ffff001fc4780000
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff11030cd3bf0c004f4345414e2e58595a002effffffff02e412bf13000000001976a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac0000000000000000266a24aa21a9ed68d3d720d695be2c9d1e2f33c3d9d1042bdec1ce26b563afdc471db6a02a12520120000000000000000000000000000000000000000000000000000000000000000000000000
96cb0dce25d42069df3ad24623a1d0c303e32b30412821f25cb7f6619d2e66c5
dcd522b3588c7adb0418454539e1a929fff936f211e5a20383fdcbc3ad8751b9
Expand Down
189 changes: 189 additions & 0 deletions trial.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from Helper import *\n",
"from Transacttions import *\n",
"from Block import *"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'version': 2,\n",
" 'locktime': 0,\n",
" 'vin': [{'txid': '64ca1941edef34b690dd6672c7d395c60882067f7f3fc396e64d88e39c1da5b4',\n",
" 'vout': 0,\n",
" 'prevout': {'scriptpubkey': '0014d5bfb7a6d05d44c1e14443919b30d284c0c0a10a',\n",
" 'scriptpubkey_asm': 'OP_0 OP_PUSHBYTES_20 d5bfb7a6d05d44c1e14443919b30d284c0c0a10a',\n",
" 'scriptpubkey_type': 'v0_p2wpkh',\n",
" 'scriptpubkey_address': 'bc1q6klm0fkst4zvrc2ygwgekvxjsnqvpgg2jjfurm',\n",
" 'value': 10740},\n",
" 'scriptsig': '',\n",
" 'scriptsig_asm': '',\n",
" 'witness': ['3044022100884219ecbb54a6ec4d09597ca6aca49692ded3c2ffb13d1858ca5b70e59fabb4021f2de73021471a01d8f03a71a923b662f00120d181d0f7fa8e06faa1bb750e8f01',\n",
" '0271d4e7a84804c075017593271c370e8983f704f123d22aa747cd321268981cba'],\n",
" 'is_coinbase': False,\n",
" 'sequence': 4294967293}],\n",
" 'vout': [{'scriptpubkey': 'a91450feb99697a4901d3fe082eca341204fb6711b9487',\n",
" 'scriptpubkey_asm': 'OP_HASH160 OP_PUSHBYTES_20 50feb99697a4901d3fe082eca341204fb6711b94 OP_EQUAL',\n",
" 'scriptpubkey_type': 'p2sh',\n",
" 'scriptpubkey_address': '395H8VPYPtAoZWa2bx5SRyN2VojXrsb7j3',\n",
" 'value': 9520}]}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_files= list_all_tx()\n",
"all_files[0]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'version': 2,\n",
" 'locktime': 0,\n",
" 'vin': [{'txid': '64ca1941edef34b690dd6672c7d395c60882067f7f3fc396e64d88e39c1da5b4',\n",
" 'vout': 0,\n",
" 'prevout': {'scriptpubkey': '0014d5bfb7a6d05d44c1e14443919b30d284c0c0a10a',\n",
" 'scriptpubkey_asm': 'OP_0 OP_PUSHBYTES_20 d5bfb7a6d05d44c1e14443919b30d284c0c0a10a',\n",
" 'scriptpubkey_type': 'v0_p2wpkh',\n",
" 'scriptpubkey_address': 'bc1q6klm0fkst4zvrc2ygwgekvxjsnqvpgg2jjfurm',\n",
" 'value': 10740},\n",
" 'scriptsig': '',\n",
" 'scriptsig_asm': '',\n",
" 'witness': ['3044022100884219ecbb54a6ec4d09597ca6aca49692ded3c2ffb13d1858ca5b70e59fabb4021f2de73021471a01d8f03a71a923b662f00120d181d0f7fa8e06faa1bb750e8f01',\n",
" '0271d4e7a84804c075017593271c370e8983f704f123d22aa747cd321268981cba'],\n",
" 'is_coinbase': False,\n",
" 'sequence': 4294967293,\n",
" 'sequence_result': {'rbf': True, 'relative_locktime': False}}],\n",
" 'vout': [{'scriptpubkey': 'a91450feb99697a4901d3fe082eca341204fb6711b9487',\n",
" 'scriptpubkey_asm': 'OP_HASH160 OP_PUSHBYTES_20 50feb99697a4901d3fe082eca341204fb6711b94 OP_EQUAL',\n",
" 'scriptpubkey_type': 'p2sh',\n",
" 'scriptpubkey_address': '395H8VPYPtAoZWa2bx5SRyN2VojXrsb7j3',\n",
" 'value': 9520}],\n",
" 'is_segwit': True,\n",
" 'fee': 1220,\n",
" 'fee_rate': 2.766439909297052,\n",
" 'weights': 441}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_files= Tx.modified_tx_files(all_files)\n",
"new_files[0]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'version': 2,\n",
" 'locktime': 0,\n",
" 'vin': [{'txid': 'e3acb27a9fb6593822a4d8bbb8be9f9516e33123f196a41fc2801a9cae278ba4',\n",
" 'vout': 0,\n",
" 'prevout': {'scriptpubkey': '5120d7b0161160dc8ce46dfbf55c602ffeef5ed4ebcadebbe077918819b0aa1b73e6',\n",
" 'scriptpubkey_asm': 'OP_PUSHNUM_1 OP_PUSHBYTES_32 d7b0161160dc8ce46dfbf55c602ffeef5ed4ebcadebbe077918819b0aa1b73e6',\n",
" 'scriptpubkey_type': 'v1_p2tr',\n",
" 'scriptpubkey_address': 'bc1p67cpvytqmjxwgm0m74wxqtl7aa0df672m6a7qau33qvmp2smw0nqrgxc8d',\n",
" 'value': 546},\n",
" 'scriptsig': '',\n",
" 'scriptsig_asm': '',\n",
" 'witness': ['265f6c3128efad948b8e1d11e1cd165c457e9e5da32f0e4093d81221aa6ecd6ec9bebbda812f4aa8c2caabf50575d22b551cd95d27ec015031950589869b5d26'],\n",
" 'is_coinbase': False,\n",
" 'sequence': 2147483649,\n",
" 'sequence_result': {'rbf': True, 'relative_locktime': False}},\n",
" {'txid': 'ef575d36c46466f419678345bd0fc17d65cc1a5741a47ac272d9e329dab7c0c9',\n",
" 'vout': 1,\n",
" 'prevout': {'scriptpubkey': '5120d7b0161160dc8ce46dfbf55c602ffeef5ed4ebcadebbe077918819b0aa1b73e6',\n",
" 'scriptpubkey_asm': 'OP_PUSHNUM_1 OP_PUSHBYTES_32 d7b0161160dc8ce46dfbf55c602ffeef5ed4ebcadebbe077918819b0aa1b73e6',\n",
" 'scriptpubkey_type': 'v1_p2tr',\n",
" 'scriptpubkey_address': 'bc1p67cpvytqmjxwgm0m74wxqtl7aa0df672m6a7qau33qvmp2smw0nqrgxc8d',\n",
" 'value': 1811712},\n",
" 'scriptsig': '',\n",
" 'scriptsig_asm': '',\n",
" 'witness': ['8b8b8a72ac3d38c85fb59b0ba462d6a4ef39b73bd86a3ac8a300e5ab52033729ecd442e2b0eb5aba0ba9111ad0bccbdb4d24483caf482ebe6d2b819825f27731'],\n",
" 'is_coinbase': False,\n",
" 'sequence': 2147483649,\n",
" 'sequence_result': {'rbf': True, 'relative_locktime': False}}],\n",
" 'vout': [{'scriptpubkey': '5120d7b0161160dc8ce46dfbf55c602ffeef5ed4ebcadebbe077918819b0aa1b73e6',\n",
" 'scriptpubkey_asm': 'OP_PUSHNUM_1 OP_PUSHBYTES_32 d7b0161160dc8ce46dfbf55c602ffeef5ed4ebcadebbe077918819b0aa1b73e6',\n",
" 'scriptpubkey_type': 'v1_p2tr',\n",
" 'scriptpubkey_address': 'bc1p67cpvytqmjxwgm0m74wxqtl7aa0df672m6a7qau33qvmp2smw0nqrgxc8d',\n",
" 'value': 546},\n",
" {'scriptpubkey': '5120d7b0161160dc8ce46dfbf55c602ffeef5ed4ebcadebbe077918819b0aa1b73e6',\n",
" 'scriptpubkey_asm': 'OP_PUSHNUM_1 OP_PUSHBYTES_32 d7b0161160dc8ce46dfbf55c602ffeef5ed4ebcadebbe077918819b0aa1b73e6',\n",
" 'scriptpubkey_type': 'v1_p2tr',\n",
" 'scriptpubkey_address': 'bc1p67cpvytqmjxwgm0m74wxqtl7aa0df672m6a7qau33qvmp2smw0nqrgxc8d',\n",
" 'value': 1809391}],\n",
" 'is_segwit': True,\n",
" 'fee': 2321,\n",
" 'fee_rate': 2.2231800766283527,\n",
" 'weights': 1044}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_files[4]"
]
},
{
"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
}

0 comments on commit 39046a4

Please sign in to comment.