-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathibc.rs
141 lines (127 loc) · 4.14 KB
/
ibc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
attr, DepsMut, Env, IbcBasicResponse, IbcChannel, IbcChannelCloseMsg, IbcChannelConnectMsg,
IbcChannelOpenMsg, IbcOrder, IbcPacket, IbcPacketAckMsg, IbcPacketReceiveMsg,
IbcPacketTimeoutMsg, IbcReceiveResponse, Reply, Response, StdError,
};
use crate::{msg::UCS00PingPong, state::CONFIG, ContractError};
pub const PROTOCOL_VERSION: &str = "ucs00-pingpong-1";
pub const PROTOCOL_ORDERING: IbcOrder = IbcOrder::Unordered;
fn ack_success() -> Vec<u8> {
[1].into()
}
fn ack_fail() -> Vec<u8> {
[0].into()
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(_deps: DepsMut, _env: Env, _reply: Reply) -> Result<Response, ContractError> {
Ok(Response::default())
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_open(
_deps: DepsMut,
_env: Env,
msg: IbcChannelOpenMsg,
) -> Result<(), ContractError> {
enforce_order_and_version(msg.channel(), msg.counterparty_version())?;
Ok(())
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_connect(
_deps: DepsMut,
_env: Env,
msg: IbcChannelConnectMsg,
) -> Result<IbcBasicResponse, ContractError> {
enforce_order_and_version(msg.channel(), msg.counterparty_version())?;
Ok(IbcBasicResponse::default())
}
fn enforce_order_and_version(
channel: &IbcChannel,
counterparty_version: Option<&str>,
) -> Result<(), ContractError> {
if channel.version != PROTOCOL_VERSION {
return Err(ContractError::InvalidIbcVersion {
version: channel.version.clone(),
});
}
if let Some(version) = counterparty_version {
if version != PROTOCOL_VERSION {
return Err(ContractError::InvalidIbcVersion {
version: version.to_string(),
});
}
}
if channel.order != PROTOCOL_ORDERING {
return Err(ContractError::OnlyOrderedChannel {});
}
Ok(())
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_close(
_deps: DepsMut,
_env: Env,
_channel: IbcChannelCloseMsg,
) -> Result<IbcBasicResponse, ContractError> {
Err(StdError::generic_err("The game is infinite").into())
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_receive(
deps: DepsMut,
env: Env,
msg: IbcPacketReceiveMsg,
) -> Result<IbcReceiveResponse, ContractError> {
let packet = msg.packet;
let ping_packet = UCS00PingPong::decode(packet.data)?;
do_ibc_packet_receive(deps, env, packet.dest.channel_id, ping_packet).or_else(|err| {
Ok(IbcReceiveResponse::new()
.set_ack(ack_fail())
.add_attributes(vec![
attr("success", "false"),
attr("error", err.to_string()),
]))
})
}
fn do_ibc_packet_receive(
deps: DepsMut,
env: Env,
dest_channel_id: String,
packet: UCS00PingPong,
) -> Result<IbcReceiveResponse, ContractError> {
let config = CONFIG.load(deps.storage)?;
let ibc_packet = packet.reverse(&config, env.block.time.nanos(), dest_channel_id);
let res = IbcReceiveResponse::new()
.set_ack(ack_success())
.add_message(ibc_packet)
.add_attribute("action", if packet.ping { "ping" } else { "pong" })
.add_attribute("success", "true");
Ok(res)
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_ack(
_deps: DepsMut,
_env: Env,
_msg: IbcPacketAckMsg,
) -> Result<IbcBasicResponse, ContractError> {
let attributes = vec![attr("action", "acknowledge")];
Ok(IbcBasicResponse::new().add_attributes(attributes))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_timeout(
deps: DepsMut,
_env: Env,
msg: IbcPacketTimeoutMsg,
) -> Result<IbcBasicResponse, ContractError> {
on_packet_failure(deps, msg.packet, "timeout".to_string())
}
fn on_packet_failure(
_deps: DepsMut,
_packet: IbcPacket,
err: String,
) -> Result<IbcBasicResponse, ContractError> {
let res = IbcBasicResponse::new()
.add_attribute("action", "acknowledge")
.add_attribute("success", "false")
.add_attribute("error", err);
Ok(res)
}