Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply prettier config #40

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trailingComma: none
semi: false
singleQuote: true
printWidth: 120
printWidth: 120
81 changes: 43 additions & 38 deletions contracts/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

pragma solidity ^0.8.9;

import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./IbcDispatcher.sol";
import {IbcChannelReceiver, IbcPacketReceiver} from "./IbcReceiver.sol";
import "./IbcVerifier.sol";
import "./ConsensusStateManager.sol";
import '@openzeppelin/contracts/utils/Strings.sol';
import '@openzeppelin/contracts/access/Ownable.sol';
import './IbcDispatcher.sol';
import {IbcChannelReceiver, IbcPacketReceiver} from './IbcReceiver.sol';
import './IbcVerifier.sol';
import './ConsensusStateManager.sol';

// InitClientMsg is used to create a new Polymer client on an EVM chain
// TODO: replace bytes with explictly typed fields for gas cost saving
Expand Down Expand Up @@ -145,9 +145,7 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
// updateClientWithConsensusState updates the client with the
// latest consensus state. The zkproof related to this consensus
// state is used to verify the consensus state.
function updateClientWithConsensusState(ConsensusState calldata consensusState, ZkProof calldata zkProof)
external
{
function updateClientWithConsensusState(ConsensusState calldata consensusState, ZkProof calldata zkProof) external {
if (!isClientCreated) {
revert clientNotCreated();
}
Expand All @@ -163,10 +161,10 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
// with the optimistic consensus state. The optimistic consensus
// is accepted and will be open for verify in the fraud proof
// window.
function updateClientWithOptimisticConsensusState(uint256 height, uint256 appHash)
external
returns (uint256 fraudProofEndTime, bool ended)
{
function updateClientWithOptimisticConsensusState(
uint256 height,
uint256 appHash
) external returns (uint256 fraudProofEndTime, bool ended) {
if (!isClientCreated) {
revert clientNotCreated();
}
Expand All @@ -175,11 +173,9 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
}

// getOptimisticConsensusState
function getOptimisticConsensusState(uint256 height)
external
view
returns (uint256 appHash, uint256 fraudProofEndTime, bool ended)
{
function getOptimisticConsensusState(
uint256 height
) external view returns (uint256 appHash, uint256 fraudProofEndTime, bool ended) {
if (!isClientCreated) {
revert clientNotCreated();
}
Expand Down Expand Up @@ -253,9 +249,9 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
// TODO: fill below proof path
consensusStateManager.verifyMembership(
proof,
"channel/path/to/be/added/here",
bytes("expected channel bytes constructed from params. Channel.State = {Try_Pending}"),
"Fail to prove channel state"
'channel/path/to/be/added/here',
bytes('expected channel bytes constructed from params. Channel.State = {Try_Pending}'),
'Fail to prove channel state'
);
}

Expand Down Expand Up @@ -298,9 +294,9 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
) external {
consensusStateManager.verifyMembership(
proof,
bytes("channel/path/to/be/added/here"),
bytes("expected channel bytes constructed from params. Channel.State = {Ack_Pending, Confirm_Pending}"),
"Fail to prove channel state"
bytes('channel/path/to/be/added/here'),
bytes('expected channel bytes constructed from params. Channel.State = {Ack_Pending, Confirm_Pending}'),
'Fail to prove channel state'
);

portAddress.onConnectIbcChannel(channelId, counterpartyChannelId, counterpartyVersion);
Expand Down Expand Up @@ -362,9 +358,9 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
// verify VIBC/IBC hub chain has processed ChanCloseConfirm event
consensusStateManager.verifyMembership(
proof,
bytes("channel/path/to/be/added/here"),
bytes("expected channel bytes constructed from params. Channel.State = {Closed(_Pending?)}"),
"Fail to prove channel state"
bytes('channel/path/to/be/added/here'),
bytes('expected channel bytes constructed from params. Channel.State = {Closed(_Pending?)}'),
'Fail to prove channel state'
);

// ensure port owns channel
Expand Down Expand Up @@ -442,7 +438,10 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {

// prove ack packet is on Polymer chain
consensusStateManager.verifyMembership(
proof, bytes("ack/packet/path"), bytes("expected ack receipt hash on Polymer chain"), "Fail to prove ack"
proof,
bytes('ack/packet/path'),
bytes('expected ack receipt hash on Polymer chain'),
'Fail to prove ack'
);
// verify packet has been committed and not yet ack'ed or timed out
bool hasCommitment = sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
Expand Down Expand Up @@ -485,7 +484,7 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
}

// prove absence of packet receipt on Polymer chain
consensusStateManager.verifyNonMembership(proof, "packet/receipt/path", "Fail to prove timeout");
consensusStateManager.verifyNonMembership(proof, 'packet/receipt/path', 'Fail to prove timeout');

// verify packet has been committed and not yet ack'ed or timed out
bool hasCommitment = sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
Expand Down Expand Up @@ -519,9 +518,9 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
}
consensusStateManager.verifyMembership(
proof,
"packet/commitment/path",
"expected virtual packet commitment hash on Polymer chain",
"Fail to prove packet commitment"
'packet/commitment/path',
'expected virtual packet commitment hash on Polymer chain',
'Fail to prove packet commitment'
);

// verify packet has not been received yet
Expand Down Expand Up @@ -549,7 +548,11 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
if (isPacketTimeout(packet)) {
address writerPortAddress = address(receiver);
emit WriteTimeoutPacket(
writerPortAddress, packet.dest.channelId, packet.sequence, packet.timeoutHeight, packet.timeoutTimestamp
writerPortAddress,
packet.dest.channelId,
packet.sequence,
packet.timeoutHeight,
packet.timeoutTimestamp
);
return;
}
Expand Down Expand Up @@ -580,11 +583,9 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {

// isPacketTimeout returns true if the given packet has timed out acoording to host chain's block height and timestamp
function isPacketTimeout(IbcPacket calldata packet) internal view returns (bool) {
return (
(packet.timeoutTimestamp != 0 && block.timestamp >= packet.timeoutTimestamp)
return ((packet.timeoutTimestamp != 0 && block.timestamp >= packet.timeoutTimestamp) ||
// TODO: check timeoutHeight.revision_number?
|| (packet.timeoutHeight.revision_height != 0 && block.number >= packet.timeoutHeight.revision_height)
);
(packet.timeoutHeight.revision_height != 0 && block.number >= packet.timeoutHeight.revision_height));
}

// TODO: remove below writeTimeoutPacket() function
Expand Down Expand Up @@ -613,7 +614,11 @@ contract Dispatcher is IbcDispatcher, IbcEventsEmitter, Ownable {
}

emit WriteTimeoutPacket(
receiver, packet.dest.channelId, packet.sequence, packet.timeoutHeight, packet.timeoutTimestamp
receiver,
packet.dest.channelId,
packet.sequence,
packet.timeoutHeight,
packet.timeoutTimestamp
);
}
}
13 changes: 8 additions & 5 deletions contracts/DummyConsensusStateManager.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "./ConsensusStateManager.sol";
import './ConsensusStateManager.sol';

/**
* @title DummyConsensusStateManager
* @dev This contract is a dummy implementation of a consensus state manager.
Expand All @@ -24,10 +25,12 @@ contract DummyConsensusStateManager is ConsensusStateManager {
return 0;
}

function verifyMembership(Proof calldata proof, bytes memory key, bytes memory expectedValue, string memory message)
external
view
{
function verifyMembership(
Proof calldata proof,
bytes memory key,
bytes memory expectedValue,
string memory message
) external view {
require(proof.proof.length > 0, message);
}

Expand Down
47 changes: 25 additions & 22 deletions contracts/Earth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

pragma solidity ^0.8.9;

import "./Ibc.sol";
import "./IbcReceiver.sol";
import "./IbcDispatcher.sol";
import "./IbcMiddleware.sol";
import './Ibc.sol';
import './IbcReceiver.sol';
import './IbcDispatcher.sol';
import './IbcMiddleware.sol';

contract Earth is IbcMwUser, IbcUniversalPacketReceiver {
struct UcPacketWithChannel {
Expand All @@ -30,36 +30,39 @@ contract Earth is IbcMwUser, IbcUniversalPacketReceiver {

function greet(address destPortAddr, bytes32 channelId, bytes calldata message, uint64 timeoutTimestamp) external {
IbcUniversalPacketSender(mw).sendUniversalPacket(
channelId, Ibc.toBytes32(destPortAddr), message, timeoutTimestamp
channelId,
Ibc.toBytes32(destPortAddr),
message,
timeoutTimestamp
);
}

// For testing only; real dApps should implment their own ack logic
function generateAckPacket(bytes32 channelId, address srcPortAddr, bytes calldata appData)
external
view
returns (AckPacket memory ackPacket)
{
return AckPacket(true, abi.encodePacked(this, srcPortAddr, "ack-", appData));
function generateAckPacket(
bytes32 channelId,
address srcPortAddr,
bytes calldata appData
) external view returns (AckPacket memory ackPacket) {
return AckPacket(true, abi.encodePacked(this, srcPortAddr, 'ack-', appData));
}

function onRecvUniversalPacket(bytes32 channelId, UniversalPacket calldata packet)
external
onlyIbcMw
returns (AckPacket memory ackPacket)
{
function onRecvUniversalPacket(
bytes32 channelId,
UniversalPacket calldata packet
) external onlyIbcMw returns (AckPacket memory ackPacket) {
recvedPackets.push(UcPacketWithChannel(channelId, packet));
return this.generateAckPacket(channelId, Ibc.toAddress(packet.srcPortAddr), packet.appData);
}

function onUniversalAcknowledgement(bytes32 channelId, UniversalPacket memory packet, AckPacket calldata ack)
external
onlyIbcMw
{
function onUniversalAcknowledgement(
bytes32 channelId,
UniversalPacket memory packet,
AckPacket calldata ack
) external onlyIbcMw {
// verify packet's destPortAddr is the ack's first encoded address. See generateAckPacket())
require(ack.data.length >= 20, "ack data too short");
require(ack.data.length >= 20, 'ack data too short');
address ackSender = address(bytes20(ack.data[0:20]));
require(Ibc.toAddress(packet.destPortAddr) == ackSender, "ack address mismatch");
require(Ibc.toAddress(packet.destPortAddr) == ackSender, 'ack address mismatch');
ackPackets.push(UcAckWithChannel(channelId, packet, ack));
}

Expand Down
Loading
Loading