Skip to content

Commit

Permalink
Merge pull request #2 from SummerOfBitcoin/validation
Browse files Browse the repository at this point in the history
Add P2PKH with P2WPKH
  • Loading branch information
davidchocholaty authored Nov 9, 2024
2 parents 94b4ecd + e1ff1fc commit 4822ae2
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 395 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
ecdsa==0.19.0
pycryptodome==3.20.0
#sha3==0.2.1
6 changes: 5 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
#venv/bin/pip install --upgrade pip
#venv/bin/pip install -r requirements.txt
#source /venv/bin/activate
python3 src/main.py --mempool=mempool > output.txt
python3 -m venv venv
venv/bin/pip install --upgrade pip
venv/bin/pip install -r requirements.txt
source venv/bin/activate
python3 src/main.py --mempool=mempool > output.txt
71 changes: 40 additions & 31 deletions src/coinbase_transaction.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
COINBASE_TRANSACTION = {
"version": 2,
"locktime": 0xffffffff,
"vin": [
{
"txid": "0000000000000000000000000000000000000000000000000000000000000000",
"vout": 0xffffffff,
"sequence": 0xffffffff,
"is_coinbase": True,
"scriptsig": "160014fd91039e25b0827748473fce351afd8ead4ecdce",
"scriptsig_asm": "OP_PUSHBYTES_22 0014fd91039e25b0827748473fce351afd8ead4ecdce",
"witness": [
"0000000000000000000000000000000000000000000000000000000000000000",
]
}
],
"vout": [
{
"scriptpubkey": "0014ad4cc1cc859c57477bf90d0f944360d90a3998bf",
"scriptpubkey_asm": "OP_0 OP_PUSHBYTES_20 ad4cc1cc859c57477bf90d0f944360d90a3998bf",
"scriptpubkey_type": "v0_p2wpkh",
"scriptpubkey_address": "bc1q44xvrny9n3t5w7lep58egsmqmy9rnx9lt6u0tc",
"value": 100000
},
{
"scriptpubkey": "",
"scriptpubkey_type": "op_return",
"value": 0
}
]
}
COINBASE_TRANSACTION = {
"version": 1,
"locktime": 0,
"vin": [
{
"txid": "0000000000000000000000000000000000000000000000000000000000000000",
"vout": 4294967295,
"prevout": {
"scriptpubkey": "41047eda6bd04fb27cab6e7c28c99b94977f073e912f25d1ff7165d9c95cd9bbe6da7e7ad7f2acb09e0ced91705f7616af53bee51a238b7dc527f2be0aa60469d140ac",
"scriptpubkey_asm": "OP_HASH160 OP_PUSHBYTES_20 423877331b30a905240c7e1f2adee4ebaa47c5f6 OP_EQUAL",
"scriptpubkey_type": "p2sh",
"scriptpubkey_address": "14gnf7L2DjBYKFuWb6iftBoWE9hmAoFbcF",
"value": 2504928,
},
"scriptsig": "03233708184d696e656420627920416e74506f6f6c373946205b8160a4256c0000946e0100",
"scriptsig_asm": "OP_PUSHBYTES_22 001415ff0337937ecadd10ce56ffdfd4674817613223",
"witness": [
"0000000000000000000000000000000000000000000000000000000000000000",
],
"is_coinbase": True,
"sequence": 4294967295,
}
],
"vout": [
{
"scriptpubkey": "76a914edf10a7fac6b32e24daa5305c723f3de58db1bc888ac",
"scriptpubkey_asm": "OP_DUP OP_HASH160 OP_PUSHBYTES_20 71a3d2f54b0917dc9d2c877b2861ac52967dec7f OP_EQUALVERIFY OP_CHECKSIG",
"scriptpubkey_type": "p2pkh",
"scriptpubkey_address": "1BMscNZbFKdUDYi3bnF5XEmkWT3WPmRBDJ",
"value": 28278016,
},
{
"scriptpubkey": "",
"scriptpubkey_asm": "OP_HASH160 OP_PUSHBYTES_20 423877331b30a905240c7e1f2adee4ebaa47c5f6 OP_EQUAL",
"scriptpubkey_type": "p2sh",
"scriptpubkey_address": "37jAAWEdJ9D9mXybRobcveioxSkt7Lkwog",
"value": 0000000000000000,
},
],
}
28 changes: 24 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from src.mining import calculate_witness_commitment, block_mining
from src.serialize import serialize_transaction
from src.transaction import calculate_txid
from src.utils import double_spending

def parse_arguments():
parser = argparse.ArgumentParser(description='Simulation of the mining process of a block')
Expand All @@ -28,11 +29,30 @@ def parse_arguments():

mempool = MemPool(args.mempool)

# TODO pokracovani
# Check double spending
non_double_spend = []
non_double_spend = [tx for i, tx in enumerate(mempool.valid_transactions) if not double_spending(non_double_spend[:i], tx)]

block_transactions = [COINBASE_TRANSACTION] + mempool.valid_transactions

transaction_hashes = [calculate_txid(COINBASE_TRANSACTION)] + [calculate_txid(json_transaction) for json_transaction in block_transactions[1:]]
mempool.valid_transactions = non_double_spend

block_transactions = []

total_weight = 0
total_fees = 0
max_block_weight = 4000000

# Sort the transactions by the fee in descending order
transactions_sorted_by_fee = sorted(mempool.valid_transactions, key=lambda tx: tx.fee, reverse=True)

for tx in transactions_sorted_by_fee:
tx_weight = tx.calculate_weight()
if total_weight + tx_weight > max_block_weight:
break
block_transactions.append(tx)
total_weight = total_weight + tx_weight
total_fees = total_fees + tx.fee

transaction_hashes = [calculate_txid(COINBASE_TRANSACTION, True)] + [calculate_txid(transaction.json_transaction, transaction.has_witness) for transaction in block_transactions]
block_hash = block_mining(transaction_hashes).hex()

wtxids = ["0000000000000000000000000000000000000000000000000000000000000000"] + transaction_hashes[1:]
Expand Down
2 changes: 1 addition & 1 deletion src/mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def __init__(self, root_dir):
self.root_dir = root_dir
self.transaction_files = [os.path.join(self.root_dir, file) for file in os.listdir(self.root_dir) if file.endswith('.json')]
self.transactions = [Transaction(file) for file in self.transaction_files]
self.valid_transactions = [transaction.json_transaction for transaction in self.transactions if transaction.is_valid()]
self.valid_transactions = [transaction for transaction in self.transactions if transaction.is_valid()]
Loading

0 comments on commit 4822ae2

Please sign in to comment.