Skip to content

Commit

Permalink
rearrange tests, improve testConfidentialResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroXbrock committed Jul 18, 2024
1 parent f69da9a commit 26269db
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions test/Forge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,6 @@ import "src/suavelib/Suave.sol";
import "src/Context.sol";
import {Suapp} from "src/Suapp.sol";

contract NumberSuapp is Suapp {
uint256 public number;

function onSetNumber(uint256 num) public {
number = num;
}

function setNumber() public confidential returns (bytes memory) {
uint256 num = abi.decode(Context.confidentialInputs(), (uint256));
return abi.encodeWithSelector(this.onSetNumber.selector, num);
}
}

contract TestConfidential is Test, SuaveEnabled {
function testConfidentialResponse() public {
NumberSuapp suapp = new NumberSuapp();

ctx.setConfidentialInputs(abi.encode(123));

bytes memory suaveCalldata = suapp.setNumber();
assertEq(suaveCalldata.length, 4 + 32);
}
}

contract TestForge is Test, SuaveEnabled {
address[] public addressList = [0xC8df3686b4Afb2BB53e60EAe97EF043FE03Fb829];

Expand Down Expand Up @@ -104,3 +80,45 @@ contract TestForge is Test, SuaveEnabled {
assertEq(found3, address(0));
}
}

contract TestConfidential is Test, SuaveEnabled {
/**
* @notice Assumes 36 bytes are given, returns `data[4..]`.
*/
function stripSelector(bytes memory data) internal pure returns (bytes memory trimmedData) {
trimmedData = new bytes(data.length - 4);
assembly {
mstore(add(trimmedData, 0x20), sub(mload(add(data, 0x20)), 0x04))
mstore(add(trimmedData, 0x20), mload(add(data, 0x24)))
}
}

function testConfidentialResponse() public {
NumberSuapp suapp = new NumberSuapp();

ctx.setConfidentialInputs(abi.encode(123));

// call confidential/offchain function, verify calldata
bytes memory suaveCalldata = suapp.setNumber();
assertEq(suaveCalldata.length, 4 + 32);
uint256 num = abi.decode(stripSelector(suaveCalldata), (uint256));
assertEq(num, 123);

// call onchain function, verify number
suapp.onSetNumber(num);
assertEq(suapp.number(), num);
}
}

contract NumberSuapp is Suapp {
uint256 public number;

function onSetNumber(uint256 num) public {
number = num;
}

function setNumber() public confidential returns (bytes memory) {
uint256 num = abi.decode(Context.confidentialInputs(), (uint256));
return abi.encodeWithSelector(this.onSetNumber.selector, num);
}
}

0 comments on commit 26269db

Please sign in to comment.