Skip to content

Commit

Permalink
refactor: Virtual functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PacificYield committed Nov 8, 2024
1 parent 6325371 commit 69c3636
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 43 deletions.
27 changes: 17 additions & 10 deletions contracts/governance/Comp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ abstract contract Comp is IComp, EncryptedERC20, Ownable2Step {
* @notice Delegate votes from `msg.sender` to `delegatee`.
* @param delegatee The address to delegate votes to.
*/
function delegate(address delegatee) public {
function delegate(address delegatee) public virtual {
return _delegate(msg.sender, delegatee);
}

Expand All @@ -105,7 +105,14 @@ abstract contract Comp is IComp, EncryptedERC20, Ownable2Step {
* @param r Half of the ECDSA signature pair.
* @param s Half of the ECDSA signature pair.
*/
function delegateBySig(address delegatee, uint256 nonce, uint256 expiry, uint8 v, bytes32 r, bytes32 s) public {
function delegateBySig(
address delegatee,
uint256 nonce,
uint256 expiry,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
bytes32 domainSeparator = keccak256(
abi.encode(DOMAIN_TYPEHASH, keccak256(bytes(name())), block.chainid, address(this))
);
Expand All @@ -122,7 +129,7 @@ abstract contract Comp is IComp, EncryptedERC20, Ownable2Step {
/**
* @notice See {IComp-getPriorVotesForGovernor}.
*/
function getPriorVotesForGovernor(address account, uint256 blockNumber) public returns (euint64 votes) {
function getPriorVotesForGovernor(address account, uint256 blockNumber) public virtual returns (euint64 votes) {
if (msg.sender != governor) {
revert GovernorInvalid();
}
Expand All @@ -140,7 +147,7 @@ abstract contract Comp is IComp, EncryptedERC20, Ownable2Step {
* @param account Account address
* @return votes Current (encrypted) votes.
*/
function getCurrentVotes(address account) public view returns (euint64 votes) {
function getCurrentVotes(address account) public view virtual returns (euint64 votes) {
uint32 nCheckpoints = numCheckpoints[account];
if (nCheckpoints > 0) {
votes = _checkpoints[account][nCheckpoints - 1].votes;
Expand All @@ -154,7 +161,7 @@ abstract contract Comp is IComp, EncryptedERC20, Ownable2Step {
* @param blockNumber The block number to get the vote balance at.
* @return votes Number of votes the account as of the given block.
*/
function getPriorVotes(address account, uint256 blockNumber) public view returns (euint64 votes) {
function getPriorVotes(address account, uint256 blockNumber) public view virtual returns (euint64 votes) {
if (blockNumber >= block.number) {
revert BlockNumberEqualOrHigherThanCurrentBlock();
}
Expand All @@ -166,12 +173,12 @@ abstract contract Comp is IComp, EncryptedERC20, Ownable2Step {
* @notice Set a governor contract.
* @param newGovernor New governor contract that can reencrypt/access votes.
*/
function setGovernor(address newGovernor) public onlyOwner {
function setGovernor(address newGovernor) public virtual onlyOwner {
governor = newGovernor;
emit NewGovernor(newGovernor);
}

function _delegate(address delegator, address delegatee) internal {
function _delegate(address delegator, address delegatee) internal virtual {
address currentDelegate = delegates[delegator];
euint64 delegatorBalance = _balances[delegator];
TFHE.allowThis(delegatorBalance);
Expand Down Expand Up @@ -217,7 +224,7 @@ abstract contract Comp is IComp, EncryptedERC20, Ownable2Step {
}
}

function _moveDelegates(address srcRep, address dstRep, euint64 amount) internal {
function _moveDelegates(address srcRep, address dstRep, euint64 amount) internal virtual {
if (srcRep != dstRep) {
if (srcRep != address(0)) {
uint32 srcRepNum = numCheckpoints[srcRep];
Expand All @@ -237,12 +244,12 @@ abstract contract Comp is IComp, EncryptedERC20, Ownable2Step {

/// @dev Original restrictions to transfer from/to address(0) are removed since they
/// are inherited.
function _transfer(address from, address to, euint64 amount, ebool isTransferable) internal override {
function _transfer(address from, address to, euint64 amount, ebool isTransferable) internal virtual override {
super._transfer(from, to, amount, isTransferable);
_moveDelegates(delegates[from], delegates[to], amount);
}

function _writeCheckpoint(address delegatee, uint32 nCheckpoints, euint64 newVotes) internal {
function _writeCheckpoint(address delegatee, uint32 nCheckpoints, euint64 newVotes) internal virtual {
if (nCheckpoints > 0 && _checkpoints[delegatee][nCheckpoints - 1].fromBlock == block.number) {
_checkpoints[delegatee][nCheckpoints - 1].votes = newVotes;
} else {
Expand Down
62 changes: 32 additions & 30 deletions contracts/governance/GovernorAlphaZama.sol
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
* In the original GovernorAlpha, the proposer can cancel only if
* her votes are still above the threshold.
*/
function cancel(uint256 proposalId) public {
function cancel(uint256 proposalId) public virtual {
Proposal memory proposal = _proposals[proposalId];

if (
Expand Down Expand Up @@ -305,7 +305,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
* @param value Encrypted value.
* @param inputProof Input proof.
*/
function castVote(uint256 proposalId, einput value, bytes calldata inputProof) public {
function castVote(uint256 proposalId, einput value, bytes calldata inputProof) public virtual {
return castVote(proposalId, TFHE.asEbool(value, inputProof));
}

Expand All @@ -314,7 +314,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
* @param proposalId Proposal id.
* @param support Support (true ==> votesFor, false ==> votesAgainst)
*/
function castVote(uint256 proposalId, ebool support) public {
function castVote(uint256 proposalId, ebool support) public virtual {
return _castVote(msg.sender, proposalId, support);
}

Expand All @@ -323,7 +323,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
* @dev Anyone can execute a proposal once it has been queued and the
* delay in the timelock is sufficient.
*/
function execute(uint256 proposalId) public payable {
function execute(uint256 proposalId) public payable virtual {
Proposal memory proposal = _proposals[proposalId];

if (proposal.state != ProposalState.Queued) {
Expand Down Expand Up @@ -360,7 +360,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
string[] memory signatures,
bytes[] memory calldatas,
string memory description
) public returns (uint256 proposalId) {
) public virtual returns (uint256 proposalId) {
{
uint256 length = targets.length;

Expand Down Expand Up @@ -449,10 +449,11 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {

/**
* @notice Queue a new proposal.
* @dev It can be done only if the proposal is adopted.
* @dev It can be done only if the proposal has succeeded.
* Anyone can queue a proposal.
* @param proposalId Proposal id.
*/
function queue(uint256 proposalId) public {
function queue(uint256 proposalId) public virtual {
Proposal memory proposal = _proposals[proposalId];

if (proposal.state != ProposalState.Succeeded) {
Expand All @@ -473,9 +474,10 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {

/**
* @notice Request the vote results to be decrypted.
* @dev Anyone can request the decryption of the vote.
* @param proposalId Proposal id.
*/
function requestVoteDecryption(uint256 proposalId) public {
function requestVoteDecryption(uint256 proposalId) public virtual {
if (_proposals[proposalId].state != ProposalState.Active) {
revert ProposalStateInvalid();
}
Expand Down Expand Up @@ -505,7 +507,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
* @param requestId Request id (from the Gateway)
* @param canInitiate Whether the proposal can be initiated.
*/
function callbackInitiateProposal(uint256 requestId, bool canInitiate) public onlyGateway {
function callbackInitiateProposal(uint256 requestId, bool canInitiate) public virtual onlyGateway {
uint256 proposalId = _requestIdToProposalId[requestId];

if (canInitiate) {
Expand All @@ -526,7 +528,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
uint256 requestId,
uint256 forVotesDecrypted,
uint256 againstVotesDecrypted
) public onlyGateway {
) public virtual onlyGateway {
uint256 proposalId = _requestIdToProposalId[requestId];

/// @dev It is safe to downcast since the original values were euint64.
Expand All @@ -545,7 +547,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
/**
* @dev Only callable by `owner`.
*/
function acceptTimelockAdmin() public onlyOwner {
function acceptTimelockAdmin() public virtual onlyOwner {
TIMELOCK.acceptAdmin();
}

Expand All @@ -554,7 +556,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
* @param newPendingAdmin Address of the new pending admin for the timelock.
* @param eta Eta for executing the transaction in the timelock.
*/
function executeSetTimelockPendingAdmin(address newPendingAdmin, uint256 eta) public onlyOwner {
function executeSetTimelockPendingAdmin(address newPendingAdmin, uint256 eta) public virtual onlyOwner {
TIMELOCK.executeTransaction(address(TIMELOCK), 0, "setPendingAdmin(address)", abi.encode(newPendingAdmin), eta);
}

Expand All @@ -563,7 +565,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
* @param newPendingAdmin Address of the new pending admin for the timelock.
* @param eta Eta for queuing the transaction in the timelock.
*/
function queueSetTimelockPendingAdmin(address newPendingAdmin, uint256 eta) public onlyOwner {
function queueSetTimelockPendingAdmin(address newPendingAdmin, uint256 eta) public virtual onlyOwner {
TIMELOCK.queueTransaction(address(TIMELOCK), 0, "setPendingAdmin(address)", abi.encode(newPendingAdmin), eta);
}

Expand All @@ -574,7 +576,7 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
* @param proposalId Proposal id.
* @return proposalInfo Proposal information.
*/
function getProposalInfo(uint256 proposalId) public view returns (ProposalInfo memory proposalInfo) {
function getProposalInfo(uint256 proposalId) public view virtual returns (ProposalInfo memory proposalInfo) {
Proposal memory proposal = _proposals[proposalId];
proposalInfo.proposer = proposal.proposer;
proposalInfo.state = proposal.state;
Expand Down Expand Up @@ -605,26 +607,12 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
* @return support The support for the account (true ==> vote for, false ==> vote against).
* @return votes The number of votes cast.
*/
function getReceipt(uint256 proposalId, address account) public view returns (bool, ebool, euint64) {
function getReceipt(uint256 proposalId, address account) public view virtual returns (bool, ebool, euint64) {
Receipt memory receipt = _accountReceiptForProposalId[proposalId][account];
return (receipt.hasVoted, receipt.support, receipt.votes);
}

function _queueOrRevert(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
) internal {
if (TIMELOCK.queuedTransactions(keccak256(abi.encode(target, value, signature, data, eta)))) {
revert ProposalActionsAlreadyQueued();
}

TIMELOCK.queueTransaction(target, value, signature, data, eta);
}

function _castVote(address voter, uint256 proposalId, ebool support) private {
function _castVote(address voter, uint256 proposalId, ebool support) internal virtual {
Proposal storage proposal = _proposals[proposalId];

if (proposal.state != ProposalState.Active) {
Expand Down Expand Up @@ -660,4 +648,18 @@ abstract contract GovernorAlphaZama is Ownable2Step, GatewayCaller {
/// There is no need to include them in the event.
emit VoteCast(voter, proposalId);
}

function _queueOrRevert(
address target,
uint256 value,
string memory signature,
bytes memory data,
uint256 eta
) internal virtual {
if (TIMELOCK.queuedTransactions(keccak256(abi.encode(target, value, signature, data, eta)))) {
revert ProposalActionsAlreadyQueued();
}

TIMELOCK.queueTransaction(target, value, signature, data, eta);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract contract EncryptedERC20Mintable is Ownable2Step, EncryptedERC20 {
* @notice Mint tokens.
* @param amount Amount of tokens to mint.
*/
function mint(uint64 amount) public onlyOwner {
function mint(uint64 amount) public virtual onlyOwner {
_unsafeMint(msg.sender, TFHE.asEuint64(amount));
/// @dev Since _totalSupply is not encrypted and _totalSupply >= balances[msg.sender],
/// the next line contains an overflow check for the encrypted operation above.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ abstract contract EncryptedERC20WithErrors is EncryptedERC20, EncryptedErrors {
/**
* @notice Returns the error code corresponding to `transferId`.
*/
function getErrorCodeForTransferId(uint256 transferId) external view virtual returns (euint8 errorCode) {
function getErrorCodeForTransferId(uint256 transferId) public view virtual returns (euint8 errorCode) {
return _errorCodeForTransferId[transferId];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract contract EncryptedERC20WithErrorsMintable is Ownable2Step, EncryptedERC
* @notice Mint tokens.
* @param amount Amount of tokens to mint.
*/
function mint(uint64 amount) public onlyOwner {
function mint(uint64 amount) public virtual onlyOwner {
_unsafeMint(msg.sender, TFHE.asEuint64(amount));
/// @dev Since _totalSupply is not encrypted and _totalSupply >= balances[msg.sender],
/// the next line contains an overflow check for the encrypted operation above.
Expand Down

0 comments on commit 69c3636

Please sign in to comment.