mirror of
https://github.com/vacp2p/mix.git
synced 2026-01-09 22:18:09 -05:00
Revert - remove mix message structure
This commit is contained in:
@@ -12,5 +12,4 @@ const
|
||||
addrSize* = (t * k) - delaySize # Address size
|
||||
messageSize* = 2413 - headerSize - k - powSize # Size of the message itself
|
||||
payloadSize* = messageSize + powSize + k # Total payload size
|
||||
packetSize* = headerSize + payloadSize # Total packet size
|
||||
mixMsgSize* = 8 + packetSize # Total mix protocol message size
|
||||
packetSize* = headerSize + payloadSize # Total packet size
|
||||
@@ -113,26 +113,4 @@ proc deserializeSphinxPacket*(data: openArray[byte]): SphinxPacket =
|
||||
result.Hdr.Alpha = data[0..(alphaSize - 1)]
|
||||
result.Hdr.Beta = data[alphaSize..(alphaSize + betaSize - 1)]
|
||||
result.Hdr.Gamma = data[(alphaSize + betaSize)..(headerSize - 1)]
|
||||
result.Payload = data[headerSize..^1]
|
||||
|
||||
type
|
||||
MixProtocolMsg* = object
|
||||
SeqNo: uint64
|
||||
SphinxPkt: SphinxPacket
|
||||
|
||||
proc initMixProtocolMsg*(seqNo: uint64, sphinxPkt: SphinxPacket): MixProtocolMsg =
|
||||
result.SeqNo = seqNo
|
||||
result.SphinxPkt = sphinxPkt
|
||||
|
||||
proc getMixProtocolMsg*(msg: MixProtocolMsg): (uint64, SphinxPacket) =
|
||||
(msg.SeqNo, msg.SphinxPkt)
|
||||
|
||||
proc serializeMixProtocolMsg*(msg: MixProtocolMsg): seq[byte] =
|
||||
let seqNoBytes = msg.SeqNo.toBytesBE()
|
||||
let sphinxPktBytes = serializeSphinxPacket(msg.SphinxPkt)
|
||||
result = @seqNoBytes & sphinxPktBytes
|
||||
|
||||
proc deserializeMixProtocolMsg*(data: openArray[byte]): MixProtocolMsg =
|
||||
assert len(data) == mixMsgSize, "Sphinx packet size must be exactly " & $(mixMsgSize) & " bytes"
|
||||
result.SeqNo = fromBytesBE(uint64, @data[0..7])
|
||||
result.SphinxPkt = deserializeSphinxPacket(data[8..^1])
|
||||
result.Payload = data[headerSize..^1]
|
||||
@@ -65,28 +65,4 @@ suite "serialization_tests":
|
||||
assert alpha == dAlpha, "Deserialized alpha does not match the original alpha"
|
||||
assert beta == dBeta, "Deserialized beta does not match the original beta"
|
||||
assert gamma == dGamma, "Deserialized gamma does not match the original gammaa"
|
||||
assert payload == dPayload, "Deserialized payload does not match the original payload"
|
||||
|
||||
test "serialize_and_deserialize_mix_protocol_msg":
|
||||
let header = initHeader(
|
||||
newSeq[byte](alphaSize),
|
||||
newSeq[byte](betaSize),
|
||||
newSeq[byte](gammaSize)
|
||||
)
|
||||
let payload = newSeq[byte](payloadSize)
|
||||
let packet = initSphinxPacket(header, payload)
|
||||
let seqNo: uint64 = 12345
|
||||
let mixProtocolMsg = initMixProtocolMsg(seqNo, packet)
|
||||
let serialized = serializeMixProtocolMsg(mixProtocolMsg)
|
||||
let deserialized = deserializeMixProtocolMsg(serialized)
|
||||
|
||||
let (dSeqNo, dPacket) = getMixProtocolMsg(deserialized)
|
||||
let (dHeader, dPayload) = getSphinxPacket(dPacket)
|
||||
let (alpha, beta, gamma) = getHeader(header)
|
||||
let (dAlpha, dBeta, dGamma) = getHeader(dHeader)
|
||||
|
||||
assert seqNo == dSeqNo, "Deserialized sequence number does not match the original sequence number"
|
||||
assert alpha == dAlpha, "Deserialized alpha does not match the original alpha"
|
||||
assert beta == dBeta, "Deserialized beta does not match the original beta"
|
||||
assert gamma == dGamma, "Deserialized gamma does not match the original gamma"
|
||||
assert payload == dPayload, "Deserialized payload does not match the original payload"
|
||||
Reference in New Issue
Block a user