Skip to content

Commit

Permalink
fix literal type discrimination
Browse files Browse the repository at this point in the history
  • Loading branch information
mfornos committed Jan 23, 2025
1 parent 098fb0b commit 7b6a295
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
6 changes: 4 additions & 2 deletions packages/client/src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Server, WebSocket } from 'mock-socket'
import nock from 'nock'

import samples from '../test/.data/samples.json'
import type { QueryResult } from './lib'
import { type QueryResult } from './lib'
import { AssetMetadata, StewardQueryArgs } from './steward/types'
import { XcmInputs, XcmMessagePayload } from './xcm/types'
import { isXcmHop, XcmInputs, XcmMessagePayload, XcmSent } from './xcm/types'

vi.mock('isows', () => {
return {
Expand Down Expand Up @@ -80,9 +80,11 @@ describe('OcelloidsClient', () => {
switch (called) {
case 1:
expect(isXcmSent(msg)).toBeTruthy()
expect(isXcmHop(msg)).toBeFalsy()
break
case 2:
expect(isXcmReceived(msg)).toBeTruthy()
expect(isXcmHop(msg)).toBeFalsy()
break
default:
//
Expand Down
24 changes: 14 additions & 10 deletions packages/server/src/services/agents/xcm/matching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,15 @@ export class MatchingEngine extends (EventEmitter as new () => TelemetryXcmEvent
outcome: 'Success', // always 'Success' since it's delivered
error: null,
}
const bridgeOutMsg: XcmBridge = new GenericXcmBridge(originMsg, waypointContext, {
bridgeMessageType: 'accepted',
bridgeKey,
forwardId,
})
const bridgeOutMsg: XcmBridge = new GenericXcmBridge(
originMsg as unknown as XcmBridge,
waypointContext,
{
bridgeMessageType: 'accepted',
bridgeKey,
forwardId,
},
)
const sublevelBridgeKey = `${bridgeKey}`
await this.#bridgeAccepted.put(sublevelBridgeKey, bridgeOutMsg)
await this.#janitor.schedule({
Expand Down Expand Up @@ -682,9 +686,9 @@ export class MatchingEngine extends (EventEmitter as new () => TelemetryXcmEvent
)
await this.#inbound.del(keys.hash)
if (isHop) {
this.#onXcmHopIn(msg, inMsg)
this.#onXcmHopIn(msg as XcmSent, inMsg)
} else {
this.#onXcmMatched(msg, inMsg)
this.#onXcmMatched(msg as XcmSent, inMsg)
}
} else {
// Still we don't know if the inbound is upgraded, so get both id and hash keys
Expand All @@ -707,9 +711,9 @@ export class MatchingEngine extends (EventEmitter as new () => TelemetryXcmEvent
)
await this.#inbound.batch().del(keys.id).del(keys.hash).write()
if (isHop) {
this.#onXcmHopIn(msg, inMsg)
this.#onXcmHopIn(msg as XcmSent, inMsg)
} else {
this.#onXcmMatched(msg, inMsg)
this.#onXcmMatched(msg as XcmSent, inMsg)
}
}
}
Expand Down Expand Up @@ -860,7 +864,7 @@ export class MatchingEngine extends (EventEmitter as new () => TelemetryXcmEvent

async #putHops(...entries: [string, XcmJourney][]) {
for (const [key, journey] of entries) {
await this.#hop.put(key, journey)
await this.#hop.put(key, journey as XcmSent)
await this.#janitor.schedule({
sublevel: prefixes.matching.hop,
key,
Expand Down
37 changes: 24 additions & 13 deletions packages/server/src/services/agents/xcm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ export interface XcmJourney {
* @public
*/
export interface XcmHop extends XcmJourney {
type: 'xcm.hop'
direction: 'out' | 'in'
}

Expand All @@ -388,29 +389,38 @@ export interface XcmHop extends XcmJourney {
*
* @public
*/
export type XcmReceived = XcmJourney
export interface XcmReceived extends XcmJourney {
type: 'xcm.received'
}

/**
* Event emitted when an XCM is sent.
*
* @public
*/
export type XcmSent = XcmJourney

export interface XcmSent extends XcmJourney {
type: 'xcm.sent'
}

/**
* Event emitted when an XCM is not received within a specified timeframe.
*
* @public
*/
export type XcmTimeout = XcmJourney
export interface XcmTimeout extends XcmJourney {
type: 'xcm.timeout'
}

/**
* Event emitted when an XCM is received on the relay chain
* for an HRMP message.
*
* @public
*/
export type XcmRelayed = XcmJourney
export interface XcmRelayed extends XcmJourney {
type: 'xcm.relayed'
}

export class XcmInbound extends BaseXcmEvent {
chainId: NetworkURN
Expand Down Expand Up @@ -443,7 +453,7 @@ abstract class BaseXcmJourney {
}

export class GenericXcmSent extends BaseXcmJourney implements XcmSent {
type: XcmNotificationType = 'xcm.sent'
type = 'xcm.sent' as const
waypoint: XcmWaypointContext
origin: XcmTerminusContext
destination: XcmTerminus
Expand Down Expand Up @@ -484,7 +494,7 @@ export class GenericXcmSent extends BaseXcmJourney implements XcmSent {
}

export class GenericXcmTimeout extends BaseXcmJourney implements XcmTimeout {
type: XcmNotificationType = 'xcm.timeout'
type = 'xcm.timeout' as const
waypoint: XcmWaypointContext
origin: XcmTerminusContext
destination: XcmTerminus
Expand All @@ -499,7 +509,7 @@ export class GenericXcmTimeout extends BaseXcmJourney implements XcmTimeout {
}

export class GenericXcmReceived extends BaseXcmJourney implements XcmReceived {
type: XcmNotificationType = 'xcm.received'
type = 'xcm.received' as const
waypoint: XcmWaypointContext
origin: XcmTerminusContext
destination: XcmTerminusContext
Expand Down Expand Up @@ -534,7 +544,7 @@ export class GenericXcmReceived extends BaseXcmJourney implements XcmReceived {
}

export class GenericXcmRelayed extends BaseXcmJourney implements XcmRelayed {
type: XcmNotificationType = 'xcm.relayed'
type = 'xcm.relayed' as const
waypoint: XcmWaypointContext
origin: XcmTerminusContext
destination: XcmTerminus
Expand Down Expand Up @@ -563,7 +573,7 @@ export class GenericXcmRelayed extends BaseXcmJourney implements XcmRelayed {
}

export class GenericXcmHop extends BaseXcmJourney implements XcmHop {
type: XcmNotificationType = 'xcm.hop'
type = 'xcm.hop' as const
direction: 'out' | 'in'
waypoint: XcmWaypointContext
origin: XcmTerminusContext
Expand All @@ -588,7 +598,8 @@ export type BridgeMessageType = 'accepted' | 'delivered' | 'received'
*
* @public
*/
export interface XcmBridge extends XcmSent {
export interface XcmBridge extends XcmJourney {
type: 'xcm.bridge'
bridgeKey: HexString
bridgeMessageType: BridgeMessageType
}
Expand All @@ -600,15 +611,15 @@ type XcmBridgeContext = {
}

export class GenericXcmBridge extends BaseXcmJourney implements XcmBridge {
type: XcmNotificationType = 'xcm.bridge'
type = 'xcm.bridge' as const
bridgeMessageType: BridgeMessageType
bridgeKey: HexString
waypoint: XcmWaypointContext
origin: XcmTerminusContext
destination: XcmTerminus

constructor(
originMsg: XcmSent,
originMsg: XcmBridge,
waypoint: XcmWaypointContext,
{ bridgeKey, bridgeMessageType, forwardId }: XcmBridgeContext,
) {
Expand All @@ -627,7 +638,7 @@ export class GenericXcmBridge extends BaseXcmJourney implements XcmBridge {
*
* @public
*/
export type XcmMessagePayload = XcmSent | XcmReceived | XcmRelayed | XcmHop | XcmBridge
export type XcmMessagePayload = XcmSent | XcmReceived | XcmRelayed | XcmHop | XcmBridge | XcmTimeout

export function isXcmSent(object: any): object is XcmSent {
return object.type !== undefined && object.type === 'xcm.sent'
Expand Down

0 comments on commit 7b6a295

Please sign in to comment.