Skip to content

Commit

Permalink
Switch ephemeral anchors to OP_TRUE
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Feb 2, 2023
1 parent 9680cbc commit bf7676d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const

bool CScript::IsTrue() const
{
return (this->size() == 1 && (*this)[0] == OP_2);
return (this->size() == 1 && (*this)[0] == OP_TRUE);
}

bool CScript::IsPayToScriptHash() const
Expand Down
2 changes: 1 addition & 1 deletion src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ class CScript : public CScriptBase
unsigned int GetSigOpCount(const CScript& scriptSig) const;

/**
* OP_2, which is truth-y
* OP_TRUE
*/
bool IsTrue() const;

Expand Down
2 changes: 1 addition & 1 deletion src/test/transaction_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
CheckIsStandard(t);
g_dust = CFeeRate{DUST_RELAY_TX_FEE};

t.vout[0].scriptPubKey = CScript() << OP_1;
t.vout[0].scriptPubKey = CScript() << OP_2;
CheckIsNotStandard(t, "scriptpubkey");

// MAX_OP_RETURN_RELAY-byte TxoutType::NULL_DATA (standard)
Expand Down
2 changes: 1 addition & 1 deletion src/test/txvalidation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, int3
mtx.vin[i].prevout = inputs[i];
}
for (auto i{0}; i < 25; ++i) {
mtx.vout[i].scriptPubKey = CScript() << OP_TRUE;
mtx.vout[i].scriptPubKey = CScript() << OP_2;
mtx.vout[i].nValue = 10000;
}
return MakeTransactionRef(mtx);
Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_cltv.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_cltv_info(self, *, is_active):

def run_test(self):
peer = self.nodes[0].add_p2p_connection(P2PInterface())
wallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_OP_TRUE)
wallet = MiniWallet(self.nodes[0], mode=MiniWalletMode.RAW_OP_2)

self.test_cltv_info(is_active=False)

