Skip to content

Commit

Permalink
Implement getting blocks identifiers (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrostriletskyi authored May 16, 2019
1 parent 3c18461 commit 83a1fd9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ Get a list of blocks — ``remme block get-list``:
| ids | String | No | Identifiers to get a list of blocks by. |
| limit | Integer | No | Maximum amount of blocks to return. |
| head | Integer | No | Block identifier to get a list of transactions to. |
| ids-only | Bool | No | The flag to get a list of blocks' identifiers. |
| reverse | Bool | No | Parameter to reverse result. |
| node-url | String | No | Node URL to apply a command to. |

Expand Down Expand Up @@ -311,6 +312,19 @@ $ remme block get-list \
}
```

Get a list of blocks' identifiers (can be combined with other parameters like `--limit`):

```bash
$ remme block get-list --ids-only --node-url=node-6-testnet.remme.io
{
"result": [
"b757c74fbcd57ae12577b71490878affb6b688434c2e20170138760e72e937ca1bb3d6773e2ef37b5151ed74dcb663114a181072e0870e7a4d452c58659a6dbb",
"585f23725d1236e90e2b961b0c0c1404aba0ba5a96e4d85cd2f048b1d61b027669153e3618c84fc09a8041f8e149b97d50a89ee7761d0458cd57c63d5f354cbd",
...
]
}
```

Get information about the block by its identifier — ``remme block get``:

| Arguments | Type | Required | Description |
Expand Down
11 changes: 9 additions & 2 deletions cli/block/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
BLOCK_IDENTIFIER_ARGUMENT_HELP_MESSAGE,
BLOCKS_HEAD_ARGUMENT_HELP_MESSAGE,
BLOCKS_IDENTIFIERS_ARGUMENT_HELP_MESSAGE,
BLOCKS_IDENTIFIERS_ONLY_ARGUMENT_HELP_MESSAGE,
BLOCKS_LIMIT_ARGUMENT_HELP_MESSAGE,
BLOCKS_REVERSE_ARGUMENT_HELP_MESSAGE,
)
Expand Down Expand Up @@ -41,9 +42,10 @@ def block_commands():
@click.option('--limit', required=False, type=int, help=BLOCKS_LIMIT_ARGUMENT_HELP_MESSAGE)
@click.option('--head', required=False, type=str, help=BLOCKS_HEAD_ARGUMENT_HELP_MESSAGE)
@click.option('--reverse', required=False, is_flag=True, help=BLOCKS_REVERSE_ARGUMENT_HELP_MESSAGE)
@click.option('--ids-only', required=False, is_flag=True, help=BLOCKS_IDENTIFIERS_ONLY_ARGUMENT_HELP_MESSAGE)
@click.option('--node-url', required=False, type=str, help=NODE_URL_ARGUMENT_HELP_MESSAGE, default=default_node_url())
@block_commands.command('get-list')
def get_blocks(ids, head, limit, reverse, node_url):
def get_blocks(ids, head, limit, reverse, ids_only, node_url):
"""
Get a list of blocks.
"""
Expand All @@ -52,6 +54,7 @@ def get_blocks(ids, head, limit, reverse, node_url):
'limit': limit,
'head': head,
'reverse': reverse,
'ids_only': ids_only,
'node_url': node_url,
})

Expand All @@ -62,13 +65,17 @@ def get_blocks(ids, head, limit, reverse, node_url):
block_ids = arguments.get('ids')
limit = arguments.get('limit')
head = arguments.get('head')
ids_only = arguments.get('ids_only')
node_url = arguments.get('node_url')

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

result, errors = Block(service=remme).get_list(ids=block_ids, head=head, limit=limit, reverse=reverse)
if ids_only:
result, errors = Block(service=remme).get_list_ids(ids=block_ids, head=head, limit=limit, reverse=reverse)
else:
result, errors = Block(service=remme).get_list(ids=block_ids, head=head, limit=limit, reverse=reverse)

if errors is not None:
print_errors(errors=errors)
Expand Down
1 change: 1 addition & 0 deletions cli/block/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class GetBlocksListForm(Schema):
],
)
head = BlockIdentifierField(allow_none=True, required=False)
ids_only = fields.Boolean(required=False)
node_url = NodeUrlField(required=False)


Expand Down
1 change: 1 addition & 0 deletions cli/block/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
BLOCKS_LIMIT_ARGUMENT_HELP_MESSAGE = 'Maximum amount of blocks to return.'
BLOCKS_HEAD_ARGUMENT_HELP_MESSAGE = 'Block identifier to get a list of transactions to.'
BLOCKS_REVERSE_ARGUMENT_HELP_MESSAGE = 'Parameter to reverse result.'
BLOCKS_IDENTIFIERS_ONLY_ARGUMENT_HELP_MESSAGE = 'The flag to get a list of blocks\' identifiers.'
25 changes: 25 additions & 0 deletions cli/block/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,28 @@ def get_list(self, ids, head, limit, reverse):
return None, str(error)

return blocks.get('data'), None

def get_list_ids(self, ids, head, limit, reverse):
"""
Get a list of blocks identifiers.
A list of blocks identifiers could be filtered by blocks identifiers, limit, head, reverse.
Arguments:
ids (list, optional): identifiers to get a list of blocks by.
limit (int, optional): maximum amount of blocks to return.
head (string, optional): block identifier to get a list of transactions to.
reverse (bool, optional): parameter to reverse result.
"""
blocks, errors = self.get_list(ids=ids, head=head, limit=limit, reverse=reverse)

if errors is not None:
return None, errors

blocks_identifiers = []

for block in blocks:
block_identifier = block.get('header_signature')
blocks_identifiers.append(block_identifier)

return blocks_identifiers, None
22 changes: 22 additions & 0 deletions tests/block/test_get_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,28 @@ def test_get_blocks_with_invalid_limit():
assert dict_to_pretty_json(expected_error) in result.output


def test_get_blocks_identifiers():
"""
Case: get a list of blocks' identifiers.
Expect: a list of blocks' identifiers is returned.
"""
runner = CliRunner()
result = runner.invoke(cli, [
'block',
'get-list',
'--ids-only',
'--node-url',
DEV_BRANCH_NODE_IP_ADDRESS_FOR_TESTING,
])

blocks = json.loads(result.output).get('result')

assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code

for block_identifier in blocks:
assert re.match(pattern=BLOCK_IDENTIFIER_REGEXP, string=block_identifier) is not None


def test_get_blocks_invalid_node_url():
"""
Case: get a list of blocks by passing invalid node URL.
Expand Down

0 comments on commit 83a1fd9

Please sign in to comment.