Changeset View
Changeset View
Standalone View
Standalone View
qa/rpc-tests/abc-p2p-fullblocktest.py
Show First 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | class FullBlockTest(ComparisonTestFramework): | ||||
# sign a transaction, using the key we know about | # sign a transaction, using the key we know about | ||||
# this signs input 0 in tx, which is assumed to be spending output n in spend_tx | # this signs input 0 in tx, which is assumed to be spending output n in spend_tx | ||||
def sign_tx(self, tx, spend_tx, n): | def sign_tx(self, tx, spend_tx, n): | ||||
scriptPubKey = bytearray(spend_tx.vout[n].scriptPubKey) | scriptPubKey = bytearray(spend_tx.vout[n].scriptPubKey) | ||||
if (scriptPubKey[0] == OP_TRUE): # an anyone-can-spend | if (scriptPubKey[0] == OP_TRUE): # an anyone-can-spend | ||||
tx.vin[0].scriptSig = CScript() | tx.vin[0].scriptSig = CScript() | ||||
return | return | ||||
(sighash, err) = SignatureHash(spend_tx.vout[n].scriptPubKey, tx, 0, SIGHASH_ALL) | sighash = SignatureHashForkId(spend_tx.vout[n].scriptPubKey, tx, 0, SIGHASH_ALL|SIGHASH_FORKID, spend_tx.vout[n].nValue) | ||||
tx.vin[0].scriptSig = CScript([self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL]))]) | tx.vin[0].scriptSig = CScript([self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL|SIGHASH_FORKID]))]) | ||||
def create_and_sign_transaction(self, spend_tx, n, value, script=CScript([OP_TRUE])): | def create_and_sign_transaction(self, spend_tx, n, value, script=CScript([OP_TRUE])): | ||||
tx = self.create_tx(spend_tx, n, value, script) | tx = self.create_tx(spend_tx, n, value, script) | ||||
self.sign_tx(tx, spend_tx, n) | self.sign_tx(tx, spend_tx, n) | ||||
tx.rehash() | tx.rehash() | ||||
return tx | return tx | ||||
def next_block(self, number, spend=None, additional_coinbase_value=0, script=None, extra_sigops=0, block_size=0, solve=True): | def next_block(self, number, spend=None, additional_coinbase_value=0, script=None, extra_sigops=0, block_size=0, solve=True): | ||||
Show All 30 Lines | def next_block(self, number, spend=None, additional_coinbase_value=0, script=None, extra_sigops=0, block_size=0, solve=True): | ||||
# Now sign it if necessary | # Now sign it if necessary | ||||
scriptSig = b"" | scriptSig = b"" | ||||
scriptPubKey = bytearray(spend.tx.vout[spend.n].scriptPubKey) | scriptPubKey = bytearray(spend.tx.vout[spend.n].scriptPubKey) | ||||
if (scriptPubKey[0] == OP_TRUE): # looks like an anyone-can-spend | if (scriptPubKey[0] == OP_TRUE): # looks like an anyone-can-spend | ||||
scriptSig = CScript([OP_TRUE]) | scriptSig = CScript([OP_TRUE]) | ||||
else: | else: | ||||
# We have to actually sign it | # We have to actually sign it | ||||
(sighash, err) = SignatureHash(spend.tx.vout[spend.n].scriptPubKey, tx, 0, SIGHASH_ALL) | sighash = SignatureHashForkId(spend.tx.vout[spend.n].scriptPubKey, tx, 0, SIGHASH_ALL|SIGHASH_FORKID, spend.tx.vout[spend.n].nValue) | ||||
scriptSig = CScript([self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL]))]) | scriptSig = CScript([self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL|SIGHASH_FORKID]))]) | ||||
tx.vin[0].scriptSig = scriptSig | tx.vin[0].scriptSig = scriptSig | ||||
# Now add the transaction to the block | # Now add the transaction to the block | ||||
self.add_transactions_to_block(block, [tx]) | self.add_transactions_to_block(block, [tx]) | ||||
block.hashMerkleRoot = block.calc_merkle_root() | block.hashMerkleRoot = block.calc_merkle_root() | ||||
if spendable_output != None and block_size > 0: | if spendable_output != None and block_size > 0: | ||||
while len(block.serialize()) < block_size: | while len(block.serialize()) < block_size: | ||||
tx = CTransaction() | tx = CTransaction() | ||||
script_length = block_size - len(block.serialize()) - 79 | script_length = block_size - len(block.serialize()) - 79 | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | def get_tests(self): | ||||
p2sh_tx = self.create_and_sign_transaction(out[22].tx, out[22].n, 1, p2sh_script) | p2sh_tx = self.create_and_sign_transaction(out[22].tx, out[22].n, 1, p2sh_script) | ||||
# Add the transaction to the block | # Add the transaction to the block | ||||
block(30) | block(30) | ||||
update_block(30, [p2sh_tx]) | update_block(30, [p2sh_tx]) | ||||
yield accepted() | yield accepted() | ||||
# Creates a new transaction using the p2sh transaction included in the last block | # Creates a new transaction using the p2sh transaction included in the last block | ||||
def spend_p2sh_tx (output_script=CScript([OP_TRUE])): | def spend_p2sh_tx(output_script=CScript([OP_TRUE])): | ||||
# Create the transaction | # Create the transaction | ||||
spent_p2sh_tx = CTransaction() | spent_p2sh_tx = CTransaction() | ||||
spent_p2sh_tx.vin.append(CTxIn(COutPoint(p2sh_tx.sha256, 0), b'')) | spent_p2sh_tx.vin.append(CTxIn(COutPoint(p2sh_tx.sha256, 0), b'')) | ||||
spent_p2sh_tx.vout.append(CTxOut(1, output_script)) | spent_p2sh_tx.vout.append(CTxOut(1, output_script)) | ||||
# Sign the transaction using the redeem script | # Sign the transaction using the redeem script | ||||
(sighash, err) = SignatureHash(redeem_script, spent_p2sh_tx, 0, SIGHASH_ALL) | sighash = SignatureHashForkId(redeem_script, spent_p2sh_tx, 0, SIGHASH_ALL|SIGHASH_FORKID, p2sh_tx.vout[0].nValue) | ||||
sig = self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL])) | sig = self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL|SIGHASH_FORKID])) | ||||
spent_p2sh_tx.vin[0].scriptSig = CScript([sig, redeem_script]) | spent_p2sh_tx.vin[0].scriptSig = CScript([sig, redeem_script]) | ||||
spent_p2sh_tx.rehash() | spent_p2sh_tx.rehash() | ||||
return spent_p2sh_tx | return spent_p2sh_tx | ||||
# Sigops p2sh limit | # Sigops p2sh limit | ||||
p2sh_sigops_limit = MAX_BLOCK_SIGOPS_PER_MB - redeem_script.GetSigOpCount(True) | p2sh_sigops_limit = MAX_BLOCK_SIGOPS_PER_MB - redeem_script.GetSigOpCount(True) | ||||
# Too many sigops in one p2sh txn | # Too many sigops in one p2sh txn | ||||
too_many_p2sh_sigops = CScript([OP_CHECKSIG] * (p2sh_sigops_limit + 1)) | too_many_p2sh_sigops = CScript([OP_CHECKSIG] * (p2sh_sigops_limit + 1)) | ||||
▲ Show 20 Lines • Show All 98 Lines • Show Last 20 Lines |