Expand Down
10 changes: 5 additions & 5 deletions test/functional/mempool_ephemeral_anchor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
from test_framework.script import (
CScript,
OP_2,
OP_TRUE,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
Expand Down Expand Up @@ -79,7 +79,7 @@ def create_simple_package(self, parent_coin, parent_fee=0, child_fee=DEFAULT_FEE
"""

if additional_outputs is None:
additional_outputs=[CTxOut(0, CScript([OP_2]))]
additional_outputs=[CTxOut(0, CScript([OP_TRUE]))]

child_inputs = []
self.ctr += 1
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_multianchor(self):
parent_coin = self.coins[-1]
del self.coins[-1]

package_hex0, package_txns0 = self.create_simple_package(parent_coin=parent_coin, parent_fee=0, child_fee=DEFAULT_FEE, version=3, additional_outputs=[CTxOut(0, CScript([OP_2]))] * 2)
package_hex0, package_txns0 = self.create_simple_package(parent_coin=parent_coin, parent_fee=0, child_fee=DEFAULT_FEE, version=3, additional_outputs=[CTxOut(0, CScript([OP_TRUE]))] * 2)
assert_raises_rpc_error(-26, "too-many-ephemeral-anchors", node.submitpackage, package_hex0)
assert_equal(node.getrawmempool(), [])

Expand All @@ -201,7 +201,7 @@ def inner_test_anchor_value(output_value):
parent_coin = self.coins[-1]
del self.coins[-1]

package_hex0, package_txns0 = self.create_simple_package(parent_coin=parent_coin, parent_fee=0, child_fee=DEFAULT_FEE, version=3, additional_outputs=[CTxOut(output_value, CScript([OP_2]))])
package_hex0, package_txns0 = self.create_simple_package(parent_coin=parent_coin, parent_fee=0, child_fee=DEFAULT_FEE, version=3, additional_outputs=[CTxOut(output_value, CScript([OP_TRUE]))])
node.submitpackage(package_hex0)
self.assert_mempool_contents(expected=package_txns0, unexpected=[])

Expand Down Expand Up @@ -261,7 +261,7 @@ def test_xor_rbf(self):
version=3
)

self.insert_additional_outputs(parent_result, [CTxOut(0, CScript([OP_2]))])
self.insert_additional_outputs(parent_result, [CTxOut(0, CScript([OP_TRUE]))])

child_inputs.append(parent_result["new_utxo"])
child_inputs.append({**parent_result["new_utxo"], 'vout': 1, 'value': 0, 'anchor': True})
Expand Down
35 changes: 18 additions & 17 deletions test/functional/p2p_segwit.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def run_test(self):

assert self.test_node.nServices & NODE_WITNESS != 0

# Keep a place to store utxo's that can be used in later tests
# Keep a place to store bare, non-standard utxo's that can be used in later tests
self.utxo = []

self.log.info("Starting tests before segwit activation")
Expand Down Expand Up @@ -330,7 +330,7 @@ def test_unnecessary_witness_before_segwit_activation(self):

tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, CScript([OP_TRUE])))
tx.vout.append(CTxOut(self.utxo[0].nValue - 1000, CScript([OP_2])))
tx.wit.vtxinwit.append(CTxInWitness())
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)])]

Expand Down Expand Up @@ -470,7 +470,7 @@ def test_v0_outputs_arent_spendable(self):
tx = CTransaction()
tx.vin = [CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b'')]
tx.vout = [CTxOut(value, script_pubkey), CTxOut(value, p2sh_script_pubkey)]
tx.vout.append(CTxOut(value, CScript([OP_TRUE])))
tx.vout.append(CTxOut(value, CScript([OP_2])))
tx.rehash()
txid = tx.sha256

Expand All @@ -486,14 +486,14 @@ def test_v0_outputs_arent_spendable(self):
# Now try to spend the outputs. This should fail since SCRIPT_VERIFY_WITNESS is always enabled.
p2wsh_tx = CTransaction()
p2wsh_tx.vin = [CTxIn(COutPoint(txid, 0), b'')]
p2wsh_tx.vout = [CTxOut(value, CScript([OP_TRUE]))]
p2wsh_tx.vout = [CTxOut(value, CScript([OP_2]))]
p2wsh_tx.wit.vtxinwit.append(CTxInWitness())
p2wsh_tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]
p2wsh_tx.rehash()

p2sh_p2wsh_tx = CTransaction()
p2sh_p2wsh_tx.vin = [CTxIn(COutPoint(txid, 1), CScript([script_pubkey]))]
p2sh_p2wsh_tx.vout = [CTxOut(value, CScript([OP_TRUE]))]
p2sh_p2wsh_tx.vout = [CTxOut(value, CScript([OP_2]))]
p2sh_p2wsh_tx.wit.vtxinwit.append(CTxInWitness())
p2sh_p2wsh_tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]
p2sh_p2wsh_tx.rehash()
Expand Down Expand Up @@ -699,7 +699,7 @@ def test_p2sh_witness(self):
# Now test attempts to spend the output.
spend_tx = CTransaction()
spend_tx.vin.append(CTxIn(COutPoint(tx.sha256, 0), script_sig))
spend_tx.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE])))
spend_tx.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_2])))
spend_tx.rehash()

# This transaction should not be accepted into the mempool pre- or
Expand Down Expand Up @@ -902,7 +902,7 @@ def test_witness_block_size(self):
child_tx = CTransaction()
for i in range(NUM_OUTPUTS):
child_tx.vin.append(CTxIn(COutPoint(parent_tx.sha256, i), b""))
child_tx.vout = [CTxOut(value - 100000, CScript([OP_TRUE]))]
child_tx.vout = [CTxOut(value - 100000, CScript([OP_2]))]
for _ in range(NUM_OUTPUTS):
child_tx.wit.vtxinwit.append(CTxInWitness())
child_tx.wit.vtxinwit[-1].scriptWitness.stack = [b'a' * 195] * (2 * NUM_DROPS) + [witness_script]
Expand Down Expand Up @@ -991,7 +991,7 @@ def test_extra_witness_data(self):
tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
tx.vout.append(CTxOut(self.utxo[0].nValue - 2000, script_pubkey))
tx.vout.append(CTxOut(1000, CScript([OP_TRUE]))) # non-witness output
tx.vout.append(CTxOut(1000, CScript([OP_2]))) # non-witness output
tx.wit.vtxinwit.append(CTxInWitness())
tx.wit.vtxinwit[0].scriptWitness.stack = [CScript([])]
tx.rehash()
Expand All @@ -1015,7 +1015,7 @@ def test_extra_witness_data(self):
tx2 = CTransaction()
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b"")) # witness output
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 1), b"")) # non-witness
tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_2])))
tx2.wit.vtxinwit.extend([CTxInWitness(), CTxInWitness()])
tx2.wit.vtxinwit[0].scriptWitness.stack = [CScript([CScriptNum(1)]), CScript([CScriptNum(1)]), witness_script]
tx2.wit.vtxinwit[1].scriptWitness.stack = [CScript([OP_TRUE])]
Expand Down Expand Up @@ -1069,7 +1069,7 @@ def test_max_witness_push_length(self):

tx2 = CTransaction()
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE])))
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_2])))
tx2.wit.vtxinwit.append(CTxInWitness())
# First try a 521-byte stack element
tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a' * (MAX_SCRIPT_ELEMENT_SIZE + 1), witness_script]
Expand Down Expand Up @@ -1110,7 +1110,7 @@ def test_max_witness_script_length(self):

tx2 = CTransaction()
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_TRUE])))
tx2.vout.append(CTxOut(tx.vout[0].nValue - 1000, CScript([OP_2])))
tx2.wit.vtxinwit.append(CTxInWitness())
tx2.wit.vtxinwit[0].scriptWitness.stack = [b'a'] * 44 + [long_witness_script]
tx2.rehash()
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def serialize_with_witness(self):
tx2 = BrokenCTransaction()
for i in range(10):
tx2.vin.append(CTxIn(COutPoint(tx.sha256, i), b""))
tx2.vout.append(CTxOut(value - 3000, CScript([OP_TRUE])))
tx2.vout.append(CTxOut(value - 3000, CScript([OP_2])))

# First try using a too long vtxinwit
for i in range(11):
Expand Down Expand Up @@ -1327,7 +1327,7 @@ def test_segwit_versions(self):
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
split_value = (self.utxo[0].nValue - 4000) // NUM_SEGWIT_VERSIONS
for _ in range(NUM_SEGWIT_VERSIONS):
tx.vout.append(CTxOut(split_value, CScript([OP_TRUE])))
tx.vout.append(CTxOut(split_value, CScript([2])))
tx.rehash()
block = self.build_next_block()
self.update_witness_block_with_transactions(block, [tx])
Expand All @@ -1352,6 +1352,7 @@ def test_segwit_versions(self):
tx.vin = [CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b"")]
tx.vout = [CTxOut(self.utxo[0].nValue - 1000, script_pubkey)]
tx.rehash()
# This will fail since all utxos currently in self.utxo are non-standard bare scripts
test_transaction_acceptance(self.nodes[1], self.std_node, tx, with_witness=True, accepted=False)
test_transaction_acceptance(self.nodes[0], self.test_node, tx, with_witness=True, accepted=True)
self.utxo.pop(0)
Expand Down Expand Up @@ -1530,7 +1531,7 @@ def test_uncompressed_pubkey(self):
# transactions.
tx5 = CTransaction()
tx5.vin.append(CTxIn(COutPoint(tx4.sha256, 0), b""))
tx5.vout.append(CTxOut(tx4.vout[0].nValue - 1000, CScript([OP_TRUE])))
tx5.vout.append(CTxOut(tx4.vout[0].nValue - 1000, CScript([OP_2])))
(sig_hash, err) = LegacySignatureHash(script_pubkey, tx5, 0, SIGHASH_ALL)
signature = key.sign_ecdsa(sig_hash) + b'\x01' # 0x1 is SIGHASH_ALL
tx5.vin[0].scriptSig = CScript([signature, pubkey])
Expand Down Expand Up @@ -1681,7 +1682,7 @@ def test_signature_version_1(self):
sign_p2pk_witness_input(witness_script, tx, 0, SIGHASH_ALL, temp_utxos[0].nValue, key)
tx2 = CTransaction()
tx2.vin.append(CTxIn(COutPoint(tx.sha256, 0), b""))
tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_TRUE])))
tx2.vout.append(CTxOut(tx.vout[0].nValue, CScript([OP_2])))

script = keyhash_to_p2pkh_script(pubkeyhash)
sig_hash = SegwitV0SignatureHash(script, tx2, 0, SIGHASH_ALL, tx.vout[0].nValue)
Expand Down Expand Up @@ -1713,7 +1714,7 @@ def test_signature_version_1(self):
tx = CTransaction()
index = 0
# Just spend to our usual anyone-can-spend output
tx.vout = [CTxOut(output_value, CScript([OP_TRUE]))] * 2
tx.vout = [CTxOut(output_value, CScript([OP_2]))] * 2
for i in temp_utxos:
# Use SIGHASH_ALL|SIGHASH_ANYONECANPAY so we can build up
# the signatures as we go.
Expand Down Expand Up @@ -1930,7 +1931,7 @@ def test_witness_sigops(self):
tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_script]
total_value += tx.vout[i].nValue
tx2.wit.vtxinwit[-1].scriptWitness.stack = [witness_script_toomany]
tx2.vout.append(CTxOut(total_value, CScript([OP_TRUE])))
tx2.vout.append(CTxOut(total_value, CScript([OP_2])))
tx2.rehash()

block_2 = self.build_next_block()
Expand Down
15 changes: 8 additions & 7 deletions test/functional/test_framework/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
CScript,
LegacySignatureHash,
LEAF_VERSION_TAPSCRIPT,
OP_2,
OP_NOP,
OP_RETURN,
OP_TRUE,
Expand Down Expand Up @@ -67,18 +68,18 @@ class MiniWalletMode(Enum):
witness stack of OP_TRUE, i.e. following an anyone-can-spend policy.
However, if the transactions need to be modified by the user (e.g. prepending
scriptSig for testing opcodes that are activated by a soft-fork), or the txs
should contain an actual signature, the raw modes RAW_OP_TRUE and RAW_P2PK
should contain an actual signature, the raw modes RAW_OP_2 and RAW_P2PK
can be useful. Summary of modes:
| output | | tx is | can modify | needs
mode | description | address | standard | scriptSig | signing
----------------+-------------------+-----------+----------+------------+----------
ADDRESS_OP_TRUE | anyone-can-spend | bech32m | yes | no | no
RAW_OP_TRUE | anyone-can-spend | - (raw) | no | yes | no
RAW_OP_2 | anyone-can-spend | - (raw) | no | yes | no
RAW_P2PK | pay-to-public-key | - (raw) | yes | yes | yes
"""
ADDRESS_OP_TRUE = 1
RAW_OP_TRUE = 2
RAW_OP_2 = 2
RAW_P2PK = 3


Expand All @@ -89,8 +90,8 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
self._mode = mode

assert isinstance(mode, MiniWalletMode)
if mode == MiniWalletMode.RAW_OP_TRUE:
self._scriptPubKey = bytes(CScript([OP_TRUE]))
if mode == MiniWalletMode.RAW_OP_2:
self._scriptPubKey = bytes(CScript([OP_2]))
elif mode == MiniWalletMode.RAW_P2PK:
# use simple deterministic private key (k=1)
self._priv_key = ECKey()
Expand Down Expand Up @@ -179,7 +180,7 @@ def sign_tx(self, tx, fixed_length=True):
break
tx.vin[0].scriptSig = CScript([der_sig + bytes(bytearray([SIGHASH_ALL]))])
tx.rehash()
elif self._mode == MiniWalletMode.RAW_OP_TRUE:
elif self._mode == MiniWalletMode.RAW_OP_2:
for i in range(len(tx.vin)):
tx.vin[i].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
Expand Down Expand Up @@ -333,7 +334,7 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), fee=Decimal("0"), u
assert fee_rate >= 0
assert fee >= 0
# calculate fee
if self._mode in (MiniWalletMode.RAW_OP_TRUE, MiniWalletMode.ADDRESS_OP_TRUE):
if self._mode in (MiniWalletMode.RAW_OP_2, MiniWalletMode.ADDRESS_OP_TRUE):
vsize = Decimal(104) # anyone-can-spend
elif self._mode == MiniWalletMode.RAW_P2PK:
vsize = Decimal(168) # P2PK (73 bytes scriptSig + 35 bytes scriptPubKey + 60 bytes other)
Expand Down

0 comments on commit bf7676d

Please sign in to comment.