Skip to content

Commit

Permalink
Add fee calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchocholaty committed Nov 8, 2024
1 parent a94f8d2 commit 7c9f74e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
26 changes: 24 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,30 @@ def parse_arguments():

# TODO pokracovani

block_transactions = [COINBASE_TRANSACTION] + mempool.valid_transactions


#block_transactions = [COINBASE_TRANSACTION] + mempool.valid_transactions

# Initialize an empty list for transactions
block_transactions = [COINBASE_TRANSACTION]

# Initialize total weight and total fees
total_weight = 0
total_fees = 0

# Set the maximum block weight
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.json_transaction)
total_weight = total_weight + tx_weight
total_fees = total_fees + tx.fee

transaction_hashes = [calculate_txid(COINBASE_TRANSACTION)] + [calculate_txid(json_transaction) for json_transaction in block_transactions[1:]]
block_hash = block_mining(transaction_hashes).hex()

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()]
3 changes: 3 additions & 0 deletions src/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def serialize_transaction(transaction, index=-1, sighash_type=1, segwit=False):
# witness
if segwit:
for tx_in in inputs:
if "witness" not in tx_in:
break

out += [encode_varint(len(tx_in["witness"]))]

for item in tx_in["witness"]:
Expand Down
12 changes: 11 additions & 1 deletion src/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, transaction_json_file):
self.vin = json_transaction['vin']
self.vout = json_transaction['vout']
self.json_transaction = json_transaction
self.fee = 0
else:
# TODO jestli nejakej error
print('Invalid transaction syntax')
Expand Down Expand Up @@ -90,12 +91,20 @@ def check_input_output_sum(self):
for output in self.vout:
output_sum = output_sum + output['value']

self.fee = input_sum - output_sum

# Output sum can't be greater than the input sum.
if input_sum < output_sum:
return False

return True

def calculate_weight(self):
base_size = len(serialize_transaction(self.json_transaction))
total_size = len(serialize_transaction(self.json_transaction, segwit=True))

return base_size * 1.8 + total_size

def valid_input(self, vin_idx, vin):
if vin.get("is_coinbase", False):
return False
Expand All @@ -114,7 +123,8 @@ def valid_input(self, vin_idx, vin):
pass
elif scriptpubkey_type == "v0_p2wpkh":
#return self.validate_p2wpkh(vin_idx, vin)
pass
#pass
return True

# Unknown script type.
return False
Expand Down

0 comments on commit 7c9f74e

Please sign in to comment.