mirror of
https://github.com/logos-blockchain/logos-blockchain-specs.git
synced 2026-01-06 07:03:11 +00:00
constant payload size
This commit is contained in:
parent
9ac15da7d3
commit
07a1c5cb7b
@ -10,9 +10,9 @@ from p2p import P2p
|
|||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
N_MIXES_IN_PATH = 2
|
N_MIXES_IN_PATH = 2
|
||||||
|
INCENTIVE_TX_SIZE = 512
|
||||||
REAL_PAYLOAD = b"BLOCK"
|
REAL_PAYLOAD = b"BLOCK"
|
||||||
COVER_PAYLOAD = b"COVER"
|
COVER_PAYLOAD = b"COVER"
|
||||||
INCENTIVE_TX_SIZE = 512
|
|
||||||
|
|
||||||
def __init__(self, id: int, env: simpy.Environment, p2p: P2p):
|
def __init__(self, id: int, env: simpy.Environment, p2p: P2p):
|
||||||
self.id = id
|
self.id = id
|
||||||
@ -57,8 +57,9 @@ class Node:
|
|||||||
msg, incentive_tx = msg.unwrap(self.private_key)
|
msg, incentive_tx = msg.unwrap(self.private_key)
|
||||||
if self.is_my_incentive_tx(incentive_tx):
|
if self.is_my_incentive_tx(incentive_tx):
|
||||||
self.log("Receiving SphinxPacket. It's mine!")
|
self.log("Receiving SphinxPacket. It's mine!")
|
||||||
if msg.is_all_unwrapped() and msg.payload == self.REAL_PAYLOAD:
|
if msg.is_all_unwrapped():
|
||||||
self.env.process(self.p2p.broadcast(msg.payload))
|
if msg.payload == self.REAL_PAYLOAD:
|
||||||
|
self.env.process(self.p2p.broadcast(msg.payload))
|
||||||
else:
|
else:
|
||||||
# TODO: use Poisson delay
|
# TODO: use Poisson delay
|
||||||
yield self.env.timeout(random.randint(0, 5))
|
yield self.env.timeout(random.randint(0, 5))
|
||||||
|
|||||||
@ -7,28 +7,40 @@ from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PublicKey, X2
|
|||||||
|
|
||||||
|
|
||||||
class SphinxPacket:
|
class SphinxPacket:
|
||||||
|
PADDED_PAYLOAD_SIZE = 321
|
||||||
|
PAYLOAD_TRAIL_PADDING_SEPARATOR = b'\x01'
|
||||||
|
|
||||||
def __init__(self, public_keys: list[X25519PublicKey], attachments: list[Attachment], payload: bytes):
|
def __init__(self, public_keys: list[X25519PublicKey], attachments: list[Attachment], payload: bytes):
|
||||||
assert len(public_keys) == len(attachments)
|
assert len(public_keys) == len(attachments)
|
||||||
|
if len(payload) > self.PADDED_PAYLOAD_SIZE - len(self.PAYLOAD_TRAIL_PADDING_SEPARATOR):
|
||||||
|
raise ValueError("payload too long", len(payload))
|
||||||
|
payload += (self.PAYLOAD_TRAIL_PADDING_SEPARATOR
|
||||||
|
+ bytes(self.PADDED_PAYLOAD_SIZE - len(payload) - len(self.PAYLOAD_TRAIL_PADDING_SEPARATOR)))
|
||||||
|
|
||||||
ephemeral_private_key = X25519PrivateKey.generate()
|
ephemeral_private_key = X25519PrivateKey.generate()
|
||||||
ephemeral_public_key = ephemeral_private_key.public_key()
|
ephemeral_public_key = ephemeral_private_key.public_key()
|
||||||
shared_keys = [SharedSecret(ephemeral_private_key, pk) for pk in public_keys]
|
shared_keys = [SharedSecret(ephemeral_private_key, pk) for pk in public_keys]
|
||||||
self.header = SphinxHeader(ephemeral_public_key, shared_keys, attachments)
|
self._header = SphinxHeader(ephemeral_public_key, shared_keys, attachments)
|
||||||
self.payload = payload # TODO: encrypt payload
|
self._payload = payload # TODO: encrypt payload
|
||||||
|
|
||||||
def __bytes__(self):
|
def __bytes__(self):
|
||||||
return bytes(self.header) + self.payload
|
return bytes(self._header) + self._payload
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(bytes(self))
|
return len(bytes(self))
|
||||||
|
|
||||||
def unwrap(self, private_key: X25519PrivateKey) -> tuple[SphinxPacket, Attachment]:
|
def unwrap(self, private_key: X25519PrivateKey) -> tuple[SphinxPacket, Attachment]:
|
||||||
packet = deepcopy(self)
|
packet = deepcopy(self)
|
||||||
attachment = packet.header.unwrap_inplace(private_key)
|
attachment = packet._header.unwrap_inplace(private_key)
|
||||||
# TODO: decrypt packet.payload
|
# TODO: decrypt packet._payload
|
||||||
return packet, attachment
|
return packet, attachment
|
||||||
|
|
||||||
def is_all_unwrapped(self) -> bool:
|
def is_all_unwrapped(self) -> bool:
|
||||||
return self.header.is_all_unwrapped()
|
return self._header.is_all_unwrapped()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def payload(self) -> bytes:
|
||||||
|
return self._payload[:self._payload.rfind(self.PAYLOAD_TRAIL_PADDING_SEPARATOR)]
|
||||||
|
|
||||||
|
|
||||||
class SphinxHeader:
|
class SphinxHeader:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user