Fix gossip messages seqno according to spec

This commit is contained in:
Giovanni Petrantoni 2020-07-08 16:08:19 +09:00
parent a52763cc6d
commit 74dd254158
2 changed files with 5 additions and 4 deletions

View File

@ -39,6 +39,7 @@ type
privateKey*: PrivateKey privateKey*: PrivateKey
of HasPublic: of HasPublic:
key: Option[PublicKey] key: Option[PublicKey]
seqno*: uint64
proc id*(p: PeerInfo): string = proc id*(p: PeerInfo): string =
if not(isNil(p)): if not(isNil(p)):

View File

@ -19,6 +19,7 @@ import messages, protobuf,
../../../peerinfo, ../../../peerinfo,
../../../crypto/crypto, ../../../crypto/crypto,
../../../protobuf/minprotobuf ../../../protobuf/minprotobuf
import stew/endians2
logScope: logScope:
topics = "pubsubmessage" topics = "pubsubmessage"
@ -63,13 +64,12 @@ proc init*(
topic: string, topic: string,
sign: bool = true): Message {.gcsafe, raises: [CatchableError, Defect].} = sign: bool = true): Message {.gcsafe, raises: [CatchableError, Defect].} =
var seqno: seq[byte] = newSeq[byte](8) var seqno: seq[byte] = newSeq[byte](8)
if randomBytes(addr seqno[0], 8) <= 0: # peer is a ref obj
raise (ref CatchableError)(msg: "Cannot get randomness for message") inc p.seqno
result = Message( result = Message(
fromPeer: p.peerId, fromPeer: p.peerId,
data: data, data: data,
seqno: seqno, seqno: @(p.seqno.toBytesBE), # unefficient, fine for now
topicIDs: @[topic]) topicIDs: @[topic])
if sign and p.publicKey.isSome: if sign and p.publicKey.isSome: