use explicit return statement

This commit is contained in:
Ivan FB 2026-04-23 08:36:20 +02:00
parent 1c86443600
commit c6f1fa1846
No known key found for this signature in database
GPG Key ID: DF0C67A04C543270
17 changed files with 51 additions and 50 deletions

View File

@ -26,7 +26,7 @@ proc isAcknowledged*(
if rbf.isSome():
return rbf.get().contains(msg.message.messageId)
false
return false
proc reviewAckStatus(rm: ReliabilityManager, msg: SdsMessage) {.gcsafe.} =
var rbf: Option[RollingBloomFilter]

View File

@ -15,7 +15,7 @@ proc hashN(item: string, n: int, maxValue: int): int =
let
hashA = abs(hash(item)) mod maxValue # Use abs to handle negative hashes
hashB = abs(hash(item & " b")) mod maxValue # string concatenation
abs((hashA + n * hashB)) mod maxValue
return abs((hashA + n * hashB)) mod maxValue
{.pop.}
@ -30,7 +30,7 @@ proc getMOverNBitsForK*(
if probabilityTable[k][mOverN] < targetError:
return ok(mOverN)
err(
return err(
"Specified value of k and error rate not achievable using less than 4 bytes / element."
)
@ -68,7 +68,7 @@ proc initializeBloomFilter*(
mBits = capacity * nBitsPerElem
mInts = 1 + mBits div (sizeof(int) * 8)
ok(
return ok(
BloomFilter.init(
capacity = capacity,
errorRate = errorRate,
@ -80,19 +80,19 @@ proc initializeBloomFilter*(
proc `$`*(bf: BloomFilter): string =
## Prints the configuration of the Bloom filter.
"Bloom filter with $1 capacity, $2 error rate, $3 hash functions, and requiring $4 bits of memory." %
[
$bf.capacity,
formatFloat(bf.errorRate, format = ffScientific, precision = 1),
$bf.kHashes,
$(bf.mBits div bf.capacity),
]
return "Bloom filter with $1 capacity, $2 error rate, $3 hash functions, and requiring $4 bits of memory." %
[
$bf.capacity,
formatFloat(bf.errorRate, format = ffScientific, precision = 1),
$bf.kHashes,
$(bf.mBits div bf.capacity),
]
proc computeHashes(bf: BloomFilter, item: string): seq[int] =
var hashes = newSeq[int](bf.kHashes)
for i in 0 ..< bf.kHashes:
hashes[i] = hashN(item, i, bf.mBits)
hashes
return hashes
proc insert*(bf: var BloomFilter, item: string) =
## Insert an item (string) into the Bloom filter.
@ -116,4 +116,4 @@ proc lookup*(bf: BloomFilter, item: string): bool =
currentInt = bf.intArray[intAddress]
if currentInt != (currentInt or (1 shl bitOffset)):
return false
true
return true

View File

@ -24,7 +24,7 @@ proc encode*(msg: SdsMessage): ProtoBuffer =
pb.write(6, msg.bloomFilter)
pb.finish()
pb
return pb
proc decode*(T: type SdsMessage, buffer: seq[byte]): ProtobufResult[T] =
let pb = initProtoBuffer(buffer)
@ -66,7 +66,7 @@ proc decode*(T: type SdsMessage, buffer: seq[byte]): ProtobufResult[T] =
if not ?pb.getField(6, msg.bloomFilter):
msg.bloomFilter = @[] # Empty if not present
ok(msg)
return ok(msg)
proc extractChannelId*(data: seq[byte]): Result[SdsChannelID, ReliabilityError] =
## For extraction of channel ID without full message deserialization
@ -77,18 +77,18 @@ proc extractChannelId*(data: seq[byte]): Result[SdsChannelID, ReliabilityError]
return err(ReliabilityError.reDeserializationError)
if not fieldOk:
return err(ReliabilityError.reDeserializationError)
ok(channelId)
return ok(channelId)
except:
err(ReliabilityError.reDeserializationError)
return err(ReliabilityError.reDeserializationError)
proc serializeMessage*(msg: SdsMessage): Result[seq[byte], ReliabilityError] =
let pb = encode(msg)
ok(pb.buffer)
return ok(pb.buffer)
proc deserializeMessage*(data: seq[byte]): Result[SdsMessage, ReliabilityError] =
let msg = SdsMessage.decode(data).valueOr:
return err(ReliabilityError.reDeserializationError)
ok(msg)
return ok(msg)
proc serializeBloomFilter*(filter: BloomFilter): Result[seq[byte], ReliabilityError] =
var pb = initProtoBuffer()
@ -110,7 +110,7 @@ proc serializeBloomFilter*(filter: BloomFilter): Result[seq[byte], ReliabilityEr
return err(ReliabilityError.reSerializationError)
pb.finish()
ok(pb.buffer)
return ok(pb.buffer)
proc deserializeBloomFilter*(data: seq[byte]): Result[BloomFilter, ReliabilityError] =
if data.len == 0:
@ -143,7 +143,7 @@ proc deserializeBloomFilter*(data: seq[byte]): Result[BloomFilter, ReliabilityEr
copyMem(addr leVal, unsafeAddr bytes[start], sizeof(int))
littleEndian64(addr intArray[i], addr leVal)
ok(
return ok(
BloomFilter.init(
capacity = int(cap),
errorRate = float(errRate) / 1_000_000,

View File

@ -11,9 +11,9 @@ export minprotobuf, varint, protobuf_error
converter toProtobufError*(err: minprotobuf.ProtoError): ProtobufError =
case err
of minprotobuf.ProtoError.RequiredFieldMissing:
ProtobufError(kind: ProtobufErrorKind.MissingRequiredField, field: "unknown")
return ProtobufError(kind: ProtobufErrorKind.MissingRequiredField, field: "unknown")
else:
ProtobufError(kind: ProtobufErrorKind.DecodeFailure, error: err)
return ProtobufError(kind: ProtobufErrorKind.DecodeFailure, error: err)
proc missingRequiredField*(T: type ProtobufError, field: string): T =
ProtobufError.init(field)
return ProtobufError.init(field)

View File

@ -93,4 +93,4 @@ proc add*(rbf: var RollingBloomFilter, messageId: SdsMessageID) {.gcsafe.} =
proc contains*(rbf: RollingBloomFilter, messageId: SdsMessageID): bool =
## Checks if a message ID is in the rolling bloom filter.
rbf.filter.lookup(cast[string](messageId))
return rbf.filter.lookup(cast[string](messageId))

View File

@ -12,7 +12,7 @@ export
reliability_manager
proc defaultConfig*(): ReliabilityConfig =
ReliabilityConfig.init()
return ReliabilityConfig.init()
proc cleanup*(rm: ReliabilityManager) {.raises: [].} =
if not rm.isNil():
@ -62,7 +62,7 @@ proc updateLamportTimestamp*(
channelId = channelId, msgTs = msgTs, error = getCurrentExceptionMsg()
proc newHistoryEntry*(messageId: SdsMessageID, retrievalHint: seq[byte] = @[]): HistoryEntry =
HistoryEntry.init(messageId, retrievalHint)
return HistoryEntry.init(messageId, retrievalHint)
proc toCausalHistory*(messageIds: seq[SdsMessageID]): seq[HistoryEntry] =
return messageIds.mapIt(newHistoryEntry(it))
@ -116,13 +116,13 @@ proc getMessageHistory*(
withLock rm.lock:
try:
if channelId in rm.channels:
result = rm.channels[channelId].messageHistory
return rm.channels[channelId].messageHistory
else:
result = @[]
return @[]
except Exception:
error "Failed to get message history",
channelId = channelId, error = getCurrentExceptionMsg()
result = @[]
return @[]
proc getOutgoingBuffer*(
rm: ReliabilityManager, channelId: SdsChannelID
@ -130,13 +130,13 @@ proc getOutgoingBuffer*(
withLock rm.lock:
try:
if channelId in rm.channels:
result = rm.channels[channelId].outgoingBuffer
return rm.channels[channelId].outgoingBuffer
else:
result = @[]
return @[]
except Exception:
error "Failed to get outgoing buffer",
channelId = channelId, error = getCurrentExceptionMsg()
result = @[]
return @[]
proc getIncomingBuffer*(
rm: ReliabilityManager, channelId: SdsChannelID
@ -144,13 +144,13 @@ proc getIncomingBuffer*(
withLock rm.lock:
try:
if channelId in rm.channels:
result = rm.channels[channelId].incomingBuffer
return rm.channels[channelId].incomingBuffer
else:
result = initTable[SdsMessageID, IncomingMessage]()
return initTable[SdsMessageID, IncomingMessage]()
except Exception:
error "Failed to get incoming buffer",
channelId = channelId, error = getCurrentExceptionMsg()
result = initTable[SdsMessageID, IncomingMessage]()
return initTable[SdsMessageID, IncomingMessage]()
proc getOrCreateChannel*(
rm: ReliabilityManager, channelId: SdsChannelID
@ -160,7 +160,7 @@ proc getOrCreateChannel*(
rm.channels[channelId] = ChannelContext.new(
RollingBloomFilter.init(rm.config.bloomFilterCapacity, rm.config.bloomFilterErrorRate)
)
result = rm.channels[channelId]
return rm.channels[channelId]
except Exception:
error "Failed to get or create channel",
channelId = channelId, error = getCurrentExceptionMsg()

View File

@ -16,7 +16,7 @@ proc new*(
periodicSyncCb: PeriodicSyncCallback = nil,
retrievalHintProvider: RetrievalHintProvider = nil,
): T =
T(
return T(
messageReadyCb: messageReadyCb,
messageSentCb: messageSentCb,
missingDependenciesCb: missingDependenciesCb,

View File

@ -13,7 +13,7 @@ proc init*(
mBits: int,
intArray: seq[int],
): T =
T(
return T(
capacity: capacity,
errorRate: errorRate,
kHashes: kHashes,

View File

@ -13,7 +13,7 @@ type ChannelContext* = ref object
incomingBuffer*: Table[SdsMessageID, IncomingMessage]
proc new*(T: type ChannelContext, bloomFilter: RollingBloomFilter): T =
T(
return T(
lamportTimestamp: 0,
messageHistory: @[],
bloomFilter: bloomFilter,

View File

@ -5,4 +5,4 @@ type HistoryEntry* = object
retrievalHint*: seq[byte] ## Optional hint for efficient retrieval (e.g., Waku message hash)
proc init*(T: type HistoryEntry, messageId: SdsMessageID, retrievalHint: seq[byte] = @[]): T =
T(messageId: messageId, retrievalHint: retrievalHint)
return T(messageId: messageId, retrievalHint: retrievalHint)

View File

@ -10,4 +10,4 @@ type IncomingMessage* {.requiresInit.} = object
proc init*(
T: type IncomingMessage, message: SdsMessage, missingDeps: HashSet[SdsMessageID]
): T =
T(message: message, missingDeps: missingDeps)
return T(message: message, missingDeps: missingDeps)

View File

@ -16,7 +16,7 @@ type
ProtobufResult*[T] = Result[T, ProtobufError]
proc init*(T: type ProtobufError, error: minprotobuf.ProtoError): T =
T(kind: ProtobufErrorKind.DecodeFailure, error: error)
return T(kind: ProtobufErrorKind.DecodeFailure, error: error)
proc init*(T: type ProtobufError, field: string): T =
T(kind: ProtobufErrorKind.MissingRequiredField, field: field)
return T(kind: ProtobufErrorKind.MissingRequiredField, field: field)

View File

@ -33,7 +33,7 @@ proc init*(
syncMessageInterval: Duration = DefaultSyncMessageInterval,
bufferSweepInterval: Duration = DefaultBufferSweepInterval,
): T =
T(
return T(
bloomFilterCapacity: bloomFilterCapacity,
bloomFilterErrorRate: bloomFilterErrorRate,
maxMessageHistory: maxMessageHistory,

View File

@ -19,8 +19,9 @@ type ReliabilityManager* = ref object
onRetrievalHint*: RetrievalHintProvider
proc new*(T: type ReliabilityManager, config: ReliabilityConfig): T =
result = T(
let rm = T(
channels: initTable[SdsChannelID, ChannelContext](),
config: config,
)
result.lock.initLock()
rm.lock.initLock()
return rm

View File

@ -22,7 +22,7 @@ proc init*(
maxCapacity: int,
messages: seq[SdsMessageID] = @[],
): T =
T(
return T(
filter: filter,
capacity: capacity,
minCapacity: minCapacity,

View File

@ -19,7 +19,7 @@ proc init*(
content: seq[byte],
bloomFilter: seq[byte],
): T =
T(
return T(
messageId: messageId,
lamportTimestamp: lamportTimestamp,
causalHistory: causalHistory,

View File

@ -10,4 +10,4 @@ type UnacknowledgedMessage* = object
proc init*(
T: type UnacknowledgedMessage, message: SdsMessage, sendTime: Time, resendAttempts: int
): T =
T(message: message, sendTime: sendTime, resendAttempts: resendAttempts)
return T(message: message, sendTime: sendTime, resendAttempts: resendAttempts)