Have handle take in a Payload object.

Since Payload is no longer directly parsed, meaning the message IDs 
can't be guaranteed to be set, the ID field is no longer public. A 
function has been added to calculate it on first access.
This commit is contained in:
Luke Parker 2020-07-15 05:54:36 -05:00
parent 6f0be31134
commit 438883dce3
4 changed files with 33 additions and 38 deletions

View File

@ -22,8 +22,5 @@ proc offer*(node: MVDSNode, msg: Message, epoch: int) {.inline.} =
proc updateEpoch*(node: MVDSNode, msgID: seq[byte], epoch: int): bool =
node.state.updateEpoch(msgID, epoch)
proc handle*(node: MVDSNode, msg: seq[byte]): tuple[messages: seq[Message], response: seq[byte]] =
var payload: Payload = Protobuf.decode(msg, Payload)
for msg in payload.messages:
msg.hash()
return (payload.messages, Protobuf.encode(node.state.handle(payload)))
proc handle*(node: MVDSNode, payload: Payload): tuple[messages: seq[Message], response: Payload] {.inline.} =
(payload.messages, node.state.handle(payload))

View File

@ -9,7 +9,7 @@ type
group* {.fieldNumber: 1.}: seq[byte] #Assigned into a group by developer, not protocol.
time* {.pint, fieldNumber: 2.}: int64
body* {.fieldNumber: 3.}: seq[byte]
id* {.dontSerialize.}: seq[byte]
id {.dontSerialize.}: seq[byte]
Payload* = object
acks* {.fieldNumber: 1.}: seq[seq[byte]]
@ -17,29 +17,27 @@ type
requests* {.fieldNumber: 3.}: seq[seq[byte]]
messages* {.fieldNumber: 4.}: seq[Message]
proc hash*(msg: Message) =
msg.id = @(
sha256.digest(
cast[seq[byte]]("MESSAGE_ID") &
msg.group &
@(uint64(msg.time).toBytesLE()) &
msg.body
).data
)
proc id*(msg: Message): seq[byte] =
if msg.id.len == 0:
msg.id = @(
sha256.digest(
cast[seq[byte]]("MESSAGE_ID") &
msg.group &
@(uint64(msg.time).toBytesLE()) &
msg.body
).data
)
result = msg.id
proc newMessage*(group: seq[byte], body: seq[byte], time: int64 = getTime().toUnix()): Message =
result = Message(
proc newMessage*(group: seq[byte], body: seq[byte], time: int64 = getTime().toUnix()): Message {.inline.} =
Message(
group: group,
time: time,
body: body
)
result.hash()
when defined MVDS_TESTS:
proc `==`*(lhs: Message, rhs: Message): bool {.inline.} =
(
(lhs.group == rhs.group) and
(lhs.time == rhs.time) and
(lhs.body == rhs.body) and
(lhs.id == rhs.id)
)
(lhs.group == rhs.group) and
(lhs.time == rhs.time) and
(lhs.body == rhs.body)

View File

@ -14,7 +14,7 @@ suite "Batch":
var
alice: MVDSNode = newMVDSNode(false)
bob: MVDSNode = newMVDSNode(false)
res: tuple[messages: seq[Message], response: seq[byte]]
res: tuple[messages: seq[Message], response: Payload]
var
groupID: seq[byte] = newSeq[byte](rand(100))
body: seq[byte] = newSeq[byte](rand(500))
@ -25,11 +25,11 @@ suite "Batch":
var msg: Message = newMessage(groupID, body)
test "Nothing":
check alice.handle(@[]) == res
check alice.handle(Payload()) == res
test "Single message":
alice.offer(msg, 0)
res = alice.handle(@[])
res = alice.handle(Payload())
check:
res.messages.len == 0
alice.state.messages.len == 1
@ -49,14 +49,14 @@ suite "Batch":
bob.state.messages.len == 0
check:
alice.handle(res.response) == (messages: @[], response: @[])
alice.handle(res.response) == (messages: @[], response: Payload())
alice.state.messages.len == 0
test "No ack":
alice.offer(msg, 0)
var origRes: tuple[messages: seq[Message], response: seq[byte]] = alice.handle(@[])
var origRes: tuple[messages: seq[Message], response: Payload] = alice.handle(Payload())
for i in 2 ..< 7:
res = alice.handle(@[])
res = alice.handle(Payload())
check:
res == origRes
res.messages.len == 0
@ -72,13 +72,13 @@ suite "Batch":
test "Epoch manipulation":
alice.offer(msg, 0)
var origRes: tuple[messages: seq[Message], response: seq[byte]] = alice.handle(@[])
var origRes: tuple[messages: seq[Message], response: Payload] = alice.handle(Payload())
check alice.updateEpoch(msg.id, 7)
for i in 1 ..< 7:
res = alice.handle(@[])
res = alice.handle(Payload())
check:
res.messages.len == 0
res.response.len == 0
res.response == Payload()
alice.state.messages.len == 1
alice.state.messages.hasKey(msg.id)
@ -90,7 +90,7 @@ suite "Batch":
)
check:
alice.handle(@[]) == origRes
alice.handle(Payload()) == origRes
alice.state.messages.len == 1
discard alice.handle(bob.handle(origRes.response).response)

View File

@ -14,10 +14,10 @@ suite "Interactive":
var
alice: MVDSNode = newMVDSNode(true)
bob: MVDSNode = newMVDSNode(true)
res: tuple[messages: seq[Message], response: seq[byte]]
res: tuple[messages: seq[Message], response: Payload]
test "Nothing":
check alice.handle(@[]) == res
check alice.handle(Payload()) == res
test "Single message":
var
@ -30,7 +30,7 @@ suite "Interactive":
var msg: Message = newMessage(groupID, body)
alice.offer(msg, 0)
res = alice.handle(@[])
res = alice.handle(Payload())
check:
res.messages.len == 0
alice.state.messages.len == 1
@ -77,5 +77,5 @@ suite "Interactive":
res = alice.handle(res.response)
check:
alice.handle(res.response) == (messages: @[], response: @[])
alice.handle(res.response) == (messages: @[], response: Payload())
alice.state.messages.len == 0