From e40fb899420ded7a3fd8c86c90073626d66bc9a6 Mon Sep 17 00:00:00 2001 From: Artem Saprykin Date: Tue, 16 Apr 2019 18:03:44 +0300 Subject: [PATCH] REM-1296: Get list of the batches --- README.md | 23 +++++++++++++ cli/batch/__init__.py | 0 cli/batch/cli.py | 76 +++++++++++++++++++++++++++++++++++++++++ cli/batch/help.py | 8 +++++ cli/batch/interfaces.py | 21 ++++++++++++ cli/batch/service.py | 34 ++++++++++++++++++ cli/constants.py | 3 +- cli/entrypoint.py | 2 ++ 8 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 cli/batch/__init__.py create mode 100644 cli/batch/cli.py create mode 100644 cli/batch/help.py create mode 100644 cli/batch/interfaces.py create mode 100644 cli/batch/service.py diff --git a/README.md b/README.md index 6640345..c6df97a 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,29 @@ $ remme account get-balance \ 368440.0 ``` +### Batches + +Get batches — ``remme batch get-list``: + +| Arguments | Type | Required | Description | +| :-------: | :----: | :-------: | --------------------------------------------------- | +| id | String | No | Get batch by its identifier. | +| start | String | No | Get batches by start. | +| limit | Integer| No | Limit amount of batches. | +| head | String | No | Get batches by head. | +| reverse | String | No | Reverse result. | +| node-url | String | No | Apply the command to the specified node by its URL. | + +```bash +$ remme batch get-list \ + --id=ce5d2047f1a34bafbe228fac392573435b7048352b1f013af69e6bbf8fc10e5156915d7ee94f0d4a2fe363e7583f7039f29b1ab50d2e139e0957b5d08740bfd9 \ + --start=ce5d2047f1a34bafbe228fac392573435b7048352b1f013af69e6bbf8fc10e5156915d7ee94f0d4a2fe363e7583f7039f29b1ab50d2e139e0957b5d08740bfd9 \ + --limit=3 \ + --head=ce5d2047f1a34bafbe228fac392573435b7048352b1f013af69e6bbf8fc10e5156915d7ee94f0d4a2fe363e7583f7039f29b1ab50d2e139e0957b5d08740bfd9 \ + --reverse=false \ + --node-url=node-genesis-testnet.remme.io +``` + ## Development

Requirements

diff --git a/cli/batch/__init__.py b/cli/batch/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cli/batch/cli.py b/cli/batch/cli.py new file mode 100644 index 0000000..e245adc --- /dev/null +++ b/cli/batch/cli.py @@ -0,0 +1,76 @@ +""" +Provide implementation of the command line interface's batch commands. +""" +import sys +import re + +import asyncio +import click +from remme import Remme + +from cli.batch.help import ( + GET_BATCH_ID_ARGUMENT_HELP_MESSAGE, + GET_BATCH_START_ARGUMENT_HELP_MESSAGE, + GET_BATCH_LIMIT_ARGUMENT_HELP_MESSAGE, + GET_BATCH_HEAD_ARGUMENT_HELP_MESSAGE, + GET_BATCH_REVERSE_ARGUMENT_HELP_MESSAGE, +) +from cli.batch.service import Batch +from cli.constants import ( + BATCH_ID_REGEXP, + FAILED_EXIT_FROM_COMMAND, + NODE_URL_ARGUMENT_HELP_MESSAGE, +) + +loop = asyncio.get_event_loop() + + +@click.group('batch', chain=True) +def batch_commands(): + """ + Provide commands for working with batches. + """ + pass + + +@click.option('--id', type=str, required=False, help=GET_BATCH_ID_ARGUMENT_HELP_MESSAGE) +@click.option('--start', type=str, required=False, help=GET_BATCH_START_ARGUMENT_HELP_MESSAGE) +@click.option('--limit', type=int, required=False, help=GET_BATCH_LIMIT_ARGUMENT_HELP_MESSAGE) +@click.option('--head', type=str, required=False, help=GET_BATCH_HEAD_ARGUMENT_HELP_MESSAGE) +@click.option('--reverse', type=str, required=False, help=GET_BATCH_REVERSE_ARGUMENT_HELP_MESSAGE) +@click.option('--node-url', type=str, help=NODE_URL_ARGUMENT_HELP_MESSAGE) +@batch_commands.command('get-list') +def get_list(id, start, limit, head, reverse, node_url): + """ + List batches. + """ + for batch_id in (id, start, head): + if batch_id is not None and re.match(pattern=BATCH_ID_REGEXP, string=batch_id) is None: + click.echo('The following batch id `{batch_id}` is not valid.'.format(batch_id=id)) + sys.exit(FAILED_EXIT_FROM_COMMAND) + if limit is not None and limit < 0: + click.echo("Limit can't be negative.") + sys.exit(FAILED_EXIT_FROM_COMMAND) + if reverse is not None and reverse not in ("true", "false"): + click.echo("Invalid reverse field. Should be either 'true' or 'false'") + sys.exit(FAILED_EXIT_FROM_COMMAND) + + if node_url is None: + node_url = 'localhost' + + remme = Remme(private_key_hex=None, network_config={ + 'node_address': str(node_url) + ':8080', + }) + + query = { + 'id': id, + 'start': start, + 'limit': limit, + 'head': head, + 'reverse': reverse, + } + + batch_service = Batch(service=remme) + batches = loop.run_until_complete(batch_service.get_list(query)) + + click.echo(batches) diff --git a/cli/batch/help.py b/cli/batch/help.py new file mode 100644 index 0000000..edddd91 --- /dev/null +++ b/cli/batch/help.py @@ -0,0 +1,8 @@ +""" +Provide help messages for command line interface's batch commands. +""" +GET_BATCH_ID_ARGUMENT_HELP_MESSAGE = 'Get batch by identifier.' +GET_BATCH_START_ARGUMENT_HELP_MESSAGE = 'Get batch by start.' +GET_BATCH_LIMIT_ARGUMENT_HELP_MESSAGE = 'Limit amount of batches.' +GET_BATCH_HEAD_ARGUMENT_HELP_MESSAGE = 'Get batch by head.' +GET_BATCH_REVERSE_ARGUMENT_HELP_MESSAGE = 'Reverse result.' diff --git a/cli/batch/interfaces.py b/cli/batch/interfaces.py new file mode 100644 index 0000000..49af2b9 --- /dev/null +++ b/cli/batch/interfaces.py @@ -0,0 +1,21 @@ +""" +Provide implementation of the batch interfaces. +""" + + +class BatchInterface: + """ + Implements batch interface. + """ + + async def get_list(self, query=None): + """ + Get all batches from REMChain. + + Arguments: + query (dict, optional): dictionary with specific parameters + + Returns: + List of batches. + """ + pass diff --git a/cli/batch/service.py b/cli/batch/service.py new file mode 100644 index 0000000..d66a1e7 --- /dev/null +++ b/cli/batch/service.py @@ -0,0 +1,34 @@ +""" +Provide implementation of the batch service. +""" +from accessify import implements + +from cli.batch.interfaces import BatchInterface + + +@implements(BatchInterface) +class Batch: + """ + Implements batch interface. + """ + + def __init__(self, service): + """ + Constructor. + + Arguments: + service: object to interact with Remme core API. + """ + self.service = service + + async def get_list(self, query=None): + """ + Get all batches from REMChain. + + Arguments: + query (dict, optional): dictionary with specific parameters + + Returns: + List of batches. + """ + return await self.service.blockchain_info.get_batches(query=query) diff --git a/cli/constants.py b/cli/constants.py index 87013ee..feec119 100644 --- a/cli/constants.py +++ b/cli/constants.py @@ -2,9 +2,8 @@ Provide constants for command line interface. """ ADDRESS_REGEXP = '[0-9a-f]{70}' +BATCH_ID_REGEXP = '[0-9a-f]{128}' FAILED_EXIT_FROM_COMMAND = -1 NODE_URL_ARGUMENT_HELP_MESSAGE = 'Apply the command to the specified node by its URL.' - -CLI_CONFIG_FILE_NAME = 'remme-core-cli' diff --git a/cli/entrypoint.py b/cli/entrypoint.py index e45563e..eb76ddd 100644 --- a/cli/entrypoint.py +++ b/cli/entrypoint.py @@ -4,6 +4,7 @@ import click from cli.account.cli import account_commands +from cli.batch.cli import batch_commands @click.group() @@ -17,3 +18,4 @@ def cli(): cli.add_command(account_commands) +cli.add_command(batch_commands)