Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:6.0
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-libp2p open source project
Expand Down Expand Up @@ -35,16 +35,16 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.0.0")),

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

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

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

// Swift Protobuf
//.package(url: "https://github.com/apple/swift-protobuf.git", .exact("1.19.0")),
.package(url: "https://github.com/apple/swift-protobuf.git", .upToNextMajor(from: "1.33.3")),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -56,7 +56,7 @@ let package = Package(
.product(name: "Logging", package: "swift-log"),
.product(name: "PeerID", package: "swift-peer-id"),
.product(name: "Multiaddr", package: "swift-multiaddr"),
//.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
]
),
.testTarget(
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let package = Package(
...
dependencies: [
...
.package(name: "LibP2PCore", url: "https://github.com/swift-libp2p/swift-libp2p-core.git", .upToNextMajor(from: "0.0.1"))
.package(name: "LibP2PCore", url: "https://github.com/swift-libp2p/swift-libp2p-core.git", .upToNextMinor(from: "0.4.0"))
],
...
.target(
Expand All @@ -55,7 +55,7 @@ check out the [tests]() for more examples

import LibP2PCore

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

```

Expand Down
4 changes: 2 additions & 2 deletions Sources/LibP2PCore/ConnectionManager/ConnectionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import NIOCore

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

/// Peer Connectedness
public enum Connectedness {
public enum Connectedness: Sendable {
/// We have not yet attempted to connect to the peer in question
case NotConnected
/// We have an existing open connection to the peer in question
Expand Down
4 changes: 2 additions & 2 deletions Sources/LibP2PCore/Crypto/Crypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//

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

/// Cryptographically sign the given bytes
Expand All @@ -24,7 +24,7 @@ public protocol PrivateKey {
}

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

/// Verify that 'sig' is the signed hash of 'data'
Expand Down
2 changes: 1 addition & 1 deletion Sources/LibP2PCore/DHT/DHTCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public protocol DHTCore: Discovery, EventLoopService {

}

public protocol DHTRecord {
public protocol DHTRecord: Sendable {
var key: Data { get }
var value: Data { get }
var author: Data { get }
Expand Down
20 changes: 8 additions & 12 deletions Sources/LibP2PCore/Discovery/Discovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,33 @@ import NIOCore
import PeerID

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

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

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

/// Discovery is an interface that combines service advertisement and peer discovery
public protocol Discovery: Advertiser, Discoverer {
public protocol Discovery: Advertiser, Discoverer, Sendable {
static var key: String { get }
}

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

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

public struct DiscoverdPeers {
public struct DiscoverdPeers: Sendable {
public let cookie: Data?
public let peers: [PeerInfo]

Expand Down
2 changes: 1 addition & 1 deletion Sources/LibP2PCore/Event/PeerDiscoveryEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import Multiaddr

public enum PeerDiscoveryEvent {
public enum PeerDiscoveryEvent: Sendable {
//case ready
/// Every time a peer is discovered by a discovery service, it emits a peer event with the discovered peers information
case onPeer(PeerInfo)
Expand Down
18 changes: 14 additions & 4 deletions Sources/LibP2PCore/Identify/Identify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,30 @@ import Multiaddr
import NIOCore
import PeerID

public struct IdentifyMessage {
var listenAddresses: [Multiaddr] = []
public struct IdentifyMessage: Sendable {
var listenAddresses: [Multiaddr]
var observedAddress: Multiaddr?
var protocols: [String] = []
var protocols: [String]
var publicKey: PeerID?
var agentVersion: String?
var protocolVersion: String?
}

public protocol IdentityManager {
public protocol IdentityManager: Sendable {

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

}

extension IdentityManager {
public func ping(peer: PeerID) async throws -> TimeAmount {
try await self.ping(peer: peer).get()
}

public func ping(addr: Multiaddr) async throws -> TimeAmount {
try await self.ping(addr: addr).get()
}
}
2 changes: 1 addition & 1 deletion Sources/LibP2PCore/LibP2PCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

//public protocol Codecs { }

public enum Mode: String {
public enum Mode: String, Sendable {
case initiator
case listener
}
4 changes: 2 additions & 2 deletions Sources/LibP2PCore/Network/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import PeerID
/// - 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)
///
/// [LibP2P Connection Interface Documentation](https://github.com/libp2p/js-libp2p-interfaces/tree/master/src/connection)
public protocol Connection: AnyObject {
public protocol Connection: AnyObject, Sendable {

typealias NegotiationResult = (protocol: String, leftoverBytes: ByteBuffer?)
typealias SecuredResult = (securityCodec: String, remotePeer: PeerID?, warning: SecurityWarnings?)
Expand Down Expand Up @@ -173,7 +173,7 @@ public class ConnectionStats: CustomStringConvertible {
case closing
case closed
}
public enum Direction {
public enum Direction: Sendable {
case inbound
case outbound
}
Expand Down
11 changes: 6 additions & 5 deletions Sources/LibP2PCore/Network/Stream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
//
//===----------------------------------------------------------------------===//

import NIOConcurrencyHelpers
import NIOCore

public protocol Stream: AnyObject {
public protocol Stream: AnyObject, Sendable {
/// The underlying connection this stream belongs to
/// - Important: This must be a weakly held reference to our parent Connection
var connection: Connection? { get }
Expand Down Expand Up @@ -51,7 +52,7 @@ public protocol Stream: AnyObject {
func write(_ buffer: ByteBuffer) -> EventLoopFuture<Void>

/// A method that gets called when Stream Events are triggered
var on: ((LibP2PCore.StreamEvent) -> EventLoopFuture<Void>)? { get set }
var on: (@Sendable (LibP2PCore.StreamEvent) -> EventLoopFuture<Void>)? { get set }
//func on(_ event:LibP2P.StreamEvent) -> EventLoopFuture<Void>

/// Requests the Stream be closed on our end
Expand Down Expand Up @@ -83,8 +84,8 @@ public protocol _Stream: Stream {
/// The underlying connection this stream belongs to
/// - Important: This must be a weakly held reference to our parent Connection
/// Holding a reference to the parent Connection object, gives us a bridge to accessing the channel in order to kick off writes and access the channels allocater, etc...
var _connection: Connection? { get set }
var _streamState: LibP2PCore.StreamState { get set }
var _connection: NIOLockedValueBox<Connection?> { get }
var _streamState: NIOLockedValueBox<LibP2PCore.StreamState> { get }
var mode: LibP2PCore.Mode { get }
//var _channel:Channel? { get }
}
Expand Down Expand Up @@ -223,7 +224,7 @@ public final class StreamHandler {
}
}

public enum StreamState: UInt8 {
public enum StreamState: UInt8, Sendable {
case initialized = 0
case open
case receiveClosed
Expand Down
15 changes: 14 additions & 1 deletion Sources/LibP2PCore/Peer/Peer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import PeerID
//}

/// A peer (PeerID) and their known addresses (Multiaddr)
public struct PeerInfo {
public struct PeerInfo: Sendable {
public let peer: PeerID
public let addresses: [Multiaddr]

Expand All @@ -55,3 +55,16 @@ extension Multiaddr {
return try PeerID(cid: cid)
}
}

extension PeerInfo: CustomStringConvertible {
public var description: String {
if self.addresses.isEmpty {
return self.peer.description
}
return """
\(self.peer) [
\(self.addresses.map({ $0.description }).joined(separator: "\n") )
]
"""
}
}
Loading
Loading