Merge bitcoin/bitcoin#29500: test: create assert_not_equal util

7bb83f6718 test: create assert_not_equal util and add to where imports are needed (kevkevin)

Pull request description:

  In the functional tests there are lots of cases where we assert != which we now swap with assert_not_equal to be more readable

  This is motivated/uses logic from this PR which was closed https://github.com/bitcoin/bitcoin/pull/28528
  This partially helps https://github.com/bitcoin/bitcoin/issues/23119

  I've broken it up to just `assert_not_equal` to keep the PR smaller as suggested in https://github.com/bitcoin/bitcoin/pull/28528#issuecomment-1959945805

  I can create follow up PR's if this is wanted

ACKs for top commit:
  hodlinator:
    re-ACK 7bb83f6718
  ryanofsky:
    Code review ACK 7bb83f6718. Only change since last review is fixing error message formatting and passing it as a keyword argument
  janb84:
    Re-ACK [7bb83f6](7bb83f6718)

Tree-SHA512: de09f41a690033a5b61e6f861d3bd69a32b889d6655a28fbc0d5cfac9f7ec9c642432967d33913970882b4cfdd47bdd377d0ddc44e25976cbaa49f7f9d8f7b10
This commit is contained in:
merge-script
2025-04-01 13:52:41 -04:00
41 changed files with 137 additions and 83 deletions

View File

@ -17,6 +17,7 @@ Exports:
import unittest
from hashlib import sha256
from test_framework.util import assert_not_equal
class FE:
"""Objects of this class represent elements of the field GF(2**256 - 2**32 - 977).
@ -40,7 +41,7 @@ class FE:
num = (num * b._den) % FE.SIZE
else:
den = (den * b) % FE.SIZE
assert den != 0
assert_not_equal(den, 0)
if num == 0:
den = 1
self._num = num

View File

@ -14,7 +14,7 @@ import random
import unittest
from test_framework.crypto import secp256k1
from test_framework.util import random_bitflip
from test_framework.util import assert_not_equal, random_bitflip
# Point with no known discrete log.
H_POINT = "50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"
@ -283,7 +283,7 @@ def sign_schnorr(key, msg, aux=None, flip_p=False, flip_r=False):
sec = ORDER - sec
t = (sec ^ int.from_bytes(TaggedHash("BIP0340/aux", aux), 'big')).to_bytes(32, 'big')
kp = int.from_bytes(TaggedHash("BIP0340/nonce", t + P.to_bytes_xonly() + msg), 'big') % ORDER
assert kp != 0
assert_not_equal(kp, 0)
R = kp * secp256k1.G
k = kp if R.y.is_even() != flip_r else ORDER - kp
e = int.from_bytes(TaggedHash("BIP0340/challenge", R.to_bytes_xonly() + P.to_bytes_xonly() + msg), 'big') % ORDER

View File

@ -77,6 +77,7 @@ from test_framework.messages import (
sha256,
)
from test_framework.util import (
assert_not_equal,
MAX_NODES,
p2p_port,
wait_until_helper_internal,
@ -897,7 +898,7 @@ class P2PDataStore(P2PInterface):
if success:
self.wait_until(lambda: node.getbestblockhash() == blocks[-1].hash, timeout=timeout)
else:
assert node.getbestblockhash() != blocks[-1].hash
assert_not_equal(node.getbestblockhash(), blocks[-1].hash)
def send_txs_and_test(self, txs, node, *, success=True, expect_disconnect=False, reject_reason=None):
"""Send txs to test node and test whether they're accepted to the mempool.

View File

@ -33,6 +33,7 @@ from .p2p import P2P_SERVICES, P2P_SUBVERSION
from .util import (
MAX_NODES,
assert_equal,
assert_not_equal,
append_config,
delete_cookie_file,
get_auth_cookie,
@ -668,7 +669,7 @@ class TestNode():
self.start(extra_args, stdout=log_stdout, stderr=log_stderr, *args, **kwargs)
ret = self.process.wait(timeout=self.rpc_timeout)
self.log.debug(self._node_msg(f'bitcoind exited with status {ret} during initialization'))
assert ret != 0 # Exit code must indicate failure
assert_not_equal(ret, 0) # Exit code must indicate failure
self.running = False
self.process = None
# Check stderr for expected message

View File

@ -76,6 +76,10 @@ def assert_equal(thing1, thing2, *args):
if thing1 != thing2 or any(thing1 != arg for arg in args):
raise AssertionError("not(%s)" % " == ".join(str(arg) for arg in (thing1, thing2) + args))
def assert_not_equal(thing1, thing2, *, error_message=""):
if thing1 == thing2:
raise AssertionError(f"Both values are {thing1}{f', {error_message}' if error_message else ''}")
def assert_greater_than(thing1, thing2):
if thing1 <= thing2: