Skip to content

Commit

Permalink
chore: fix timestamp conversion bug and issue comments mentioned in t…
Browse files Browse the repository at this point in the history
…he PR
  • Loading branch information
liyukun committed Aug 7, 2023
1 parent a86d041 commit 08a0258
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
25 changes: 14 additions & 11 deletions crates/relayer/src/chain/axon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use ethers::{
prelude::{k256::ecdsa::SigningKey, EthLogDecode, SignerMiddleware},
providers::{Middleware, Provider, Ws},
signers::{Signer as _, Wallet},
types::{TransactionRequest, TxHash, U64},
types::{BlockNumber, TransactionRequest, TxHash, U64},
utils::rlp,
};
use ibc_proto::google::protobuf::Any;
Expand Down Expand Up @@ -277,19 +277,22 @@ impl ChainEndpoint for AxonChain {
_key_name: Option<&str>,
_denom: Option<&str>,
) -> Result<Balance, Error> {
// use mock coin to eliminate warns in wallet worker
// TODO: implement the real `query_balance` function later
warn!("axon query_balance() cannot implement");
Ok(Balance {
amount: "100".to_owned(),
denom: "AT".to_owned(),
})
}

fn query_all_balances(&self, _key_name: Option<&str>) -> Result<Vec<Balance>, Error> {
// TODO: implement the real `query_all_balances` function later
warn!("axon query_all_balances() cannot implement");
Ok(vec![])
}

fn query_denom_trace(&self, _hash: String) -> Result<DenomTrace, Error> {
// TODO: implement the real `query_denom_trace` function later
warn!("axon query_denom_trace() cannot implement");
Ok(DenomTrace {
path: "".to_owned(),
Expand All @@ -303,23 +306,23 @@ impl ChainEndpoint for AxonChain {
}

fn query_application_status(&self) -> Result<ChainStatus, Error> {
let tip_block_number = self
.rt
.block_on(self.client.get_block_number())
.map_err(|e| Error::rpc_response(e.to_string()))?
.as_u64();
let tip_block = self
.rt
.block_on(self.client.get_block(tip_block_number))
.block_on(self.client.get_block(BlockNumber::Latest))
.map_err(|e| Error::rpc_response(e.to_string()))?;
if let Some(block) = tip_block {
let height = if let Some(number) = block.number {
Height::from_noncosmos_height(number.as_u64())
} else {
Height::default()
};
Ok(ChainStatus {
height: Height::from_noncosmos_height(tip_block_number),
timestamp: Timestamp::from_nanoseconds(block.timestamp.as_u64() * 100000).unwrap(),
height,
timestamp: to_timestamp(block.timestamp.as_u64())?,
})
} else {
Ok(ChainStatus {
height: Height::from_noncosmos_height(tip_block_number),
height: Height::default(),
timestamp: Timestamp::now(),
})
}
Expand Down
6 changes: 5 additions & 1 deletion crates/relayer/src/chain/axon/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ impl AxonEventMonitor {
if let Some(cmd) = result {
match cmd {
MonitorCmd::Shutdown => return Next::Abort,
MonitorCmd::Subscribe(tx) => tx.send(self.event_bus.subscribe()).unwrap(),
MonitorCmd::Subscribe(tx) => {
if let Err(e) = tx.send(self.event_bus.subscribe()) {
error!("failed to send back subscription: {e}");
}
}
}
}
Next::Continue
Expand Down
11 changes: 6 additions & 5 deletions crates/relayer/src/chain/axon/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ use ibc_relayer_types::{
},
events::IbcEvent,
proofs::Proofs,
timestamp::Timestamp,
tx_msg::Msg,
Height,
};

use super::contract;
use super::{
contract,
utils::{to_timestamp, SEC_TO_NANO},
};
use crate::error::Error;

fn into_ethers_client_id(value: Option<ClientId>) -> String {
Expand Down Expand Up @@ -293,8 +295,7 @@ impl From<contract::PacketData> for Packet {
destination_channel: value.destination_channel.as_str().parse().unwrap(),
data: value.data.as_ref().to_vec(),
timeout_height,
timeout_timestamp: Timestamp::from_nanoseconds(value.timeout_timestamp * 100000)
.unwrap(),
timeout_timestamp: to_timestamp(value.timeout_timestamp).unwrap(),
}
}
}
Expand All @@ -312,7 +313,7 @@ impl From<Packet> for contract::PacketData {
TimeoutHeight::At(h) => h.into(),
TimeoutHeight::Never => Default::default(),
},
timeout_timestamp: value.timeout_timestamp.nanoseconds() / 100000,
timeout_timestamp: value.timeout_timestamp.nanoseconds() / SEC_TO_NANO,
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions crates/relayer/src/chain/axon/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ use ibc_relayer_types::{
ics07_ckb::{client_state::CkbClientState, consensus_state::CkbConsensusState},
},
core::{ics02_client::client_type::ClientType, ics24_host::identifier::ClientId},
timestamp::Timestamp,
};

pub const SEC_TO_NANO: u64 = 1_000_000_000;

pub fn to_timestamp(seconds: u64) -> Result<Timestamp, Error> {
Timestamp::from_nanoseconds(seconds * SEC_TO_NANO).map_err(convert_err)
}

pub fn convert_err<T: ToString>(err: T) -> Error {
Error::other_error(err.to_string())
}
Expand Down
3 changes: 3 additions & 0 deletions crates/relayer/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ impl AnyClientState {
pub fn trust_threshold(&self) -> Option<TrustThreshold> {
match self {
AnyClientState::Tendermint(state) => Some(state.trust_threshold),

// `ONE_THRID` of TrustThreshold is just a mock value to pass runtime check
// for non-cosmos chains
AnyClientState::Eth(_) => Some(TrustThreshold::ONE_THIRD),
AnyClientState::Ckb(_) => Some(TrustThreshold::ONE_THIRD),
AnyClientState::Axon(_) => Some(TrustThreshold::ONE_THIRD),
Expand Down

0 comments on commit 08a0258

Please sign in to comment.