feat: SDK improvements

Add a way to pull stateless contracts code from node, add more settings to test_assets.yaml, add logic to allow dynamic stateless contract by calling another contract
This commit is contained in:
Florian Pellissier
2024-07-30 12:18:45 +02:00
committed by kayibal
parent 3fab5d6ea7
commit a6cff51bf6
6 changed files with 77 additions and 22 deletions

View File

@@ -61,6 +61,7 @@ class TestRunner:
"""Run all tests specified in the configuration."""
print(f"Running tests ...")
for test in self.config["tests"]:
self.tycho_runner.empty_database(self.db_url)
spkg_path = self.build_spkg(
os.path.join(self.spkg_src, self.config["substreams_yaml_path"]),
@@ -71,6 +72,7 @@ class TestRunner:
test["start_block"],
test["stop_block"],
self.config["protocol_type_names"],
test.get("initialized_accounts", []),
)
result = self.tycho_runner.run_with_rpc_server(
@@ -83,7 +85,6 @@ class TestRunner:
else:
print(f"❗️ {test['name']} failed: {result.message}")
self.tycho_runner.empty_database(self.db_url)
def validate_state(self, expected_state: dict, stop_block: int) -> TestResult:
"""Validate the current protocol state against the expected state."""
@@ -104,6 +105,8 @@ class TestRunner:
component = components[comp_id]
for key, value in expected_component.items():
if key not in ["tokens", "static_attributes", "creation_tx"]:
continue
if key not in component:
return TestResult.Failed(
f"Missing '{key}' in component '{comp_id}'."
@@ -148,10 +151,13 @@ class TestRunner:
f"from rpc call and {tycho_balance} from Substreams"
)
contract_states = self.tycho_rpc_client.get_contract_state()
filtered_components = {'protocol_components': [pc for pc in protocol_components["protocol_components"] if
pc["id"] in [c["id"].lower() for c in
expected_state["protocol_components"] if c["skip_simulation"] is False]]}
simulation_failures = self.simulate_get_amount_out(
stop_block,
protocol_states,
protocol_components,
filtered_components,
contract_states,
)
if len(simulation_failures):

View File

@@ -83,12 +83,15 @@ class TychoRunner:
start_block: int,
end_block: int,
protocol_type_names: list,
initialized_accounts: list,
) -> None:
"""Run the Tycho indexer with the specified SPKG and block range."""
env = os.environ.copy()
env["RUST_LOG"] = "tycho_indexer=info"
all_accounts = self._initialized_accounts + initialized_accounts
try:
process = subprocess.Popen(
[
@@ -106,16 +109,14 @@ class TychoRunner:
str(start_block),
"--stop-block",
# +2 is to make up for the cache in the index side.
str(end_block + 2),
"--initialized-accounts",
",".join(self._initialized_accounts)
],
str(end_block + 2)
] + (["--initialized-accounts", ",".join(all_accounts)] if all_accounts else []),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1,
env=env,
)
)
with process.stdout:
for line in iter(process.stdout.readline, ""):
@@ -203,7 +204,7 @@ class TychoRunner:
def empty_database(db_url: str) -> None:
"""Drop and recreate the Tycho indexer database."""
try:
conn = psycopg2.connect(db_url)
conn = psycopg2.connect(db_url[:db_url.rfind('/')])
conn.autocommit = True
cursor = conn.cursor()