2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2015-2016 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2015-04-28 18:39:47 +02:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Utilities for manipulating blocks and transactions."""
|
2015-04-28 18:39:47 +02:00
|
|
|
|
2016-03-19 21:36:32 +01:00
|
|
|
from .mininode import *
|
|
|
|
from .script import CScript, OP_TRUE, OP_CHECKSIG
|
2015-04-28 18:39:47 +02:00
|
|
|
|
|
|
|
# Create a block (with regtest difficulty)
|
|
|
|
def create_block(hashprev, coinbase, nTime=None):
|
|
|
|
block = CBlock()
|
|
|
|
if nTime is None:
|
|
|
|
import time
|
|
|
|
block.nTime = int(time.time()+600)
|
|
|
|
else:
|
|
|
|
block.nTime = nTime
|
|
|
|
block.hashPrevBlock = hashprev
|
|
|
|
block.nBits = 0x207fffff # Will break after a difficulty adjustment...
|
|
|
|
block.vtx.append(coinbase)
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
block.calc_sha256()
|
|
|
|
return block
|
|
|
|
|
|
|
|
def serialize_script_num(value):
|
|
|
|
r = bytearray(0)
|
|
|
|
if value == 0:
|
|
|
|
return r
|
|
|
|
neg = value < 0
|
|
|
|
absvalue = -value if neg else value
|
|
|
|
while (absvalue):
|
2016-03-19 21:36:32 +01:00
|
|
|
r.append(int(absvalue & 0xff))
|
2015-04-28 18:39:47 +02:00
|
|
|
absvalue >>= 8
|
|
|
|
if r[-1] & 0x80:
|
|
|
|
r.append(0x80 if neg else 0)
|
|
|
|
elif neg:
|
|
|
|
r[-1] |= 0x80
|
|
|
|
return r
|
|
|
|
|
2015-08-05 23:47:34 +02:00
|
|
|
# Create a coinbase transaction, assuming no miner fees.
|
|
|
|
# If pubkey is passed in, the coinbase output will be a P2PK output;
|
|
|
|
# otherwise an anyone-can-spend output.
|
2018-05-29 14:13:50 +02:00
|
|
|
def create_coinbase(height, pubkey = None, dip4_activated=False):
|
2015-04-28 18:39:47 +02:00
|
|
|
coinbase = CTransaction()
|
2020-07-29 03:23:12 +02:00
|
|
|
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
|
2015-08-05 23:47:34 +02:00
|
|
|
ser_string(serialize_script_num(height)), 0xffffffff))
|
2015-04-28 18:39:47 +02:00
|
|
|
coinbaseoutput = CTxOut()
|
2016-08-26 03:00:27 +02:00
|
|
|
coinbaseoutput.nValue = 500 * COIN
|
2015-08-05 23:47:34 +02:00
|
|
|
halvings = int(height/150) # regtest
|
2015-04-28 18:39:47 +02:00
|
|
|
coinbaseoutput.nValue >>= halvings
|
2015-08-05 23:47:34 +02:00
|
|
|
if (pubkey != None):
|
|
|
|
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
|
|
|
|
else:
|
|
|
|
coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
|
2015-04-28 18:39:47 +02:00
|
|
|
coinbase.vout = [ coinbaseoutput ]
|
2018-05-29 14:13:50 +02:00
|
|
|
if dip4_activated:
|
|
|
|
coinbase.nVersion = 3
|
|
|
|
coinbase.nType = 5
|
2019-04-04 12:08:21 +02:00
|
|
|
cbtx_payload = CCbTx(2, height, 0, 0)
|
2018-05-29 14:13:50 +02:00
|
|
|
coinbase.vExtraPayload = cbtx_payload.serialize()
|
2015-04-28 18:39:47 +02:00
|
|
|
coinbase.calc_sha256()
|
|
|
|
return coinbase
|
|
|
|
|
2016-06-13 11:36:48 +02:00
|
|
|
# Create a transaction.
|
|
|
|
# If the scriptPubKey is not specified, make it anyone-can-spend.
|
|
|
|
def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
|
2015-04-28 18:39:47 +02:00
|
|
|
tx = CTransaction()
|
|
|
|
assert(n < len(prevtx.vout))
|
|
|
|
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
|
2016-06-13 11:36:48 +02:00
|
|
|
tx.vout.append(CTxOut(value, scriptPubKey))
|
2015-04-28 18:39:47 +02:00
|
|
|
tx.calc_sha256()
|
|
|
|
return tx
|
2016-06-13 11:36:48 +02:00
|
|
|
|
|
|
|
def get_legacy_sigopcount_block(block, fAccurate=True):
|
|
|
|
count = 0
|
|
|
|
for tx in block.vtx:
|
|
|
|
count += get_legacy_sigopcount_tx(tx, fAccurate)
|
|
|
|
return count
|
|
|
|
|
|
|
|
def get_legacy_sigopcount_tx(tx, fAccurate=True):
|
|
|
|
count = 0
|
|
|
|
for i in tx.vout:
|
|
|
|
count += i.scriptPubKey.GetSigOpCount(fAccurate)
|
|
|
|
for j in tx.vin:
|
|
|
|
# scriptSig might be of type bytes, so convert to CScript for the moment
|
|
|
|
count += CScript(j.scriptSig).GetSigOpCount(fAccurate)
|
|
|
|
return count
|
2018-09-11 16:32:45 +02:00
|
|
|
|
|
|
|
# Identical to GetMasternodePayment in C++ code
|
|
|
|
def get_masternode_payment(nHeight, blockValue):
|
|
|
|
ret = int(blockValue / 5)
|
|
|
|
|
|
|
|
nMNPIBlock = 350
|
|
|
|
nMNPIPeriod = 10
|
|
|
|
|
|
|
|
if nHeight > nMNPIBlock:
|
|
|
|
ret += int(blockValue / 20)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 1):
|
|
|
|
ret += int(blockValue / 20)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 2):
|
|
|
|
ret += int(blockValue / 20)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 3):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 4):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 5):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 6):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 7):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
if nHeight > nMNPIBlock+(nMNPIPeriod* 9):
|
|
|
|
ret += int(blockValue / 40)
|
|
|
|
|
|
|
|
return ret
|