Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Ortega committed Oct 9, 2023
1 parent ae9391d commit c303766
Showing 1 changed file with 70 additions and 38 deletions.
108 changes: 70 additions & 38 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 ProofNotFound(
LibServerManager.OutputEnum outputEnum,
uint256 inputIndex
uint256 inputIndexWithinEpoch
);

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

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

function checkOutputEnum(
uint256 inputIndex,
uint256 inputIndexWithinEpoch,
LibServerManager.OutputEnum expected
) internal view {
LibServerManager.OutputEnum obtained = outputEnums[inputIndex];
if (expected != obtained) {
revert UnexpectedOutputEnum(expected, obtained, inputIndex);
LibServerManager.OutputEnum obtained = outputEnums[
inputIndexWithinEpoch
]; if (expected != obtained) {
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 @@ -655,11 +668,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 @@ -711,7 +727,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 @@ -723,37 +739,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);
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 @@ -830,20 +853,29 @@ 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 @@ -859,13 +891,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

0 comments on commit c303766

Please sign in to comment.