Skip to content

Commit 4403392

Browse files
authored
Merge pull request #4 from swift-libp2p/soundness
Soundness
2 parents 2cfdabb + ece4c8c commit 4403392

15 files changed

+805
-456
lines changed

.license_header_template

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@@===----------------------------------------------------------------------===@@
2+
@@
3+
@@ This source file is part of the swift-libp2p open source project
4+
@@
5+
@@ Copyright (c) YEARS swift-libp2p project authors
6+
@@ Licensed under MIT
7+
@@
8+
@@ See LICENSE for license information
9+
@@ See CONTRIBUTORS for the list of swift-libp2p project authors
10+
@@
11+
@@ SPDX-License-Identifier: MIT
12+
@@
13+
@@===----------------------------------------------------------------------===@@

.licenseignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.github/workflows/configs/.flake8
2+
**/*.yml
3+
*.md
4+
*.txt
5+
Package.swift
6+
Package.resolved
7+
.gitignore
8+
.swift-format
9+
.licenseignore
10+
.license_header_template

.swift-format

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"version" : 1,
3+
"indentation" : {
4+
"spaces" : 4
5+
},
6+
"tabWidth" : 4,
7+
"fileScopedDeclarationPrivacy" : {
8+
"accessLevel" : "private"
9+
},
10+
"spacesAroundRangeFormationOperators" : false,
11+
"indentConditionalCompilationBlocks" : false,
12+
"indentSwitchCaseLabels" : false,
13+
"lineBreakAroundMultilineExpressionChainComponents" : false,
14+
"lineBreakBeforeControlFlowKeywords" : false,
15+
"lineBreakBeforeEachArgument" : true,
16+
"lineBreakBeforeEachGenericRequirement" : true,
17+
"lineLength" : 120,
18+
"maximumBlankLines" : 1,
19+
"respectsExistingLineBreaks" : true,
20+
"prioritizeKeepingFunctionOutputTogether" : true,
21+
"noAssignmentInExpressions" : {
22+
"allowedFunctions" : [
23+
"XCTAssertNoThrow",
24+
"XCTAssertThrowsError"
25+
]
26+
},
27+
"rules" : {
28+
"AllPublicDeclarationsHaveDocumentation" : false,
29+
"AlwaysUseLiteralForEmptyCollectionInit" : false,
30+
"AlwaysUseLowerCamelCase" : false,
31+
"AmbiguousTrailingClosureOverload" : true,
32+
"BeginDocumentationCommentWithOneLineSummary" : false,
33+
"DoNotUseSemicolons" : true,
34+
"DontRepeatTypeInStaticProperties" : true,
35+
"FileScopedDeclarationPrivacy" : true,
36+
"FullyIndirectEnum" : true,
37+
"GroupNumericLiterals" : true,
38+
"IdentifiersMustBeASCII" : true,
39+
"NeverForceUnwrap" : false,
40+
"NeverUseForceTry" : false,
41+
"NeverUseImplicitlyUnwrappedOptionals" : false,
42+
"NoAccessLevelOnExtensionDeclaration" : true,
43+
"NoAssignmentInExpressions" : true,
44+
"NoBlockComments" : true,
45+
"NoCasesWithOnlyFallthrough" : true,
46+
"NoEmptyTrailingClosureParentheses" : true,
47+
"NoLabelsInCasePatterns" : true,
48+
"NoLeadingUnderscores" : false,
49+
"NoParensAroundConditions" : true,
50+
"NoVoidReturnOnFunctionSignature" : true,
51+
"OmitExplicitReturns" : true,
52+
"OneCasePerLine" : true,
53+
"OneVariableDeclarationPerLine" : true,
54+
"OnlyOneTrailingClosureArgument" : true,
55+
"OrderedImports" : true,
56+
"ReplaceForEachWithForLoop" : true,
57+
"ReturnVoidInsteadOfEmptyTuple" : true,
58+
"UseEarlyExits" : false,
59+
"UseExplicitNilCheckInConditions" : false,
60+
"UseLetInEveryBoundCaseVariable" : false,
61+
"UseShorthandTypeNames" : true,
62+
"UseSingleLinePropertyGetter" : false,
63+
"UseSynthesizedInitializer" : false,
64+
"UseTripleSlashForDocumentationComments" : true,
65+
"UseWhereClausesInForLoops" : false,
66+
"ValidateDocumentationComments" : false
67+
}
68+
}

LICENSE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2025 swift-libp2p
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19+
OR OTHER DEALINGS IN THE SOFTWARE.

Package.swift

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,58 @@
1-
// swift-tools-version: 5.4
2-
// The swift-tools-version declares the minimum version of Swift required to build this package.
1+
// swift-tools-version: 5.5
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// This source file is part of the swift-libp2p open source project
5+
//
6+
// Copyright (c) 2022-2025 swift-libp2p project authors
7+
// Licensed under MIT
8+
//
9+
// See LICENSE for license information
10+
// See CONTRIBUTORS for the list of swift-libp2p project authors
11+
//
12+
// SPDX-License-Identifier: MIT
13+
//
14+
//===----------------------------------------------------------------------===//
315

416
import PackageDescription
517

618
let package = Package(
719
name: "swift-libp2p-mplex",
820
platforms: [
921
.macOS(.v10_15),
10-
.iOS(.v13)
22+
.iOS(.v13),
1123
],
1224
products: [
1325
// Products define the executables and libraries a package produces, and make them visible to other packages.
1426
.library(
1527
name: "LibP2PMPLEX",
16-
targets: ["LibP2PMPLEX"]),
28+
targets: ["LibP2PMPLEX"]
29+
)
1730
],
1831
dependencies: [
1932
// Dependencies declare other packages that this package depends on.
20-
33+
2134
// LibP2P Core Modules
2235
.package(url: "https://github.com/swift-libp2p/swift-libp2p.git", .upToNextMinor(from: "0.1.0")),
23-
36+
2437
// NIO Test Utils
2538
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
26-
39+
2740
],
2841
targets: [
2942
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
3043
// Targets can depend on other targets in this package, and on products in packages this package depends on.
3144
.target(
3245
name: "LibP2PMPLEX",
3346
dependencies: [
34-
.product(name: "LibP2P", package: "swift-libp2p"),
35-
]),
47+
.product(name: "LibP2P", package: "swift-libp2p")
48+
]
49+
),
3650
.testTarget(
3751
name: "LibP2PMPLEXTests",
3852
dependencies: [
3953
"LibP2PMPLEX",
4054
.product(name: "NIOTestUtils", package: "swift-nio"),
41-
]),
55+
]
56+
),
4257
]
4358
)

Sources/LibP2PMPLEX/LibP2PMPLEX.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
1-
import NIO
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the swift-libp2p open source project
4+
//
5+
// Copyright (c) 2022-2025 swift-libp2p project authors
6+
// Licensed under MIT
7+
//
8+
// See LICENSE for license information
9+
// See CONTRIBUTORS for the list of swift-libp2p project authors
10+
//
11+
// SPDX-License-Identifier: MIT
12+
//
13+
//===----------------------------------------------------------------------===//
14+
215
import LibP2P
316
import LibP2PCore
17+
import NIO
418

519
protocol MessageExtractable {
620
func messageBytes() -> ByteBuffer
721
}
822

9-
protocol MessageExtractableHandler:ChannelInboundHandler where InboundOut:MessageExtractable { }
23+
protocol MessageExtractableHandler: ChannelInboundHandler where InboundOut: MessageExtractable {}
1024

1125
public struct MPLEX: MuxerUpgrader {
12-
13-
public static let key:String = MPLEXStreamMultiplexer.protocolCodec
14-
let application:Application
26+
27+
public static let key: String = MPLEXStreamMultiplexer.protocolCodec
28+
let application: Application
1529

1630
public func upgradeConnection(_ conn: Connection, muxedPromise: EventLoopPromise<Muxer>) -> EventLoopFuture<Void> {
17-
return conn.channel.pipeline.addHandlers(
31+
conn.channel.pipeline.addHandlers(
1832
[
1933
ByteToMessageHandler(MPLEXFrameDecoder()),
2034
MessageToByteHandler(MPLEXFrameEncoder()),
21-
MPLEXStreamMultiplexer(connection: conn, muxedPromise: muxedPromise, supportedProtocols: [])
35+
MPLEXStreamMultiplexer(connection: conn, muxedPromise: muxedPromise, supportedProtocols: []),
2236
],
2337
position: .last
2438
)
2539
}
26-
40+
2741
public func printSelf() {
2842
application.logger.notice("Hi I'm MPLEX v6.7.0")
2943
}

Sources/LibP2PMPLEX/MPLEX/MPLEXErrorCode.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
//===----------------------------------------------------------------------===//
22
//
3+
// This source file is part of the swift-libp2p open source project
4+
//
5+
// Copyright (c) 2022-2025 swift-libp2p project authors
6+
// Licensed under MIT
7+
//
8+
// See LICENSE for license information
9+
// See CONTRIBUTORS for the list of swift-libp2p project authors
10+
//
11+
// SPDX-License-Identifier: MIT
12+
//
13+
//===----------------------------------------------------------------------===//
14+
//
15+
//===----------------------------------------------------------------------===//
16+
//
317
// This source file is part of the SwiftNIO open source project
418
//
519
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
@@ -11,12 +25,6 @@
1125
// SPDX-License-Identifier: Apache-2.0
1226
//
1327
//===----------------------------------------------------------------------===//
14-
//
15-
// MPLEXErrorCode.swift
16-
//
17-
//
18-
// Modified by Brandon Toms on 5/1/22.
19-
//
2028

2129
import NIOCore
2230

@@ -25,7 +33,7 @@ public struct MPLEXErrorCode {
2533
/// The underlying network representation of the error code.
2634
public var networkCode: Int {
2735
get {
28-
return Int(self._networkCode)
36+
Int(self._networkCode)
2937
}
3038
set {
3139
self._networkCode = UInt32(newValue)
@@ -85,9 +93,9 @@ public struct MPLEXErrorCode {
8593

8694
}
8795

88-
extension MPLEXErrorCode: Equatable { }
96+
extension MPLEXErrorCode: Equatable {}
8997

90-
extension MPLEXErrorCode: Hashable { }
98+
extension MPLEXErrorCode: Hashable {}
9199

92100
extension MPLEXErrorCode: CustomDebugStringConvertible {
93101
public var debugDescription: String {
@@ -121,28 +129,28 @@ extension MPLEXErrorCode: CustomDebugStringConvertible {
121129
}
122130
}
123131

124-
public extension UInt32 {
132+
extension UInt32 {
125133
/// Create a 32-bit integer corresponding to the given `MPLEXErrorCode`.
126-
init(mplexErrorCode code: MPLEXErrorCode) {
134+
public init(mplexErrorCode code: MPLEXErrorCode) {
127135
self = code._networkCode
128136
}
129137
}
130138

131-
public extension Int {
139+
extension Int {
132140
/// Create an integer corresponding to the given `MPLEXErrorCode`.
133-
init(mplexErrorCode code: MPLEXErrorCode) {
141+
public init(mplexErrorCode code: MPLEXErrorCode) {
134142
self = code.networkCode
135143
}
136144
}
137145

138-
public extension ByteBuffer {
146+
extension ByteBuffer {
139147
/// Serializes a `MPLEXErrorCode` into a `ByteBuffer` in the appropriate endianness
140148
/// for use in MPLEX.
141149
///
142150
/// - parameters:
143151
/// - code: The `MPLEXErrorCode` to serialize.
144152
/// - returns: The number of bytes written.
145-
mutating func write(mplexErrorCode code: MPLEXErrorCode) -> Int {
146-
return self.writeInteger(UInt32(mplexErrorCode: code))
153+
public mutating func write(mplexErrorCode code: MPLEXErrorCode) -> Int {
154+
self.writeInteger(UInt32(mplexErrorCode: code))
147155
}
148156
}

0 commit comments

Comments
 (0)