Skip to content

Commit

Permalink
store ibc traces when minting
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed May 27, 2024
1 parent 6dc1612 commit 54c555d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 284 deletions.
19 changes: 1 addition & 18 deletions crates/ibc/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use namada_core::ibc::core::channel::types::timeout::TimeoutHeight;
use namada_core::ibc::MsgTransfer;
use namada_core::tendermint::Time as TmTime;
use namada_core::token::Amount;
use namada_events::{EmitEvents, EventTypeBuilder};
use namada_events::EmitEvents;
use namada_governance::storage::proposal::PGFIbcTarget;
use namada_parameters::read_epoch_duration_parameter;
use namada_state::{
Expand Down Expand Up @@ -124,23 +124,6 @@ where
Ok(())
}

/// Get IBC events
fn get_ibc_events(
&self,
event_type: impl AsRef<str>,
) -> Result<Vec<IbcEvent>, StorageError> {
let event_type = EventTypeBuilder::new_of::<IbcEvent>()
.with_segment(event_type)
.build();

Ok(self
.state
.write_log()
.lookup_events_with_prefix(&event_type)
.filter_map(|event| IbcEvent::try_from(event).ok())
.collect())
}

/// Transfer token
fn transfer_token(
&mut self,
Expand Down
24 changes: 24 additions & 0 deletions crates/ibc/src/context/nft_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ where
.store_withdraw(token, added_withdraw)
.map_err(NftTransferError::from)
}

fn store_ibc_trace(
&self,
owner: &Address,
class_id: &PrefixedClassId,
token_id: &TokenId,
) -> Result<(), NftTransferError> {
let ibc_trace = format!("{class_id}/{token_id}");
let trace_hash = storage::calc_hash(&ibc_trace);

self.inner
.borrow_mut()
.store_ibc_trace(owner.to_string(), &trace_hash, &ibc_trace)
.map_err(NftTransferError::from)?;

self.inner
.borrow_mut()
.store_ibc_trace(token_id, &trace_hash, &ibc_trace)
.map_err(NftTransferError::from)
}
}

impl<C> NftTransferValidationContext for NftTransferContext<C>
Expand Down Expand Up @@ -342,6 +362,10 @@ where
self.update_mint_amount(&ibc_token, true)?;
self.add_deposit(&ibc_token)?;

// Store the IBC trace with the token hash to be able to retrieve it
// later
self.store_ibc_trace(account, class_id, token_id)?;

self.inner
.borrow_mut()
.mint_token(account, &ibc_token, Amount::from_u64(1))
Expand Down
6 changes: 0 additions & 6 deletions crates/ibc/src/context/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ pub trait IbcStorageContext: StorageRead + StorageWrite {
/// Emit an IBC event
fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), Error>;

/// Get IBC events
fn get_ibc_events(
&self,
event_type: impl AsRef<str>,
) -> Result<Vec<IbcEvent>, Error>;

/// Transfer token
fn transfer_token(
&mut self,
Expand Down
30 changes: 30 additions & 0 deletions crates/ibc/src/context/token_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ where
.store_withdraw(token, added_withdraw)
.map_err(TokenTransferError::from)
}

fn store_ibc_denom(
&self,
owner: &Address,
coin: &PrefixedCoin,
) -> Result<(), TokenTransferError> {
if coin.denom.trace_path.is_empty() {
// It isn't an IBC denom
return Ok(());
}
let ibc_denom = coin.denom.to_string();
let trace_hash = storage::calc_hash(&ibc_denom);

self.inner
.borrow_mut()
.store_ibc_trace(owner.to_string(), &trace_hash, &ibc_denom)
.map_err(TokenTransferError::from)?;

let base_token = Address::decode(coin.denom.base_denom.as_str())
.map(|a| a.to_string())
.unwrap_or(coin.denom.base_denom.to_string());
self.inner
.borrow_mut()
.store_ibc_trace(base_token, &trace_hash, &ibc_denom)
.map_err(TokenTransferError::from)
}
}

impl<C> TokenTransferValidationContext for TokenTransferContext<C>
Expand Down Expand Up @@ -277,6 +303,10 @@ where
self.insert_verifier(&ibc_token);
}

// Store the IBC denom with the token hash to be able to retrieve it
// later
self.store_ibc_denom(account, coin)?;

self.inner
.borrow_mut()
.mint_token(account, &ibc_token, amount)
Expand Down
59 changes: 1 addition & 58 deletions crates/ibc/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use namada_core::ibc::core::host::types::identifiers::{
use namada_core::ibc::primitives::Timestamp;
use namada_core::tendermint::abci::Event as AbciEvent;
use namada_events::extend::{
event_domain_of, AttributesMap, EventAttributeEntry, ExtendAttributesMap,
event_domain_of, AttributesMap, EventAttributeEntry,
ReadFromEventAttributes as _,
};
use namada_events::{
Expand Down Expand Up @@ -140,63 +140,6 @@ pub struct IbcEvent {
pub attributes: HashMap<String, String>,
}

fn validate_ibc_event_type(
namada_event: &Event,
) -> Result<IbcEventType, EventError> {
if namada_event.kind().domain() != IbcEvent::DOMAIN {
return Err(EventError::InvalidEventType);
}

let event_type = namada_event.kind().sub_domain();

// TODO(namada#3229): validate IBC event types. eg:
//
// ```ignore
// if !matches!(
// event_type,
// "update_client" | "send_packet" | "write_acknowledgement"
// ) {
// return Err(EventError::InvalidEventType);
// }
// ```

Ok(IbcEventType(event_type.to_owned()))
}

impl TryFrom<&Event> for IbcEvent {
type Error = EventError;

fn try_from(
namada_event: &Event,
) -> std::result::Result<Self, Self::Error> {
Ok(Self {
event_type: validate_ibc_event_type(namada_event)?,
#[allow(deprecated)]
attributes: namada_event
.attributes()
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
})
}
}

impl TryFrom<Event> for IbcEvent {
type Error = EventError;

fn try_from(namada_event: Event) -> std::result::Result<Self, Self::Error> {
Ok(Self {
event_type: validate_ibc_event_type(&namada_event)?,
attributes: {
let mut attrs: HashMap<_, _> =
namada_event.into_attributes().into_iter().collect();
attrs.with_attribute(event_domain_of::<Self>());
attrs
},
})
}
}

impl std::cmp::PartialOrd for IbcEvent {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
Expand Down
Loading

0 comments on commit 54c555d

Please sign in to comment.