Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct the use of inputIndex in tests #85

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
109 changes: 73 additions & 36 deletions onchain/rollups/test/foundry/dapp/CartesiDApp.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ contract CartesiDAppTest is TestBase {
error UnexpectedOutputEnum(
LibServerManager.OutputEnum expected,
LibServerManager.OutputEnum obtained,
uint256 inputIndex
uint256 inputIndexWithinEpoch
);

error InputIndexOutOfBounds(uint256 length, uint256 inputIndex);
error InputIndexWithinEpochOutOfBounds(
uint256 length,
uint256 inputIndexWithinEpoch
);

error ProofNotFound(
LibServerManager.OutputEnum outputEnum,
uint256 inputIndex
uint256 inputIndexWithinEpoch
);

CartesiDApp dapp;
Expand Down Expand Up @@ -622,29 +625,43 @@ contract CartesiDAppTest is TestBase {
vouchers[index] = Voucher(destination, payload);
}

function checkInputIndex(uint256 inputIndex) internal view {
function checkInputIndexWithinEpoch(
uint256 inputIndexWithinEpoch
) internal view {
uint256 length = outputEnums.length;
if (inputIndex >= length) {
revert InputIndexOutOfBounds(length, inputIndex);
if (inputIndexWithinEpoch >= length) {
revert InputIndexWithinEpochOutOfBounds(
length,
inputIndexWithinEpoch
);
}
}

function checkOutputEnum(
uint256 inputIndex,
uint256 inputIndexWithinEpoch,
LibServerManager.OutputEnum expected
) internal view {
LibServerManager.OutputEnum obtained = outputEnums[inputIndex];
LibServerManager.OutputEnum obtained = outputEnums[
inputIndexWithinEpoch
];
if (expected != obtained) {
revert UnexpectedOutputEnum(expected, obtained, inputIndex);
revert UnexpectedOutputEnum(
expected,
obtained,
inputIndexWithinEpoch
);
}
}

function getVoucher(
uint256 inputIndex
uint256 inputIndexWithinEpoch
) internal view returns (Voucher memory) {
checkInputIndex(inputIndex);
checkOutputEnum(inputIndex, LibServerManager.OutputEnum.VOUCHER);
return vouchers[inputIndex];
checkInputIndexWithinEpoch(inputIndexWithinEpoch);
checkOutputEnum(
inputIndexWithinEpoch,
LibServerManager.OutputEnum.VOUCHER
);
return vouchers[inputIndexWithinEpoch];
}

function getVoucher(
Expand All @@ -660,11 +677,14 @@ contract CartesiDAppTest is TestBase {
}

function getNotice(
uint256 inputIndex
uint256 inputIndexWithinEpoch
) internal view returns (bytes memory) {
checkInputIndex(inputIndex);
checkOutputEnum(inputIndex, LibServerManager.OutputEnum.NOTICE);
return notices[inputIndex];
checkInputIndexWithinEpoch(inputIndexWithinEpoch);
checkOutputEnum(
inputIndexWithinEpoch,
LibServerManager.OutputEnum.NOTICE
);
return notices[inputIndexWithinEpoch];
}

function getNotice(
Expand Down Expand Up @@ -716,7 +736,7 @@ contract CartesiDAppTest is TestBase {
}

function getInputPath(
string memory inputIndexStr
string memory inputIndexWithinEpochStr
) internal view returns (string memory) {
string memory root = vm.projectRoot();
return
Expand All @@ -728,37 +748,44 @@ contract CartesiDAppTest is TestBase {
"/helper",
"/input",
"/",
inputIndexStr,
inputIndexWithinEpochStr,
".json"
);
}

function getInputPath(
uint256 inputIndex
uint256 inputIndexWithinEpoch
) internal view returns (string memory) {
string memory inputIndexStr = vm.toString(inputIndex);
return getInputPath(inputIndexStr);
string memory inputIndexWithinEpochStr = vm.toString(
inputIndexWithinEpoch
);
return getInputPath(inputIndexWithinEpochStr);
}

function writeInput(
uint256 inputIndex,
uint256 inputIndexWithinEpoch,
address sender,
bytes memory payload
) internal {
string memory inputIndexStr = vm.toString(inputIndex);
string memory objectKey = string.concat("input", inputIndexStr);
string memory inputIndexWithinEpochStr = vm.toString(
inputIndexWithinEpoch
);
string memory objectKey = string.concat(
"input",
inputIndexWithinEpochStr
);
vm.serializeAddress(objectKey, "sender", sender);
string memory json = vm.serializeBytes(objectKey, "payload", payload);
string memory path = getInputPath(inputIndexStr);
string memory path = getInputPath(inputIndexWithinEpochStr);
vm.writeJson(json, path);
}

function removeExtraInputs() internal {
uint256 inputIndex = outputEnums.length;
string memory path = getInputPath(inputIndex);
uint256 inputIndexWithinEpoch = outputEnums.length;
string memory path = getInputPath(inputIndexWithinEpoch);
while (vm.isFile(path)) {
vm.removeFile(path);
path = getInputPath(++inputIndex);
path = getInputPath(++inputIndexWithinEpoch);
}
}

Expand Down Expand Up @@ -835,20 +862,30 @@ contract CartesiDAppTest is TestBase {
}

function getNoticeProof(
uint256 inputIndex
uint256 inputIndexWithinEpoch
) internal view returns (Proof memory) {
return getProof(LibServerManager.OutputEnum.NOTICE, inputIndex, 0);
return
getProof(
LibServerManager.OutputEnum.NOTICE,
inputIndexWithinEpoch,
0
);
}

function getVoucherProof(
uint256 inputIndex
uint256 inputIndexWithinEpoch
) internal view returns (Proof memory) {
return getProof(LibServerManager.OutputEnum.VOUCHER, inputIndex, 0);
return
getProof(
LibServerManager.OutputEnum.VOUCHER,
inputIndexWithinEpoch,
0
);
}

function getProof(
LibServerManager.OutputEnum outputEnum,
uint256 inputIndex,
uint256 inputIndexWithinEpoch,
uint256 outputIndex
) internal view returns (Proof memory) {
// Decode ABI-encoded data into raw struct
Expand All @@ -864,13 +901,13 @@ contract CartesiDAppTest is TestBase {
LibServerManager.Proof[] memory proofs = response.proofs;
for (uint256 i; i < proofs.length; ++i) {
LibServerManager.Proof memory proof = proofs[i];
if (proof.proves(outputEnum, inputIndex, outputIndex)) {
if (proof.proves(outputEnum, inputIndexWithinEpoch, outputIndex)) {
return convert(proof);
}
}

// If a proof was not found, raise an error
revert ProofNotFound(outputEnum, inputIndex);
revert ProofNotFound(outputEnum, inputIndexWithinEpoch);
}

function convert(
Expand Down
4 changes: 2 additions & 2 deletions onchain/rollups/test/foundry/util/LibServerManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ library LibServerManager {
function proves(
Proof memory p,
OutputEnum outputEnum,
uint256 inputIndex,
uint256 inputIndexWithinEpoch,
uint256 outputIndex
) internal pure returns (bool) {
return
p.outputEnum == outputEnum &&
p.inputIndex == inputIndex &&
p.validity.inputIndexWithinEpoch == inputIndexWithinEpoch &&
p.outputIndex == outputIndex;
}
}
Loading