refactor: Several testing SDK improvements such as:

fix db reset, use latest wheel, print trace in case of failure and add logic to pull stateless contracts
This commit is contained in:
Florian Pellissier
2024-07-25 15:02:57 +02:00
parent 1ce64123b9
commit ec435d6094
6 changed files with 59 additions and 6 deletions

View File

@@ -7,7 +7,7 @@ WORKDIR /app
# Add current directory code to /app in container # Add current directory code to /app in container
ADD . /app/testing ADD . /app/testing
RUN chmod +x /app/testing/tycho-indexer RUN chmod +x /app/testing/tycho-indexer-linux-x64
# Create a new conda environment and install pip # Create a new conda environment and install pip
RUN conda create -n myenv pip python=3.9 RUN conda create -n myenv pip python=3.9

View File

@@ -6,6 +6,7 @@ from collections import defaultdict
from datetime import datetime from datetime import datetime
from decimal import Decimal from decimal import Decimal
from pathlib import Path from pathlib import Path
import traceback
import yaml import yaml
from pydantic import BaseModel from pydantic import BaseModel
@@ -164,7 +165,8 @@ class TestRunner:
return TestResult.Passed() return TestResult.Passed()
except Exception as e: except Exception as e:
return TestResult.Failed(str(e)) error_message = f"An error occurred: {str(e)}\n" + traceback.format_exc()
return TestResult.Failed(error_message)
def simulate_get_amount_out( def simulate_get_amount_out(
self, self,

View File

@@ -22,7 +22,7 @@ def get_wheel_file():
return str( return str(
path path
/ "wheels" / "wheels"
/ f"protosim_py-0.4.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl" / f"protosim_py-0.4.11-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl"
) )
else: else:
raise RuntimeError("Unsupported platform or architecture") raise RuntimeError("Unsupported platform or architecture")

View File

@@ -73,10 +73,21 @@ class ThirdPartyPoolTychoDecoder:
pool_id = attributes.get("pool_id") or component["id"] pool_id = attributes.get("pool_id") or component["id"]
balance_owner = attributes.get("balance_owner") balance_owner = attributes.get("balance_owner")
stateless_contracts = {} stateless_contracts = {}
static_attributes = snap["component"]["static_attributes"]
index = 0
while f"stateless_contract_addr_{index}" in static_attributes:
encoded_address = static_attributes[f"stateless_contract_addr_{index}"]
address = bytes.fromhex(encoded_address[2:] if encoded_address.startswith('0x') else encoded_address).decode('utf-8')
code = static_attributes.get(f"stateless_contract_code_{index}") or get_code_for_address(address)
stateless_contracts[address] = code
index += 1
index = 0 index = 0
while f"stateless_contract_addr_{index}" in attributes: while f"stateless_contract_addr_{index}" in attributes:
address = attributes[f"stateless_contract_addr_{index}"] address = attributes[f"stateless_contract_addr_{index}"]
code = attributes[f"stateless_contract_code_{index}"] code = attributes.get(f"stateless_contract_code_{index}") or get_code_for_address(address)
stateless_contracts[address] = code stateless_contracts[address] = code
index += 1 index += 1
return { return {

View File

@@ -11,6 +11,7 @@ import eth_abi
from eth_typing import HexStr from eth_typing import HexStr
from hexbytes import HexBytes from hexbytes import HexBytes
from protosim_py import SimulationEngine, AccountInfo from protosim_py import SimulationEngine, AccountInfo
import requests
from web3 import Web3 from web3 import Web3
from .constants import EXTERNAL_ACCOUNT, MAX_BALANCE, ASSETS_FOLDER from .constants import EXTERNAL_ACCOUNT, MAX_BALANCE, ASSETS_FOLDER
@@ -299,3 +300,42 @@ def maybe_coerce_error(
repr(pool_state), repr(pool_state),
) )
return err return err
def exec_rpc_method(url, method, params, timeout=240) -> dict:
payload = {"jsonrpc": "2.0", "method": method, "params": params, "id": 1}
headers = {"Content-Type": "application/json"}
r = requests.post(url, data=json.dumps(payload), headers=headers, timeout=timeout)
if r.status_code >= 400:
raise RuntimeError(
"RPC failed: status_code not ok. (method {}: {})".format(
method, r.status_code
)
)
data = r.json()
if "result" in data:
return data["result"]
elif "error" in data:
raise RuntimeError(
"RPC failed with Error {} - {}".format(data["error"], method)
)
def get_code_for_address(address: str, connection_string: str = None):
if connection_string is None:
connection_string = os.getenv("RPC_URL")
if connection_string is None:
raise EnvironmentError("RPC_URL environment variable is not set")
method = "eth_getCode"
params = [address, "latest"]
try:
code = exec_rpc_method(connection_string, method, params)
return bytes.fromhex(code[2:])
except RuntimeError as e:
print(f"Error fetching code for address {address}: {e}")
return None

View File

@@ -183,7 +183,7 @@ class TychoRunner:
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute( cursor.execute(
sql.SQL("DROP DATABASE IF EXISTS {}").format( sql.SQL("DROP DATABASE IF EXISTS {} WITH (FORCE)").format(
sql.Identifier("tycho_indexer_0") sql.Identifier("tycho_indexer_0")
) )
) )