Skip to content

Commit 787162a

Browse files
committed
Merge branch 'yuji/store-ibc-trace' (#3319)
* origin/yuji/store-ibc-trace: fix func name add changelog new is_receiving_success store ibc traces when minting
2 parents 5a6e83a + bc40dd2 commit 787162a

File tree

9 files changed

+80
-271
lines changed

9 files changed

+80
-271
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Store IBC denom when minting the IBC token
2+
([\#3317](https://github.com/anoma/namada/issues/3317))

crates/ibc/src/actions.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use namada_core::ibc::core::channel::types::timeout::TimeoutHeight;
1313
use namada_core::ibc::MsgTransfer;
1414
use namada_core::tendermint::Time as TmTime;
1515
use namada_core::token::Amount;
16-
use namada_events::{EmitEvents, EventTypeBuilder};
16+
use namada_events::EmitEvents;
1717
use namada_governance::storage::proposal::PGFIbcTarget;
1818
use namada_parameters::read_epoch_duration_parameter;
1919
use namada_state::{
@@ -124,23 +124,6 @@ where
124124
Ok(())
125125
}
126126

127-
/// Get IBC events
128-
fn get_ibc_events(
129-
&self,
130-
event_type: impl AsRef<str>,
131-
) -> Result<Vec<IbcEvent>, StorageError> {
132-
let event_type = EventTypeBuilder::new_of::<IbcEvent>()
133-
.with_segment(event_type)
134-
.build();
135-
136-
Ok(self
137-
.state
138-
.write_log()
139-
.lookup_events_with_prefix(&event_type)
140-
.filter_map(|event| IbcEvent::try_from(event).ok())
141-
.collect())
142-
}
143-
144127
/// Transfer token
145128
fn transfer_token(
146129
&mut self,

crates/ibc/src/context/nft_transfer.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ where
8989
.store_withdraw(token, added_withdraw)
9090
.map_err(NftTransferError::from)
9191
}
92+
93+
fn store_ibc_trace(
94+
&self,
95+
owner: &Address,
96+
class_id: &PrefixedClassId,
97+
token_id: &TokenId,
98+
) -> Result<(), NftTransferError> {
99+
let ibc_trace = format!("{class_id}/{token_id}");
100+
let trace_hash = storage::calc_hash(&ibc_trace);
101+
102+
self.inner
103+
.borrow_mut()
104+
.store_ibc_trace(owner.to_string(), &trace_hash, &ibc_trace)
105+
.map_err(NftTransferError::from)?;
106+
107+
self.inner
108+
.borrow_mut()
109+
.store_ibc_trace(token_id, &trace_hash, &ibc_trace)
110+
.map_err(NftTransferError::from)
111+
}
92112
}
93113

94114
impl<C> NftTransferValidationContext for NftTransferContext<C>
@@ -342,6 +362,10 @@ where
342362
self.update_mint_amount(&ibc_token, true)?;
343363
self.add_deposit(&ibc_token)?;
344364

365+
// Store the IBC trace with the token hash to be able to retrieve it
366+
// later
367+
self.store_ibc_trace(account, class_id, token_id)?;
368+
345369
self.inner
346370
.borrow_mut()
347371
.mint_token(account, &ibc_token, Amount::from_u64(1))

crates/ibc/src/context/storage.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ pub trait IbcStorageContext: StorageRead + StorageWrite {
1212
/// Emit an IBC event
1313
fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), Error>;
1414

15-
/// Get IBC events
16-
fn get_ibc_events(
17-
&self,
18-
event_type: impl AsRef<str>,
19-
) -> Result<Vec<IbcEvent>, Error>;
20-
2115
/// Transfer token
2216
fn transfer_token(
2317
&mut self,

crates/ibc/src/context/token_transfer.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,32 @@ where
141141
.store_withdraw(token, added_withdraw)
142142
.map_err(TokenTransferError::from)
143143
}
144+
145+
fn maybe_store_ibc_denom(
146+
&self,
147+
owner: &Address,
148+
coin: &PrefixedCoin,
149+
) -> Result<(), TokenTransferError> {
150+
if coin.denom.trace_path.is_empty() {
151+
// It isn't an IBC denom
152+
return Ok(());
153+
}
154+
let ibc_denom = coin.denom.to_string();
155+
let trace_hash = storage::calc_hash(&ibc_denom);
156+
157+
self.inner
158+
.borrow_mut()
159+
.store_ibc_trace(owner.to_string(), &trace_hash, &ibc_denom)
160+
.map_err(TokenTransferError::from)?;
161+
162+
let base_token = Address::decode(coin.denom.base_denom.as_str())
163+
.map(|a| a.to_string())
164+
.unwrap_or(coin.denom.base_denom.to_string());
165+
self.inner
166+
.borrow_mut()
167+
.store_ibc_trace(base_token, &trace_hash, &ibc_denom)
168+
.map_err(TokenTransferError::from)
169+
}
144170
}
145171

146172
impl<C> TokenTransferValidationContext for TokenTransferContext<C>
@@ -277,6 +303,10 @@ where
277303
self.insert_verifier(&ibc_token);
278304
}
279305

