diff --git a/sds.nim b/sds.nim index 483e6de..58d1893 100644 --- a/sds.nim +++ b/sds.nim @@ -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] diff --git a/sds/bloom.nim b/sds/bloom.nim index 5a954d8..20854a2 100644 --- a/sds/bloom.nim +++ b/sds/bloom.nim @@ -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 diff --git a/sds/protobuf.nim b/sds/protobuf.nim index 026fabd..ba1b7ff 100644 --- a/sds/protobuf.nim +++ b/sds/protobuf.nim @@ -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, diff --git a/sds/protobufutil.nim b/sds/protobufutil.nim index 1ffdc15..3153017 100644 --- a/sds/protobufutil.nim +++ b/sds/protobufutil.nim @@ -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) diff --git a/sds/rolling_bloom_filter.nim b/sds/rolling_bloom_filter.nim index 6ba02c1..1fe2c79 100644 --- a/sds/rolling_bloom_filter.nim +++ b/sds/rolling_bloom_filter.nim @@ -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)) diff --git a/sds/sds_utils.nim b/sds/sds_utils.nim index 40fbca4..f1a68ca 100644 --- a/sds/sds_utils.nim +++ b/sds/sds_utils.nim @@ -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() diff --git a/sds/types/app_callbacks.nim b/sds/types/app_callbacks.nim index 4322277..985a97f 100644 --- a/sds/types/app_callbacks.nim +++ b/sds/types/app_callbacks.nim @@ -16,7 +16,7 @@ proc new*( periodicSyncCb: PeriodicSyncCallback = nil, retrievalHintProvider: RetrievalHintProvider = nil, ): T = - T( + return T( messageReadyCb: messageReadyCb, messageSentCb: messageSentCb, missingDependenciesCb: missingDependenciesCb, diff --git a/sds/types/bloom_filter.nim b/sds/types/bloom_filter.nim index 07b9835..8ca5be0 100644 --- a/sds/types/bloom_filter.nim +++ b/sds/types/bloom_filter.nim @@ -13,7 +13,7 @@ proc init*( mBits: int, intArray: seq[int], ): T = - T( + return T( capacity: capacity, errorRate: errorRate, kHashes: kHashes, diff --git a/sds/types/channel_context.nim b/sds/types/channel_context.nim index b75cf52..0346d18 100644 --- a/sds/types/channel_context.nim +++ b/sds/types/channel_context.nim @@ -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, diff --git a/sds/types/history_entry.nim b/sds/types/history_entry.nim index e5ce684..2435e6f 100644 --- a/sds/types/history_entry.nim +++ b/sds/types/history_entry.nim @@ -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) diff --git a/sds/types/incoming_message.nim b/sds/types/incoming_message.nim index 016b1fc..47206b4 100644 --- a/sds/types/incoming_message.nim +++ b/sds/types/incoming_message.nim @@ -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) diff --git a/sds/types/protobuf_error.nim b/sds/types/protobuf_error.nim index 47ad045..aff41df 100644 --- a/sds/types/protobuf_error.nim +++ b/sds/types/protobuf_error.nim @@ -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) diff --git a/sds/types/reliability_config.nim b/sds/types/reliability_config.nim index d51c0fc..f4e4e78 100644 --- a/sds/types/reliability_config.nim +++ b/sds/types/reliability_config.nim @@ -33,7 +33,7 @@ proc init*( syncMessageInterval: Duration = DefaultSyncMessageInterval, bufferSweepInterval: Duration = DefaultBufferSweepInterval, ): T = - T( + return T( bloomFilterCapacity: bloomFilterCapacity, bloomFilterErrorRate: bloomFilterErrorRate, maxMessageHistory: maxMessageHistory, diff --git a/sds/types/reliability_manager.nim b/sds/types/reliability_manager.nim index 958674d..9bfc244 100644 --- a/sds/types/reliability_manager.nim +++ b/sds/types/reliability_manager.nim @@ -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 diff --git a/sds/types/rolling_bloom_filter.nim b/sds/types/rolling_bloom_filter.nim index 624882d..1348384 100644 --- a/sds/types/rolling_bloom_filter.nim +++ b/sds/types/rolling_bloom_filter.nim @@ -22,7 +22,7 @@ proc init*( maxCapacity: int, messages: seq[SdsMessageID] = @[], ): T = - T( + return T( filter: filter, capacity: capacity, minCapacity: minCapacity, diff --git a/sds/types/sds_message.nim b/sds/types/sds_message.nim index 264fe4a..12f7add 100644 --- a/sds/types/sds_message.nim +++ b/sds/types/sds_message.nim @@ -19,7 +19,7 @@ proc init*( content: seq[byte], bloomFilter: seq[byte], ): T = - T( + return T( messageId: messageId, lamportTimestamp: lamportTimestamp, causalHistory: causalHistory, diff --git a/sds/types/unacknowledged_message.nim b/sds/types/unacknowledged_message.nim index b977be1..ff4a4d3 100644 --- a/sds/types/unacknowledged_message.nim +++ b/sds/types/unacknowledged_message.nim @@ -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)