This repository was archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Implements BOLT1 and BOLT9 #208
Open
sr-gi
wants to merge
16
commits into
talaia-labs:master
Choose a base branch
from
sr-gi:ln-net
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
73a74d4
lnnet - Adds bigsize and utils
sr-gi abda49d
lnnet - Adds TLVRecord and moves NetworksTLV
sr-gi 06e680e
lnnet - Implements BOLT1
sr-gi 613ee82
lnnet - Implements BOLT9
sr-gi 8b96771
test - Adds lnnet bigsize unit tests
sr-gi 8ff6f5a
test - Adds bolt9 unit tests
sr-gi 4f09f50
test: Adds utils unit test
sr-gi 74fb734
test: Adds tlv unit tests
sr-gi 89717fd
lnnet - Improves BOLT1
sr-gi 647962b
test - Adds BOLT1 unit tests
sr-gi 785502a
lnnet - Fixes typos and improves docs
sr-gi b9f06f4
lnnet - Some code improvements and fixes from @bigspider's review
sr-gi fe914bc
lnnet - Changes defaults from None
sr-gi 2690660
lnnet - Adds to_dict to
sr-gi dc4655b
test - typos and improvements from PR review
sr-gi 13b5e13
test - Adds Message.to_dict test for lnnet
sr-gi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| from common.tools import is_256b_hex_str | ||
|
|
||
| import common.net.bigsize as bigsize | ||
| from common.net.utils import message_sanity_checks | ||
|
|
||
| tlv_types = { | ||
| "networks": b"\x01", | ||
| "amt_to_forward": b"\x02", | ||
| "outgoing_cltv_value": b"\x04", | ||
| "short_channel_id": b"\x06", | ||
| "payment_data": b"\x08", | ||
| } | ||
|
|
||
|
|
||
| class TLVRecord: | ||
| """ | ||
| Base class for TLV records. | ||
|
|
||
| Args: | ||
| t (:obj:`bytes`): the message type. | ||
| l (:obj:`bytes`): the value length. | ||
| v (:obj:`bytes`): the message value. | ||
| """ | ||
|
|
||
| def __init__(self, t=b"", l=b"", v=b""): | ||
| if not isinstance(t, bytes): | ||
| raise TypeError("t must be bytes") | ||
| if not isinstance(l, bytes): | ||
| raise TypeError("l must be bytes") | ||
| if not isinstance(v, bytes): | ||
| raise TypeError("v must be bytes") | ||
|
|
||
| self.type = t | ||
| self.length = l | ||
| self.value = v | ||
|
|
||
| def __len__(self): | ||
| """Returns the length of the serialised TLV record""" | ||
|
sr-gi marked this conversation as resolved.
Outdated
|
||
| return len(self.serialize()) | ||
|
|
||
| def __eq__(self, other): | ||
| return isinstance(other, TLVRecord) and self.value == other.value | ||
|
bigspider marked this conversation as resolved.
Outdated
|
||
|
|
||
| @classmethod | ||
| def from_bytes(cls, message): | ||
| """ | ||
| Builds a TLV record from bytes. | ||
|
|
||
| Args: | ||
| message (:obj:`bytes`): the byte representation of the TLV record. | ||
|
|
||
| Returns: | ||
| :obj:`TLVRecord`: The TLVRecord built from the provided bytes. | ||
|
|
||
| Raises: | ||
| :obj:`TypeError`: If the provided message is not in bytes. | ||
| :obj:`ValueError`: If the provided message is not properly encoded. | ||
| """ | ||
|
|
||
| if not isinstance(message, bytes): | ||
| raise TypeError("message must be bytes") | ||
|
|
||
| try: | ||
| t, t_offset = bigsize.parse(message) | ||
|
bigspider marked this conversation as resolved.
Outdated
|
||
| if t.to_bytes(t_offset, "big") == tlv_types["networks"]: | ||
| return NetworksTLV.from_bytes(message) | ||
| else: | ||
| l, l_offset = bigsize.parse(message[t_offset:]) | ||
| v = message[t_offset + l_offset :] | ||
| if l > len(v): | ||
| # Value is not long enough | ||
| raise ValueError() # This message get overwritten so it does not matter | ||
|
sr-gi marked this conversation as resolved.
Outdated
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be helpful to define a dict for |
||
|
|
||
| if len(message) != t_offset + l_offset + len(v): | ||
| # The is additional tailing data | ||
|
sr-gi marked this conversation as resolved.
Outdated
|
||
| raise ValueError() # This message get overwritten so it does not matter | ||
|
sr-gi marked this conversation as resolved.
Outdated
|
||
|
|
||
| return cls(t.to_bytes(t_offset, "big"), l.to_bytes(l_offset, "big"), v) | ||
| except ValueError as e: | ||
| raise ValueError("Wrong tlv message format. Unexpected EOF") | ||
|
|
||
| def serialize(self): | ||
| """Returns the serialised representation of the TLV record.""" | ||
| return self.type + self.length + self.value | ||
|
|
||
|
|
||
| class NetworksTLV(TLVRecord): | ||
| """ | ||
| TLV record for networks in the init message. Contains the genesis block hash of the networks the node is interested | ||
| in. | ||
|
|
||
| Args: | ||
| networks (:obj:`list`): a list of genesis block hashes (hex str). | ||
|
sr-gi marked this conversation as resolved.
Outdated
|
||
| """ | ||
|
|
||
| def __init__(self, networks=None): | ||
| if not networks: | ||
| super().__init__(tlv_types["networks"], bigsize.encode(0)) | ||
| self.networks = [] | ||
| elif isinstance(networks, list): | ||
| chains = b"" | ||
| for chain in networks: | ||
| if not is_256b_hex_str(chain): | ||
| raise ValueError("All networks must be 32-byte hex str") | ||
| chains += bytes.fromhex(chain) | ||
| super().__init__(tlv_types["networks"], bigsize.encode(32 * len(networks)), chains) | ||
| self.networks = networks | ||
| else: | ||
| raise TypeError("networks must be a list if set") | ||
|
|
||
| @classmethod | ||
| def from_bytes(cls, message): | ||
| """ | ||
| Builds a NetworksTLV record from bytes. | ||
|
|
||
| Args: | ||
| message (:obj:`bytes`): the byte representation of the TLV record. | ||
|
|
||
| Returns: | ||
| :obj:`NetworksTLV`: The NetworksTLV built from the provided bytes. | ||
|
|
||
| Raises: | ||
| :obj:`TypeError`: If the provided message is not in bytes or networks is not a list. | ||
| :obj:`ValueError`: If the provided message is not properly encoded or the items in networks are not 32-byte | ||
| hex strings. | ||
| """ | ||
|
|
||
| message_sanity_checks(message, tlv_types["networks"], 2, tlv=True) | ||
|
|
||
| try: | ||
| clen, length_offset = bigsize.parse(message[1:]) | ||
| except ValueError: | ||
| # TLV can be defined with no data. | ||
| return cls() | ||
|
|
||
| # Chains is an array of genesis block hashes (32-byte each) | ||
| if clen % 32: | ||
| raise ValueError(f"chains must be multiple of 32, {clen} received") | ||
|
|
||
| networks = [] | ||
| offset = 1 + length_offset # type + length fields | ||
| for i in range(clen // 32): | ||
| networks.append(message[offset : offset + 32].hex()) | ||
| offset += 32 | ||
|
|
||
| return cls(networks) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
mypyannotations would remove the need for theisinstancechecks below.