Skip to content

Commit

Permalink
feat(Validator): adding CONTRACT_ID() to the blacklistValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
KristenPire committed May 16, 2024
1 parent 1c8d364 commit bce422a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 16 deletions.
2 changes: 1 addition & 1 deletion script/connectV2toV1.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.20;

import "forge-std/Script.sol";
import "../src/ControllerToken.sol";
import "../src/TokenFrontend.sol";
import "../src/tests/TokenFrontend.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

contract All is Script {
Expand Down
7 changes: 7 additions & 0 deletions src/BlacklistValidatorUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ contract BlacklistValidatorUpgradeable is
IValidator
{
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
// keccak256("monerium.validator")
bytes32 private constant ID = 0x5341d189213c4172d0c7256f80bc5f8e6350af3aaff7a029625d8dd94f0f82a5;

struct BlacklistValidatorStorage {
mapping(address => bool) blacklist;
Expand Down Expand Up @@ -83,6 +85,11 @@ contract BlacklistValidatorUpgradeable is
address newImplementation
) internal override onlyOwner {}

// CONTRACT_ID returns the contract identifier.
function CONTRACT_ID() public pure returns (bytes32) {
return ID;
}

/**
* @dev Adds an address to the blacklist.
* @param adversary Address to add.
Expand Down
6 changes: 6 additions & 0 deletions src/IValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ interface IValidator {
address to,
uint256 amount
) external returns (bool valid);

/**
* @dev Returns the contract identifier.
*/
function CONTRACT_ID() external view returns (bytes32);

}
2 changes: 2 additions & 0 deletions src/Token.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ contract Token is
__UUPSUpgradeable_init();
__SystemRole_init();
validator = IValidator(_validator);
require(validator.CONTRACT_ID() == keccak256("monerium.validator"), "Not Monerium Validator Contract");
}

// _authorizeUpgrade is a crucial part of the UUPS upgrade pattern in OpenZeppelin.
Expand Down Expand Up @@ -109,6 +110,7 @@ contract Token is
// Function to set the validator, restricted to owner
function setValidator(address _validator) public onlyOwner {
validator = IValidator(_validator);
require(validator.CONTRACT_ID() == keccak256("monerium.validator"), "Not Monerium Validator Contract");
}

// Override transfer function to invoke validator
Expand Down
32 changes: 32 additions & 0 deletions src/tests/NotAMoneriumValidator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import "../IValidator.sol";

/**
* @title NotAMoneriumValidator
* @dev This contract is not a Monerium validator.
*/
contract NotAMoneriumValidator is IValidator {
bytes32 private constant ID =
0x0000000000000000000000000000000000000000000000000000000000000000;

/**
* @dev returns the contract identifier.
*/
function CONTRACT_ID() public pure returns (bytes32) {
return ID;
}

/**
* @dev Validates token transfer.
* Implements IValidator interface.
*/
function validate(
address ,
address ,
uint256
) external returns (bool valid) {}

}

File renamed without changes.
23 changes: 23 additions & 0 deletions test/Blacklist.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.20;
import "forge-std/Test.sol";
import "../src/Token.sol";
import "../src/BlacklistValidatorUpgradeable.sol";
import "../src/tests/NotAMoneriumValidator.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "forge-std/console.sol";

Expand All @@ -13,6 +14,7 @@ contract BlackListValidatorTest is Test {
Token public token;
ERC1967Proxy public proxy;
BlacklistValidatorUpgradeable public validator;
NotAMoneriumValidator public notAMoneriumValidator;

address owner = address(this);

Expand All @@ -25,6 +27,7 @@ contract BlackListValidatorTest is Test {
// Deploy the implementation contract
Token implementation = new Token();
BlacklistValidatorUpgradeable blacklistValidator = new BlacklistValidatorUpgradeable();
notAMoneriumValidator = new NotAMoneriumValidator();

// Deploy the proxy contract
bytes memory initDataProxy = abi.encodeWithSelector(
Expand Down Expand Up @@ -68,6 +71,26 @@ contract BlackListValidatorTest is Test {
vm.stopPrank();
}

function test_shouldNotAcceptANotMoneriumValidator() public {
vm.expectRevert("Not Monerium Validator Contract");
token.setValidator(address(notAMoneriumValidator));
}

function test_shouldNotAcceptANotMoneriumValidatorAtInitialization() public {
// Deploy the implementation contract
Token implementation = new Token();

bytes memory initData = abi.encodeWithSelector(
Token.initialize.selector,
"token",
"EURE",
address(notAMoneriumValidator)
);

vm.expectRevert("Not Monerium Validator Contract");
new ERC1967Proxy(address(implementation), initData);
}

function test_ban() public {
// Add user1 to blacklist
vm.prank(admin);
Expand Down
16 changes: 1 addition & 15 deletions test/ControllerToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.20;
import "forge-std/Test.sol";
import "../src/ControllerToken.sol";
import "../src/BlacklistValidatorUpgradeable.sol";
import "../src/tokenfrontend.sol";
import "../src/tests/tokenfrontend.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "forge-std/console.sol";

Expand Down Expand Up @@ -123,20 +123,6 @@ contract ControllerTokenTest is Test {
frontend.transferFrom(user1, user2, 1);
}

function test_from_banned_user_should_not_transferFrom() public {
// Add user1 to blacklist
vm.prank(admin);
validator.ban(user1);
assertTrue(validator.isBan(user1));

vm.prank(user1);
frontend.approve(user2, 1e18);

vm.prank(user2);
vm.expectRevert("Transfer not validated");
frontend.transferFrom(user1, user2, 1);
}

function testFail_shouldNotTransferIfNotFromFrontend() public {
vm.prank(user1);
token.transfer_withCaller(user1, user2, 1e18);
Expand Down

0 comments on commit bce422a

Please sign in to comment.