-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathauxpow_testing.py
79 lines (60 loc) · 2.25 KB
/
auxpow_testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# Copyright (c) 2014-2022 Daniel Kraft
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
# Utility routines for auxpow that are needed specifically by the regtests.
# This is mostly about actually *solving* an auxpow block (with regtest
# difficulty) or inspecting the information for verification.
import binascii
from test_framework import auxpow
def computeAuxpow (block, target, ok):
"""
Build an auxpow object (serialised as hex string) that solves
(ok = True) or doesn't solve (ok = False) the block.
"""
(tx, header) = auxpow.constructAuxpow (block)
(header, _) = mineBlock (header, target, ok)
return auxpow.finishAuxpow (tx, header)
def mineAuxpowBlock (node, addr):
"""
Mine an auxpow block on the given RPC connection. This uses the
createauxblock and submitauxblock command pair.
"""
def create ():
return node.createauxblock (addr)
return mineAuxpowBlockWithMethods (create, node.submitauxblock)
def mineAuxpowBlockWithMethods (create, submit):
"""
Mine an auxpow block, using the given methods for creation and submission.
"""
auxblock = create ()
target = auxpow.reverseHex (auxblock['_target'])
apow = computeAuxpow (auxblock['hash'], target, True)
res = submit (auxblock['hash'], apow)
assert res
return auxblock['hash']
def getCoinbaseAddr (node, blockHash):
"""
Extract the coinbase tx' payout address for the given block.
"""
blockData = node.getblock (blockHash)
txn = blockData['tx']
assert len (txn) >= 1
txData = node.getrawtransaction (txn[0], True, blockHash)
assert len (txData['vout']) >= 1 and len (txData['vin']) == 1
assert 'coinbase' in txData['vin'][0]
return txData['vout'][0]['scriptPubKey']['address']
def mineBlock (header, target, ok):
"""
Given a block header, update the nonce until it is ok (or not)
for the given target.
"""
data = bytearray (binascii.unhexlify (header))
while True:
assert data[79] < 255
data[79] += 1
hexData = binascii.hexlify (data)
blockhash = auxpow.doubleHashHex (hexData)
if (ok and blockhash < target) or ((not ok) and blockhash > target):
break
return (hexData, blockhash)