|
| 1 | +## Verifies the SDS message wire stays backward-compatible with pre-SDS-R nodes |
| 2 | +## (v0.2.x/v0.3.x): causalHistory is a repeated string of message IDs in field 3, |
| 3 | +## and the richer per-entry metadata (senderId, retrievalHint) rides in the |
| 4 | +## additive field 8, which older nodes skip. |
| 5 | + |
| 6 | +import std/unittest |
| 7 | +import protobuf_serialization |
| 8 | +import protobuf_serialization/pkg/results |
| 9 | +import ../sds/protobuf |
| 10 | +import ../sds/types/[sds_message, history_entry, sds_message_id] |
| 11 | + |
| 12 | +converter toParticipantID(s: string): SdsParticipantID = |
| 13 | + s.SdsParticipantID |
| 14 | + |
| 15 | +func toStr(b: seq[byte]): string = |
| 16 | + result = newString(b.len) |
| 17 | + if b.len > 0: |
| 18 | + copyMem(addr result[0], unsafeAddr b[0], b.len) |
| 19 | + |
| 20 | +func toBytes(s: string): seq[byte] = |
| 21 | + result = newSeq[byte](s.len) |
| 22 | + if s.len > 0: |
| 23 | + copyMem(addr result[0], unsafeAddr s[0], s.len) |
| 24 | + |
| 25 | +# How a pre-SDS-R node sees the message: field 3 is a repeated string of message |
| 26 | +# IDs; it knows nothing of the additive field 8. |
| 27 | +type OldSdsMessagePB {.proto3.} = object |
| 28 | + messageId {.fieldNumber: 1.}: Opt[seq[byte]] |
| 29 | + causalHistory {.fieldNumber: 3.}: seq[seq[byte]] |
| 30 | + channelId {.fieldNumber: 4.}: Opt[seq[byte]] |
| 31 | + content {.fieldNumber: 5.}: Opt[seq[byte]] |
| 32 | + |
| 33 | +suite "SDS v0.4 wire backward-compatibility": |
| 34 | + test "old node reads causal history IDs from a v0.4-encoded message": |
| 35 | + let msg = SdsMessage.init( |
| 36 | + messageId = "0xdeadbeef", |
| 37 | + lamportTimestamp = 7, |
| 38 | + causalHistory = @[ |
| 39 | + HistoryEntry.init("0xaaa", @[byte 1, 2, 3], "0xsender".SdsParticipantID), |
| 40 | + HistoryEntry.init("0xbbb"), |
| 41 | + ], |
| 42 | + channelId = "0xchannel", |
| 43 | + content = @[byte 9, 9, 9], |
| 44 | + bloomFilter = @[], |
| 45 | + senderId = "0xmsgsender".SdsParticipantID, |
| 46 | + repairRequest = @[], |
| 47 | + ) |
| 48 | + |
| 49 | + let wire = serializeMessage(msg).get() |
| 50 | + |
| 51 | + # An old node recovers the EXACT message IDs from field 3 and never sees the |
| 52 | + # senderId/hint that ride in field 8. |
| 53 | + let old = Protobuf.decode(wire, OldSdsMessagePB) |
| 54 | + check old.causalHistory.len == 2 |
| 55 | + check old.causalHistory[0].toStr == "0xaaa" |
| 56 | + check old.causalHistory[1].toStr == "0xbbb" |
| 57 | + check old.channelId.get(@[]).toStr == "0xchannel" |
| 58 | + check old.content.get(@[]) == @[byte 9, 9, 9] |
| 59 | + |
| 60 | + test "v0.4 round-trips message IDs, hints and senderIds": |
| 61 | + let msg = SdsMessage.init( |
| 62 | + messageId = "0xfeed", |
| 63 | + lamportTimestamp = 3, |
| 64 | + causalHistory = @[ |
| 65 | + HistoryEntry.init("0xaaa", @[byte 1, 2, 3], "0xsender".SdsParticipantID), |
| 66 | + HistoryEntry.init("0xbbb"), |
| 67 | + ], |
| 68 | + channelId = "0xchannel", |
| 69 | + content = @[byte 1], |
| 70 | + bloomFilter = @[], |
| 71 | + ) |
| 72 | + |
| 73 | + let decoded = deserializeMessage(serializeMessage(msg).get()).get() |
| 74 | + check decoded.causalHistory.len == 2 |
| 75 | + check decoded.causalHistory[0].messageId == "0xaaa" |
| 76 | + check decoded.causalHistory[0].retrievalHint == @[byte 1, 2, 3] |
| 77 | + check decoded.causalHistory[0].senderId.string == "0xsender" |
| 78 | + check decoded.causalHistory[1].messageId == "0xbbb" |
| 79 | + check decoded.causalHistory[1].retrievalHint.len == 0 |
| 80 | + check decoded.causalHistory[1].senderId.string.len == 0 |
| 81 | + |
| 82 | + test "v0.4 decodes a legacy message (field 3 IDs only, no field 8)": |
| 83 | + # Build a message the way a pre-SDS-R node would: field 3 = bare ID strings. |
| 84 | + let legacy = OldSdsMessagePB( |
| 85 | + messageId: Opt.some("0xlegacy".toBytes), |
| 86 | + causalHistory: @["0xaaa".toBytes, "0xbbb".toBytes], |
| 87 | + channelId: Opt.some("0xchannel".toBytes), |
| 88 | + content: Opt.some(@[byte 5]), |
| 89 | + ) |
| 90 | + let decoded = deserializeMessage(Protobuf.encode(legacy)).get() |
| 91 | + check decoded.causalHistory.len == 2 |
| 92 | + check decoded.causalHistory[0].messageId == "0xaaa" |
| 93 | + check decoded.causalHistory[0].retrievalHint.len == 0 |
| 94 | + check decoded.causalHistory[0].senderId.string.len == 0 |
| 95 | + check decoded.causalHistory[1].messageId == "0xbbb" |
0 commit comments