Soweit ich weiß, muss ich jede Transaktionsinformation hashen und einen Merkle-Baum erstellen. Danach erhalten wir den Root-Hash und verketten diesen Wert mit dem Nonce und Hash und prüfen, ob er am Anfang eine bestimmte Anzahl von Nullen hat.
Dies ist eine Skizze dieser Implementierung, aber warum brauche ich die verwendeten öffentlichen Parameter? zur Validierung usw. Ich habe sie nicht verwendet. Muss ich jede Transaktion auch validieren? Die Implementierung des Merkle-Baums ist korrekt, aber ich erhalte nicht den richtigen Hash-Wert vom Block. Was kann der Grund sein?
# Function to check if PoW is valid
def CheckPow(p, q, g, PoWLen, TxCnt, filename):
# Read the transaction file
with open(filename, 'r') as file:
lines = file.readlines()
nonce = lines[0].strip().split(":")[1].strip()
lines = lines[1:]
# Extract transactions from the file
txs = []
for i in range(TxCnt):
# Here you would extract the transaction data and hash it (simplified here)
tx_data = ("".join(lines[i*7+3: i*7+7]))
print(tx_data)
txs.append(tx_data)
# Build the Merkle tree and get the root hash (Hr)
merktree= tree.MerkleTree(txs)
H_r = merktree.get_merkle_root()
print("H_r:",H_r)
print(H_r.hex())
# Convert nonce to bytes and concatenate with Hr
nonce = int(nonce)
nonce_bytes = nonce.to_bytes((nonce.bit_length() + 7) // 8, byteorder='big')
#print(nonce_bytes)
Hr_nonce = H_r + nonce_bytes
# Compute the SHA3-256 hash of the concatenation
hash_value = sha3_256(Hr_nonce).hexdigest()
print(hash_value)
return hash_value
Überprüfung des Arbeitsnachweises bei Bitcoin-Transaktionen ⇐ Python
-
- Similar Topics
- Replies
- Views
- Last post