test: create assert_not_equal util and add to where imports are needed

In the functional tests there are lots of cases where we assert != which
this new util will replace, we also are adding the imports and the new assertion
This commit is contained in:
kevkevin
2024-02-27 19:41:12 -06:00
committed by kevkevinpal
parent 930b237f16
commit 7bb83f6718
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: