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

REM-1312: Get particular Atomic Swap information by its identifier #24

Merged
merged 9 commits into from
May 6, 2019
Merged
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [Configuration file](#configuration-file)
* [Service](#service)
* [Account](#account)
* [Atomic Swap](#atomic-swap)
* [Node](#node)
* [Public key](#public-key)
* [Development](#development)
Expand Down Expand Up @@ -158,6 +159,36 @@ $ remme atomic-swap get-public-key --node-url=node-6-testnet.remme.io
}
```

Get information about atomic swap by its identifier — ``remme atomic-swap get-info``:

| Arguments | Type | Required | Description |
| :-------: | :----: | :------: | ---------------------------------------------------- |
| id | String | Yes | Swap identifier to get an information about swap by. |
| node-url | String | No | Node URL to apply a command to. |

```bash
$ remme atomic-swap get-info \
--id=033402fe1346742486b15a3a9966eb5249271025fc7fb0b37ed3fdb4bcce6808 \
--node-url=node-genesis-testnet.remme.io
{
"result": {
"information": {
"amount": "10.0000",
"created_at": 1556803765,
"email_address_encrypted_optional": "",
"is_initiator": false,
"receiver_address": "112007484def48e1c6b77cf784aeabcac51222e48ae14f3821697f4040247ba01558b1",
"secret_key": "",
"secret_lock": "0728356568862f9da0825aa45ae9d3642d64a6a732ad70b8857b2823dbf2a0b8",
"sender_address": "1120076ecf036e857f42129b58303bcf1e03723764a1702cbe98529802aad8514ee3cf",
"sender_address_non_local": "0xe6ca0e7c974f06471759e9a05d18b538c5ced11e",
"state": "OPENED",
"swap_id": "033402fe1346742486b15a3a9966eb5249271025fc7fb0b37ed3fdb4bcce6808"
}
}
}
```

### Node

Get node configurations — ``remme node get-configs``:
Expand Down
38 changes: 37 additions & 1 deletion cli/atomic_swap/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import click
from remme import Remme

from cli.atomic_swap.forms import GetAtomicSwapPublicKeyForm
from cli.atomic_swap.forms import (
GetAtomicSwapInformationForm,
GetAtomicSwapPublicKeyForm,
)
from cli.atomic_swap.help import SWAP_IDENTIFIER_ARGUMENT_HELP_MESSAGE
from cli.atomic_swap.service import AtomicSwap
from cli.constants import (
FAILED_EXIT_FROM_COMMAND_CODE,
Expand Down Expand Up @@ -54,3 +58,35 @@ def get_public_key(node_url):
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE)

print_result(result=result)


@click.option('--id', type=str, required=True, help=SWAP_IDENTIFIER_ARGUMENT_HELP_MESSAGE)
@click.option('--node-url', type=str, required=False, help=NODE_URL_ARGUMENT_HELP_MESSAGE, default=default_node_url())
@atomic_swap_commands.command('get-info')
def get_swap_info(id, node_url):
"""
Get information about atomic swap by its identifier.
"""
arguments, errors = GetAtomicSwapInformationForm().load({
'id': id,
'node_url': node_url,
})

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

swap_id = arguments.get('id')
node_url = arguments.get('node_url')

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

result, errors = AtomicSwap(service=remme).get(swap_id=swap_id)

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

print_result(result=result)
14 changes: 13 additions & 1 deletion cli/atomic_swap/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
"""
from marshmallow import Schema

from cli.generic.forms.fields import NodeUrlField
from cli.generic.forms.fields import (
NodeUrlField,
SwapIdentifierField,
)


class GetAtomicSwapInformationForm(Schema):
"""
Get information about atomic swap by its identifier form.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get information about atomic swap by its identifier form.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


id = SwapIdentifierField(required=True)
node_url = NodeUrlField(required=False)


class GetAtomicSwapPublicKeyForm(Schema):
Expand Down
1 change: 1 addition & 0 deletions cli/atomic_swap/help.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""
Provide help messages for command line interface's atomic swap commands.
"""
SWAP_IDENTIFIER_ARGUMENT_HELP_MESSAGE = 'Swap identifier to get an information about swap by.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get information about swap by..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alladin9393, done.

6 changes: 6 additions & 0 deletions cli/atomic_swap/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ def get_public_key(self):
Get public key of atomic swap.
"""
pass

def get(self, swap_id):
"""
Get information about atomic swap by its identifier.
"""
pass
18 changes: 18 additions & 0 deletions cli/atomic_swap/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import asyncio

from accessify import implements
from aiohttp_json_rpc import RpcGenericServerDefinedError

from cli.atomic_swap.interfaces import AtomicSwapInterface

Expand Down Expand Up @@ -38,3 +39,20 @@ def get_public_key(self):
return {
'public_key': public_key,
}, None

def get(self, swap_id):
"""
Get information about atomic swap by its identifier.
"""
try:
swap_info = loop.run_until_complete(self.service.swap.get_info(swap_id=swap_id))

except RpcGenericServerDefinedError as error:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add except RpcGenericServerDefinedError to method get_public_key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ticket is not related to get_public_key. It should be fixed in another ticket.

return None, str(error.message)

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

return {
'information': swap_info.data,
}, None
1 change: 1 addition & 0 deletions cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
PUBLIC_KEY_REGEXP = r'^[0-9a-f]{66}$'
PUBLIC_KEY_ADDRESS_REGEXP = r'^[0-9a-f]{70}$'
HEADER_SIGNATURE_REGEXP = r'^[0-9a-f]{128}$'
SWAP_IDENTIFIER_REGEXP = r'^[a-f0-9]{64}$'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change according to alphabetical order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constants not in alphabetical order. It should be fixed in another ticket for technical dept.

DOMAIN_NAME_REGEXP = r'(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]'

PASSED_EXIT_FROM_COMMAND_CODE = 0
Expand Down
21 changes: 21 additions & 0 deletions cli/generic/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DOMAIN_NAME_REGEXP,
PRIVATE_KEY_REGEXP,
PUBLIC_KEY_ADDRESS_REGEXP,
SWAP_IDENTIFIER_REGEXP,
)


Expand Down Expand Up @@ -102,3 +103,23 @@ def _deserialize(self, value, attr, data, **kwargs):
raise ValidationError(f'The following public key address `{public_key_address}` is invalid.')

return value


class SwapIdentifierField(fields.Field):
"""
Implements validation of the swap identifier.

References:
- https://marshmallow.readthedocs.io/en/3.0/custom_fields.html
"""

def _deserialize(self, value, attr, data, **kwargs):
"""
Validate data (swap identifier) that was passed to field.
"""
swap_identifier = value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is it for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For more detail, variable value says nothing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You create a class that named SwapIdentifierField that have docs that say

Implements validation of the swap identifier.

and you have a method _deserialize that also have docs:

Validate data (swap identifier) that was passed to field.

Marshmallow

Example of creating a field

Also, you validate one variable and return another, it not logical.

Copy link
Contributor

@dmytrostriletskyi dmytrostriletskyi May 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alladin9393,

  1. Documentation could lag behind code during the project life.
  2. Sometimes programmers read the code but not the docs.

These points are the core arguments why we should create the separated explicit (!) variable and return it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmytrostriletskyi, ok, but we don't return swap_identifier, we validate one argument, but return other value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alladin9393, refresh page.


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

return value
Loading