aboutsummaryrefslogtreecommitdiff
path: root/test/functional/test_framework/auxpow.py
blob: e145e7ce7196024533a3ea4b23d845c2cb48ed4a (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
# Copyright (c) 2014-2018 Daniel Kraft
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

# Basic code for working with auxpow.  This is used for the regtests (e.g. from
# auxpow_testing.py), but also for contrib/auxpow/getwork-wrapper.py.

import binascii
import codecs
import hashlib

def constructAuxpow (block):
  """
  Starts to construct a minimal auxpow, ready to be mined.  Returns the fake
  coinbase tx and the unmined parent block header as hex strings.
  """

  block = codecs.encode (block, 'ascii')

  # Start by building the merge-mining coinbase.  The merkle tree
  # consists only of the block hash as root.
  coinbase = b"fabe" + binascii.hexlify (b"m" * 2)
  coinbase += block
  coinbase += b"01000000" + (b"00" * 4)

  # Construct "vector" of transaction inputs.
  vin = b"01"
  vin += (b"00" * 32) + (b"ff" * 4)
  vin += codecs.encode ("%02x" % (len (coinbase) // 2), "ascii") + coinbase
  vin += (b"ff" * 4)

  # Build up the full coinbase transaction.  It consists only
  # of the input and has no outputs.
  tx = b"01000000" + vin + b"00" + (b"00" * 4)
  txHash = doubleHashHex (tx)

  # Construct the parent block header.  It need not be valid, just good
  # enough for auxpow purposes.
  header = b"01000000"
  header += b"00" * 32
  header += reverseHex (txHash)
  header += b"00" * 4
  header += b"00" * 4
  header += b"00" * 4

  return (tx.decode ('ascii'), header.decode ('ascii'))

def finishAuxpow (tx, header):
  """
  Constructs the finished auxpow hex string based on the mined header.
  """

  blockhash = doubleHashHex (header)

  # Build the MerkleTx part of the auxpow.
  auxpow = codecs.encode (tx, 'ascii')
  auxpow += blockhash
  auxpow += b"00"
  auxpow += b"00" * 4

  # Extend to full auxpow.
  auxpow += b"00"
  auxpow += b"00" * 4
  auxpow += header

  return auxpow.decode ("ascii")

def doubleHashHex (data):
  """
  Perform Bitcoin's Double-SHA256 hash on the given hex string.
  """

  hasher = hashlib.sha256 ()
  hasher.update (binascii.unhexlify (data))
  data = hasher.digest ()

  hasher = hashlib.sha256 ()
  hasher.update (data)

  return reverseHex (hasher.hexdigest ())

def reverseHex (data):
  """
  Flip byte order in the given data (hex string).
  """

  b = bytearray (binascii.unhexlify (data))
  b.reverse ()

  return binascii.hexlify (b)

def getworkByteswap (data):
  """
  Run the byte-order swapping step necessary for working with getwork.
  """

  data = bytearray (data)
  assert len (data) % 4 == 0
  for i in range (0, len (data), 4):
    data[i], data[i + 3] = data[i + 3], data[i]
    data[i + 1], data[i + 2] = data[i + 2], data[i + 1]

  return data