Skip to content

Commit

Permalink
work towards bringing mininode.py up to date with the main node and t…
Browse files Browse the repository at this point in the history
…owards re-enabling rpc tests
  • Loading branch information
Arvid Norberg committed Sep 19, 2017
1 parent 1e1a2e5 commit 3c21411
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 127 deletions.
2 changes: 1 addition & 1 deletion qa/rpc-tests/listtransactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.mininode import CTransaction, setCTxOutValue, CTxOutValue, COIN
from test_framework.mininode import CTransaction, COIN
from io import BytesIO
import binascii

Expand Down
10 changes: 5 additions & 5 deletions qa/rpc-tests/p2p-compactblocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def build_block_on_tip(self, node, segwit=False):
height = node.getblockcount()
tip = node.getbestblockhash()
mtp = node.getblockheader(tip)['mediantime']
block = create_block(int(tip, 16), create_coinbase(height + 1), mtp + 1)
block = create_block(int(tip, 16), create_coinbase(height + 1), mtp + 1, height + 1)
block.nVersion = 4
if segwit:
add_witness_commitment(block)
Expand All @@ -146,12 +146,12 @@ def make_utxos(self):
assert(int(self.nodes[0].getbestblockhash(), 16) == block.sha256)
self.nodes[0].generate(100)

total_value = block.vtx[0].vout[0].nValue
out_value = total_value // 10
total_value = block.vtx[0].vout[0].nValue.getAmount()
out_value = total_value
tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(block.vtx[0].sha256, 0), b''))
for i in range(10):
tx.vout.append(CTxOut(out_value, CScript([OP_TRUE])))
tx.vout.append(CTxOut(CTxOutValue(out_value), CScript([OP_TRUE])))
tx.rehash()

block2 = self.build_block_on_tip(self.nodes[0])
Expand Down Expand Up @@ -458,7 +458,7 @@ def build_block_with_transactions(self, node, utxo, num_transactions):
for i in range(num_transactions):
tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(utxo[0], utxo[1]), b''))
tx.vout.append(CTxOut(utxo[2] - 1000, CScript([OP_TRUE])))
tx.vout.append(CTxOut(CTxOutValue(utxo[2].getAmount() - 1000), CScript([OP_TRUE])))
tx.rehash()
utxo = [tx.sha256, 0, tx.vout[0].nValue]
block.vtx.append(tx)
Expand Down
45 changes: 18 additions & 27 deletions qa/rpc-tests/test_framework/blocktools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
from .script import CScript, OP_TRUE, OP_CHECKSIG, OP_RETURN

# Create a block (with regtest difficulty)
def create_block(hashprev, coinbase, nTime=None):
def create_block(hashprev, coinbase, nTime=None, height=0):
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.nHeight = height
block.proof.challenge = CScript([OP_TRUE])
block.proof.solution = b''
block.calc_sha256()
return block

Expand All @@ -39,39 +41,28 @@ def add_witness_commitment(block, nonce=0):

# witness commitment is the last OP_RETURN output in coinbase
output_data = WITNESS_COMMITMENT_HEADER + ser_uint256(witness_commitment)
block.vtx[0].vout.append(CTxOut(0, CScript([OP_RETURN, output_data])))
block.vtx[0].vout.append(CTxOut(CTxOutValue(0), CScript([OP_RETURN, output_data])))
block.vtx[0].rehash()
block.hashMerkleRoot = block.calc_merkle_root()
block.rehash()


def serialize_script_num(value):
r = bytearray(0)
if value == 0:
return r
neg = value < 0
absvalue = -value if neg else value
while (absvalue):
r.append(int(absvalue & 0xff))
absvalue >>= 8
if r[-1] & 0x80:
r.append(0x80 if neg else 0)
elif neg:
r[-1] |= 0x80
return r

# 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.
def create_coinbase(height, pubkey = None):
def create_coinbase(height, pubkey = None, amount = 0):
coinbase = CTransaction()
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
ser_string(serialize_script_num(height)), 0xffffffff))
# coinbase transaction scriptsigs must be at least 2 bytes
coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
CScript([height, OP_TRUE]), 0xffffffff))
coinbaseoutput = CTxOut()
coinbaseoutput.nValue = 50 * COIN
halvings = int(height/150) # regtest
coinbaseoutput.nValue >>= halvings
if (pubkey != None):

coinbaseoutput.nValue.setToAmount(amount)
coinbaseoutput.nAsset.setToAsset(b'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')

if amount == 0:
# zero utxo's must be made unspendable
coinbaseoutput.scriptPubKey = CScript([OP_RETURN])
elif pubkey != None:
coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
else:
coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
Expand All @@ -85,7 +76,7 @@ def create_transaction(prevtx, n, sig, value, scriptPubKey=CScript()):
tx = CTransaction()
assert(n < len(prevtx.vout))
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
tx.vout.append(CTxOut(value, scriptPubKey))
tx.vout.append(CTxOut(CTxOutValue(value), scriptPubKey))
tx.calc_sha256()
return tx

Expand Down
Loading

0 comments on commit 3c21411

Please sign in to comment.