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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. # According to BIP141, blocks with witness rules active must commit to the
  25. # hash of all in-block transactions including witness.
  26. def add_witness_commitment(block, nonce=0):
  27. # First calculate the merkle root of the block's
  28. # transactions, with witnesses.
  29. witness_nonce = nonce
  30. witness_root = block.calc_witness_merkle_root()
  31. witness_commitment = uint256_from_str(hash256(ser_uint256(witness_root)+ser_uint256(witness_nonce)))
  32. # witness_nonce should go to coinbase witness.
  33. block.vtx[0].wit.vtxinwit = [CTxInWitness()]
  34. block.vtx[0].wit.vtxinwit[0].scriptWitness.stack = [ser_uint256(witness_nonce)]
  35. # witness commitment is the last OP_RETURN output in coinbase
  36. output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
  37. block.vtx[0].vout.append(CTxOut(0, CScript([OP_RETURN, output_data])))
  38. block.vtx[0].rehash()
  39. block.hashMerkleRoot = block.calc_merkle_root()
  40. block.rehash()
  41. def serialize_script_num(value):
  42. r = bytearray(0)
  43. if value == 0:
  44. return r
  45. neg = value < 0
  46. absvalue = -value if neg else value
  47. while (absvalue):
  48. r.append(int(absvalue & 0xff))
  49. absvalue >>= 8
  50. if r[-1] & 0x80:
  51. r.append(0x80 if neg else 0)
  52. elif neg:
  53. r[-1] |= 0x80
  54. return r
  55. # Create a coinbase transaction, assuming no miner fees.
  56. # If pubkey is passed in, the coinbase output will be a P2PK output;
  57. # otherwise an anyone-can-spend output.
  58. def create_coinbase(height, pubkey = None):
  59. coinbase = CTransaction()
  60. coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
  61. ser_string(serialize_script_num(height)), 0xffffffff))
  62. coinbaseoutput = CTxOut()
  63. coinbaseoutput.nValue = 50 * COIN
  64. halvings = int(height/150) # regtest
  65. coinbaseoutput.nValue >>= halvings
  66. if (pubkey != None):
  67. coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
  68. else:
  69. coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
  70. coinbase.vout = [ coinbaseoutput ]
  71. coinbase.calc_sha256()
  72. return coinbase
  73. # Create a transaction.
  74. # If the scriptPubKey is not specified, make it anyone-can-spend.
  75. def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
  76. tx = CTransaction()
  77. assert(n < len(prevtx.vout))
  78. tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
  79. tx.vout.append(CTxOut(value, scriptPubKey))
  80. tx.calc_sha256()
  81. return tx
  82. def get_legacy_sigopcount_block(block, fAccurate=True):
  83. count = 0
  84. for tx in block.vtx:
  85. count += get_legacy_sigopcount_tx(tx, fAccurate)
  86. return count
  87. def get_legacy_sigopcount_tx(tx, fAccurate=True):
  88. count = 0
  89. for i in tx.vout:
  90. count += i.scriptPubKey.GetSigOpCount(fAccurate)
  91. for j in tx.vin:
  92. # scriptSig might be of type bytes, so convert to CScript for the moment
  93. count += CScript(j.scriptSig).GetSigOpCount(fAccurate)
  94. return count