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 May 8, 2019
1 parent bb56429 commit 751cf24
Show file tree
Hide file tree
Showing 10 changed files with 741 additions and 0 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [Service](#service)
* [Account](#account)
* [Atomic Swap](#atomic-swap)
* [Batch](#batch)
* [Node](#node)
* [Public key](#public-key)
* [State](#state)
Expand Down Expand Up @@ -190,6 +191,75 @@ $ remme atomic-swap get-info \
}
```

### Batch

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

| Arguments | Type | Required | Description |
| :---------: | :----: | :-------: | ----------------------------------------------------- |
| ids | String | No | Identifiers to get a list of batches by. |
| start | String | No | Batch identifier to get a list of batches starting from.|
| limit | Integer| No | Maximum amount of batches to return. |
| head | String | No | Block identifier to get a list of batches from. |
| reverse | Bool | No | Parameter to reverse result. |
| node-url | String | No | Node URL to apply a command to. |

```bash
$ remme batch get-list \
--ids='6bd3382e3deef34d0bc63a7b450c88c7ae00152f5168c7b4dc4357feff6d52175209919cd0710441fa2768f4c12adf97143440ef8414bb5144b9459d78ff3e0e, 7a5daba99d5757adc997ea6a0b1b83263b3c16604dbd83c0153dc01c9fd780af4b570338c2ec60e086b1db58a4397a4dc661d6c93b0a7250fe75642e15b26e81' \
--start=6bd3382e3deef34d0bc63a7b450c88c7ae00152f5168c7b4dc4357feff6d52175209919cd0710441fa2768f4c12adf97143440ef8414bb5144b9459d78ff3e0e \
--limit=2 \
--head=57a7944497ca41f424932ae6b70897e7086652ab98450d4aba6a02a2d891501460947812a41028b8041f087066df6dc7e1100c4b0e5cc94bb58b002f6950eb02 \
--node-url=node-6-testnet.remme.io \
--reverse
"result": {
"data": [
{
"header": {
"signer_public_key": "03738df3f4ac3621ba8e89413d3ff4ad036c3a0a4dbb164b695885aab6aab614ad",
"transaction_ids": [
"ed7baa5ab9bc5ef49077ddd22de3ebef56157f188980edb6401a57729f7226195d849f227fd941cc01c5eeeedd1d9cadf2e48140098422f2cb641044d971a374",
]
},
"header_signature": "6bd3382e3deef34d0bc63a7b450c88c7ae00152f5168c7b4dc4357feff6d52175209919cd0710441fa2768f4c12adf97143440ef8414bb5144b9459d78ff3e0e",
"trace": false,
"transactions": [
{
"header": {
"batcher_public_key": "03738df3f4ac3621ba8e89413d3ff4ad036c3a0a4dbb164b695885aab6aab614ad",
"dependencies": [],
"family_name": "sawtooth_settings",
"family_version": "1.0",
"inputs": [
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c1c0cbf0fbcaf64c0b",
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c12840f169a04216b7",
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c1918142591ba4e8a7",
"000000a87cb5eafdcca6a8f82af32160bc5311783bdad381ea57b4e3b0c44298fc1c14"
],
"nonce": "",
"outputs": [
"000000a87cb5eafdcca6a8cde0fb0dec1400c5ab274474a6aa82c1c0cbf0fbcaf64c0b",
"000000a87cb5eafdcca6a8f82af32160bc5311783bdad381ea57b4e3b0c44298fc1c14"
],
"payload_sha512": "a39230835da9f4ae154abd7197f196acb9ed9a7d4d0bae8de71714bfe4636c8c6516d774e7b721b9d08d2abbafa42b01acdfe0e3c9b6d35ecef7452442e63b49",
"signer_public_key": "03738df3f4ac3621ba8e89413d3ff4ad036c3a0a4dbb164b695885aab6aab614ad"
},
"header_signature": "ed7baa5ab9bc5ef49077ddd22de3ebef56157f188980edb6401a57729f7226195d849f227fd941cc01c5eeeedd1d9cadf2e48140098422f2cb641044d971a374",
"payload": "CAESRwoic2F3dG9vdGgudmFsaWRhdG9yLmJhdGNoX2luamVjdG9ycxINcmVtbWVfYmF0Y2hlcxoSMHg5NzBkZGE2MjRlZTFhZmFh"
},
]
},
],
"head": "57a7944497ca41f424932ae6b70897e7086652ab98450d4aba6a02a2d891501460947812a41028b8041f087066df6dc7e1100c4b0e5cc94bb58b002f6950eb02",
"paging": {
"limit": 2,
"next": "",
"start": "6bd3382e3deef34d0bc63a7b450c88c7ae00152f5168c7b4dc4357feff6d52175209919cd0710441fa2768f4c12adf97143440ef8414bb5144b9459d78ff3e0e"
}
}
}
```

### Node

Get node configurations — ``remme node get-configs``:
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 sys

import click
from remme import Remme

from cli.batch.forms import GetBatchesListForm
from cli.batch.help import (
BATCH_HEAD_ARGUMENT_HELP_MESSAGE,
BATCH_IDS_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 (
FAILED_EXIT_FROM_COMMAND_CODE,
NODE_URL_ARGUMENT_HELP_MESSAGE,
)
from cli.utils import (
default_node_url,
print_errors,
print_result,
)


@click.group('batch', chain=True)
def batch_commands():
"""
Provide commands for working with batch.
"""
pass


@click.option('--ids', required=False, type=str, help=BATCH_IDS_ARGUMENT_HELP_MESSAGE)
@click.option('--start', required=False, type=str, help=BATCH_START_ARGUMENT_HELP_MESSAGE)
@click.option('--limit', required=False, type=int, help=BATCH_LIMIT_ARGUMENT_HELP_MESSAGE)
@click.option('--head', required=False, type=str, help=BATCH_HEAD_ARGUMENT_HELP_MESSAGE)
@click.option('--reverse', required=False, is_flag=True, help=BATCH_REVERSE_ARGUMENT_HELP_MESSAGE)
@click.option('--node-url', required=False, type=str, help=NODE_URL_ARGUMENT_HELP_MESSAGE, default=default_node_url())
@batch_commands.command('get-list')
def get_batches(ids, start, limit, head, reverse, node_url):
"""
Get a list of batches.
"""
arguments, errors = GetBatchesListForm().load({
'ids': ids,
'start': start,
'limit': limit,
'head': head,
'reverse': reverse,
'node_url': node_url,
})

if errors:
print_errors(errors=errors)
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE)

batch_ids = arguments.get('ids')
start = arguments.get('start')
limit = arguments.get('limit')
head = arguments.get('head')
node_url = arguments.get('node_url')

remme = Remme(network_config={
'node_address': str(node_url) + ':8080',
})

batches, errors = Batch(service=remme).get_list(
batch_ids=batch_ids,
start=start,
limit=limit,
head=head,
reverse=reverse,
)

if errors is not None:
print_errors(errors=errors)
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE)

print_result(result=batches)
33 changes: 33 additions & 0 deletions cli/batch/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Provide forms for command line interface's batch commands.
"""
from marshmallow import (
Schema,
fields,
validate,
)

from cli.generic.forms.fields import (
BatchIdentifierField,
BatchIdentifiersListField,
NodeUrlField,
)


class GetBatchesListForm(Schema):
"""
Get a list of batch form.
"""

ids = BatchIdentifiersListField(allow_none=True, required=False)
start = BatchIdentifierField(allow_none=True, required=False)
head = BatchIdentifierField(allow_none=True, required=False)
limit = fields.Integer(
allow_none=True,
strict=True,
required=False,
validate=[
validate.Range(min=1, error='Limit must be greater than 0.'),
],
)
node_url = NodeUrlField(required=False)
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_IDS_ARGUMENT_HELP_MESSAGE = 'Identifiers to get a list of batches by.'
BATCH_START_ARGUMENT_HELP_MESSAGE = 'Batch identifier to get a list of batches starting from.'
BATCH_LIMIT_ARGUMENT_HELP_MESSAGE = 'Maximum amount of batches to return.'
BATCH_HEAD_ARGUMENT_HELP_MESSAGE = 'Block identifier to get a list of batches from.'
BATCH_REVERSE_ARGUMENT_HELP_MESSAGE = 'Parameter to reverse result.'
25 changes: 25 additions & 0 deletions cli/batch/interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
Provide implementation of the batch interfaces.
"""


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

def get_list(self, batch_ids, start, limit, head, reverse):
"""
Get a list of batches.
A list of batches could be filtered by batch identifiers, start identifier, limit, head identifier,
reverse.
Arguments:
batch_ids (list, optional): identifiers to get a list of batches by.
start (string, optional): batch identifier to get a list of batches starting from.
limit (int, optional): maximum amount of batches to return.
head (string, optional): block identifier to get a list of batches from.
reverse (string, optional): parameter to reverse result.
"""
pass
59 changes: 59 additions & 0 deletions cli/batch/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
Provide implementation of the batch.
"""
import asyncio

from accessify import implements
from aiohttp_json_rpc import RpcGenericServerDefinedError

from cli.batch.interfaces import BatchInterface

loop = asyncio.get_event_loop()


@implements(BatchInterface)
class Batch:
"""
Implements batch.
"""

def __init__(self, service):
"""
Constructor.
Arguments:
service: object to interact with Remme core API.
"""
self.service = service

def get_list(self, batch_ids, start, limit, head, reverse):
"""
Get a list of batches.
Arguments:
batch_ids (list, optional): identifiers to get a list of batches by.
start (string, optional): batch identifier to get a list of batches starting from.
limit (int, optional): maximum amount of batches to return.
head (string, optional): block identifier to get a list of batches from.
reverse (string, optional): parameter to reverse result.
"""
reverse = '' if reverse else 'false'

try:
batches = loop.run_until_complete(
self.service.blockchain_info.get_batches(query={
'ids': batch_ids,
'start': start,
'limit': limit,
'head': head,
'reverse': reverse,
}),
)

except RpcGenericServerDefinedError as error:
return None, str(error.message)

except Exception as error:
return None, str(error)

return batches, None
2 changes: 2 additions & 0 deletions cli/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from cli.account.cli import account_commands
from cli.atomic_swap.cli import atomic_swap_commands
from cli.batch.cli import batch_commands
from cli.node.cli import node_commands
from cli.public_key.cli import public_key_commands
from cli.state.cli import state_command
Expand All @@ -23,6 +24,7 @@ def cli():

cli.add_command(account_commands)
cli.add_command(atomic_swap_commands)
cli.add_command(batch_commands)
cli.add_command(node_commands)
cli.add_command(public_key_commands)
cli.add_command(state_command)
Expand Down
44 changes: 44 additions & 0 deletions cli/generic/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ADDRESS_REGEXP,
DOMAIN_NAME_REGEXP,
FAMILY_NAMES,
HEADER_SIGNATURE_REGEXP,
PRIVATE_KEY_REGEXP,
PUBLIC_KEY_ADDRESS_REGEXP,
SWAP_IDENTIFIER_REGEXP,
Expand Down Expand Up @@ -100,6 +101,49 @@ def _deserialize(self, value, attr, obj, **kwargs):
return value


class BatchIdentifiersListField(fields.Field):
"""
Implements validation of the list of the identifiers.
References:
- https://marshmallow.readthedocs.io/en/3.0/custom_fields.html
"""

def _deserialize(self, value, attr, obj, **kwargs):
"""
Validate data (list of the identifiers) that was passed to field.
"""
validated_identifiers = []

for identifier in value.split(','):
identifier = identifier.strip()

if re.match(pattern=HEADER_SIGNATURE_REGEXP, string=identifier) is None:
raise ValidationError(f'The following identifier `{identifier}` is invalid.')

validated_identifiers.append(identifier)

return validated_identifiers


class BatchIdentifierField(fields.Field):
"""
Implements validation of the identifier.
References:
- https://marshmallow.readthedocs.io/en/3.0/custom_fields.html
"""

def _deserialize(self, value, attr, obj, **kwargs):
"""
Validate data (identifier) that was passed to field.
"""
if re.match(pattern=HEADER_SIGNATURE_REGEXP, string=value) is None:
raise ValidationError(f'The following identifier `{value}` is invalid.')

return value


class NodeUrlField(fields.Field):
"""
Implements validation of the node URL.
Expand Down
Loading

0 comments on commit 751cf24

Please sign in to comment.