Skip to content

Commit

Permalink
[wip] Adding case gathering code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipp committed Oct 20, 2023
1 parent 04ea485 commit bff84a3
Showing 1 changed file with 40 additions and 57 deletions.
97 changes: 40 additions & 57 deletions tests/unitary/math/test_newton_D.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

0 comments on commit bff84a3

Please sign in to comment.