Skip to content

Commit

Permalink
fmt + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
RnkSngh committed Mar 18, 2024
1 parent 2343e16 commit 78d4f8a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 52 deletions.
94 changes: 47 additions & 47 deletions contracts/core/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
string public portPrefix;
uint32 public portPrefixLen;

mapping(address => mapping(bytes32 => Channel)) portChannelMap;
mapping(address => mapping(bytes32 => uint64)) nextSequenceSend;
mapping(address => mapping(bytes32 => Channel)) private _portChannelMap;
mapping(address => mapping(bytes32 => uint64)) private _nextSequenceSend;
// keep track of received packets' sequences to ensure channel ordering is enforced for ordered channels
mapping(address => mapping(bytes32 => uint64)) nextSequenceRecv;
mapping(address => mapping(bytes32 => uint64)) nextSequenceAck;
mapping(address => mapping(bytes32 => uint64)) private _nextSequenceRecv;
mapping(address => mapping(bytes32 => uint64)) private _nextSequenceAck;
// only stores a bit to mark packet has not been ack'ed or timed out yet; actual IBC packet verification is done on
// Polymer chain.
// Keep track of sent packets
mapping(address => mapping(bytes32 => mapping(uint64 => bool))) sendPacketCommitment;
mapping(address => mapping(bytes32 => mapping(uint64 => bool))) private _sendPacketCommitment;
// keep track of received packets to prevent replay attack
mapping(address => mapping(bytes32 => mapping(uint64 => bool))) recvPacketReceipt;
mapping(address => mapping(bytes32 => mapping(uint64 => bool))) private _recvPacketReceipt;
// keep track of outbound ack packets to prevent replay attack
mapping(address => mapping(bytes32 => mapping(uint64 => bool))) ackPacketCommitment;
mapping(address => mapping(bytes32 => mapping(uint64 => bool))) private _ackPacketCommitment;

LightClient lightClient;
LightClient _lightClient;

//
// methods
Expand All @@ -62,11 +62,11 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
_disableInitializers();
}

function initialize(string memory initPortPrefix, LightClient _lightClient) public initializer {
function initialize(string memory initPortPrefix, LightClient lightClient) public initializer {
__Ownable_init();
portPrefix = initPortPrefix;
portPrefixLen = uint32(bytes(initPortPrefix).length);
lightClient = _lightClient;
_lightClient = lightClient;
}

//
Expand All @@ -87,7 +87,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
uint256 height,
uint256 appHash
) external returns (uint256 fraudProofEndTime, bool ended) {
return lightClient.addOpConsensusState(l1header, proof, height, appHash);
return _lightClient.addOpConsensusState(l1header, proof, height, appHash);
}

