Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Oct 23, 2024
1 parent 6b754db commit 3e8bbd7
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Boka/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Boka/Sources/Boka.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ struct Boka: AsyncParsableCommand {
}

let rpcConfig = rpc.asOptional.map { addr -> RPCConfig in
logger.info("RPC listen address: \(addr)")
let (address, port) = addr.getAddressAndPort()
return RPCConfig(listenAddress: address, port: Int(port))
}
Expand All @@ -130,6 +129,7 @@ struct Boka: AsyncParsableCommand {
}
}()

logger.info("Network key: \(networkKey.publicKey.data.toHexString())")
let networkConfig = NetworkConfig(
mode: validator ? .validator : .builder,
listenAddress: p2p,
Expand All @@ -144,7 +144,7 @@ struct Boka: AsyncParsableCommand {
handlerMiddleware: .tracing(prefix: "Handler")
)

let config = Node.Config(rpc: rpcConfig, network: networkConfig)
let config = Node.Config(rpc: rpcConfig, network: networkConfig, peers: peers)

let node: Node = if validator {
try await ValidatorNode(
Expand Down
6 changes: 1 addition & 5 deletions Networking/Sources/MsQuicSwift/NetAddr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import msquic
import Darwin
#endif

public struct NetAddr: Sendable {
public struct NetAddr: Sendable, Equatable, Hashable {
var quicAddr: QUIC_ADDR

public init?(address: String) {
Expand Down Expand Up @@ -38,17 +38,13 @@ public struct NetAddr: Sendable {
let (host, port, _) = parseQuicAddr(quicAddr) ?? ("::dead:beef", 0, false)
return (host, port)
}
}

extension NetAddr: Equatable {
public static func == (lhs: NetAddr, rhs: NetAddr) -> Bool {
var addr1 = lhs.quicAddr
var addr2 = rhs.quicAddr
return QuicAddrCompare(&addr1, &addr2) == 1
}
}

extension NetAddr: Hashable {
public func hash(into hasher: inout Hasher) {
var addr = quicAddr
let hash = QuicAddrHash(&addr)
Expand Down
4 changes: 4 additions & 0 deletions Networking/Sources/Networking/Peer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public final class Peer<Handler: StreamHandler>: Sendable {
)
}

public func listenAddress() throws -> NetAddr {
try listener.listenAddress()
}

public func connect(to address: NetAddr, mode: PeerMode) throws -> Connection<Handler> {
let conn = impl.connections.read { connections in
connections.byType[mode]?[address]
Expand Down
4 changes: 4 additions & 0 deletions Node/Sources/Node/NetworkingProtocol/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public final class Network: Sendable {
public func broadcast(kind: UniquePresistentStreamKind, message: any MessageProtocol) {
peer.broadcast(kind: kind, message: message)
}

public func listenAddress() throws -> NetAddr {
try peer.listenAddress()
}
}

struct HandlerDef: StreamHandler {
Expand Down
12 changes: 10 additions & 2 deletions Node/Sources/Node/NetworkingProtocol/NetworkManager.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Blockchain
import Foundation
import MsQuicSwift
import TracingUtils
import Utils

Expand All @@ -9,16 +10,23 @@ public final class NetworkManager: Sendable {
private let network: Network
// This is for development only
// Will assume those peers are also validators
private let devPeers: Set<NetAddr> = []
private let devPeers: Set<NetAddr>

public init(config: Network.Config, blockchain: Blockchain) throws {
public init(config: Network.Config, blockchain: Blockchain, devPeers: Set<NetAddr>) throws {
let handler = HandlerImpl(blockchain: blockchain)
network = try Network(
config: config,
protocolConfig: blockchain.config,
genesisHeader: blockchain.dataProvider.genesisBlockHash,
handler: handler
)
self.devPeers = devPeers

for peer in devPeers {
_ = try network.connect(to: peer, mode: .validator)
}

logger.info("P2P Listening on \(try! network.listenAddress())")
}
}

Expand Down
13 changes: 7 additions & 6 deletions Node/Sources/Node/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public typealias NetworkConfig = Network.Config

public class Node {
public struct Config {
public var rpc: Server.Config?
public var network: Network.Config
public var rpc: RPCConfig?
public var network: NetworkConfig
public var peers: [NetAddr]

public init(rpc: Server.Config?, network: Network.Config) {
public init(rpc: RPCConfig?, network: NetworkConfig, peers: [NetAddr] = []) {
self.rpc = rpc
self.network = network
self.peers = peers
}
}

Expand All @@ -33,8 +35,6 @@ public class Node {
eventBus: EventBus,
keystore: KeyStore
) async throws {
logger.debug("Initializing node")

let (genesisState, genesisBlock, protocolConfig) = try await genesis.load()
dataProvider = try await BlockchainDataProvider(InMemoryDataProvider(genesisState: genesisState, genesisBlock: genesisBlock))
timeProvider = SystemTimeProvider()
Expand All @@ -50,7 +50,8 @@ public class Node {

network = try NetworkManager(
config: config.network,
blockchain: blockchain
blockchain: blockchain,
devPeers: Set(config.peers)
)

rpcServer = try config.rpc.map {
Expand Down
2 changes: 1 addition & 1 deletion scripts/devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ create_node() {
local port=$((9000 + node_number))
local p2p_port=$((19000 + node_number))

tmux send-keys -t boka "$bin_path --chain=minimal --rpc 127.0.0.1:$port --validator --dev-seed $node_number --p2p 127.0.0.1:$p2p_port --peers=127.0.0.1:19001 --peers=127.0.0.1:19002 --peers=127.0.0.1:19003" C-m
tmux send-keys -t boka "$bin_path --chain=minimal --rpc 127.0.0.1:$port --validator --dev-seed $node_number --p2p 127.0.0.1:$p2p_port --peers=127.0.0.1:19001 --peers=127.0.0.1:19002 --peers=127.0.0.1:19003 --name=node-$node_number" C-m
}

# Start a new tmux session
Expand Down

0 comments on commit 3e8bbd7

Please sign in to comment.