Skip to content

Commit

Permalink
feat: reshape tracking response schema
Browse files Browse the repository at this point in the history
  • Loading branch information
sdlyy committed Mar 7, 2024
1 parent 0bc4073 commit 5b21bfb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 41 deletions.
68 changes: 40 additions & 28 deletions packages/backend/src/tracking/http/TrackingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
OAppConfigurationRecord,
OAppConfigurationRepository,
} from '../../peripherals/database/OAppConfigurationRepository'
import { OAppDefaultConfigurationRepository } from '../../peripherals/database/OAppDefaultConfigurationRepository'
import {
OAppDefaultConfigurationRecord,
OAppDefaultConfigurationRepository,
} from '../../peripherals/database/OAppDefaultConfigurationRepository'
import {
OAppRecord,
OAppRepository,
Expand Down Expand Up @@ -103,6 +106,27 @@ describe(TrackingController.name, () => {
},
]

const mockDefaultConfigurations: OAppDefaultConfigurationRecord[] = [
{
sourceChainId: chainId,
targetChainId: ChainId.ETHEREUM,
protocolVersion: ProtocolVersion.V1,
configuration: defaultConfiguration,
},
{
sourceChainId: chainId,
targetChainId: ChainId.OPTIMISM,
protocolVersion: ProtocolVersion.V1,
configuration: defaultConfiguration,
},
{
sourceChainId: chainId,
targetChainId: ChainId.BSC,
protocolVersion: ProtocolVersion.V1,
configuration: defaultConfiguration,
},
]

const oAppRepo = mockObject<OAppRepository>({
getBySourceChain: () => Promise.resolve([oAppA, oAppB]),
})
Expand All @@ -112,27 +136,7 @@ describe(TrackingController.name, () => {
})
const oAppDefaultConfigRepo =
mockObject<OAppDefaultConfigurationRepository>({
getBySourceChain: (chainId) =>
Promise.resolve([
{
sourceChainId: chainId,
targetChainId: ChainId.ETHEREUM,
protocolVersion: ProtocolVersion.V1,
configuration: defaultConfiguration,
},
{
sourceChainId: chainId,
targetChainId: ChainId.OPTIMISM,
protocolVersion: ProtocolVersion.V1,
configuration: defaultConfiguration,
},
{
sourceChainId: chainId,
targetChainId: ChainId.BSC,
protocolVersion: ProtocolVersion.V1,
configuration: defaultConfiguration,
},
]),
getBySourceChain: () => Promise.resolve(mockDefaultConfigurations),
})

const controller = new TrackingController(
Expand All @@ -155,14 +159,15 @@ describe(TrackingController.name, () => {
configurations: [
{
targetChainId: ChainId.ETHEREUM,
configuration: defaultConfiguration,
isDefault: true,
},
{
targetChainId: ChainId.BSC,
configuration: customConfiguration,
changedConfiguration: {
relayer: customConfiguration.relayer,
oracle: customConfiguration.oracle,
},
isDefault: false,
diffs: ['relayer', 'oracle'],
},
],
},
Expand All @@ -175,18 +180,25 @@ describe(TrackingController.name, () => {
configurations: [
{
targetChainId: ChainId.ETHEREUM,
configuration: customConfiguration,
changedConfiguration: {
oracle: customConfiguration.oracle,
relayer: customConfiguration.relayer,
},
isDefault: false,
diffs: ['relayer', 'oracle'],
},
{
targetChainId: ChainId.OPTIMISM,
configuration: defaultConfiguration,
isDefault: true,
},
],
},
])
expect(result.defaultConfigurations).toEqual(
mockDefaultConfigurations.map((c) => ({
targetChainId: c.targetChainId,
configuration: c.configuration,
})),
)
})
})
})
Expand Down
23 changes: 19 additions & 4 deletions packages/backend/src/tracking/http/TrackingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class TrackingController {
return {
sourceChainId: chainId,
oApps: oAppsWithConfigs,

defaultConfigurations: defaultConfigurations.map((record) => ({
targetChainId: record.targetChainId,
configuration: record.configuration,
})),
}
}
}
Expand Down Expand Up @@ -104,16 +109,26 @@ function resolveConfigurationChanges(
})

if (diffKeys.length === 0) {
return {
...configuration,
const result = {
oAppId: configuration.oAppId,
targetChainId: configuration.targetChainId,
isDefault: true,
} as const

return result
}

return {
...configuration,
oAppId: configuration.oAppId,
targetChainId: configuration.targetChainId,
isDefault: false,
diffs: diffKeys,
changedConfiguration: diffKeys.reduce<Partial<OAppConfiguration>>(
(acc, key) => ({
...acc,
[key]: configuration.configuration[key],
}),
{},
),
} as const
})
}
26 changes: 17 additions & 9 deletions packages/libs/src/apis/TrackingApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ export {
ResolvedConfigurationWithAppId,
}

const OAppConfiguration = z.object({
relayer: branded(z.string(), EthereumAddress),
oracle: branded(z.string(), EthereumAddress),
inboundProofLibraryVersion: z.number(),
outboundProofType: z.number(),
outboundBlockConfirmations: z.number(),
inboundBlockConfirmations: z.number(),
})
type OAppConfiguration = z.infer<typeof OAppConfiguration>

const BaseConfiguration = z.object({
targetChainId: branded(z.number(), ChainId),
configuration: z.object({
relayer: branded(z.string(), EthereumAddress),
oracle: branded(z.string(), EthereumAddress),
inboundProofLibraryVersion: z.number(),
outboundProofType: z.number(),
outboundBlockConfirmations: z.number(),
inboundBlockConfirmations: z.number(),
}),
isDefault: z.boolean(),
})
type BaseConfiguration = z.infer<typeof BaseConfiguration>
Expand All @@ -31,7 +33,7 @@ type DefaultConfiguration = z.infer<typeof DefaultConfiguration>

const ChangedConfiguration = BaseConfiguration.extend({
isDefault: z.literal(false),
diffs: z.array(z.string()), // keyof OAppConfiguration
changedConfiguration: OAppConfiguration.partial(),
})
type ChangedConfiguration = z.infer<typeof ChangedConfiguration>

Expand Down Expand Up @@ -66,6 +68,12 @@ type OAppWithConfigs = z.infer<typeof OAppWithConfigs>
const OAppsResponse = z.object({
sourceChainId: branded(z.number(), ChainId),
oApps: z.array(OAppWithConfigs),
defaultConfigurations: z.array(
z.object({
targetChainId: branded(z.number(), ChainId),
configuration: OAppConfiguration,
}),
),
})

type OAppsResponse = z.infer<typeof OAppsResponse>

0 comments on commit 5b21bfb

Please sign in to comment.