Skip to content

Refactor: Service config.json migrations #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions operate/services/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2205,9 +2205,7 @@ def refill_requirements( # pylint: disable=too-many-locals,too-many-statements,

agent_addresses = {key.address for key in service.keys}
service_safe = (
chain_data.multisig
if chain_data.multisig and chain_data.multisig != NON_EXISTENT_MULTISIG
else "service_safe"
chain_data.multisig if chain_data.multisig else "service_safe"
)

if not master_safe_exists:
Expand Down
42 changes: 24 additions & 18 deletions operate/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@
ALL_PARTICIPANTS = "all_participants"
CONSENSUS_THRESHOLD = "consensus_threshold"
DELETE_PREFIX = "delete_"
SERVICE_CONFIG_VERSION = 6
SERVICE_CONFIG_VERSION = 7
SERVICE_CONFIG_PREFIX = "sc-"

NON_EXISTENT_MULTISIG = "0xm"
NON_EXISTENT_MULTISIG = None
NON_EXISTENT_TOKEN = -1

DEFAULT_TRADER_ENV_VARS = {
Expand Down Expand Up @@ -935,25 +935,31 @@ def migrate_format(cls, path: Path) -> bool: # pylint: disable=too-many-stateme
new_chain_configs[chain] = chain_data # type: ignore
data["chain_configs"] = new_chain_configs

data["version"] = SERVICE_CONFIG_VERSION

# Redownload service path
if "service_path" in data:
package_absolute_path = path / Path(data["service_path"]).name
data.pop("service_path")
else:
package_absolute_path = path / data["package_path"]
if version < 6:
# Redownload service path
if "service_path" in data:
package_absolute_path = path / Path(data["service_path"]).name
data.pop("service_path")
else:
package_absolute_path = path / data["package_path"]

if package_absolute_path.exists() and package_absolute_path.is_dir():
shutil.rmtree(package_absolute_path)
if package_absolute_path.exists() and package_absolute_path.is_dir():
shutil.rmtree(package_absolute_path)

package_absolute_path = Path(
IPFSTool().download(
hash_id=data["hash"],
target_dir=path,
package_absolute_path = Path(
IPFSTool().download(
hash_id=data["hash"],
target_dir=path,
)
)
)
data["package_path"] = str(package_absolute_path.name)
data["package_path"] = str(package_absolute_path.name)

if version < 7:
for _, chain_data in data.get("chain_configs", {}).items():
if chain_data["chain_data"]["multisig"] == "0xm":
chain_data["chain_data"]["multisig"] = NON_EXISTENT_MULTISIG

data["version"] = SERVICE_CONFIG_VERSION

with open(path / Service._file, "w", encoding="utf-8") as file:
json.dump(data, file, indent=2)
Expand Down
56 changes: 54 additions & 2 deletions tests/test_services_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from deepdiff import DeepDiff

from operate.services.service import (
NON_EXISTENT_MULTISIG,
SERVICE_CONFIG_PREFIX,
SERVICE_CONFIG_VERSION,
Service,
Expand Down Expand Up @@ -55,7 +56,7 @@
"keys_address_0": "0x0000000000000000000000000000000000000001",
"keys_private_key_0": "0x0000000000000000000000000000000000000000000000000000000000000001",
"instance_0": "0x0000000000000000000000000000000000000001",
"multisig": "0x0000000000000000000000000000000000000020",
"multisig": "0xm",
"service_config_id": "sc-00000000-0000-0000-0000-000000000000",
"package_path": "trader_pearl",
}
Expand Down Expand Up @@ -332,7 +333,57 @@ def get_config_json_data_v6(**kwargs: t.Any) -> t.Dict[str, t.Any]:
}


get_expected_data = get_config_json_data_v6
def get_config_json_data_v7(**kwargs: t.Any) -> t.Dict[str, t.Any]:
"""get_config_json_data_v7"""

return {
"version": 7,
"service_config_id": kwargs.get("service_config_id"),
"hash": kwargs.get("hash"),
"hash_history": {kwargs.get("hash_timestamp"): kwargs.get("hash")},
"keys": [
{
"ledger": "ethereum",
"address": kwargs.get("keys_address_0"),
"private_key": kwargs.get("keys_private_key_0"),
}
],
"home_chain": "gnosis",
"chain_configs": {
"gnosis": {
"ledger_config": {"rpc": kwargs.get("rpc"), "chain": "gnosis"},
"chain_data": {
"instances": [kwargs.get("instance_0")],
"token": kwargs.get("token"),
"multisig": NON_EXISTENT_MULTISIG,
"staked": kwargs.get("staked"),
"on_chain_state": kwargs.get("on_chain_state"),
"user_params": {
"staking_program_id": kwargs.get("staking_program_id"),
"nft": kwargs.get("nft"),
"threshold": kwargs.get("threshold"),
"agent_id": kwargs.get("agent_id"),
"use_staking": kwargs.get("use_staking"),
"use_mech_marketplace": kwargs.get("use_mech_marketplace"),
"cost_of_bond": kwargs.get("cost_of_bond"),
"fund_requirements": {
"0x0000000000000000000000000000000000000000": {
"agent": kwargs.get("fund_requirements_agent"),
"safe": kwargs.get("fund_requirements_safe"),
}
},
},
},
}
},
"description": kwargs.get("description"),
"env_variables": {},
"package_path": kwargs.get("package_path"),
"name": kwargs.get("name"),
}


get_expected_data = get_config_json_data_v7


class TestService:
Expand All @@ -351,6 +402,7 @@ class TestService:
get_config_json_data_v3,
get_config_json_data_v4,
get_config_json_data_v5,
get_config_json_data_v6,
],
)
def test_service_migrate_format(
Expand Down