Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extension CmxIrohClientRuntime {
return preparation
}
localBinding = nil
lastRegistrationRefreshState = nil
lifecyclePhase = .inactive
currentSnapshot = CmxIrohClientRuntimeSnapshot(
state: .inactive,
Expand Down Expand Up @@ -65,14 +66,18 @@ extension CmxIrohClientRuntime {
registrationRefreshTask = nil
registrationRefreshTaskID = nil
registrationRefreshPending = false
registrationRefreshPendingRequiresDiscovery = false
registrationRefreshEnabled = false
supervisorEventTask?.cancel()
supervisorEventTask = nil
await relayCoordinator?.deactivate()
relayCoordinator = nil
await contextRouter.clear()
authoritativeDiscovery = nil
if !preserveBinding { localBinding = nil }
if !preserveBinding {
localBinding = nil
lastRegistrationRefreshState = nil
}
await connectivityEngine.stop()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ extension CmxIrohClientRuntime {
expectedEndpointID: CmxIrohPeerIdentity,
revision: UInt64,
prefetchedDiscovery: CmxIrohDiscoveryResponse? = nil,
brokerPreparationComplete: Bool = false
brokerPreparationComplete: Bool = false,
allowReadOnlyRegistrationRefresh: Bool = false
) async throws -> ResolvedPolicy {
if !brokerPreparationComplete {
try await preparePolicyResolution(revision: revision)
Expand Down Expand Up @@ -34,11 +35,11 @@ extension CmxIrohClientRuntime {
)
}

// The supervisor snapshot and the live endpoint address are separate
// actor reads. Re-verify identity before every cached or online policy
// path so an endpoint replacement cannot inherit the old tuple.
let address = try await connectivityEngine.endpointAddress()
guard address.identity == expectedEndpointID else {
// Cached discovery may return before a registration payload is built.
// Verify the live endpoint address first so a replaced driver cannot
// inherit the prior generation's broker tuple.
let liveAddress = try await connectivityEngine.endpointAddress()
guard liveAddress.identity == expectedEndpointID else {
throw CmxIrohClientRuntimeError.invalidLocalBinding
}

Expand Down Expand Up @@ -74,31 +75,31 @@ extension CmxIrohClientRuntime {
prefetchedDiscoveryRejectedCachedBinding = true
}

let publicHints = Array(address.pathHints.compactMap {
$0.publicDisclosure(at: now())
}.prefix(CmxAttachEndpoint.maximumIrohPathHintCount))
let directPorts = CmxIrohDirectPorts(
localDirectAddresses: try await connectivityEngine.localDirectAddresses()
)
let payload = try CmxIrohRegistrationPayload(
deviceID: configuration.deviceID,
appInstanceID: configuration.appInstanceID,
tag: configuration.tag,
platform: .ios,
displayName: configuration.displayName,
endpointID: expectedEndpointID.endpointID,
identityGeneration: configuration.identity.generation,
pairingEnabled: false,
capabilities: configuration.capabilities,
pathHints: publicHints,
directPorts: directPorts,
now: now()
let payload = try await registrationPayload(
expectedEndpointID: expectedEndpointID
)
let signer = try CmxIrohRegistrationSigner(
identity: configuration.identity,
endpointID: expectedEndpointID.endpointID
)
let prepared = try signer.prepare(payload: payload)
let refreshState = Self.registrationRefreshState(
payload: payload,
now: now()
)
if allowReadOnlyRegistrationRefresh,
shouldUseReadOnlyRegistrationRefresh(refreshState, at: now()) {
do {
return try await readOnlyResolvedPolicy(
expectation: expectation,
offlineExpectation: offlineExpectation
)
} catch CmxIrohClientRuntimeError.localBindingMissingFromDiscovery {
// The server no longer has this binding. Fall through to a
// signed mutation so the client self-heals instead of staying
// read-only forever.
}
}
Comment thread
azooz2003-bit marked this conversation as resolved.
Resolved
let registration: CmxIrohRegistrationResponse?
do {
registration = try await broker.register(prepared: prepared, signer: signer)
Expand Down Expand Up @@ -129,6 +130,9 @@ extension CmxIrohClientRuntime {
if let registration, !expectation.matches(registration.binding) {
throw CmxIrohClientRuntimeError.invalidLocalBinding
}
if registration != nil {
lastRegistrationRefreshState = refreshState
}
let discovery: CmxIrohDiscoveryResponse
do {
if let embedded = registration?.discovery,
Expand Down Expand Up @@ -197,6 +201,91 @@ extension CmxIrohClientRuntime {
)
}

func readOnlyResolvedPolicy(
expectation: CmxIrohLocalBindingExpectation,
offlineExpectation: CmxIrohClientOfflinePolicyExpectation?
) async throws -> ResolvedPolicy {
let discovery = try await discoverAuthoritatively()
guard discovery.routeContractVersion
== CmxIrohRegistrationPayload.currentRouteContractVersion else {
throw CmxIrohClientRuntimeError.routeContractMismatch
}
try validateRelayFleet(discovery.relayFleet)
let localMatches = discovery.bindings.filter(expectation.matches)
guard localMatches.count == 1,
let discovered = localMatches.first else {
throw CmxIrohClientRuntimeError.localBindingMissingFromDiscovery
}
return ResolvedPolicy(
registration: nil,
discovery: discovery,
binding: discovered,
expectation: expectation,
offlineExpectation: offlineExpectation,
cachedTargetBindings: [],
cachedLANRendezvous: nil
)
}

func shouldUseReadOnlyRegistrationRefresh(
_ state: CmxIrohRegistrationPublicationState,
at now: Date
) -> Bool {
!state.requiresPublication(after: lastRegistrationRefreshState, now: now)
}

func registrationRefreshState(
expectedEndpointID: CmxIrohPeerIdentity
) async throws -> CmxIrohRegistrationPublicationState {
let timestamp = now()
return CmxIrohRegistrationPublicationState(
payload: try await registrationPayload(
expectedEndpointID: expectedEndpointID,
timestamp: timestamp
),
now: timestamp
)
}

func registrationPayload(
expectedEndpointID: CmxIrohPeerIdentity,
timestamp: Date? = nil
) async throws -> CmxIrohRegistrationPayload {
// The supervisor snapshot and live endpoint address are separate actor
// reads. Re-verify identity before comparing or publishing the tuple.
let address = try await connectivityEngine.endpointAddress()
guard address.identity == expectedEndpointID else {
throw CmxIrohClientRuntimeError.invalidLocalBinding
}
let payloadTime = timestamp ?? now()
let publicHints = Array(address.pathHints.compactMap {
$0.publicDisclosure(at: payloadTime)
}.prefix(CmxAttachEndpoint.maximumIrohPathHintCount))
return try CmxIrohRegistrationPayload(
deviceID: configuration.deviceID,
appInstanceID: configuration.appInstanceID,
tag: configuration.tag,
platform: .ios,
displayName: configuration.displayName,
endpointID: expectedEndpointID.endpointID,
identityGeneration: configuration.identity.generation,
pairingEnabled: false,
capabilities: configuration.capabilities,
pathHints: publicHints,
directPorts: CmxIrohDirectPorts(
localDirectAddresses: try await connectivityEngine.localDirectAddresses()
),
now: payloadTime
)
}

static func registrationRefreshState(
payload: CmxIrohRegistrationPayload,
now: Date
) -> CmxIrohRegistrationPublicationState {
CmxIrohRegistrationPublicationState(payload: payload, now: now)
}

func preparePolicyResolution(revision: UInt64) async throws {
try await pendingRevocations.revokePending(
accountID: configuration.accountID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,36 @@ extension CmxIrohClientRuntime {
scheduleRegistrationRefresh(revision: revision)
}

func scheduleRegistrationRefresh(revision: UInt64) {
func scheduleRegistrationRefresh(
revision: UInt64,
requiresDiscovery: Bool = false
) {
guard lifecyclePhase == .active,
lifecycleRevision == revision else { return }
guard registrationRefreshTask == nil else {
registrationRefreshPending = true
registrationRefreshPendingRequiresDiscovery =
registrationRefreshPendingRequiresDiscovery || requiresDiscovery
return
}
registrationRefreshPending = false
registrationRefreshPendingRequiresDiscovery = false
let refreshID = UUID()
registrationRefreshTaskID = refreshID
registrationRefreshTask = Task { [weak self] in
guard let self else { return .failed(.superseded) }
return try await self.refreshRegistration(
revision: revision,
refreshID: refreshID
refreshID: refreshID,
requiresDiscovery: requiresDiscovery
)
}
}

func refreshRegistration(
revision: UInt64,
refreshID: UUID
refreshID: UUID,
requiresDiscovery: Bool
) async throws -> CmxIrohLiveDiscoveryRefreshOutcome {
defer {
if lifecycleRevision == revision,
Expand All @@ -55,7 +63,12 @@ extension CmxIrohClientRuntime {
if registrationRefreshEnabled,
registrationRefreshPending,
lifecyclePhase == .active {
scheduleRegistrationRefresh(revision: revision)
let pendingRequiresDiscovery =
registrationRefreshPendingRequiresDiscovery
scheduleRegistrationRefresh(
revision: revision,
requiresDiscovery: pendingRequiresDiscovery
)
}
}
}
Expand All @@ -68,9 +81,19 @@ extension CmxIrohClientRuntime {
}
do {
let endpointID = try await connectivityEngine.localEndpointIdentity()
if !requiresDiscovery {
let state = try await registrationRefreshState(
expectedEndpointID: endpointID
)
guard state.requiresPublication(
after: lastRegistrationRefreshState,
now: now()
) else { return .refreshed }
}
let policy = try await resolvePolicy(
expectedEndpointID: endpointID,
revision: revision
revision: revision,
allowReadOnlyRegistrationRefresh: requiresDiscovery
Comment thread
azooz2003-bit marked this conversation as resolved.
Resolved
)
guard policy.binding.bindingID == previousBinding.bindingID else {
throw CmxIrohClientRuntimeError.invalidLocalBinding
Expand All @@ -82,8 +105,7 @@ extension CmxIrohClientRuntime {
endpointID: endpointID,
bindingID: policy.binding.bindingID
)
if policy.registration != nil,
let discovery = policy.discovery {
if let discovery = policy.discovery {
let published = await handleBinding(policy.binding, discovery)
try requireCurrent(revision)
guard published else { return .failed(.superseded) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ public actor CmxIrohClientRuntime {
private var connectivityReconciliationOperation: ConnectivityReconciliationOperation?
private var pendingConnectivityRevision: UInt64?
var registrationRefreshPending = false
var registrationRefreshPendingRequiresDiscovery = false
var registrationRefreshEnabled = false
var liveDiscoveryGeneration: UInt64 = 0
var authoritativeDiscovery: CmxIrohDiscoveryResponse?
var localBinding: CmxIrohBrokerBinding?
var lastRegistrationRefreshState: CmxIrohRegistrationPublicationState?
var registryContextProvider: CmxIrohRegistryContextProvider?
var currentSnapshot = CmxIrohClientRuntimeSnapshot(
state: .inactive,
Expand Down Expand Up @@ -401,7 +403,10 @@ public actor CmxIrohClientRuntime {
var mayScheduleFreshRequest = registrationRefreshTask != nil
var latestOutcome: CmxIrohLiveDiscoveryRefreshOutcome = .failed(.superseded)
if registrationRefreshTask == nil {
scheduleRegistrationRefresh(revision: lifecycleRevision)
scheduleRegistrationRefresh(
revision: lifecycleRevision,
requiresDiscovery: true
)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}
var lastAwaitedTaskID: UUID?
while lifecyclePhase == .active,
Expand All @@ -420,7 +425,10 @@ public actor CmxIrohClientRuntime {
}
guard mayScheduleFreshRequest else { return latestOutcome }
mayScheduleFreshRequest = false
scheduleRegistrationRefresh(revision: lifecycleRevision)
scheduleRegistrationRefresh(
revision: lifecycleRevision,
requiresDiscovery: true
)
}
return lifecyclePhase == .active
? latestOutcome
Expand Down Expand Up @@ -461,6 +469,7 @@ public actor CmxIrohClientRuntime {
lifecycleRevision &+= 1
let revision = lifecycleRevision
registrationRefreshPending = false
registrationRefreshPendingRequiresDiscovery = false
registrationRefreshEnabled = false
currentSnapshot = CmxIrohClientRuntimeSnapshot(
state: .starting,
Expand Down Expand Up @@ -597,6 +606,7 @@ public actor CmxIrohClientRuntime {
}
try requireCurrent(revision)
registrationRefreshPending = false
registrationRefreshPendingRequiresDiscovery = false
registrationRefreshEnabled = true
_ = try await refreshLiveDiscoveryThrowing()
try requireCurrent(revision)
Expand Down
Loading