Skip to content

Commit

Permalink
refactor: comply with solidity style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzzzHui committed Jan 16, 2024
1 parent 779f722 commit 5b6645b
Show file tree
Hide file tree
Showing 30 changed files with 1,166 additions and 1,159 deletions.
5 changes: 5 additions & 0 deletions onchain/rollups/.changeset/pink-penguins-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cartesi/rollups": major
---

Comply with Solidity style guide.
22 changes: 11 additions & 11 deletions onchain/rollups/contracts/consensus/authority/AuthorityFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ import {Authority} from "./Authority.sol";
/// @notice Allows anyone to reliably deploy a new `Authority` contract.
contract AuthorityFactory is IAuthorityFactory {
function newAuthority(
address _authorityOwner
address authorityOwner
) external override returns (Authority) {
Authority authority = new Authority(_authorityOwner);
Authority authority = new Authority(authorityOwner);

emit AuthorityCreated(_authorityOwner, authority);
emit AuthorityCreated(authorityOwner, authority);

return authority;
}

function newAuthority(
address _authorityOwner,
bytes32 _salt
address authorityOwner,
bytes32 salt
) external override returns (Authority) {
Authority authority = new Authority{salt: _salt}(_authorityOwner);
Authority authority = new Authority{salt: salt}(authorityOwner);

emit AuthorityCreated(_authorityOwner, authority);
emit AuthorityCreated(authorityOwner, authority);

return authority;
}

function calculateAuthorityAddress(
address _authorityOwner,
bytes32 _salt
address authorityOwner,
bytes32 salt
) external view override returns (address) {
return
Create2.computeAddress(
_salt,
salt,
keccak256(
abi.encodePacked(
type(Authority).creationCode,
abi.encode(_authorityOwner)
abi.encode(authorityOwner)
)
)
);
Expand Down
22 changes: 11 additions & 11 deletions onchain/rollups/contracts/consensus/authority/IAuthorityFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ interface IAuthorityFactory {
// Permissionless functions

/// @notice Deploy a new authority.
/// @param _authorityOwner The initial authority owner
/// @param authorityOwner The initial authority owner
/// @return The authority
/// @dev On success, MUST emit an `AuthorityCreated` event.
function newAuthority(address _authorityOwner) external returns (Authority);
function newAuthority(address authorityOwner) external returns (Authority);

/// @notice Deploy a new authority deterministically.
/// @param _authorityOwner The initial authority owner
/// @param _salt The salt used to deterministically generate the authority address
/// @param authorityOwner The initial authority owner
/// @param salt The salt used to deterministically generate the authority address
/// @return The authority
/// @dev On success, MUST emit an `AuthorityCreated` event.
function newAuthority(
address _authorityOwner,
bytes32 _salt
address authorityOwner,
bytes32 salt
) external returns (Authority);

/// @notice Calculate the address of an authority to be deployed deterministically.
/// @param _authorityOwner The initial authority owner
/// @param _salt The salt used to deterministically generate the authority address
/// @param authorityOwner The initial authority owner
/// @param salt The salt used to deterministically generate the authority address
/// @return The deterministic authority address
/// @dev Beware that only the `newAuthority` function with the `_salt` parameter
/// @dev Beware that only the `newAuthority` function with the `salt` parameter
/// is able to deterministically deploy an authority.
function calculateAuthorityAddress(
address _authorityOwner,
bytes32 _salt
address authorityOwner,
bytes32 salt
) external view returns (address);
}
184 changes: 92 additions & 92 deletions onchain/rollups/contracts/dapp/Application.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,138 +76,152 @@ contract Application is
using LibInputRange for InputRange;
using Address for address;

/// @notice Raised when executing an already executed voucher.
error VoucherReexecutionNotAllowed();

/// @notice Raised when the transfer fails.
error EtherTransferFailed();

/// @notice Raised when a mehtod is not called by application itself.
error OnlyApplication();

/// @notice The initial machine state hash.
/// @dev See the `getTemplateHash` function.
bytes32 internal immutable templateHash;
bytes32 internal immutable _templateHash;

/// @notice The executed voucher bitmask, which keeps track of which vouchers
/// were executed already in order to avoid re-execution.
/// @dev See the `wasVoucherExecuted` function.
mapping(uint256 => BitMaps.BitMap) internal voucherBitmaps;
mapping(uint256 => BitMaps.BitMap) internal _voucherBitmaps;

/// @notice The current consensus contract.
/// @dev See the `getConsensus` and `migrateToConsensus` functions.
IConsensus internal consensus;
IConsensus internal _consensus;

/// @notice The input box contract.
/// @dev See the `getInputBox` function.
IInputBox internal immutable inputBox;
IInputBox internal immutable _inputBox;

/// @notice The input relays.
/// @dev See the `getInputRelays` function.
IInputRelay[] internal inputRelays;
IInputRelay[] internal _inputRelays;

/// @notice Raised when executing an already executed voucher.
error VoucherReexecutionNotAllowed();

/// @notice Raised when the transfer fails.
error EtherTransferFailed();

/// @notice Raised when a mehtod is not called by application itself.
error OnlyApplication();

/// @notice Creates an `Application` contract.
/// @param _consensus The initial consensus contract
/// @param _inputBox The input box contract
/// @param _inputRelays The input relays
/// @param _initialOwner The initial application owner
/// @param _templateHash The initial machine state hash
/// @param consensus The initial consensus contract
/// @param inputBox The input box contract
/// @param inputRelays The input relays
/// @param initialOwner The initial application owner
/// @param templateHash The initial machine state hash
constructor(
IConsensus _consensus,
IInputBox _inputBox,
IInputRelay[] memory _inputRelays,
address _initialOwner,
bytes32 _templateHash
) Ownable(_initialOwner) {
templateHash = _templateHash;
consensus = _consensus;
inputBox = _inputBox;
for (uint256 i; i < _inputRelays.length; ++i) {
inputRelays.push(_inputRelays[i]);
IConsensus consensus,
IInputBox inputBox,
IInputRelay[] memory inputRelays,
address initialOwner,
bytes32 templateHash
) Ownable(initialOwner) {
_templateHash = templateHash;
_consensus = consensus;
_inputBox = inputBox;
for (uint256 i; i < inputRelays.length; ++i) {
_inputRelays.push(inputRelays[i]);
}
}

function supportsInterface(
bytes4 interfaceId
) public view virtual override(ERC1155Holder, IERC165) returns (bool) {
return
interfaceId == type(IApplication).interfaceId ||
interfaceId == type(IERC721Receiver).interfaceId ||
super.supportsInterface(interfaceId);
/// @notice Accept Ether transfers.
/// @dev If you wish to transfer Ether to an application while informing
/// the backend of it, then please do so through the Ether portal contract.
receive() external payable {}

/// @notice Transfer some amount of Ether to some recipient.
/// @param receiver The address which will receive the amount of Ether
/// @param value The amount of Ether to be transferred in Wei
/// @dev This function can only be called by the application itself through vouchers.
/// If this method is not called by application itself, `OnlyApplication` error is raised.
/// If the transfer fails, `EtherTransferFailed` error is raised.
function withdrawEther(address receiver, uint256 value) external {
if (msg.sender != address(this)) {
revert OnlyApplication();
}

(bool sent, ) = receiver.call{value: value}("");

if (!sent) {
revert EtherTransferFailed();
}
}

function executeVoucher(
address _destination,
bytes calldata _payload,
Proof calldata _proof
address destination,
bytes calldata payload,
Proof calldata proof
) external override nonReentrant {
uint256 inputIndex = _proof.calculateInputIndex();
uint256 inputIndex = proof.calculateInputIndex();

if (!_proof.inputRange.contains(inputIndex)) {
revert InputIndexOutOfRange(inputIndex, _proof.inputRange);
if (!proof.inputRange.contains(inputIndex)) {
revert InputIndexOutOfRange(inputIndex, proof.inputRange);
}

bytes32 epochHash = getEpochHash(_proof.inputRange);
bytes32 epochHash = _getEpochHash(proof.inputRange);

// reverts if proof isn't valid
_proof.validity.validateVoucher(_destination, _payload, epochHash);
proof.validity.validateVoucher(destination, payload, epochHash);

uint256 outputIndexWithinInput = _proof.validity.outputIndexWithinInput;
BitMaps.BitMap storage bitmap = voucherBitmaps[outputIndexWithinInput];
uint256 outputIndexWithinInput = proof.validity.outputIndexWithinInput;
BitMaps.BitMap storage bitmap = _voucherBitmaps[outputIndexWithinInput];

// check if voucher has been executed
if (bitmap.get(inputIndex)) {
revert VoucherReexecutionNotAllowed();
}

// execute voucher
_destination.functionCall(_payload);
destination.functionCall(payload);

// mark it as executed and emit event
bitmap.set(inputIndex);
emit VoucherExecuted(inputIndex, outputIndexWithinInput);
}

function migrateToConsensus(
IConsensus newConsensus
) external override onlyOwner {
_consensus = newConsensus;
emit NewConsensus(newConsensus);
}

function wasVoucherExecuted(
uint256 _inputIndex,
uint256 _outputIndexWithinInput
uint256 inputIndex,
uint256 outputIndexWithinInput
) external view override returns (bool) {
return voucherBitmaps[_outputIndexWithinInput].get(_inputIndex);
return _voucherBitmaps[outputIndexWithinInput].get(inputIndex);
}

function validateNotice(
bytes calldata _notice,
Proof calldata _proof
bytes calldata notice,
Proof calldata proof
) external view override {
uint256 inputIndex = _proof.calculateInputIndex();
uint256 inputIndex = proof.calculateInputIndex();

if (!_proof.inputRange.contains(inputIndex)) {
revert InputIndexOutOfRange(inputIndex, _proof.inputRange);
if (!proof.inputRange.contains(inputIndex)) {
revert InputIndexOutOfRange(inputIndex, proof.inputRange);
}

bytes32 epochHash = getEpochHash(_proof.inputRange);
bytes32 epochHash = _getEpochHash(proof.inputRange);

// reverts if proof isn't valid
_proof.validity.validateNotice(_notice, epochHash);
}

function migrateToConsensus(
IConsensus _newConsensus
) external override onlyOwner {
consensus = _newConsensus;
emit NewConsensus(_newConsensus);
proof.validity.validateNotice(notice, epochHash);
}

function getTemplateHash() external view override returns (bytes32) {
return templateHash;
return _templateHash;
}

function getConsensus() external view override returns (IConsensus) {
return consensus;
return _consensus;
}

function getInputBox() external view override returns (IInputBox) {
return inputBox;
return _inputBox;
}

function getInputRelays()
Expand All @@ -216,39 +230,25 @@ contract Application is
override
returns (IInputRelay[] memory)
{
return inputRelays;
return _inputRelays;
}

/// @notice Accept Ether transfers.
/// @dev If you wish to transfer Ether to an application while informing
/// the backend of it, then please do so through the Ether portal contract.
receive() external payable {}

/// @notice Transfer some amount of Ether to some recipient.
/// @param _receiver The address which will receive the amount of Ether
/// @param _value The amount of Ether to be transferred in Wei
/// @dev This function can only be called by the application itself through vouchers.
/// If this method is not called by application itself, `OnlyApplication` error is raised.
/// If the transfer fails, `EtherTransferFailed` error is raised.
function withdrawEther(address _receiver, uint256 _value) external {
if (msg.sender != address(this)) {
revert OnlyApplication();
}

(bool sent, ) = _receiver.call{value: _value}("");

if (!sent) {
revert EtherTransferFailed();
}
function supportsInterface(
bytes4 interfaceId
) public view virtual override(ERC1155Holder, IERC165) returns (bool) {
return
interfaceId == type(IApplication).interfaceId ||
interfaceId == type(IERC721Receiver).interfaceId ||
super.supportsInterface(interfaceId);
}

/// @notice Get the epoch hash regarding the given input range
/// and the application from the current consensus.
/// @param inputRange The input range
/// @return The epoch hash
function getEpochHash(
function _getEpochHash(
InputRange calldata inputRange
) internal view returns (bytes32) {
return consensus.getEpochHash(address(this), inputRange);
return _consensus.getEpochHash(address(this), inputRange);
}
}
Loading

0 comments on commit 5b6645b

Please sign in to comment.