You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

blocktools.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2015-2016 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. """Utilities for manipulating blocks and transactions."""
  6. from .mininode import *
  7. from .script import CScript, OP_TRUE, OP_CHECKSIG, OP_RETURN
  8. # Create a block (with regtest difficulty)
  9. def create_block(hashprev, coinbase, nTime=None):
  10. block = CBlock()
  11. if nTime is None:
  12. import time
  13. block.nTime = int(time.time()+600)
  14. else:
  15. block.nTime = nTime
  16. block.hashPrevBlock = hashprev
  17. block.nBits = 0x207fffff # Will break after a difficulty adjustment...
  18. block.vtx.append(coinbase)
  19. block.hashMerkleRoot = block.calc_merkle_root()
  20. block.calc_sha256()
  21. return block
  22. # From BIP141
  23. WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
  24. def get_witness_script(witness_root, witness_nonce):
  25. witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
  26. output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
  27. return CScript([OP_RETURN, output_data])
  28. # According to BIP141, blocks with witness rules active must commit to the
  29. # hash of all in-block transactions including witness.
  30. def add_witness_commitment(block, nonce=0):
  31. # First calculate the merkle root of the block's
  32. # transactions, with witnesses.
  33. witness_nonce = nonce
  34. witness_root = block.calc_witness_merkle_root()
  35. # witness_nonce should go to coinbase witness.
  36. block.vtx[0].wit.vtxinwit = [CTxInWitness()]
  37. block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)]
  38. # witness commitment is the last OP_RETURN output in coinbase
  39. block.vtx[0].vout.append(CTxOut(0, get_witness_script(witness_root, witness_nonce)))
  40. block.vtx[0].rehash()
  41. block.hashMerkleRoot = block.calc_merkle_root()
  42. block.rehash()
  43. def serialize_script_num(value):
  44. r = bytearray(0)
  45. if value == 0:
  46. return r
  47. neg = value < 0
  48. absvalue = -value if neg else value
  49. while (absvalue):
  50. r.append(int(absvalue & 0xff))
  51. absvalue >>= 8
  52. if r[-1] & 0x80:
  53. r.append(0x80 if neg else 0)
  54. elif neg:
  55. r[-1] |= 0x80
  56. return r
  57. # Create a coinbase transaction, assuming no miner fees.
  58. # If pubkey is passed in, the coinbase output will be a P2PK output;
  59. # otherwise an anyone-can-spend output.
  60. def create_coinbase(height, pubkey = None):
  61. coinbase = CTransaction()
  62. coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
  63. ser_string(serialize_script_num(height)), 0xffffffff))
  64. coinbaseoutput = CTxOut()
  65. coinbaseoutput.nValue = 50 * COIN
  66. halvings = int(height/150) # regtest
  67. coinbaseoutput.nValue >>= halvings
  68. if (pubkey != None):
  69. coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
  70. else:
  71. coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
  72. coinbase.vout = [ coinbaseoutput ]
  73. coinbase.calc_sha256()
  74. return coinbase
  75. # Create a transaction.
  76. # If the scriptPubKey is not specified, make it anyone-can-spend.
  77. def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
  78. tx = CTransaction()
  79. assert(n < len(prevtx.vout))
  80. tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
  81. tx.vout.append(CTxOut(value, scriptPubKey))
  82. tx.calc_sha256()
  83. return tx
  84. def get_legacy_sigopcount_block(block, fAccurate=True):
  85. count = 0
  86. for tx in block.vtx:
  87. count += get_legacy_sigopcount_tx(tx, fAccurate)
  88. return count
  89. def get_legacy_sigopcount_tx(tx, fAccurate=True):
  90. count = 0
  91. for i in tx.vout:
  92. count += i.scriptPubKey.GetSigOpCount(fAccurate)
  93. for j in tx.vin:
  94. # scriptSig might be of type bytes, so convert to CScript for the moment
  95. count += CScript(j.scriptSig).GetSigOpCount(fAccurate)
  96. return count