Skip to content

Commit

Permalink
fix: correct the use of inputIndex in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzzHui committed Sep 20, 2023
1 parent 0e05934 commit 8d7cf23
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
94 changes: 58 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,14 @@ 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 +622,38 @@ 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 +669,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 +728,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 +740,37 @@ 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 +847,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 +886,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;
}
}

0 comments on commit 8d7cf23

Please sign in to comment.