Skip to content

Commit

Permalink
REM-1296: Get list of the batches
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Saprykin committed Apr 18, 2019
1 parent 6a312f8 commit d721d21
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 0 deletions.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [Configuration file](#configuration-file)
* [Service](#service)
* [Account](#account)
* [Batch](#batch)
* [Development](#development)
* [Requirements](#development-requirements)
* [Docker](#docker)
Expand Down Expand Up @@ -104,6 +105,74 @@ $ remme account get-balance \
368440.0
```

### Batch

Get a list of batches — ``remme batch get-list``:

| Arguments | Type | Required | Description |
| :-------: | :----: | :-------: | --------------------------------------------------- |
| id | String | No | Identifier to get a list of batches by. |
| start | String | No | Parameter to list batches starting from. |
| limit | Integer| No | Parameter to limit amount of batches. |
| head | String | No | Block identifier to get a list of batches from. |
| reverse | String | No | Parameter to reverse result. |
| node-url | String | No | Node URL to apply the command to. |

```bash
$ remme batch get-list \
--id=ce5d2047f1a34bafbe228fac392573435b7048352b1f013af69e6bbf8fc10e5156915d7ee94f0d4a2fe363e7583f7039f29b1ab50d2e139e0957b5d08740bfd9 \
--start=ce5d2047f1a34bafbe228fac392573435b7048352b1f013af69e6bbf8fc10e5156915d7ee94f0d4a2fe363e7583f7039f29b1ab50d2e139e0957b5d08740bfd9 \
--limit=1 \
--head=ce5d2047f1a34bafbe228fac392573435b7048352b1f013af69e6bbf8fc10e5156915d7ee94f0d4a2fe363e7583f7039f29b1ab50d2e139e0957b5d08740bfd9 \
--reverse=false \
--node-url=node-genesis-testnet.remme.io
{
"head": "2eeebae97b8a813ca64a76717a44de9fdd787342ed122e9e9399570be35ba354279639ad2a5848b136891bcdac343e2628938e63dbf9dc0f3d2a0e63cc6cb51f",
"paging": {
"limit": 1,
"start": null,
"next": "4da4185d8c69ade0d1e85a8c260554e8d14f072af9f0629a14b840c52abc92142617024977765abd240735b7bca4b1c44ce1034e964e11083c7405c7adcc6448"
},
"data": [
{
"header": {
"signer_public_key": "03d425d2d17b64e3ef8fee028089a567fbb05bd556f98c0b6fb62bc5750ea62b8f",
"transaction_ids": [
"5a84ff8747e16d15a988a8b13134d24981a6b516bb41042e6ea95c47f6c9429c1c6fdf787ca2ea7fb8725b2bc2d0cd6aa3836aadfe85354deb714e048d41b4d7"
]
},
"header_signature": "57692f2bcc9be7fe2b59c052d5938eb92bd7be8a36487c1c7efc2c5758bf108e232892987e898071e5ea13b4cbe283e96ac45d8f63cd9065522df7b85b050977",
"transactions": [
{
"header": {
"batcher_public_key": "03d425d2d17b64e3ef8fee028089a567fbb05bd556f98c0b6fb62bc5750ea62b8f",
"family_name": "sawtooth_settings",
"family_version": "1.0",
"inputs": [
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c1c0cbf0fbcaf64c0b",
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c12840f169a04216b7",
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c1918142591ba4e8a7",
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c12840f169a04216b7"
],
"outputs": [
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c1c0cbf0fbcaf64c0b",
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c12840f169a04216b7"
],
"payload_sha512": "ed160d8590575eba6c657ff0a8d87dca296be4241ade26de39d8e402be4d9f97a2cf2dd6de0d49a9fed6cd985523f283d7edd39e1913e3d3fa4c05924646bc31",
"signer_public_key": "03d425d2d17b64e3ef8fee028089a567fbb05bd556f98c0b6fb62bc5750ea62b8f",
"dependencies": [],
"nonce": ""
},
"header_signature": "5a84ff8747e16d15a988a8b13134d24981a6b516bb41042e6ea95c47f6c9429c1c6fdf787ca2ea7fb8725b2bc2d0cd6aa3836aadfe85354deb714e048d41b4d7",
"payload": "CAESgAEKJnNhd3Rvb3RoLnNldHRpbmdzLnZvdGUuYXV0aG9yaXplZF9rZXlzEkIwM2Q0MjVkMmQxN2I2NGUzZWY4ZmVlMDI4MDg5YTU2N2ZiYjA1YmQ1NTZmOThjMGI2ZmI2MmJjNTc1MGVhNjJiOGYaEjB4ZDU0NzJhOTY1NWJkYTNmNg=="
}
],
"trace": false
}
]
}
```

## Development

<h3 id="development-requirements">Requirements</h4>
Expand Down
Empty file added cli/batch/__init__.py
Empty file.
83 changes: 83 additions & 0 deletions cli/batch/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Provide implementation of the command line interface's batch commands.
"""
import asyncio
import re
import sys

import click
from remme import Remme

from cli.batch.help import (
BATCH_HEAD_ARGUMENT_HELP_MESSAGE,
BATCH_ID_ARGUMENT_HELP_MESSAGE,
BATCH_LIMIT_ARGUMENT_HELP_MESSAGE,
BATCH_REVERSE_ARGUMENT_HELP_MESSAGE,
BATCH_START_ARGUMENT_HELP_MESSAGE,
)
from cli.batch.service import Batch
from cli.constants import (
BATCH_ID_REGEXP,
BLOCK_ID_REGEXP,
FAILED_EXIT_FROM_COMMAND,
NODE_URL_ARGUMENT_HELP_MESSAGE,
)
from cli.utils import dict_to_pretty_json

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=BATCH_ID_ARGUMENT_HELP_MESSAGE)
@click.option('--start', type=str, required=False, help=BATCH_START_ARGUMENT_HELP_MESSAGE)
@click.option('--limit', type=int, required=False, help=BATCH_LIMIT_ARGUMENT_HELP_MESSAGE)
@click.option('--head', type=str, required=False, help=BATCH_HEAD_ARGUMENT_HELP_MESSAGE)
@click.option('--reverse', type=str, required=False, help=BATCH_REVERSE_ARGUMENT_HELP_MESSAGE)
@click.option('--node-url', type=str, required=False, help=NODE_URL_ARGUMENT_HELP_MESSAGE)
@batch_commands.command('get-list')
def get_batches(id, start, limit, head, reverse, node_url):
"""
Get a list of batches.
"""
for batch_id in (id, start):
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 head is not None and re.match(pattern=BLOCK_ID_REGEXP, string=head) is None:
click.echo('The following batch id `{batch_id}` is not valid.'.format(batch_id=id))

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(dict_to_pretty_json(batches))
8 changes: 8 additions & 0 deletions cli/batch/help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
Provide help messages for command line interface's batch commands.
"""
BATCH_ID_ARGUMENT_HELP_MESSAGE = 'Identifier to get a list of batches by.'
BATCH_START_ARGUMENT_HELP_MESSAGE = 'Parameter to list batches starting from.'
BATCH_LIMIT_ARGUMENT_HELP_MESSAGE = 'Parameter to limit amount of batches.'
BATCH_HEAD_ARGUMENT_HELP_MESSAGE = 'Block identifier to get a list of batches from.'
BATCH_REVERSE_ARGUMENT_HELP_MESSAGE = 'Parameter to reverse result.'
21 changes: 21 additions & 0 deletions cli/batch/interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Provide implementation of the batch interfaces.
"""


class BatchInterface:
"""
Implements batch interface.
"""

async def get_list(self, query=None):
"""
Get a list of batches from REMChain.
Arguments:
query (dict, optional): dictionary with specific parameters
Returns:
List of batches.
"""
pass
34 changes: 34 additions & 0 deletions cli/batch/service.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Provide constants for command line interface.
"""
ADDRESS_REGEXP = '[0-9a-f]{70}'
BATCH_ID_REGEXP = '^[0-9a-f]{128}$'
BLOCK_ID_REGEXP = '^[0-9a-f]{128}$'

FAILED_EXIT_FROM_COMMAND = -1

Expand Down
2 changes: 2 additions & 0 deletions cli/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click

from cli.account.cli import account_commands
from cli.batch.cli import batch_commands


@click.group()
Expand All @@ -17,3 +18,4 @@ def cli():


cli.add_command(account_commands)
cli.add_command(batch_commands)
11 changes: 11 additions & 0 deletions cli/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
Provide utils for command line interface.
"""
import json


def dict_to_pretty_json(data):
"""
Convert dictionary to string with indents as human readable text.
"""
return json.dumps(data, indent=4, sort_keys=True)

0 comments on commit d721d21

Please sign in to comment.