Skip to content

Commit

Permalink
Switch back to bytes32 and implement reverts on the comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
corydickson committed Aug 14, 2024
1 parent 5d9bd77 commit dd007a4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
45 changes: 41 additions & 4 deletions src/ProposalTypesConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ contract ProposalTypesConfigurator is IProposalTypesConfigurator {
uint8 proposalTypeId,
bytes32 txTypeHash,
bytes calldata encodedLimit,
bytes32[] memory parameters,
bytes[] memory parameters,
Comparators[] memory comparators
) external override onlyAdmin {
if (!_proposalTypesExists[proposalTypeId]) revert InvalidProposalType();
Expand Down Expand Up @@ -168,16 +168,53 @@ contract ProposalTypesConfigurator is IProposalTypesConfigurator {
return validScope.encodedLimits;
}

function getParameter(bytes calldata limit, uint256 startIdx, uint256 endIdx) public returns (bytes memory parameter) {
return limit[startIdx: endIdx + 1];
}

function validateProposedTx(
bytes calldata proposedTx,
uint8 proposalTypeId,
bytes32 txTypeHash
) public view returns (bool valid) {
) public returns (bool valid) {
Scope memory validScope = scopes[proposalTypeId][txTypeHash];
bytes memory scopeLimit = validScope.encodedLimits;

bytes4 selector = bytes4(scopeLimit);
require(selector == bytes4(proposedTx[:4]));

for (uint8 i = 0; i < validScope.parameters.length; i++) {
uint256 startIdx = 4;
uint256 endIdx;

if (i == 0) {
endIdx = validScope.parameters[i].length;
}

else {
endIdx = startIdx + validScope.parameters[i].length;
}

bytes32 param = bytes32(proposedTx[startIdx: endIdx + 1]);

if (validScope.comparators[i] == Comparators.EQUAL) {
bytes32 scopedParam = bytes32(this.getParameter(scopeLimit, startIdx, endIdx));
require(scopedParam == param);
}

if (validScope.comparators[i] == Comparators.LESS_THAN) {
require(param < bytes32(validScope.parameters[i]));
}

if (validScope.comparators[i] == Comparators.GREATER_THAN) {
require(param > bytes32(validScope.parameters[i]));
}

if (validScope.comparators[i] == Comparators.EMPTY) {
}

startIdx = startIdx + endIdx;
}

return selector == bytes4(proposedTx[:4]);
return true;
}
}
4 changes: 2 additions & 2 deletions src/interfaces/IProposalTypesConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface IProposalTypesConfigurator {
struct Scope {
bytes32 txTypeHash;
bytes encodedLimits;
bytes32[] parameters;
bytes[] parameters;
Comparators[] comparators;
}

Expand All @@ -71,7 +71,7 @@ interface IProposalTypesConfigurator {
uint8 proposalTypeId,
bytes32 txTypeHash,
bytes calldata encodedLimit,
bytes32[] memory parameters,
bytes[] memory parameters,
Comparators[] memory comparators
) external;

Expand Down
38 changes: 19 additions & 19 deletions test/ProposalTypesConfigurator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ contract SetProposalType is ProposalTypesConfiguratorTest {
vm.prank(admin);
bytes32 txTypeHash = keccak256("transfer(address,address,uint)");
bytes memory txEncoded = abi.encode("transfer(address,address,uint)", 0xdeadbeef, 0xdeadbeef, 10);
bytes32[] memory parameters = new bytes32[](1);
bytes[] memory parameters = new bytes[](1);
IProposalTypesConfigurator.Comparators[] memory comparators = new IProposalTypesConfigurator.Comparators[](1);

proposalTypesConfigurator.setScopeForProposalType(0, txTypeHash, txEncoded, parameters, comparators);
Expand Down Expand Up @@ -154,7 +154,7 @@ contract SetProposalType is ProposalTypesConfiguratorTest {
bytes memory txEncoded = abi.encode("transfer(address,address,uint)", 0xdeadbeef, 0xdeadbeef, 10);
vm.expectRevert(IProposalTypesConfigurator.NotAdmin.selector);
proposalTypesConfigurator.setScopeForProposalType(
1, txTypeHash, txEncoded, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](1)
1, txTypeHash, txEncoded, new bytes[](1), new IProposalTypesConfigurator.Comparators[](1)
);
}

Expand All @@ -164,7 +164,7 @@ contract SetProposalType is ProposalTypesConfiguratorTest {
bytes memory txEncoded = abi.encode("transfer(address,address,uint)", 0xdeadbeef, 0xdeadbeef, 10);
vm.expectRevert(IProposalTypesConfigurator.InvalidProposalType.selector);
proposalTypesConfigurator.setScopeForProposalType(
2, txTypeHash, txEncoded, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](1)
2, txTypeHash, txEncoded, new bytes[](1), new IProposalTypesConfigurator.Comparators[](1)
);
}

