Skip to content
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

[ECHOT-315] Update async version of echopy-lib to Echo node version 0.17 and current synchronous version #12

Merged
merged 4 commits into from
Mar 13, 2020
Merged
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: 3 additions & 1 deletion echopy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@

__all__ = [
"echo",
"transaction"
"transaction",
"Echo",
"Transaction"
]
25 changes: 25 additions & 0 deletions echopy/echo.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
from .echoapi import Api
from .transaction import Transaction
from .echobase.config import Config
from .echobase.utils import solve_registration_task
from .echobase.account import BrainKey


class Echo:

def __init__(self):
self.api = Api()
self.config = Config()
self.brain_key = BrainKey

def solve_registration_task(self, block_id, rand_num, difficulty):
return solve_registration_task(block_id, rand_num, difficulty)

async def register_account(self, callback, name, active, echorand):
if self.api.ws.connection is None:
raise AttributeError("Connection is needed: use 'connect' method for connecting to node")
task = self.api.registration.request_registration_task()
nonce = self.solve_registration_task(
task["block_id"],
task["rand_num"],
task["difficulty"]
)
return await self.api.registration.submit_registration_solution(
callback,
name,
active,
echorand,
nonce,
task["rand_num"]
)

def create_transaction(self):
return Transaction(self.api)
Expand Down
2 changes: 0 additions & 2 deletions echopy/echoapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from .ws_api.database_api import DatabaseApi
from .ws_api.history_api import HistoryApi
from .ws_api.registration_api import RegistrationApi
from .ws_api.network_node_api import NetworkNodeApi


class Api:
Expand All @@ -22,7 +21,6 @@ async def connect(self, url):
self.history = HistoryApi(self.ws.history_api)
self.registration = RegistrationApi(self.ws.registration_api)
self.login = LoginApi(self.ws.login_api)
self.network_node = NetworkNodeApi(self.ws.network_node_api)

async def disconnect(self):
await self.ws.disconnect()
Expand Down
35 changes: 27 additions & 8 deletions echopy/echoapi/ws/async_websocket.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-
import ssl
import json
import logging
from async_promises import Promise
from promise import Promise
import asyncio
import websockets
from websockets import ConnectionClosed
from .exceptions import RPCError, NumRetriesReached
from .echoapi import EchoApi, register_echo_api
from .exceptions import RPCError
from .echoapi import register_echo_api

log = logging.getLogger(__name__)

Expand All @@ -22,6 +21,20 @@ def __init__(self, *args, **kwargs):
self._loop = None
self.ws = None

@property
def debug(self):
return self._debug

@debug.setter
def debug(self, debug):
self._debug = debug
if debug is not False:
logging.basicConfig(
level=logging.DEBUG,
format='\n{}%(levelname)s:{} %(asctime)s - %(message)s'.format('\x1b[1;33m', '\x1b[0m'),
datefmt='%d-%b-%y %H:%M:%S'
)

def get_request_id(self, increment=True):
if increment:
self.increment_request_id()
Expand All @@ -33,10 +46,12 @@ def increment_request_id(self):
async def listen(self):
while True:
res = json.loads(await self.ws.recv())
self.promises[res['id']](res)
if 'id' in res:
self.promises[res['id']](res)

async def connect(self, url):
async def connect(self, url, debug=False):
self.url = url
self.debug = debug
log.debug("Trying to connect to node %s" % self.url)
self._request_id = 0
self._loop = asyncio.get_event_loop()
Expand Down Expand Up @@ -68,10 +83,11 @@ def parse_response(self, res):
raise RPCError(res["error"]["detail"])
else:
raise RPCError(res["error"]["message"])
elif "method" in res:
return res["method"]
else:
return res['result']


async def make_query(self, name, params, *args, **kwargs):
if self.url:
if not self.ws:
Expand All @@ -83,11 +99,13 @@ async def make_query(self, name, params, *args, **kwargs):

promise = Promise(self._save_promise)

_id = self.get_request_id(increment=False)

payload = {
'method': 'call',
'params': [api_id, name, params],
'jsonrpc': '2.0',
'id': self.get_request_id(increment=False)
'id': _id
}
while True:
try:
Expand All @@ -102,6 +120,7 @@ async def make_query(self, name, params, *args, **kwargs):
except ConnectionClosed:
await self.connect(self.url)

del self.promises[_id]
return res

async def register_apis(self):
Expand Down
4 changes: 2 additions & 2 deletions echopy/echoapi/ws/echoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ async def rpcexec(self, method, params):
return res


async def register_echo_api(ws, api_name, params=["", ""], api=1):
api_id = await ws.make_query(api_name, params, api=api)
async def register_echo_api(ws, api_name):
api_id = await ws.make_query(api_name, ["", ""] if api_name == 'login' else [], api=1)
return EchoApi(ws, api_name, api_id)
1 change: 0 additions & 1 deletion echopy/echoapi/ws_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
"history_api",
"login_api",
"network_api",
"network_node_api",
"registration_api"
]
15 changes: 12 additions & 3 deletions echopy/echoapi/ws_api/asset_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ def __init__(self, db):
self.db = db

async def get_asset_holders(self, asset_id, start, limit):
return await self.db.rpcexec('get_asset_holders', [asset_id, start, limit])
return await self.db.rpcexec(
'get_asset_holders',
[asset_id, start, limit]
)

async def get_asset_holders_count(self, asset_id):
return await self.db.rpcexec('get_asset_holders_count', [asset_id])
return await self.db.rpcexec(
'get_asset_holders_count',
[asset_id]
)

async def get_all_asset_holders(self):
return await self.db.rpcexec('get_all_asset_holders', [])
return await self.db.rpcexec(
'get_all_asset_holders',
[]
)
Loading