/
token.rs
380 lines (356 loc) · 13.3 KB
/
token.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! IBC token transfer validation as a native validity predicate
use std::collections::{BTreeSet, HashSet};
use std::str::FromStr;
use borsh::BorshDeserialize;
use thiserror::Error;
use crate::ibc::applications::ics20_fungible_token_transfer::msgs::transfer::MsgTransfer;
use crate::ibc::core::ics04_channel::msgs::PacketMsg;
use crate::ibc::core::ics04_channel::packet::Packet;
use crate::ibc::core::ics26_routing::msgs::Ics26Envelope;
use crate::ledger::ibc::storage as ibc_storage;
use crate::ledger::native_vp::{self, Ctx, NativeVp, VpEnv};
use crate::ledger::storage::{self as ledger_storage, StorageHasher};
use crate::proto::SignedTxData;
use crate::types::address::{
Address, DecodeError as AddressError, InternalAddress,
};
use crate::types::ibc::data::{
Error as IbcDataError, FungibleTokenPacketData, IbcMessage,
};
use crate::types::storage::Key;
use crate::types::token::{self, Amount, AmountParseError};
use crate::vm::WasmCacheAccess;
#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum Error {
#[error("Native VP error: {0}")]
NativeVpError(native_vp::Error),
#[error("IBC message error: {0}")]
IbcMessage(IbcDataError),
#[error("Invalid message error")]
InvalidMessage,
#[error("Invalid address error: {0}")]
Address(AddressError),
#[error("Token error")]
NoToken,
#[error("Parsing amount error: {0}")]
Amount(AmountParseError),
#[error("Decoding error: {0}")]
Decoding(std::io::Error),
#[error("Decoding PacketData error: {0}")]
DecodingPacketData(serde_json::Error),
#[error("Invalid token transfer error: {0}")]
TokenTransfer(String),
#[error("IBC message is required as transaction data")]
NoTxData,
#[error("Invalid denom error: {0}")]
Denom(String),
}
/// Result for IBC token VP
pub type Result<T> = std::result::Result<T, Error>;
/// IBC token native VP for IBC token transfer
pub struct IbcToken<'a, DB, H, CA>
where
DB: ledger_storage::DB + for<'iter> ledger_storage::DBIter<'iter>,
H: StorageHasher,
CA: 'static + WasmCacheAccess,
{
/// Context to interact with the host structures.
pub ctx: Ctx<'a, DB, H, CA>,
}
impl<'a, DB, H, CA> NativeVp for IbcToken<'a, DB, H, CA>
where
DB: 'static + ledger_storage::DB + for<'iter> ledger_storage::DBIter<'iter>,
H: 'static + StorageHasher,
CA: 'static + WasmCacheAccess,
{
type Error = Error;
const ADDR: InternalAddress = InternalAddress::IbcBurn;
fn validate_tx(
&self,
tx_data: &[u8],
keys_changed: &BTreeSet<Key>,
_verifiers: &BTreeSet<Address>,
) -> Result<bool> {
let signed =
SignedTxData::try_from_slice(tx_data).map_err(Error::Decoding)?;
let tx_data = &signed.data.ok_or(Error::NoTxData)?;
// Check the non-onwer balance updates
let keys_changed: HashSet<Key> = keys_changed
.iter()
.filter(|k| {
matches!(
token::is_any_multitoken_balance_key(k),
Some((
_,
Address::Internal(
InternalAddress::IbcEscrow
| InternalAddress::IbcBurn
| InternalAddress::IbcMint
)
))
)
})
.cloned()
.collect();
if keys_changed.is_empty() {
// no account is checked by this VP
return Ok(true);
} else if keys_changed.len() > 1 {
// a transaction can update at most 1 special IBC account for now
// a transaction can update at most 1 non-owner balance for now
return Err(Error::TokenTransfer(
"Invalid transfer for multiple non-owner balances".to_owned(),
));
}
// Check the message
let ibc_msg = IbcMessage::decode(tx_data).map_err(Error::IbcMessage)?;
match &ibc_msg.0 {
Ics26Envelope::Ics20Msg(msg) => self.validate_sending_token(msg),
Ics26Envelope::Ics4PacketMsg(PacketMsg::RecvPacket(msg)) => {
self.validate_receiving_token(&msg.packet)
}
Ics26Envelope::Ics4PacketMsg(PacketMsg::AckPacket(msg)) => {
self.validate_refunding_token(&msg.packet)
}
Ics26Envelope::Ics4PacketMsg(PacketMsg::ToPacket(msg)) => {
self.validate_refunding_token(&msg.packet)
}
Ics26Envelope::Ics4PacketMsg(PacketMsg::ToClosePacket(msg)) => {
self.validate_refunding_token(&msg.packet)
}
_ => Err(Error::InvalidMessage),
}
}
}
impl<'a, DB, H, CA> IbcToken<'a, DB, H, CA>
where
DB: 'static + ledger_storage::DB + for<'iter> ledger_storage::DBIter<'iter>,
H: 'static + StorageHasher,
CA: 'static + WasmCacheAccess,
{
fn validate_sending_token(&self, msg: &MsgTransfer) -> Result<bool> {
let mut data = FungibleTokenPacketData::from(msg.clone());
if let Some(token_hash) =
ibc_storage::token_hash_from_denom(&data.denom).map_err(|e| {
Error::Denom(format!("Invalid denom: error {}", e))
})?
{
let denom_key = ibc_storage::ibc_denom_key(token_hash);
let denom_bytes = match self.ctx.read_bytes_pre(&denom_key) {
Ok(Some(v)) => v,
_ => {
return Err(Error::Denom(format!(
"No original denom: denom_key {}",
denom_key
)));
}
};
let denom = std::str::from_utf8(&denom_bytes).map_err(|e| {
Error::Denom(format!(
"Decoding the denom failed: denom_key {}, error {}",
denom_key, e
))
})?;
data.denom = denom.to_string();
}
let token = ibc_storage::token(&data.denom)
.map_err(|e| Error::Denom(e.to_string()))?;
let amount = Amount::from_str(&data.amount).map_err(Error::Amount)?;
let denom = if let Some(denom) = data
.denom
.strip_prefix(&format!("{}/", ibc_storage::MULTITOKEN_STORAGE_KEY))
{
let denom_key = ibc_storage::ibc_denom_key(denom);
match self.ctx.read_bytes_pre(&denom_key)? {
Some(v) => std::str::from_utf8(&v)
.map_err(|e| {
Error::TokenTransfer(format!(
"Decoding the denom failed: denom_key {}, error {}",
denom_key, e
))
})?
.to_string(),
None => {
return Err(Error::TokenTransfer(format!(
"No original denom: denom_key {}",
denom_key
)));
}
}
} else {
data.denom.clone()
};
// check the denomination field
let prefix = format!(
"{}/{}/",
msg.source_port.clone(),
msg.source_channel.clone()
);
let key_prefix = ibc_storage::ibc_account_prefix(
&msg.source_port,
&msg.source_channel,
&token,
);
let change = if denom.starts_with(&prefix) {
// sink zone
// check the amount of the token has been burned
let target_key = token::multitoken_balance_key(
&key_prefix,
&Address::Internal(InternalAddress::IbcBurn),
);
let post = try_decode_token_amount(
self.ctx.read_bytes_temp(&target_key)?,
)?
.unwrap_or_default();
// the previous balance of the burn address should be zero
post.change()
} else {
// source zone
// check the amount of the token has been escrowed
let target_key = token::multitoken_balance_key(
&key_prefix,
&Address::Internal(InternalAddress::IbcEscrow),
);
let pre =
try_decode_token_amount(self.ctx.read_bytes_pre(&target_key)?)?
.unwrap_or_default();
let post = try_decode_token_amount(
self.ctx.read_bytes_post(&target_key)?,
)?
.unwrap_or_default();
post.change() - pre.change()
};
if change == amount.change() {
Ok(true)
} else {
Err(Error::TokenTransfer(format!(
"Sending the token is invalid: {}",
data
)))
}
}
fn validate_receiving_token(&self, packet: &Packet) -> Result<bool> {
let data: FungibleTokenPacketData =
serde_json::from_slice(&packet.data)
.map_err(Error::DecodingPacketData)?;
let token = ibc_storage::token(&data.denom)
.map_err(|e| Error::Denom(e.to_string()))?;
let amount = Amount::from_str(&data.amount).map_err(Error::Amount)?;
let prefix = format!(
"{}/{}/",
packet.source_port.clone(),
packet.source_channel.clone()
);
let key_prefix = ibc_storage::ibc_account_prefix(
&packet.destination_port,
&packet.destination_channel,
&token,
);
let change = if data.denom.starts_with(&prefix) {
// this chain is the source
// check the amount of the token has been unescrowed
let source_key = token::multitoken_balance_key(
&key_prefix,
&Address::Internal(InternalAddress::IbcEscrow),
);
let pre =
try_decode_token_amount(self.ctx.read_bytes_pre(&source_key)?)?
.unwrap_or_default();
let post = try_decode_token_amount(
self.ctx.read_bytes_post(&source_key)?,
)?
.unwrap_or_default();
pre.change() - post.change()
} else {
// the sender is the source
// check the amount of the token has been minted
let source_key = token::multitoken_balance_key(
&key_prefix,
&Address::Internal(InternalAddress::IbcMint),
);
let post = try_decode_token_amount(
self.ctx.read_bytes_temp(&source_key)?,
)?
.unwrap_or_default();
// the previous balance of the mint address should be the maximum
Amount::max().change() - post.change()
};
if change == amount.change() {
Ok(true)
} else {
Err(Error::TokenTransfer(format!(
"Receivinging the token is invalid: {}",
data
)))
}
}
fn validate_refunding_token(&self, packet: &Packet) -> Result<bool> {
let data: FungibleTokenPacketData =
serde_json::from_slice(&packet.data)
.map_err(Error::DecodingPacketData)?;
let token_str = data.denom.split('/').last().ok_or(Error::NoToken)?;
let token = Address::decode(token_str).map_err(Error::Address)?;
let amount = Amount::from_str(&data.amount).map_err(Error::Amount)?;
// check the denom field
let prefix = format!(
"{}/{}/",
packet.source_port.clone(),
packet.source_channel.clone()
);
let key_prefix = ibc_storage::ibc_account_prefix(
&packet.source_port,
&packet.source_channel,
&token,
);
let change = if data.denom.starts_with(&prefix) {
// sink zone: mint the token for the refund
let source_key = token::multitoken_balance_key(
&key_prefix,
&Address::Internal(InternalAddress::IbcMint),
);
let post = try_decode_token_amount(
self.ctx.read_bytes_temp(&source_key)?,
)?
.unwrap_or_default();
// the previous balance of the mint address should be the maximum
Amount::max().change() - post.change()
} else {
// source zone: unescrow the token for the refund
let source_key = token::multitoken_balance_key(
&key_prefix,
&Address::Internal(InternalAddress::IbcEscrow),
);
let pre =
try_decode_token_amount(self.ctx.read_bytes_pre(&source_key)?)?
.unwrap_or_default();
let post = try_decode_token_amount(
self.ctx.read_bytes_post(&source_key)?,
)?
.unwrap_or_default();
pre.change() - post.change()
};
if change == amount.change() {
Ok(true)
} else {
Err(Error::TokenTransfer(format!(
"Refunding the token is invalid: {}",
data,
)))
}
}
}
impl From<native_vp::Error> for Error {
fn from(err: native_vp::Error) -> Self {
Self::NativeVpError(err)
}
}
fn try_decode_token_amount(
bytes: Option<Vec<u8>>,
) -> Result<Option<token::Amount>> {
if let Some(bytes) = bytes {
let tokens = Amount::try_from_slice(&bytes).map_err(Error::Decoding)?;
return Ok(Some(tokens));
}
Ok(None)
}