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

ucs01-relay: more coverage on both solidity and rust #1098

Merged
merged 14 commits into from
Jan 11, 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
7 changes: 7 additions & 0 deletions cosmwasm/ucs01-relay/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ pub enum ContractError {
protocol_version: String,
},

#[error("Channel {channel_id} protocol version {protocol_version} mismatch counterparty protocol version {counterparty_protocol_version}")]
ProtocolMismatch {
channel_id: String,
protocol_version: String,
counterparty_protocol_version: String,
},

#[error("Only myself is able to trigger this message")]
Unauthorized,
}
Expand Down
179 changes: 177 additions & 2 deletions cosmwasm/ucs01-relay/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ pub(crate) fn enforce_order_and_version(
protocol_version: version.to_string(),
});
}
if version != channel.version {
return Err(ContractError::ProtocolMismatch {
channel_id: channel.endpoint.channel_id.clone(),
protocol_version: channel.version.to_string(),
counterparty_protocol_version: version.to_string(),
});
}
}
if channel.order != channel_ordering {
return Err(ContractError::InvalidChannelOrdering {
Expand All @@ -108,8 +115,7 @@ pub fn ibc_channel_close(
_env: Env,
_channel: IbcChannelCloseMsg,
) -> Result<IbcBasicResponse, ContractError> {
// Not allowed.
unimplemented!();
Err(ContractError::Unauthorized)
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down Expand Up @@ -232,3 +238,172 @@ pub fn ibc_packet_timeout(
}),
}
}

#[cfg(test)]
mod tests {
use cosmwasm_std::{IbcChannel, IbcEndpoint};
use ucs01_relay_api::protocol::TransferProtocol;

use super::enforce_order_and_version;
use crate::{
error::ContractError,
protocol::{Ics20Protocol, Ucs01Protocol},
};

#[test]
fn enforce_channel_version_ucs01() {
let port_id = "port-1";
let channel_id = "channel-1";
let connection_id = "connection-1";
let protocol_version = Ucs01Protocol::VERSION;
let counterparty_port_id = "port-2";
let counterparty_channel_id = "channel-2";
assert_eq!(
enforce_order_and_version(
&IbcChannel::new(
IbcEndpoint {
port_id: port_id.into(),
channel_id: channel_id.into()
},
IbcEndpoint {
port_id: counterparty_port_id.into(),
channel_id: counterparty_channel_id.into()
},
cosmwasm_std::IbcOrder::Unordered,
protocol_version,
connection_id
),
None
),
Ok(())
);
}

#[test]
fn enforce_channel_version_ics20() {
let port_id = "port-1";
let channel_id = "channel-1";
let connection_id = "connection-1";
let protocol_version = Ics20Protocol::VERSION;
let counterparty_port_id = "port-2";
let counterparty_channel_id = "channel-2";
assert_eq!(
enforce_order_and_version(
&IbcChannel::new(
IbcEndpoint {
port_id: port_id.into(),
channel_id: channel_id.into()
},
IbcEndpoint {
port_id: counterparty_port_id.into(),
channel_id: counterparty_channel_id.into()
},
cosmwasm_std::IbcOrder::Unordered,
protocol_version,
connection_id
),
None
),
Ok(())
);
}

#[test]
fn enforce_channel_wrong_version() {
let port_id = "port-1";
let channel_id = "channel-1";
let connection_id = "connection-1";
let protocol_version = "ucs01-0999";
let counterparty_port_id = "port-2";
let counterparty_channel_id = "channel-2";
assert_eq!(
enforce_order_and_version(
&IbcChannel::new(
IbcEndpoint {
port_id: port_id.into(),
channel_id: channel_id.into()
},
IbcEndpoint {
port_id: counterparty_port_id.into(),
channel_id: counterparty_channel_id.into()
},
cosmwasm_std::IbcOrder::Unordered,
protocol_version,
connection_id
),
None
),
Err(ContractError::UnknownProtocol {
channel_id: channel_id.into(),
protocol_version: protocol_version.into()
})
);
}

#[test]
fn enforce_channel_counterparty_wrong_version() {
let port_id = "port-1";
let channel_id = "channel-1";
let connection_id = "connection-1";
let protocol_version = Ucs01Protocol::VERSION;
let counterparty_port_id = "port-2";
let counterparty_channel_id = "channel-2";
let counterparty_protocol_version = "ucs01-0999";
assert_eq!(
enforce_order_and_version(
&IbcChannel::new(
IbcEndpoint {
port_id: port_id.into(),
channel_id: channel_id.into()
},
IbcEndpoint {
port_id: counterparty_port_id.into(),
channel_id: counterparty_channel_id.into()
},
cosmwasm_std::IbcOrder::Unordered,
protocol_version,
connection_id
),
Some(counterparty_protocol_version)
),
Err(ContractError::UnknownProtocol {
channel_id: channel_id.into(),
protocol_version: counterparty_protocol_version.into()
})
);
}

#[test]
fn enforce_channel_protocol_mismatch() {
let port_id = "port-1";
let channel_id = "channel-1";
let connection_id = "connection-1";
let protocol_version = Ucs01Protocol::VERSION;
let counterparty_port_id = "port-2";
let counterparty_channel_id = "channel-2";
let counterparty_protocol_version = Ics20Protocol::VERSION;
assert_eq!(
enforce_order_and_version(
&IbcChannel::new(
IbcEndpoint {
port_id: port_id.into(),
channel_id: channel_id.into()
},
IbcEndpoint {
port_id: counterparty_port_id.into(),
channel_id: counterparty_channel_id.into()
},
cosmwasm_std::IbcOrder::Unordered,
protocol_version,
connection_id
),
Some(counterparty_protocol_version)
),
Err(ContractError::ProtocolMismatch {
channel_id: channel_id.into(),
protocol_version: protocol_version.into(),
counterparty_protocol_version: counterparty_protocol_version.into()
})
);
}
}
3 changes: 3 additions & 0 deletions dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,11 @@ feegrant
feegrantkeeper
feegrantmodule
fetchurl
fetchzip
fkey
fksmg
floorlog
fmtlib
foldl
frunk
fubar
Expand Down Expand Up @@ -385,6 +387,7 @@ jemalloc
jemallocator
jetbrains
journalctl
jsoncpp
jsonschema
jwtsecret
karel
Expand Down
2 changes: 1 addition & 1 deletion evm/contracts/Glue.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.8.21;
pragma solidity ^0.8.23;

import "./core/02-client/ILightClient.sol";
import "./core/02-client/IBCHeight.sol";
Expand Down
7 changes: 6 additions & 1 deletion evm/contracts/apps/Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract contract IBCAppBase is Context, IIBCModule {
function _checkIBC() internal view virtual {
require(
ibcAddress() == _msgSender(),
"_checkIBC: caller is not the IBC contract"
"IBCAppBase: caller is not the IBC contract"
);
}

Expand Down Expand Up @@ -127,4 +127,9 @@ abstract contract IBCAppBase is Context, IIBCModule {
bytes calldata acknowledgement,
address relayer
) external virtual override onlyIBC {}

function onTimeoutPacket(
IbcCoreChannelV1Packet.Data calldata packet,
address relayer
) external virtual override onlyIBC {}
}
2 changes: 1 addition & 1 deletion evm/contracts/apps/ucs/00-pingpong/PingPong.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.8.21;
pragma solidity ^0.8.23;

import "../../Base.sol";
import "../../../core/25-handler/IBCHandler.sol";
Expand Down
6 changes: 3 additions & 3 deletions evm/contracts/apps/ucs/01-relay/ERC20Denom.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.8.21;
pragma solidity ^0.8.23;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./IERC20Denom.sol";
Expand All @@ -11,12 +11,12 @@ contract ERC20Denom is ERC20, IERC20Denom {
}

function mint(address to, uint256 amount) external {
require(msg.sender == admin, "only admin");
require(msg.sender == admin, "ERC20Denom: only admin");
_mint(to, amount);
}

function burn(address from, uint256 amount) external {
require(msg.sender == admin, "only admin");
require(msg.sender == admin, "ERC20Denom: only admin");
_burn(from, amount);
}
}
2 changes: 1 addition & 1 deletion evm/contracts/apps/ucs/01-relay/IERC20Denom.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.8.21;
pragma solidity ^0.8.23;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

Expand Down
Loading