Skip to content

Commit

Permalink
fix: add additional validation for global parameters
Browse files Browse the repository at this point in the history
Default values for parameters masked the test cases for our global parameters
feature.

This commit adds additional tests for parameters without default values and
enhances the validations to support Global Parameters as an option.
  • Loading branch information
kanej committed Nov 15, 2024
1 parent 44ebf75 commit 0eb6f90
Show file tree
Hide file tree
Showing 17 changed files with 418 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export async function validateArtifactContractAt(
if (isModuleParameterRuntimeValue(future.address)) {
const param =
deploymentParameters[future.address.moduleId]?.[future.address.name] ??
deploymentParameters.$global?.[future.address.name] ??
future.address.defaultValue;

if (param === undefined) {
errors.push(
new IgnitionError(ERRORS.VALIDATION.MISSING_MODULE_PARAMETER, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deploy } from "../../../deploy";
import { IgnitionError } from "../../../errors";
import {
isAccountRuntimeValue,
Expand Down Expand Up @@ -55,6 +56,7 @@ export async function validateArtifactContractDeployment(
const missingParams = moduleParams.filter(
(param) =>
deploymentParameters[param.moduleId]?.[param.name] === undefined &&
deploymentParameters.$global?.[param.name] === undefined &&
param.defaultValue === undefined
);

Expand All @@ -69,7 +71,9 @@ export async function validateArtifactContractDeployment(
if (isModuleParameterRuntimeValue(future.value)) {
const param =
deploymentParameters[future.value.moduleId]?.[future.value.name] ??
deploymentParameters.$global?.[future.value.name] ??
future.value.defaultValue;

if (param === undefined) {
errors.push(
new IgnitionError(ERRORS.VALIDATION.MISSING_MODULE_PARAMETER, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export async function validateNamedContractAt(
if (isModuleParameterRuntimeValue(future.address)) {
const param =
deploymentParameters[future.address.moduleId]?.[future.address.name] ??
deploymentParameters.$global?.[future.address.name] ??
future.address.defaultValue;
if (param === undefined) {
errors.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export async function validateNamedContractCall(
const missingParams = moduleParams.filter(
(param) =>
deploymentParameters[param.moduleId]?.[param.name] === undefined &&
deploymentParameters.$global?.[param.name] === undefined &&
param.defaultValue === undefined
);

Expand All @@ -80,7 +81,9 @@ export async function validateNamedContractCall(
if (isModuleParameterRuntimeValue(future.value)) {
const param =
deploymentParameters[future.value.moduleId]?.[future.value.name] ??
deploymentParameters.$global?.[future.value.name] ??
future.value.defaultValue;

if (param === undefined) {
errors.push(
new IgnitionError(ERRORS.VALIDATION.MISSING_MODULE_PARAMETER, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export async function validateNamedContractDeployment(
const missingParams = moduleParams.filter(
(param) =>
deploymentParameters[param.moduleId]?.[param.name] === undefined &&
deploymentParameters.$global?.[param.name] === undefined &&
param.defaultValue === undefined
);

Expand All @@ -80,7 +81,9 @@ export async function validateNamedContractDeployment(
if (isModuleParameterRuntimeValue(future.value)) {
const param =
deploymentParameters[future.value.moduleId]?.[future.value.name] ??
deploymentParameters.$global?.[future.value.name] ??
future.value.defaultValue;

if (param === undefined) {
errors.push(
new IgnitionError(ERRORS.VALIDATION.MISSING_MODULE_PARAMETER, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export async function validateNamedEncodeFunctionCall(
const missingParams = moduleParams.filter(
(param) =>
deploymentParameters[param.moduleId]?.[param.name] === undefined &&
deploymentParameters.$global?.[param.name] === undefined &&
param.defaultValue === undefined
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export async function validateNamedStaticCall(
const missingParams = moduleParams.filter(
(param) =>
deploymentParameters[param.moduleId]?.[param.name] === undefined &&
deploymentParameters.$global?.[param.name] === undefined &&
param.defaultValue === undefined
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export async function validateSendData(
if (isModuleParameterRuntimeValue(future.to)) {
const param =
deploymentParameters[future.to.moduleId]?.[future.to.name] ??
deploymentParameters.$global?.[future.to.name] ??
future.to.defaultValue;

if (param === undefined) {
errors.push(
new IgnitionError(ERRORS.VALIDATION.MISSING_MODULE_PARAMETER, {
Expand All @@ -54,7 +56,9 @@ export async function validateSendData(
if (isModuleParameterRuntimeValue(future.value)) {
const param =
deploymentParameters[future.value.moduleId]?.[future.value.name] ??
deploymentParameters.$global?.[future.value.name] ??
future.value.defaultValue;

if (param === undefined) {
errors.push(
new IgnitionError(ERRORS.VALIDATION.MISSING_MODULE_PARAMETER, {
Expand Down
61 changes: 54 additions & 7 deletions packages/core/test/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,14 +762,61 @@ m.call(..., { id: "MyUniqueId"})`
(v) => v.type === FutureType.CONTRACT_CALL
);

await assert.isFulfilled(
validateNamedContractCall(
future as any,
setupMockArtifactResolver({ Another: fakeArtifact }),
{},
[]
)
const result = await validateNamedContractCall(
future as any,
setupMockArtifactResolver({ Another: fakeArtifact }),
{},
[]
);

assert.deepEqual(result, []);
});

it("should validate a missing module parameter if a global parameter is present", async () => {
const fakerArtifact: Artifact = {
...fakeArtifact,
abi: [
{
inputs: [
{
internalType: "bool",
name: "b",
type: "bool",
},
],
name: "test",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
],
};

const module = buildModule("Module1", (m) => {
const p = m.getParameter("p");

const another = m.contract("Another", fakerArtifact, []);
m.call(another, "test", [p]);

return { another };
});

const future = getFuturesFromModule(module).find(
(v) => v.type === FutureType.CONTRACT_CALL
);

const result = await validateNamedContractCall(
future as any,
setupMockArtifactResolver({ Another: fakeArtifact }),
{
$global: {
p: true,
},
},
[]
);

assert.deepEqual(result, []);
});

it("should not validate a missing module parameter (deeply nested)", async () => {
Expand Down
53 changes: 46 additions & 7 deletions packages/core/test/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,14 +688,53 @@ m.contract(..., { id: "MyUniqueId"})`

const [future] = getFuturesFromModule(module);

await assert.isFulfilled(
validateNamedContractDeployment(
future as any,
setupMockArtifactResolver({ Test: fakerArtifact }),
{},
[]
)
const result = await validateNamedContractDeployment(
future as any,
setupMockArtifactResolver({ Test: fakerArtifact }),
{},
[]
);

assert.deepEqual(result, []);
});

it("should validate a missing module parameter if a global parameter is present", async () => {
const fakerArtifact: Artifact = {
...fakeArtifact,
abi: [
{
inputs: [
{
internalType: "uint256",
name: "p",
type: "uint256",
},
],
stateMutability: "payable",
type: "constructor",
},
],
};

const module = buildModule("Module1", (m) => {
const p = m.getParameter("p");
const contract1 = m.contract("Test", [p]);

return { contract1 };
});

const [future] = getFuturesFromModule(module);

const result = await validateNamedContractDeployment(
future as any,
setupMockArtifactResolver({ Test: fakerArtifact }),
{
$global: { p: 123 },
},
[]
);

assert.deepEqual(result, []);
});

it("should not validate a missing module parameter (deeply nested)", async () => {
Expand Down
44 changes: 35 additions & 9 deletions packages/core/test/contractAt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,42 @@ m.contractAt(..., { id: "MyUniqueId"})`
(v) => v.type === FutureType.NAMED_ARTIFACT_CONTRACT_AT
);

await assert.isFulfilled(
validateNamedContractAt(
future as any,
setupMockArtifactResolver({
Another: fakeArtifact,
}),
{},
[]
)
const result = await validateNamedContractAt(
future as any,
setupMockArtifactResolver({
Another: fakeArtifact,
}),
{},
[]
);

assert.deepStrictEqual(result, []);
});

it("should validate a missing module parameter if a global parameter is present", async () => {
const module = buildModule("Module1", (m) => {
const p = m.getParameter("p");
const another = m.contractAt("Another", p);

return { another };
});

const future = getFuturesFromModule(module).find(
(v) => v.type === FutureType.NAMED_ARTIFACT_CONTRACT_AT
);

const result = await validateNamedContractAt(
future as any,
setupMockArtifactResolver({
Another: fakeArtifact,
}),
{
$global: { p: "0x1234" },
},
[]
);

assert.deepStrictEqual(result, []);
});

it("should not validate a module parameter of the wrong type", async () => {
Expand Down
46 changes: 37 additions & 9 deletions packages/core/test/contractAtFromArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,44 @@ m.contractAt(..., { id: "MyUniqueId"})`
(v) => v.type === FutureType.CONTRACT_AT
);

await assert.isFulfilled(
validateArtifactContractAt(
future as any,
setupMockArtifactResolver({
Another: fakeArtifact,
}),
{},
[]
)
const result = await validateArtifactContractAt(
future as any,
setupMockArtifactResolver({
Another: fakeArtifact,
}),
{},
[]
);

assert.deepStrictEqual(result, []);
});

it("should validate a missing module parameter if a global parameter is present", async () => {
const module = buildModule("Module1", (m) => {
const p = m.getParameter("p");
const another = m.contractAt("Another", fakeArtifact, p);

return { another };
});

const future = getFuturesFromModule(module).find(
(v) => v.type === FutureType.CONTRACT_AT
);

const result = await validateArtifactContractAt(
future as any,
setupMockArtifactResolver({
Another: fakeArtifact,
}),
{
$global: {
p: "0x1234",
},
},
[]
);

assert.deepStrictEqual(result, []);
});

it("should not validate a module parameter of the wrong type", async () => {
Expand Down
Loading

0 comments on commit 0eb6f90

Please sign in to comment.