diff --git a/agent/sn_agent/accounting/__init__.py b/agent/sn_agent/accounting/__init__.py new file mode 100644 index 0000000..5f74db6 --- /dev/null +++ b/agent/sn_agent/accounting/__init__.py @@ -0,0 +1,25 @@ +# +# sn_agent/accounting/__init__.py - manages AGI token payment processing, +# +# Copyright (c) 2017 SingularityNET +# +# Distributed under the MIT software license, see LICENSE file. +# + +from abc import ABC +from sn_agent.accounting.settings import AccountingSettings +from sn_agent.job.job_descriptor import JobDescriptor + +class Accounting(ABC): + def __init__(self, app): + self.app = app + self.settings = AccountingSettings() + + def job_is_contracted(self, job: JobDescriptor): + if not job is None: + return True + else: + return False + +def setup_accounting(app): + app['accounting'] = Accounting(app) diff --git a/agent/sn_agent/accounting/accounting.yml b/agent/sn_agent/accounting/accounting.yml new file mode 100644 index 0000000..b2d3ac5 --- /dev/null +++ b/agent/sn_agent/accounting/accounting.yml @@ -0,0 +1,14 @@ +# +# sn_agent/accounting/accounting.yml - configuration parameters for the accounting module, +# +# Copyright (c) 2017 SingularityNET +# +# Distributed under the MIT software license, see LICENSE file. +# + +accounting: + token_id: AGI + + # One COG is 1 / 10^8 of an AGI + sub_token_id: COG + sub_token_divisor: 8 diff --git a/agent/sn_agent/accounting/settings.py b/agent/sn_agent/accounting/settings.py new file mode 100644 index 0000000..22f2ef2 --- /dev/null +++ b/agent/sn_agent/accounting/settings.py @@ -0,0 +1,13 @@ +import os +from pathlib import Path + +from sn_agent import SettingsBase + +THIS_DIR = Path(__file__).parent + + +class AccountingSettings(SettingsBase): + def __init__(self, **custom_settings): + self._ENV_PREFIX = 'SN_ACCOUNTING_' + self.CONFIG_FILE = os.path.join(THIS_DIR, 'accounting.yml') + super().__init__(**custom_settings) diff --git a/agent/sn_agent/api/job.py b/agent/sn_agent/api/job.py index 23a53a1..7a0c8a2 100644 --- a/agent/sn_agent/api/job.py +++ b/agent/sn_agent/api/job.py @@ -22,13 +22,22 @@ async def perform_job(app, job_descriptor: JobDescriptor): logger.debug("perform_job: %s", job_descriptor) service_manager = app['service_manager'] + accounting = app['accounting'] - service_descriptor = job_descriptor.service + if accounting.job_is_contracted(job_descriptor): - service_adapter = service_manager.get_service_adapter_for_id(service_descriptor.ontology_node_id) + # Get the adapter for this job's service. + service_descriptor = job_descriptor.service + service_adapter = service_manager.get_service_adapter_for_id(service_descriptor.ontology_node_id) + if service_adapter is None: + raise Exception('Service not available') - if service_adapter is None: - raise Exception('Service not available') + results = service_adapter.perform(job_descriptor) + + else: + results = [{ + 'error': "Job {0} has no valid contract".format(job_descriptor.error_description()) + }] - return service_adapter.perform(job_descriptor) + return results diff --git a/agent/sn_agent/app.py b/agent/sn_agent/app.py index a48eab0..b8667c3 100644 --- a/agent/sn_agent/app.py +++ b/agent/sn_agent/app.py @@ -4,6 +4,7 @@ import uvloop from aiohttp import web +from sn_agent.accounting import setup_accounting from sn_agent.agent import setup_agent from sn_agent.api import setup_api from sn_agent.log import setup_logging @@ -31,6 +32,7 @@ def create_app(): setup_ontology(app) setup_network(app) setup_service_manager(app) + setup_accounting(app) setup_api(app) setup_agent(app) setup_routes(app) diff --git a/agent/sn_agent/job/job_descriptor.py b/agent/sn_agent/job/job_descriptor.py index 2e19cb4..d92ecba 100644 --- a/agent/sn_agent/job/job_descriptor.py +++ b/agent/sn_agent/job/job_descriptor.py @@ -50,6 +50,13 @@ def __setitem__(self, key, value): def __len__(self): return len(self.job_parameters) + def error_description(self): + if self.service is None: + description = "" + else: + description = self.service.name() + return 'Job for service %s' % (description) + def append_job_item(self, job_item: dict): self.job_parameters.append(job_item) diff --git a/docker-compose.abc.yml b/docker-compose.abc.yml index 3154da0..925aa71 100644 --- a/docker-compose.abc.yml +++ b/docker-compose.abc.yml @@ -56,7 +56,7 @@ services: - "8545:8545" - "8546:8546" - "30303:30303" - command: testrpc + command: ganache-cli relex: image: opencog/relex diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 6a9384a..e68a434 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -35,7 +35,7 @@ services: - "8545:8545" - "8546:8546" - "30303:30303" - command: testrpc + command: ganache-cli relex: image: opencog/relex diff --git a/docker-compose.yml b/docker-compose.yml index fcc0d32..4695b95 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,4 +24,4 @@ services: - "8545:8545" - "8546:8546" - "30303:30303" - command: testrpc + command: ganache-cli