Expand All @@ -174,7 +174,7 @@ contract SetProposalType is ProposalTypesConfiguratorTest {
bytes memory txEncoded = abi.encode("transfer(address,address,uint)", 0xdeadbeef, 0xdeadbeef, 10);
vm.expectRevert(IProposalTypesConfigurator.InvalidParameterConditions.selector);
proposalTypesConfigurator.setScopeForProposalType(
0, txTypeHash, txEncoded, new bytes32[](2), new IProposalTypesConfigurator.Comparators[](1)
0, txTypeHash, txEncoded, new bytes[](2), new IProposalTypesConfigurator.Comparators[](1)
);
}

Expand All @@ -183,12 +183,12 @@ contract SetProposalType is ProposalTypesConfiguratorTest {
bytes32 txTypeHash = keccak256("transfer(address,address,uint)");
bytes memory txEncoded = abi.encode("transfer(address,address,uint)", 0xdeadbeef, 0xdeadbeef, 10);
proposalTypesConfigurator.setScopeForProposalType(
0, txTypeHash, txEncoded, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](1)
0, txTypeHash, txEncoded, new bytes[](1), new IProposalTypesConfigurator.Comparators[](1)
);

vm.expectRevert(IProposalTypesConfigurator.NoDuplicateTxTypes.selector);
proposalTypesConfigurator.setScopeForProposalType(
0, txTypeHash, txEncoded, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](1)
0, txTypeHash, txEncoded, new bytes[](1), new IProposalTypesConfigurator.Comparators[](1)
);
vm.stopPrank();
}
Expand All @@ -208,13 +208,13 @@ contract UpdateScopeForProposalType is ProposalTypesConfiguratorTest {

bytes32 txTypeHash2 = keccak256("initialize(address,address)");
bytes memory txEncoded2 = abi.encode("initialize(address,address)", 0xdeadbeef, 0xdeadbeef);
bytes32[] memory parameters = new bytes32[](1);
bytes[] memory parameters = new bytes[](1);
IProposalTypesConfigurator.Comparators[] memory comparators = new IProposalTypesConfigurator.Comparators[](1);

proposalTypesConfigurator.setScopeForProposalType(0, txTypeHash1, txEncoded1, parameters, comparators);

IProposalTypesConfigurator.Scope memory scope = IProposalTypesConfigurator.Scope(
txTypeHash2, txEncoded2, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](1)
txTypeHash2, txEncoded2, new bytes[](1), new IProposalTypesConfigurator.Comparators[](1)
);
proposalTypesConfigurator.updateScopeForProposalType(0, scope);
vm.stopPrank();
Expand All @@ -232,7 +232,7 @@ contract UpdateScopeForProposalType is ProposalTypesConfiguratorTest {

vm.expectRevert(IProposalTypesConfigurator.InvalidProposalType.selector);
IProposalTypesConfigurator.Scope memory scope = IProposalTypesConfigurator.Scope(
txTypeHash, txEncoded, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](1)
txTypeHash, txEncoded, new bytes[](1), new IProposalTypesConfigurator.Comparators[](1)
);
proposalTypesConfigurator.updateScopeForProposalType(3, scope);
vm.stopPrank();
Expand All @@ -244,12 +244,12 @@ contract UpdateScopeForProposalType is ProposalTypesConfiguratorTest {
bytes memory txEncoded = abi.encode("transfer(address,address,uint)", 0xdeadbeef, 0xdeadbeef, 10);

proposalTypesConfigurator.setScopeForProposalType(
0, txTypeHash, txEncoded, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](1)
0, txTypeHash, txEncoded, new bytes[](1), new IProposalTypesConfigurator.Comparators[](1)
);

