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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| from common.tools import is_256b_hex_str | ||
|
|
||
| from common.net.tlv import NetworksTLV | ||
| from common.net.tlv import TLVRecord, NetworksTLV | ||
| from common.net.bolt9 import FeatureVector | ||
| from common.net.utils import message_sanity_checks | ||
|
|
||
|
|
@@ -24,6 +24,19 @@ class Message: | |
| """ | ||
|
|
||
| def __init__(self, mtype, payload, extension=None): | ||
| if not isinstance(mtype, bytes): | ||
| raise TypeError("mtype must be bytes") | ||
| if not isinstance(payload, bytes): | ||
| raise TypeError("payload must be bytes") | ||
| if extension is not None and not isinstance(extension, list): | ||
| raise TypeError("extension must be a list if set") | ||
| else: | ||
| # Normalize the default extension type (for empty lists) | ||
| if not extension: | ||
| extension = None | ||
| elif not all(isinstance(tlv, TLVRecord) for tlv in extension): | ||
| raise TypeError("All items in extension must be TLVRecords") | ||
|
|
||
| self.type = mtype | ||
| self.payload = payload | ||
| self.extension = extension | ||
|
|
@@ -45,25 +58,29 @@ def from_bytes(cls, message): | |
| """ | ||
|
|
||
| if not isinstance(message, bytes): | ||
| raise TypeError(f"message be must a bytearray") | ||
| raise TypeError("message be must a bytearray") | ||
| if len(message) < 2: | ||
| raise ValueError(f"message be must at least 2-byte long") | ||
| raise ValueError("message be must at least 2-byte long") | ||
|
|
||
| mtype = message[:2] | ||
|
|
||
| if message[:2] == message_types["init"]: | ||
| if mtype == message_types["init"]: | ||
| return InitMessage.from_bytes(message) | ||
| elif message[:2] == message_types["error"]: | ||
| elif mtype == message_types["error"]: | ||
| return ErrorMessage.from_bytes(message) | ||
| elif message[:2] == message_types["ping"]: | ||
| elif mtype == message_types["ping"]: | ||
| return PingMessage.from_bytes(message) | ||
| elif message[:2] == message_types["pong"]: | ||
| elif mtype == message_types["pong"]: | ||
| return PongMessage.from_bytes(message) | ||
| else: | ||
| raise ValueError("Cannot decode unknown message type") | ||
|
|
||
| def serialize(self): | ||
| """Serialises the message.""" | ||
| if not self.extension: | ||
| return self.type + self.payload | ||
| else: | ||
| tlvs = b"".join([tlv.serialize() for tlv in self.extension()]) | ||
| tlvs = b"".join([tlv.serialize() for tlv in self.extension]) | ||
| return self.type + self.payload + tlvs | ||
|
|
||
|
|
||
|
|
@@ -84,16 +101,16 @@ def __init__(self, global_features, local_features, networks=None): | |
| if not isinstance(networks, NetworksTLV): | ||
| raise TypeError("networks must be of type NetworksTLV (if set)") | ||
|
|
||
| global_features = global_features.serialize() | ||
| local_features = local_features.serialize() | ||
| gflen = len(global_features).to_bytes(2, "big") | ||
| flen = len(local_features).to_bytes(2, "big") | ||
| payload = gflen + global_features + flen + local_features | ||
| gf = global_features.serialize() | ||
| lf = local_features.serialize() | ||
| gflen = len(gf).to_bytes(2, "big") | ||
| flen = len(lf).to_bytes(2, "big") | ||
| payload = gflen + gf + flen + lf | ||
|
|
||
| # Add extensions if needed (this follows TLV format) | ||
| # FIXME: Only networks for now | ||
| if networks: | ||
| super().__init__(mtype=message_types["init"], payload=payload, extension=networks.serialize()) | ||
| super().__init__(mtype=message_types["init"], payload=payload, extension=[networks]) | ||
|
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. Very nice, this addresses my earlier TLV vs. TLVStream comment 👍 |
||
| else: | ||
| super().__init__(mtype=message_types["init"], payload=payload) | ||
| self.global_features = global_features | ||
|
|
@@ -103,7 +120,6 @@ def __init__(self, global_features, local_features, networks=None): | |
| @classmethod | ||
| def from_bytes(cls, message): | ||
| """Builds an InitMessage from its byte representation.""" | ||
|
|
||
| # Message should be at least: type (2-byte) + gflen (2-byte) + flen (2 byte) | ||
| message_sanity_checks(message, message_types["init"], 6) | ||
|
|
||
|
|
@@ -112,6 +128,8 @@ def from_bytes(cls, message): | |
| global_features = FeatureVector.from_bytes(message[4 : gflen + 4]) | ||
| flen = int.from_bytes(message[gflen + 4 : gflen + 6], "big") | ||
| local_features = FeatureVector.from_bytes(message[gflen + 6 : gflen + flen + 6]) | ||
|
Comment on lines
+133
to
+136
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. This bookkeeping would also be easier if you used an |
||
| if gflen + flen + 6 > len(message): | ||
| raise ValueError() # Unexpected EOF | ||
|
bigspider marked this conversation as resolved.
|
||
|
|
||
| # Check if there are TLVs (optional) | ||
| if len(message) > gflen + flen + 6: | ||
|
|
@@ -121,7 +139,7 @@ def from_bytes(cls, message): | |
|
|
||
| return cls(global_features, local_features) | ||
|
|
||
| except (IndexError, ValueError): | ||
| except ValueError: | ||
| raise ValueError("Wrong message format. Unexpected EOF") | ||
|
|
||
|
|
||
|
|
@@ -167,14 +185,13 @@ def from_bytes(cls, message): | |
|
|
||
| # There's associated data | ||
| if data_len: | ||
| try: | ||
| data = message[36 : 36 + data_len] | ||
| if len(message) != 36 + data_len: | ||
| raise ValueError("Wrong data format. message has additional tailing data") | ||
| return cls(channel_id, data) | ||
| data = message[36 : 36 + data_len] | ||
|
|
||
| except IndexError: | ||
| if len(data) < data_len: | ||
| raise ValueError("Wrong message format. Unexpected EOF") | ||
| if len(message) > 36 + data_len: | ||
| raise ValueError("Wrong data format. message has additional tailing data") | ||
|
sr-gi marked this conversation as resolved.
Outdated
|
||
| return cls(channel_id, data.decode("utf-8")) | ||
|
|
||
| return cls(channel_id) | ||
|
|
||
|
|
@@ -189,16 +206,18 @@ class PingMessage(Message): | |
| """ | ||
|
|
||
| def __init__(self, num_pong_bytes, ignored_bytes=None): | ||
| if num_pong_bytes > pow(2, 16): | ||
| raise ValueError(f"num_pong_bytes cannot be higher than {pow(2, 16)}") | ||
| if ignored_bytes and not isinstance(ignored_bytes, bytes): | ||
| raise TypeError("ignored_bytes must be bytes if set") | ||
| if len(ignored_bytes) > pow(2, 16) - 4: | ||
| raise ValueError(f"ignored_bytes cannot be higher than {pow(2, 16) -4}") | ||
| if not 0 <= num_pong_bytes < pow(2, 16): | ||
| raise ValueError(f"num_pong_bytes must be between 0 and {pow(2, 16)}") | ||
|
bigspider marked this conversation as resolved.
Outdated
|
||
|
|
||
| payload = num_pong_bytes.to_bytes(2, "big") | ||
|
|
||
| if ignored_bytes: | ||
| if not isinstance(ignored_bytes, bytes): | ||
| raise TypeError("ignored_bytes must be bytes if set") | ||
| if len(ignored_bytes) > pow(2, 16) - 4: | ||
| raise ValueError(f"ignored_bytes cannot be higher than {pow(2, 16) - 4}") | ||
| payload += len(ignored_bytes).to_bytes(2, "big") + ignored_bytes | ||
|
|
||
| super().__init__(message_types["ping"], payload) | ||
| self.num_pong_bytes = num_pong_bytes | ||
| self.ignored_bytes = ignored_bytes | ||
|
|
@@ -212,14 +231,12 @@ def from_bytes(cls, message): | |
| byteslen = int.from_bytes(message[4:6], "big") | ||
|
|
||
| if byteslen: | ||
| try: | ||
| ignored = message[6 : 6 + byteslen] | ||
| if len(message) != 6 + byteslen: | ||
| raise ValueError("Wrong data format. message has additional tailing data") | ||
| return cls(num_pong_bytes, ignored) | ||
|
|
||
| except IndexError: | ||
| ignored = message[6 : 6 + byteslen] | ||
| if len(ignored) < byteslen: | ||
| raise ValueError("Wrong message format. Unexpected EOF") | ||
| if len(message) > 6 + byteslen: | ||
| raise ValueError("Wrong data format. message has additional tailing data") | ||
|
bigspider marked this conversation as resolved.
Outdated
|
||
| return cls(num_pong_bytes, ignored) | ||
|
|
||
| return cls(num_pong_bytes) | ||
|
|
||
|
|
@@ -256,13 +273,11 @@ def from_bytes(cls, message): | |
| byteslen = int.from_bytes(message[2:4], "big") | ||
|
|
||
| if byteslen: | ||
| try: | ||
| ignored_bytes = message[4 : 4 + byteslen] | ||
| if len(message) != 4 + byteslen: | ||
| raise ValueError("Wrong data format. message has additional tailing data") | ||
| return cls(ignored_bytes) | ||
|
|
||
| except IndexError: | ||
| ignored_bytes = message[4 : 4 + byteslen] | ||
| if len(ignored_bytes) < byteslen: | ||
| raise ValueError("Wrong message format. Unexpected EOF") | ||
| if len(message) != 4 + byteslen: | ||
| raise ValueError("Wrong data format. message has additional tailing data") | ||
|
sr-gi marked this conversation as resolved.
Outdated
|
||
| return cls(ignored_bytes) | ||
|
|
||
| return cls() | ||
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.
Uh oh!
There was an error while loading. Please reload this page.