306+
// Store the IBC denom with the token hash to be able to retrieve it
307+
// later
308+
self.maybe_store_ibc_denom(account, coin)?;
309+
280310
self.inner
281311
.borrow_mut()
282312
.mint_token(account, &ibc_token, amount)

crates/ibc/src/event.rs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use namada_core::ibc::core::host::types::identifiers::{
1919
use namada_core::ibc::primitives::Timestamp;
2020
use namada_core::tendermint::abci::Event as AbciEvent;
2121
use namada_events::extend::{
22-
event_domain_of, AttributesMap, EventAttributeEntry, ExtendAttributesMap,
22+
event_domain_of, AttributesMap, EventAttributeEntry,
2323
ReadFromEventAttributes as _,
2424
};
2525
use namada_events::{
@@ -140,63 +140,6 @@ pub struct IbcEvent {
140140
pub attributes: HashMap<String, String>,
141141
}
142142

143-
fn validate_ibc_event_type(
144-
namada_event: &Event,
145-
) -> Result<IbcEventType, EventError> {
146-
if namada_event.kind().domain() != IbcEvent::DOMAIN {
147-
return Err(EventError::InvalidEventType);
148-
}
149-
150-
let event_type = namada_event.kind().sub_domain();
151-
152-
// TODO(namada#3229): validate IBC event types. eg:
153-
//
154-
// ```ignore
155-
// if !matches!(
156-
// event_type,
157-
// "update_client" | "send_packet" | "write_acknowledgement"
158-
// ) {
159-
// return Err(EventError::InvalidEventType);
160-
// }
161-
// ```
162-
163-
Ok(IbcEventType(event_type.to_owned()))
164-
}
165-
166-
impl TryFrom<&Event> for IbcEvent {
167-
type Error = EventError;
168-
169-
fn try_from(
170-
namada_event: &Event,
171-
) -> std::result::Result<Self, Self::Error> {
172-
Ok(Self {
173-
event_type: validate_ibc_event_type(namada_event)?,
174-
#[allow(deprecated)]
175-
attributes: namada_event
176-
.attributes()
177-
.iter()
178-
.map(|(k, v)| (k.clone(), v.clone()))
179-
.collect(),
180-
})
181-
}
182-
}
183-
184-
impl TryFrom<Event> for IbcEvent {
185-
type Error = EventError;
186-
187-
fn try_from(namada_event: Event) -> std::result::Result<Self, Self::Error> {
188-
Ok(Self {
189-
event_type: validate_ibc_event_type(&namada_event)?,
190-
attributes: {
191-
let mut attrs: HashMap<_, _> =
192-
namada_event.into_attributes().into_iter().collect();
193-
attrs.with_attribute(event_domain_of::<Self>());
194-
attrs
195-
},
196-
})
197-
}
198-
}
199-
200143
impl std::cmp::PartialOrd for IbcEvent {
201144
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
202145
Some(self.cmp(other))

0 commit comments

Comments
 (0)