vm.expectRevert(IProposalTypesConfigurator.NoDuplicateTxTypes.selector);
IProposalTypesConfigurator.Scope memory scope = IProposalTypesConfigurator.Scope(
txTypeHash, txEncoded, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](1)
txTypeHash, txEncoded, new bytes[](1), new IProposalTypesConfigurator.Comparators[](1)
);
proposalTypesConfigurator.updateScopeForProposalType(0, scope);
vm.stopPrank();
Expand All @@ -261,7 +261,7 @@ contract UpdateScopeForProposalType is ProposalTypesConfiguratorTest {
bytes memory txEncoded = abi.encode("transfer(address,address,uint)", 0xdeadbeef, 0xdeadbeef, 10);

IProposalTypesConfigurator.Scope memory scope = IProposalTypesConfigurator.Scope(
txTypeHash, txEncoded, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](2)
txTypeHash, txEncoded, new bytes[](1), new IProposalTypesConfigurator.Comparators[](2)
);
vm.expectRevert(IProposalTypesConfigurator.InvalidParameterConditions.selector);
proposalTypesConfigurator.updateScopeForProposalType(0, scope);
Expand All @@ -281,7 +281,7 @@ contract getLimit is ProposalTypesConfiguratorTest {
bytes memory txEncoded = abi.encode("transfer(address,address,uint)", 0xdeadbeef, 0xdeadbeef, 10);

proposalTypesConfigurator.setScopeForProposalType(
0, txTypeHash, txEncoded, new bytes32[](1), new IProposalTypesConfigurator.Comparators[](1)
0, txTypeHash, txEncoded, new bytes[](1), new IProposalTypesConfigurator.Comparators[](1)
);
vm.stopPrank();

Expand All @@ -305,16 +305,16 @@ contract ValidateProposedTx is ProposalTypesConfiguratorTest {
address _to = makeAddr("to");
bytes memory txEncoded = abi.encodeWithSignature("transfer(address,address,uint256)", _from, _to, uint256(10));

bytes32[] memory parameters = new bytes32[](3);
parameters[0] = bytes32(uint256(uint160(_from)));
parameters[1] = bytes32(uint256(uint160(_to)));
parameters[2] = bytes32(uint256(10));
bytes[] memory parameters = new bytes[](3);
parameters[0] = abi.encode(uint256(uint160(_from)));
parameters[1] = abi.encode(uint256(uint160(_to)));
parameters[2] = abi.encode(uint256(10));

IProposalTypesConfigurator.Comparators[] memory comparators = new IProposalTypesConfigurator.Comparators[](3);

comparators[0] = IProposalTypesConfigurator.Comparators(1); // EQ
comparators[1] = IProposalTypesConfigurator.Comparators(1); // EQ
comparators[2] = IProposalTypesConfigurator.Comparators(2); // LESS THAN
comparators[2] = IProposalTypesConfigurator.Comparators(3); // GREATER THAN

vm.startPrank(admin);
proposalTypesConfigurator.setScopeForProposalType(0, txTypeHash, txEncoded, parameters, comparators);
Expand All @@ -324,7 +324,7 @@ contract ValidateProposedTx is ProposalTypesConfiguratorTest {
assertEq(limit, txEncoded);

//Generate calldata
bytes memory proposedTx = abi.encodeWithSignature("transfer(address,address,uint256)", _from, _to, uint256(5));
bytes memory proposedTx = abi.encodeWithSignature("transfer(address,address,uint256)", _from, _to, uint256(15));
bool valid = proposalTypesConfigurator.validateProposedTx(proposedTx, 0, txTypeHash);
assertTrue(valid);
}
Expand Down

0 comments on commit dd007a4

Please sign in to comment.