-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Schema & types for OApp configuration
- Loading branch information
1 parent
5fb1838
commit 3c873ee
Showing
8 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { EndpointId } from "@layerzerolabs/lz-definitions" | ||
import { z } from "zod" | ||
|
||
export const NetworkNameSchema = z.string() | ||
|
||
export const EndpointIdSchema = z.nativeEnum(EndpointId) | ||
|
||
export const CoerceEndpointIdSchema = z.coerce.number(EndpointIdSchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export * from "./property" | ||
export * from "./common/property" | ||
export * from "./common/schema" | ||
export * from "./oapp/schema" | ||
export * from "./oapp/types" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { z } from "zod" | ||
import { CoerceEndpointIdSchema } from "@/common/schema" | ||
import { endpointIdToStage } from "@layerzerolabs/lz-definitions" | ||
|
||
export const EnforcedOptionsSchema = z.object({ | ||
msgType: z.number().int().nonnegative().max(0xffff), | ||
options: z.string(), | ||
}) | ||
|
||
export const OAppConfigConnectionSchema = z.object({ | ||
enforcedOptions: EnforcedOptionsSchema, | ||
}) | ||
|
||
export const OAppConfigContractSchema = z.record(CoerceEndpointIdSchema, OAppConfigConnectionSchema) | ||
|
||
export const OAppConfigSchema = z.record(CoerceEndpointIdSchema, OAppConfigContractSchema).superRefine((config, ctx) => { | ||
Object.entries(config).forEach(([srcEndpointId, endpointConfig]) => { | ||
const srcStage = endpointIdToStage(CoerceEndpointIdSchema.parse(srcEndpointId)) | ||
|
||
Object.entries(endpointConfig).forEach(([dstEndpointId]) => { | ||
const dstStage = endpointIdToStage(CoerceEndpointIdSchema.parse(dstEndpointId)) | ||
|
||
if (srcStage === dstStage) return | ||
|
||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: `Invalid peer: endpoint ${srcEndpointId} on stage ${srcStage} cannot be connected to endpoint ${dstEndpointId} on stage ${dstStage}`, | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { z } from "zod" | ||
import { OAppConfigSchema, OAppConfigContractSchema } from "./schema" | ||
|
||
export type OAppConfig = z.TypeOf<typeof OAppConfigSchema> | ||
|
||
export type OAppConfigContract = z.TypeOf<typeof OAppConfigContractSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { expect } from "chai" | ||
import { describe } from "mocha" | ||
import { EndpointId } from "@layerzerolabs/lz-definitions" | ||
import { OAppConfigSchema } from "@/oapp/schema" | ||
|
||
describe("oapp/schema", () => { | ||
describe("OAppConfigSchema", () => { | ||
it("should pass with an empty object", () => { | ||
const config = {} | ||
|
||
expect(OAppConfigSchema.parse(config)).to.eql({}) | ||
}) | ||
|
||
it("should pass with a single network with no connections", () => { | ||
const config = { | ||
[EndpointId.AVALANCHE_MAINNET]: {}, | ||
} | ||
|
||
expect(OAppConfigSchema.parse(config)).to.eql(config) | ||
}) | ||
|
||
it("should fail with a single network with one misconfigured connection on the same stage", () => { | ||
const config = { | ||
[EndpointId.AVALANCHE_MAINNET]: { | ||
[EndpointId.ETHEREUM_MAINNET]: {}, | ||
}, | ||
} | ||
|
||
expect(() => OAppConfigSchema.parse(config)).to.throw() | ||
}) | ||
|
||
it("should pass with a single network with one connection on the same stage", () => { | ||
const config = { | ||
[EndpointId.AVALANCHE_MAINNET]: { | ||
[EndpointId.ETHEREUM_MAINNET]: { | ||
enforcedOptions: { | ||
msgType: 1, | ||
options: "", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expect(OAppConfigSchema.parse(config)).to.eql(config) | ||
}) | ||
|
||
it("should fail with a single network with one connection on a different stage", () => { | ||
const config = { | ||
[EndpointId.AVALANCHE_MAINNET]: { | ||
[EndpointId.AVALANCHE_TESTNET]: { | ||
enforcedOptions: { | ||
msgType: 1, | ||
options: "", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expect(() => OAppConfigSchema.parse(config)).to.throw( | ||
"Invalid peer: endpoint 106 on stage mainnet cannot be connected to endpoint 10106 on stage testnet" | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters