Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1502# Should fail policy test.1503test_transaction_acceptance(self.nodes[0], self.test_node, tx3, True, False, 'mempool-script-verify-flag-failed (Using non-compressed keys in segwit)')1504# But passes consensus.1505block = self.build_next_block()1506self.update_witness_block_with_transactions(block, [tx3])1507test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1509# Test 3: P2SH(P2WSH)1510# Try to spend the P2SH output created in the last test.1511# Send it to a P2PKH output, which we'll use in the next test.1512script_pubkey = keyhash_to_p2pkh_script(pubkeyhash)1513tx4 = CTransaction()1514tx4.vin.append(CTxIn(COutPoint(tx3.txid_int, 0), script_sig))1515tx4.vout.append(CTxOut(tx3.vout[0].nValue - 1000, script_pubkey))1516tx4.wit.vtxinwit.append(CTxInWitness())1517sign_p2pk_witness_input(witness_script, tx4, 0, SIGHASH_ALL, tx3.vout[0].nValue, key)1519# Should fail policy test.1520test_transaction_acceptance(self.nodes[0], self.test_node, tx4, True, False, 'mempool-script-verify-flag-failed (Using non-compressed keys in segwit)')1521block = self.build_next_block()1522self.update_witness_block_with_transactions(block, [tx4])1523test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1525# Test 4: Uncompressed pubkeys should still be valid in non-segwit1526# transactions.1527tx5 = CTransaction()1528tx5.vin.append(CTxIn(COutPoint(tx4.txid_int, 0), b""))1529tx5.vout.append(CTxOut(tx4.vout[0].nValue - 1000, CScript([OP_TRUE])))1530tx5.vin[0].scriptSig = CScript([pubkey])1531sign_input_legacy(tx5, 0, script_pubkey, key)1532# Should pass policy and consensus.1533test_transaction_acceptance(self.nodes[0], self.test_node, tx5, True, True)1534block = self.build_next_block()1535self.update_witness_block_with_transactions(block, [tx5])1536test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1537self.utxo.append(UTXO(tx5.txid_int, 0, tx5.vout[0].nValue))1539@subtest1540def test_signature_version_1(self):1541key, pubkey = generate_keypair()1542witness_script = key_to_p2pk_script(pubkey)1543script_pubkey = script_to_p2wsh_script(witness_script)1545# First create a witness output for use in the tests.1546tx = CTransaction()1547tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))1548tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))1550test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=True, accepted=True)1551# Mine this transaction in preparation for following tests.1552block = self.build_next_block()1553self.update_witness_block_with_transactions(block, [tx])1554test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1555self.sync_blocks()1556self.utxo.pop(0)1558# Test each hashtype1559prev_utxo = UTXO(tx.txid_int, 0, tx.vout[0].nValue)1560for sigflag in [0, SIGHASH_ANYONECANPAY]:1561for hashtype in [SIGHASH_ALL, SIGHASH_NONE, SIGHASH_SINGLE]:1562hashtype |= sigflag1563block = self.build_next_block()1564tx = CTransaction()1565tx.vin.append(CTxIn(COutPoint(prev_utxo.sha256, prev_utxo.n), b""))1566tx.vout.append(CTxOut(prev_utxo.nValue - 1000, script_pubkey))1567tx.wit.vtxinwit.append(CTxInWitness())1568# Too-large input value1569sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue + 1, key)1570self.update_witness_block_with_transactions(block, [tx])1571test_witness_block(self.nodes[0], self.test_node, block, accepted=False,1572reason='block-script-verify-flag-failed (Script evaluated without error '1573'but finished with a false/empty top stack element')1575# Too-small input value1576sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue - 1, key)1577block.vtx.pop() # remove last tx1578self.update_witness_block_with_transactions(block, [tx])1579test_witness_block(self.nodes[0], self.test_node, block, accepted=False,1580reason='block-script-verify-flag-failed (Script evaluated without error '1581'but finished with a false/empty top stack element')1583# Now try correct value1584sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue, key)1585block.vtx.pop()1586self.update_witness_block_with_transactions(block, [tx])1587test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1589prev_utxo = UTXO(tx.txid_int, 0, tx.vout[0].nValue)1591# Test combinations of signature hashes.1592# Split the utxo into a lot of outputs.1593# Randomly choose up to 10 to spend, sign with different hashtypes, and1594# output to a random number of outputs. Repeat NUM_SIGHASH_TESTS times.1595# Ensure that we've tested a situation where we use SIGHASH_SINGLE with1596# an input index > number of outputs.1597NUM_SIGHASH_TESTS = 5001598temp_utxos = []1599tx = CTransaction()1600tx.vin.append(CTxIn(COutPoint(prev_utxo.sha256, prev_utxo.n), b""))1601split_value = prev_utxo.nValue // NUM_SIGHASH_TESTS1602for _ in range(NUM_SIGHASH_TESTS):1603tx.vout.append(CTxOut(split_value, script_pubkey))1604tx.wit.vtxinwit.append(CTxInWitness())1605sign_p2pk_witness_input(witness_script, tx, 0, SIGHASH_ALL, prev_utxo.nValue, key)1606for i in range(NUM_SIGHASH_TESTS):1607temp_utxos.append(UTXO(tx.txid_int, i, split_value))1609block = self.build_next_block()1610self.update_witness_block_with_transactions(block, [tx])1611test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1613block = self.build_next_block()1614used_sighash_single_out_of_bounds = False1615for i in range(NUM_SIGHASH_TESTS):1616# Ping regularly to keep the connection alive1617if (not i % 100):1618self.test_node.sync_with_ping()1619# Choose random number of inputs to use.1620num_inputs = random.randint(1, 10)1621# Create a slight bias for producing more utxos1622num_outputs = random.randint(1, 11)1623random.shuffle(temp_utxos)1624assert len(temp_utxos) > num_inputs1625tx = CTransaction()1626total_value = 01627for i in range(num_inputs):1628tx.vin.append(CTxIn(COutPoint(temp_utxos[i].sha256, temp_utxos[i].n), b""))1629tx.wit.vtxinwit.append(CTxInWitness())1630total_value += temp_utxos[i].nValue1631split_value = total_value // num_outputs1632for _ in range(num_outputs):1633tx.vout.append(CTxOut(split_value, script_pubkey))1634for i in range(num_inputs):1635# Now try to sign each input, using a random hashtype.1636anyonecanpay = 01637if random.randint(0, 1):1638anyonecanpay = SIGHASH_ANYONECANPAY1639hashtype = random.randint(1, 3) | anyonecanpay1640sign_p2pk_witness_input(witness_script, tx, i, hashtype, temp_utxos[i].nValue, key)1641if (hashtype == SIGHASH_SINGLE and i >= num_outputs):1642used_sighash_single_out_of_bounds = True1643for i in range(num_outputs):1644temp_utxos.append(UTXO(tx.txid_int, i, split_value))1645temp_utxos = temp_utxos[num_inputs:]1647block.vtx.append(tx)1649# Test the block periodically, if we're close to maxblocksize1650if block.get_weight() > MAX_BLOCK_WEIGHT - 4000:1651self.update_witness_block_with_transactions(block, [])1652test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1653block = self.build_next_block()1655if (not used_sighash_single_out_of_bounds):1656self.log.info("WARNING: this test run didn't attempt SIGHASH_SINGLE with out-of-bounds index value")1657# Test the transactions we've added to the block1658if (len(block.vtx) > 1):1659self.update_witness_block_with_transactions(block, [])1660test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1662# Now test witness version 0 P2PKH transactions1663pubkeyhash = hash160(pubkey)1664script_pkh = key_to_p2wpkh_script(pubkey)1665tx = CTransaction()1666tx.vin.append(CTxIn(COutPoint(temp_utxos[0].sha256, temp_utxos[0].n), b""))1667tx.vout.append(CTxOut(temp_utxos[0].nValue, script_pkh))1668tx.wit.vtxinwit.append(CTxInWitness())1669sign_p2pk_witness_input(witness_script, tx, 0, SIGHASH_ALL, temp_utxos[0].nValue, key)1670tx2 = CTransaction()1671tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), b""))1672tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))1674script = keyhash_to_p2pkh_script(pubkeyhash)1675tx2.wit.vtxinwit.append(CTxInWitness())1676sign_input_segwitv0(tx2, 0, script, tx.vout[0].nValue, key)1677signature = tx2.wit.vtxinwit[0].scriptWitness.stack.pop()1679# Check that we can't have a scriptSig1680tx2.vin[0].scriptSig = CScript([signature, pubkey])1681block = self.build_next_block()1682self.update_witness_block_with_transactions(block, [tx, tx2])1683test_witness_block(self.nodes[0], self.test_node, block, accepted=False,1684reason='block-script-verify-flag-failed (Witness requires empty scriptSig)')1686# Move the signature to the witness.1687block.vtx.pop()1688tx2.wit.vtxinwit.append(CTxInWitness())1689tx2.wit.vtxinwit[0].scriptWitness.stack = [signature, pubkey]1690tx2.vin[0].scriptSig = b""1692self.update_witness_block_with_transactions(block, [tx2])1693test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1695temp_utxos.pop(0)1697# Update self.utxos for later tests by creating two outputs1698# that consolidate all the coins in temp_utxos.1699output_value = sum(i.nValue for i in temp_utxos) // 21701tx = CTransaction()1702index = 01703# Just spend to our usual anyone-can-spend output1704tx.vout = [CTxOut(output_value, CScript([OP_TRUE]))] * 21705for i in temp_utxos:1706# Use SIGHASH_ALL|SIGHASH_ANYONECANPAY so we can build up1707# the signatures as we go.1708tx.vin.append(CTxIn(COutPoint(i.sha256, i.n), b""))1709tx.wit.vtxinwit.append(CTxInWitness())1710sign_p2pk_witness_input(witness_script, tx, index, SIGHASH_ALL | SIGHASH_ANYONECANPAY, i.nValue, key)1711index += 11712block = self.build_next_block()1713self.update_witness_block_with_transactions(block, [tx])1714test_witness_block(self.nodes[0], self.test_node, block, accepted=True)1716for i in range(len(tx.vout)):1717self.utxo.append(UTXO(tx.txid_int, i, tx.vout[i].nValue))1719@subtest1720def test_non_standard_witness_blinding(self):1721"""Test behavior of unnecessary witnesses in transactions does not blind the node for the transaction"""1723# Create a p2sh output -- this is so we can pass the standardness1724# rules (an anyone-can-spend OP_TRUE would be rejected, if not wrapped1725# in P2SH).1726p2sh_program = CScript([OP_TRUE])1727script_pubkey = script_to_p2sh_script(p2sh_program)1729# Now check that unnecessary witnesses can't be used to blind a node1730# to a transaction, eg by violating standardness checks.1731tx = CTransaction()1732tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))1733tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, script_pubkey))1734test_transaction_acceptance(self.nodes[0], self.test_node, tx, False, True)1735self.generate(self.nodes[0], 1)1737# We'll add an unnecessary witness to this transaction that would cause1738# it to be non-standard, to test that violating policy with a witness1739# doesn't blind a node to a transaction. Transactions1740# rejected for having a witness shouldn't be added1741# to the rejection cache.1742tx2 = CTransaction()1743tx2.vin.append(CTxIn(COutPoint(tx.txid_int, 0), CScript([p2sh_program])))1744tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, script_pubkey))1745tx2.wit.vtxinwit.append(CTxInWitness())1746tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a' * 400]1747# This will be rejected due to a policy check:1748# No witness is allowed, since it is not a witness program but a p2sh program1749test_transaction_acceptance(self.nodes[1], self.std_node, tx2, True, False, 'bad-witness-nonstandard')
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.