Skip to content

Commit b4d48f6

Browse files
authored
Swift 6 (#6)
* bumped swift-tools-version to 6.0 * bumped dependencies * sendable conformance * added async methods * import NIOConcurrencyHelpers for LockedValueBox type * added default description to PeerInfo * regenerated proto files with latest lib (sendable conformance) * removed unused comment / code * fixed deprecation warnings and moved from .bytes to .byteArray * fixed deprecation warnings and moved from .bytes to .byteArray * added TODO comment * migrated to swift-testing * license * formatting * updated readme
1 parent 462eddc commit b4d48f6

File tree

26 files changed

+278
-225
lines changed

26 files changed

+278
-225
lines changed

Package.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:6.0
22
//===----------------------------------------------------------------------===//
33
//
44
// This source file is part of the swift-libp2p open source project
@@ -35,16 +35,16 @@ let package = Package(
3535
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.0.0")),
3636

3737
// LibP2P Peer Identities
38-
.package(url: "https://github.com/swift-libp2p/swift-peer-id.git", .upToNextMinor(from: "0.1.0")),
38+
.package(url: "https://github.com/swift-libp2p/swift-peer-id.git", .upToNextMinor(from: "0.2.0")),
3939

4040
// LibP2P Multiaddr
41-
.package(url: "https://github.com/swift-libp2p/swift-multiaddr.git", .upToNextMinor(from: "0.1.0")),
41+
.package(url: "https://github.com/swift-libp2p/swift-multiaddr.git", .upToNextMinor(from: "0.2.0")),
4242

4343
// Logging
44-
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.0.0")),
44+
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.6.0")),
4545

4646
// Swift Protobuf
47-
//.package(url: "https://github.com/apple/swift-protobuf.git", .exact("1.19.0")),
47+
.package(url: "https://github.com/apple/swift-protobuf.git", .upToNextMajor(from: "1.33.3")),
4848
],
4949
targets: [
5050
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -56,7 +56,7 @@ let package = Package(
5656
.product(name: "Logging", package: "swift-log"),
5757
.product(name: "PeerID", package: "swift-peer-id"),
5858
.product(name: "Multiaddr", package: "swift-multiaddr"),
59-
//.product(name: "SwiftProtobuf", package: "swift-protobuf"),
59+
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
6060
]
6161
),
6262
.testTarget(

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ let package = Package(
3232
...
3333
dependencies: [
3434
...
35-
.package(name: "LibP2PCore", url: "https://github.com/swift-libp2p/swift-libp2p-core.git", .upToNextMajor(from: "0.0.1"))
35+
.package(name: "LibP2PCore", url: "https://github.com/swift-libp2p/swift-libp2p-core.git", .upToNextMinor(from: "0.4.0"))
3636
],
3737
...
3838
.target(
@@ -55,7 +55,7 @@ check out the [tests]() for more examples
5555

5656
import LibP2PCore
5757

58-
// You now have access to thing like PeerID, Multiaddr, Connections, Swift-NIO, etc...
58+
// You now have access to core components like PeerID, Multiaddr, Connections, Swift-NIO, etc...
5959

6060
```
6161

Sources/LibP2PCore/ConnectionManager/ConnectionManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import NIOCore
1616

1717
/// - TODO: Remove Optional Return Value
18-
public protocol ConnectionManager {
18+
public protocol ConnectionManager: Sendable {
1919
func getConnections(on: EventLoop?) -> EventLoopFuture<[Connection]>
2020
func getConnectionsToPeer(peer: PeerID, on: EventLoop?) -> EventLoopFuture<[Connection]>
2121
func getBestConnectionForPeer(peer: PeerID, on: EventLoop?) -> EventLoopFuture<Connection?>
@@ -42,7 +42,7 @@ public protocol ConnectionManager {
4242
}
4343

4444
/// Peer Connectedness
45-
public enum Connectedness {
45+
public enum Connectedness: Sendable {
4646
/// We have not yet attempted to connect to the peer in question
4747
case NotConnected
4848
/// We have an existing open connection to the peer in question

Sources/LibP2PCore/Crypto/Crypto.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
/// PrivKey represents a private key that can be used to generate a public key and sign data
16-
public protocol PrivateKey {
16+
public protocol PrivateKey: Sendable {
1717
var key: [UInt8] { get }
1818

1919
/// Cryptographically sign the given bytes
@@ -24,7 +24,7 @@ public protocol PrivateKey {
2424
}
2525

2626
/// PubKey is a public key that can be used to verifiy data signed with the corresponding private key
27-
public protocol PublicKey {
27+
public protocol PublicKey: Sendable {
2828
var key: [UInt8] { get }
2929

3030
/// Verify that 'sig' is the signed hash of 'data'

Sources/LibP2PCore/DHT/DHTCore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public protocol DHTCore: Discovery, EventLoopService {
3131

3232
}
3333

34-
public protocol DHTRecord {
34+
public protocol DHTRecord: Sendable {
3535
var key: Data { get }
3636
var value: Data { get }
3737
var author: Data { get }

Sources/LibP2PCore/Discovery/Discovery.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,33 @@ import NIOCore
1717
import PeerID
1818

1919
/// Advertiser is an interface for advertising services
20-
public protocol Advertiser {
20+
public protocol Advertiser: Sendable {
2121
/// Advertise advertises a service on the specified protocol and returns the registration TTL upon successful registration
22-
///
23-
/// - TODO: Add in options
2422
func advertise(service: String, options: Options?) -> EventLoopFuture<TimeAmount>
2523
}
2624

2725
/// Discoverer is an interface for peer discovery
28-
public protocol Discoverer {
26+
public protocol Discoverer: Sendable {
2927
/// FindPeers discovers peers providing a service
30-
///
31-
/// - TODO: Add in options
3228
func findPeers(supportingService: String, options: Options?) -> EventLoopFuture<DiscoverdPeers>
3329

3430
/// Allows LibP2P to register a callback / event handler on the Discovery mechanism to be alerted of various events, such as peer discovery.
35-
var onPeerDiscovered: ((_ peerInfo: PeerInfo) -> Void)? { get set }
31+
var onPeerDiscovered: (@Sendable (_ peerInfo: PeerInfo) -> Void)? { get set }
3632
}
3733

3834
/// Discovery is an interface that combines service advertisement and peer discovery
39-
public protocol Discovery: Advertiser, Discoverer {
35+
public protocol Discovery: Advertiser, Discoverer, Sendable {
4036
static var key: String { get }
4137
}
4238

43-
public protocol PeerDiscovery: EventLoopService {
39+
public protocol PeerDiscovery: EventLoopService, Sendable {
4440
/// Allows LibP2P to register a callback / event handler on the Discovery mechanism to be alerted of various events, such as peer discovery.
45-
var on: ((_ event: PeerDiscoveryEvent) -> Void)? { get set }
41+
var on: (@Sendable (_ event: PeerDiscoveryEvent) -> Void)? { get set }
4642
/// Allows LibP2P to query the Discovery mechanism for all of the peers it has encountered so far
4743
func knownPeers() -> EventLoopFuture<[PeerInfo]>
4844
}
4945

50-
public protocol Options {
46+
public protocol Options: Sendable {
5147
/// TTL is an option that provides a hint for the duration of an advertisement
5248
var ttl: TimeAmount { get }
5349
/// Limit is an option that provides an upper bound on the peer count for discovery
@@ -65,7 +61,7 @@ public struct StandardOptions: Options {
6561
public var limit: Int
6662
}
6763

68-
public struct DiscoverdPeers {
64+
public struct DiscoverdPeers: Sendable {
6965
public let cookie: Data?
7066
public let peers: [PeerInfo]
7167

Sources/LibP2PCore/Event/PeerDiscoveryEvent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import Multiaddr
1616

17-
public enum PeerDiscoveryEvent {
17+
public enum PeerDiscoveryEvent: Sendable {
1818
//case ready
1919
/// Every time a peer is discovered by a discovery service, it emits a peer event with the discovered peers information
2020
case onPeer(PeerInfo)

Sources/LibP2PCore/Identify/Identify.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,30 @@ import Multiaddr
1616
import NIOCore
1717
import PeerID
1818

19-
public struct IdentifyMessage {
20-
var listenAddresses: [Multiaddr] = []
19+
public struct IdentifyMessage: Sendable {
20+
var listenAddresses: [Multiaddr]
2121
var observedAddress: Multiaddr?
22-
var protocols: [String] = []
22+
var protocols: [String]
2323
var publicKey: PeerID?
2424
var agentVersion: String?
2525
var protocolVersion: String?
2626
}
2727

28-
public protocol IdentityManager {
28+
public protocol IdentityManager: Sendable {
2929

3030
func register()
3131
func ping(peer: PeerID) -> EventLoopFuture<TimeAmount>
3232
func ping(addr: Multiaddr) -> EventLoopFuture<TimeAmount>
3333
//func constructIdentifyMessage(req:Request) throws -> [UInt8]
3434

3535
}
36+
37+
extension IdentityManager {
38+
public func ping(peer: PeerID) async throws -> TimeAmount {
39+
try await self.ping(peer: peer).get()
40+
}
41+
42+
public func ping(addr: Multiaddr) async throws -> TimeAmount {
43+
try await self.ping(addr: addr).get()
44+
}
45+
}

Sources/LibP2PCore/LibP2PCore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
//public protocol Codecs { }
2424

25-
public enum Mode: String {
25+
public enum Mode: String, Sendable {
2626
case initiator
2727
case listener
2828
}

Sources/LibP2PCore/Network/Connection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import PeerID
2424
/// - Libp2p Connection ≈ Swift NIO Client (or maybe Libp2p Transport is more akin to the Client, and Channel is a parent wrapper that handles meta data surrounding the client, streams and peer)
2525
///
2626
/// [LibP2P Connection Interface Documentation](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/connection)
27-
public protocol Connection: AnyObject {
27+
public protocol Connection: AnyObject, Sendable {
2828

2929
typealias NegotiationResult = (protocol: String, leftoverBytes: ByteBuffer?)
3030
typealias SecuredResult = (securityCodec: String, remotePeer: PeerID?, warning: SecurityWarnings?)
@@ -173,7 +173,7 @@ public class ConnectionStats: CustomStringConvertible {
173173
case closing
174174
case closed
175175
}
176-
public enum Direction {
176+
public enum Direction: Sendable {
177177
case inbound
178178
case outbound
179179
}

0 commit comments

Comments
 (0)