Skip to content

Commit

Permalink
Merge pull request #82 from inflector/add-accounting
Browse files Browse the repository at this point in the history
Add accounting and replace ganache-cli for testrpc
  • Loading branch information
inflector authored Dec 5, 2017
2 parents d926ec9 + bbde464 commit ef0b61b
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 8 deletions.
25 changes: 25 additions & 0 deletions agent/sn_agent/accounting/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 14 additions & 0 deletions agent/sn_agent/accounting/accounting.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions agent/sn_agent/accounting/settings.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 14 additions & 5 deletions agent/sn_agent/api/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions agent/sn_agent/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions agent/sn_agent/job/job_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.abc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ services:
- "8545:8545"
- "8546:8546"
- "30303:30303"
command: testrpc
command: ganache-cli

relex:
image: opencog/relex
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ services:
- "8545:8545"
- "8546:8546"
- "30303:30303"
command: testrpc
command: ganache-cli

relex:
image: opencog/relex
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ services:
- "8545:8545"
- "8546:8546"
- "30303:30303"
command: testrpc
command: ganache-cli

0 comments on commit ef0b61b

Please sign in to comment.