From bff84a326166a6a0e5c9b63cf19a8ac0b6d92d93 Mon Sep 17 00:00:00 2001 From: Filipp <> Date: Fri, 20 Oct 2023 18:59:34 +0300 Subject: [PATCH] [wip] Adding case gathering code. --- tests/unitary/math/test_newton_D.py | 97 ++++++++++++----------------- 1 file changed, 40 insertions(+), 57 deletions(-) diff --git a/tests/unitary/math/test_newton_D.py b/tests/unitary/math/test_newton_D.py index 950c79b6..2f00e727 100644 --- a/tests/unitary/math/test_newton_D.py +++ b/tests/unitary/math/test_newton_D.py @@ -55,7 +55,6 @@ def inv_target_decimal_n2(A, gamma, x, D): pytest.t_start = time.time() pytest.gas_original = 0 pytest.gas_new = 0 -failed_cases = [] @given( @@ -186,62 +185,46 @@ def _test_newton_D( pytest.positive_dy += 1 X[j] = y - try: - result_sim = math_unoptimized.newton_D(A, gamma, X) - except: - raise # this is a problem + case = ( + "{" + f"'ANN': {A}, 'D': {D}, 'xD': {xD}, 'yD': {yD}, 'GAMMA': {gamma}, 'j': {j}, 'btcScalePrice': {btcScalePrice}, 'ethScalePrice': {ethScalePrice}, 'mid_fee': {mid_fee}, 'out_fee': {out_fee}, 'fee_gamma': {fee_gamma}" + "},\n" + ) + result_sim = math_unoptimized.newton_D(A, gamma, X) + pytest.gas_original += ( + math_unoptimized._computation.get_gas_used() + ) try: - result_sim = math_unoptimized.newton_D(A, gamma, X) - pytest.gas_original += ( - math_unoptimized._computation.get_gas_used() + result_contract = math_optimized.newton_D(A, gamma, X, K0) + pytest.gas_new += ( + math_optimized._computation.get_gas_used() ) - try: - case = ( - "{" - f"'ANN': {A}, 'GAMMA': {gamma}, 'x': {X}, 'K0': {K0}" - "},\n" - ) - print(case) - - result_contract = math_optimized.newton_D(A, gamma, X, K0) - pytest.gas_new += ( - math_optimized._computation.get_gas_used() - ) - except BoaError as e: - # print(e) - case = ( - "{" - f"'ANN': {A}, 'D': {D}, 'xD': {xD}, 'yD': {yD}, 'GAMMA': {gamma}, 'j': {j}, 'btcScalePrice': {btcScalePrice}, 'ethScalePrice': {ethScalePrice}, 'mid_fee': {mid_fee}, 'out_fee': {out_fee}, 'fee_gamma': {fee_gamma}" - "},\n" - ) - print(case) - raise - - A_dec = Decimal(A) / 10000 / 4 - - def calculate_D_polynome(d): - d = Decimal(d) - return abs(inv_target_decimal_n2(A_dec, gamma, X, d)) - - # print(f"ANN={A}; GAMMA={gamma}; x={X}") - - # D0 = int(2 * (X[0]*X[1])**(Decimal(1)/2)) - # D0_new = int((10**18*4*X[0]*X[1]//K0)**(Decimal(1)/2)) - # print(math_unoptimized._computation.get_gas_used(), D0, calculate_D_polynome(D0)) - # print(math_optimized._computation.get_gas_used(), D0_new, calculate_D_polynome(D0_new)) - - try: - assert abs(result_sim - result_contract) <= max( - 10000, result_sim / 1e12 - ) - except AssertionError: - case = ( - "{" - f"'ANN': {A}, 'D': {D}, 'xD': {xD}, 'yD': {yD}, 'GAMMA': {gamma}, 'j': {j}, 'btcScalePrice': {btcScalePrice}, 'ethScalePrice': {ethScalePrice}, 'mid_fee': {mid_fee}, 'out_fee': {out_fee}, 'fee_gamma': {fee_gamma}" - "},\n" - ) - with open("newton_D_n=2_cases.txt", "a") as f: - f.write(case) - except: - raise + # except BoaError as e: + except Exception as e: + with open("log/newton_D_fail.txt", "a") as f: + f.write(case) + with open("log/newton_D_fail_trace.txt", "a") as f: + f.write(str(e)) + return + + A_dec = Decimal(A) / 10000 / 4 + + def calculate_D_polynome(d): + d = Decimal(d) + return abs(inv_target_decimal_n2(A_dec, gamma, X, d)) + + # print(f"ANN={A}; GAMMA={gamma}; x={X}") + + # D0 = int(2 * (X[0]*X[1])**(Decimal(1)/2)) + # D0_new = int((10**18*4*X[0]*X[1]//K0)**(Decimal(1)/2)) + # print(math_unoptimized._computation.get_gas_used(), D0, calculate_D_polynome(D0)) + # print(math_optimized._computation.get_gas_used(), D0_new, calculate_D_polynome(D0_new)) + + assert abs(result_sim - result_contract) <= max( + 10000, result_sim / 1e12 + ) + + with open("log/newton_D_pass.txt", "a") as f: + f.write(case) +