Reorder field in the block
Miners should choose a transaction from the network based on its reward, not as part of the mining variability. That's why it's a bad idea to place the transaction as last block element. Reorder the fields to make less room for harmful mining optimization.
This commit is contained in:
@@ -68,19 +68,19 @@ class Block:
|
||||
|
||||
def from_bytes(block_raw):
|
||||
assert len(block_raw) == 292
|
||||
transaction_raw = block_raw[144:292]
|
||||
transaction_raw = block_raw[0:148]
|
||||
if transaction_raw == 148 * b"\0":
|
||||
transaction = None
|
||||
else:
|
||||
transaction = Transaction.from_bytes(transaction_raw)
|
||||
return Block(
|
||||
nonce = int.from_bytes(block_raw[0:8], "big"),
|
||||
timestamp = int.from_bytes(block_raw[8:16], "big"),
|
||||
previous_hash = block_raw[16:48],
|
||||
message = block_raw[48:80],
|
||||
difficulty_sum = int.from_bytes(block_raw[80:112], "big"),
|
||||
miner_pubkey = block_raw[112:144],
|
||||
transaction = transaction,
|
||||
message = block_raw[148:180],
|
||||
miner_pubkey = block_raw[180:212],
|
||||
previous_hash = block_raw[212:244],
|
||||
timestamp = int.from_bytes(block_raw[244:252], "big"),
|
||||
difficulty_sum = int.from_bytes(block_raw[252:284], "big"),
|
||||
nonce = int.from_bytes(block_raw[284:292], "big"),
|
||||
own_hash = hashlib.sha256(block_raw).digest(),
|
||||
balances = None,
|
||||
used_transaction_ids = None,
|
||||
@@ -162,13 +162,14 @@ class Block:
|
||||
transaction = 148 * b"\0"
|
||||
else:
|
||||
transaction = self.transaction.get_transaction_raw()
|
||||
return self.nonce.to_bytes(8, "big") + \
|
||||
self.timestamp.to_bytes(8, "big") + \
|
||||
self.previous_hash + \
|
||||
return
|
||||
transaction + \
|
||||
self.message + \
|
||||
self.difficulty_sum.to_bytes(32, "big") + \
|
||||
self.miner_pubkey + \
|
||||
transaction
|
||||
self.previous_hash + \
|
||||
self.timestamp.to_bytes(8, "big") + \
|
||||
self.difficulty_sum.to_bytes(32, "big") + \
|
||||
self.nonce.to_bytes(8, "big")
|
||||
|
||||
class Blockchain:
|
||||
def __init__(self):
|
||||
|
||||
Reference in New Issue
Block a user