/**
Expand Down Expand Up @@ -138,7 +138,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
revert IBCErrors.invalidCounterPartyPortId();
}

lightClient.verifyMembership(
_lightClient.verifyMembership(
proof,
Ibc.channelProofKey(local.portId, local.channelId),
Ibc.channelProofValue(ChannelState.TRY_PENDING, ordering, local.version, connectionHops, counterparty)
Expand Down Expand Up @@ -176,7 +176,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
CounterParty calldata counterparty,
Ics23Proof calldata proof
) external {
lightClient.verifyMembership(
_lightClient.verifyMembership(
proof,
Ibc.channelProofKey(local.portId, local.channelId),
Ibc.channelProofValue(ChannelState.ACK_PENDING, ordering, local.version, connectionHops, counterparty)
Expand Down Expand Up @@ -209,7 +209,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
CounterParty calldata counterparty,
Ics23Proof calldata proof
) external {
lightClient.verifyMembership(
_lightClient.verifyMembership(
proof,
Ibc.channelProofKey(local.portId, local.channelId),
Ibc.channelProofValue(ChannelState.CONFIRM_PENDING, ordering, local.version, connectionHops, counterparty)
Expand All @@ -231,10 +231,10 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
/**
* @dev Emits a `CloseIbcChannel` event with the given `channelId` and the address of the message sender
* @notice Close the specified IBC channel by channel ID
* Must be called by the channel owner, ie. portChannelMap[msg.sender][channelId] must exist
* Must be called by the channel owner, ie. _portChannelMap[msg.sender][channelId] must exist
*/
function closeIbcChannel(bytes32 channelId) external {
Channel memory channel = portChannelMap[msg.sender][channelId];
Channel memory channel = _portChannelMap[msg.sender][channelId];
if (channel.counterpartyChannelId == bytes32(0)) {
revert IBCErrors.channelNotOwnedBySender();
}
Expand Down Expand Up @@ -263,22 +263,22 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
// FIXME this is commented out to make the contract size smaller. We need to optimise for size
// function onCloseIbcChannel(address portAddress, bytes32 channelId, Ics23Proof calldata proof) external {
// // verify VIBC/IBC hub chain has processed ChanCloseConfirm event
// lightClient.verifyMembership(
// _lightClient.verifyMembership(
// proof,
// bytes('channel/path/to/be/added/here'),
// bytes('expected channel bytes constructed from params. Channel.State = {Closed(_Pending?)}')
// );
//
// // ensure port owns channel
// Channel memory channel = portChannelMap[portAddress][channelId];
// Channel memory channel = _portChannelMap[portAddress][channelId];
// if (channel.counterpartyChannelId == bytes32(0)) {
// revert channelNotOwnedByPortAddress();
// }
//
// // confirm with dApp by calling its callback
// IbcChannelReceiver reciever = IbcChannelReceiver(portAddress);
// reciever.onCloseIbcChannel(channelId, channel.counterpartyPortId, channel.counterpartyChannelId);
// delete portChannelMap[portAddress][channelId];
// delete _portChannelMap[portAddress][channelId];
// emit CloseIbcChannel(portAddress, channelId);
// }

Expand All @@ -299,7 +299,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
*/
function sendPacket(bytes32 channelId, bytes calldata packet, uint64 timeoutTimestamp) external {
// ensure port owns channel
Channel memory channel = portChannelMap[msg.sender][channelId];
Channel memory channel = _portChannelMap[msg.sender][channelId];
if (channel.counterpartyChannelId == bytes32(0)) {
revert IBCErrors.channelNotOwnedBySender();
}
Expand Down Expand Up @@ -330,31 +330,31 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
}

// prove ack packet is on Polymer chain
lightClient.verifyMembership(proof, Ibc.ackProofKey(packet), abi.encode(Ibc.ackProofValue(ack)));
_lightClient.verifyMembership(proof, Ibc.ackProofKey(packet), abi.encode(Ibc.ackProofValue(ack)));
// verify packet has been committed and not yet ack'ed or timed out
bool hasCommitment = sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
bool hasCommitment = _sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
if (!hasCommitment) {
revert IBCErrors.packetCommitmentNotFound();
}

// enforce ack'ed packet sequences always increment by 1 for ordered channels
Channel memory channel = portChannelMap[address(receiver)][packet.src.channelId];
Channel memory channel = _portChannelMap[address(receiver)][packet.src.channelId];
(bool success, bytes memory data) = _callIfContract(
address(receiver),
abi.encodeWithSelector(IbcPacketReceiver.onAcknowledgementPacket.selector, packet, Ibc.parseAckData(ack))
);

if (success) {
if (channel.ordering == ChannelOrder.ORDERED) {
if (packet.sequence != nextSequenceAck[address(receiver)][packet.src.channelId]) {
if (packet.sequence != _nextSequenceAck[address(receiver)][packet.src.channelId]) {
revert IBCErrors.unexpectedPacketSequence();
}

nextSequenceAck[address(receiver)][packet.src.channelId] = packet.sequence + 1;
_nextSequenceAck[address(receiver)][packet.src.channelId] = packet.sequence + 1;
}

// delete packet commitment to avoid double ack
delete sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
delete _sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
emit Acknowledgement(address(receiver), packet.src.channelId, packet.sequence);
} else {
emit AcknowledgementError(address(receiver), data);
Expand All @@ -379,10 +379,10 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {

// prove absence of packet receipt on Polymer chain
// TODO: add non membership support
lightClient.verifyNonMembership(proof, "packet/receipt/path");
_lightClient.verifyNonMembership(proof, "packet/receipt/path");

// verify packet has been committed and not yet ack'ed or timed out
bool hasCommitment = sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
bool hasCommitment = _sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
if (!hasCommitment) {
revert IBCErrors.packetCommitmentNotFound();
}
Expand All @@ -392,7 +392,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
);
if (success) {
// delete packet commitment to avoid double timeout
delete sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
delete _sendPacketCommitment[address(receiver)][packet.src.channelId][packet.sequence];
emit Timeout(address(receiver), packet.src.channelId, packet.sequence);
} else {
emit TimeoutError(address(receiver), data);
Expand All @@ -415,26 +415,26 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
if (!portIdAddressMatch(address(receiver), packet.dest.portId)) {
revert IBCErrors.receiverNotIntendedPacketDestination();
}
lightClient.verifyMembership(
_lightClient.verifyMembership(
proof, Ibc.packetCommitmentProofKey(packet), abi.encode(Ibc.packetCommitmentProofValue(packet))
);

// verify packet has not been received yet
bool hasReceipt = recvPacketReceipt[address(receiver)][packet.dest.channelId][packet.sequence];
bool hasReceipt = _recvPacketReceipt[address(receiver)][packet.dest.channelId][packet.sequence];
if (hasReceipt) {
revert IBCErrors.packetReceiptAlreadyExists();
}

recvPacketReceipt[address(receiver)][packet.dest.channelId][packet.sequence] = true;
_recvPacketReceipt[address(receiver)][packet.dest.channelId][packet.sequence] = true;

// enforce recv'ed packet sequences always increment by 1 for ordered channels
Channel memory channel = portChannelMap[address(receiver)][packet.dest.channelId];
Channel memory channel = _portChannelMap[address(receiver)][packet.dest.channelId];
if (channel.ordering == ChannelOrder.ORDERED) {
if (packet.sequence != nextSequenceRecv[address(receiver)][packet.dest.channelId]) {
if (packet.sequence != _nextSequenceRecv[address(receiver)][packet.dest.channelId]) {
revert IBCErrors.unexpectedPacketSequence();
}

nextSequenceRecv[address(receiver)][packet.dest.channelId] = packet.sequence + 1;
_nextSequenceRecv[address(receiver)][packet.dest.channelId] = packet.sequence + 1;
}

// Emit recv packet event to prove the relayer did the correct job, and pkt is received.
Expand All @@ -459,13 +459,13 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
} else {
ack = AckPacket(false, data);
}
bool hasAckPacketCommitment = ackPacketCommitment[address(receiver)][packet.dest.channelId][packet.sequence];
bool hasAckPacketCommitment = _ackPacketCommitment[address(receiver)][packet.dest.channelId][packet.sequence];
// check is not necessary for sync-acks
if (hasAckPacketCommitment) {
revert IBCErrors.ackPacketCommitmentAlreadyExists();
}

ackPacketCommitment[address(receiver)][packet.dest.channelId][packet.sequence] = true;
_ackPacketCommitment[address(receiver)][packet.dest.channelId][packet.sequence] = true;

emit WriteAckPacket(address(receiver), packet.dest.channelId, packet.sequence, ack);
}
Expand Down Expand Up @@ -497,7 +497,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
}

// verify packet does not have a receipt
bool hasReceipt = recvPacketReceipt[receiver][packet.dest.channelId][packet.sequence];
bool hasReceipt = _recvPacketReceipt[receiver][packet.dest.channelId][packet.sequence];
if (hasReceipt) {
revert IBCErrors.packetReceiptAlreadyExists();
}
Expand All @@ -521,7 +521,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
* values per EVM.
*/
function getChannel(address portAddress, bytes32 channelId) external view returns (Channel memory channel) {
channel = portChannelMap[portAddress][channelId];
channel = _portChannelMap[portAddress][channelId];
}

// getOptimisticConsensusState
Expand All @@ -530,7 +530,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
view
returns (uint256 appHash, uint256 fraudProofEndTime, bool ended)
{
return lightClient.getState(height);
return _lightClient.getState(height);
}

// verify an EVM address matches an IBC portId.
Expand All @@ -546,15 +546,15 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
// Prerequisite: must verify sender is authorized to send packet on the channel
function _sendPacket(address sender, bytes32 channelId, bytes memory packet, uint64 timeoutTimestamp) internal {
// current packet sequence
uint64 sequence = nextSequenceSend[sender][channelId];
uint64 sequence = _nextSequenceSend[sender][channelId];
if (sequence == 0) {
revert IBCErrors.invalidPacketSequence();
}

// packet commitment
sendPacketCommitment[sender][channelId][sequence] = true;
_sendPacketCommitment[sender][channelId][sequence] = true;
// increment nextSendPacketSequence
nextSequenceSend[sender][channelId] = sequence + 1;
_nextSequenceSend[sender][channelId] = sequence + 1;

emit SendPacket(sender, channelId, packet, sequence, timeoutTimestamp);
}
Expand All @@ -571,7 +571,7 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
// TODO: check duplicated channel registration?
// TODO: The call to `Channel` constructor MUST be move to `openIbcChannel` phase
// Then `connectIbcChannel` phase can use the `version` as part of `require` condition.
portChannelMap[address(portAddress)][local.channelId] = Channel(
_portChannelMap[address(portAddress)][local.channelId] = Channel(
counterparty.version, // TODO: this should be self version instead of counterparty version
ordering,
feeEnabled,
Expand All @@ -581,9 +581,9 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, IDispatcher {
);

// initialize channel sequences
nextSequenceSend[address(portAddress)][local.channelId] = 1;
nextSequenceRecv[address(portAddress)][local.channelId] = 1;
nextSequenceAck[address(portAddress)][local.channelId] = 1;
_nextSequenceSend[address(portAddress)][local.channelId] = 1;
_nextSequenceRecv[address(portAddress)][local.channelId] = 1;
_nextSequenceAck[address(portAddress)][local.channelId] = 1;
}

// Returns the result of the call if no revert, otherwise returns the error if thrown.
Expand Down
2 changes: 1 addition & 1 deletion test/TestUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ library TestUtils {
);
}

function getProxyImplementation(address proxy, Vm vm) public returns (address dispatcherImplementation) {
function getProxyImplementation(address proxy, Vm vm) public view returns (address dispatcherImplementation) {
dispatcherImplementation = address(uint160(uint256(vm.load(address(proxy), _IMPLEMENTATION_SLOT))));
}
}
9 changes: 5 additions & 4 deletions test/upgradeableProxy/Dispatcher.upgrade.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,27 @@ contract ChannelHandShakeUpgradeUtil is ChannelHandshakeUtils {

function acknowledgePacket() public {}

function findNextSequenceSendSlot(address portAddress, bytes32 channelId) public returns (bytes32 slot) {
function findNextSequenceSendSlot(address portAddress, bytes32 channelId) public view returns (bytes32 slot) {
bytes32 slot1 = keccak256(abi.encode(portAddress, nextSequenceSendSlot));
slot = keccak256(abi.encode(channelId, slot1));
}

function findPacketCommitmentSlot(address portAddress, bytes32 channelId, uint64 sequence)
public
view
returns (bytes32 slot)
{
bytes32 slot1 = keccak256(abi.encode(portAddress, sendPacketCommitmentSlot));
bytes32 slot2 = keccak256(abi.encode(channelId, slot1));
bytes32 slot = keccak256(abi.encode(sequence, slot2));
slot = keccak256(abi.encode(sequence, slot2));
}

function findNextSequenceAck(address portAddress, bytes32 channelId) public returns (bytes32 slot) {
function findNextSequenceAck(address portAddress, bytes32 channelId) public view returns (bytes32 slot) {
bytes32 slot1 = keccak256(abi.encode(portAddress, nextSequenceAckSlot));
slot = keccak256(abi.encode(channelId, slot1));
}

function findNextSequenceRecv(address portAddress, bytes32 channelId) public returns (bytes32 slot) {
function findNextSequenceRecv(address portAddress, bytes32 channelId) public view returns (bytes32 slot) {
bytes32 slot1 = keccak256(abi.encode(portAddress, nextSequenceRecvSlot));
slot = keccak256(abi.encode(channelId, slot1));
}
Expand Down

0 comments on commit 78d4f8a

Please sign in to comment.