Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
imp: check sender balance for test_send_transfer_on_cos
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Dec 1, 2023
1 parent 880de49 commit b08f238
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 252 deletions.
73 changes: 42 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions mocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ ibc-query = { workspace = true }
ibc-testkit = { workspace = true }

# cosmos dependencies
basecoin-app = { git = "https://github.com/informalsystems/basecoin-rs.git", rev = "d1cd0d7" }
basecoin-store = { git = "https://github.com/informalsystems/basecoin-rs.git", rev = "d1cd0d7" }
basecoin-app = { git = "https://github.com/informalsystems/basecoin-rs.git", rev = "96e73fd" }
basecoin-store = { git = "https://github.com/informalsystems/basecoin-rs.git", rev = "96e73fd" }
tendermint = { workspace = true }
tendermint-testgen = { workspace = true }

Expand Down
63 changes: 25 additions & 38 deletions mocks/src/cosmos/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::sync::{Arc, Mutex};
use std::time::Duration;

use basecoin_app::abci::v0_37::impls::query as basecoin_query;
use basecoin_app::modules::auth::proto::AccountId;
use basecoin_app::modules::auth::Auth;
use basecoin_app::modules::bank::Bank;
use basecoin_app::modules::bank::{Bank, BankReader, Denom};
use basecoin_app::modules::context::{prefix, Identifiable};
use basecoin_app::modules::ibc::{AnyConsensusState, Ibc, IbcContext};
use basecoin_app::modules::types::IdentifiedModule;
Expand Down Expand Up @@ -32,15 +33,14 @@ use ibc_core::host::types::identifiers::{
ChainId, ChannelId, ClientId, ConnectionId, PortId, Sequence,
};
use ibc_core::host::types::path::{
ChannelEndPath, ClientConsensusStatePath, ClientStatePath, ConnectionPath, Path, SeqAckPath,
ChannelEndPath, ClientConsensusStatePath, ClientStatePath, ConnectionPath, SeqAckPath,
SeqRecvPath, SeqSendPath,
};
use ibc_core::host::{ExecutionContext, ValidationContext};
use ibc_core_host_cosmos::IBC_QUERY_PATH;
use tendermint::abci::request::{InitChain, Query as RequestQuery};
use tendermint::abci::response::Query as ResponseQuery;
use tendermint::block::Height as TmHeight;
use tendermint::v0_37::abci::{Request as AbciRequest, Response as AbciResponse};
use tendermint::v0_37::abci::Request as AbciRequest;
use tendermint::{AppHash, Hash, Time};
use tendermint_testgen::consensus::default_consensus_params;
use tendermint_testgen::light_block::TmLightBlock;
Expand Down Expand Up @@ -133,6 +133,25 @@ impl<S: ProvableStore + Default + Debug> MockCosmosChain<S> {
self.app.ibc().ctx()
}

pub fn balance(&self, denom: &str, account: String) -> Option<u64> {
let account_id: AccountId = account.parse().unwrap();

let denom = Denom(denom.to_string());

if let Some(coin) = self
.app
.bank()
.balance_reader()
.get_all_balances(account_id)
.into_iter()
.find(|c| c.denom == denom)
{
Some(coin.amount.try_into().ok()?)
} else {
None
}
}

pub fn get_blocks(&self) -> Vec<TmLightBlock> {
self.blocks.acquire_mutex().clone()
}
Expand Down Expand Up @@ -252,39 +271,7 @@ impl<S: ProvableStore + Default + Debug> MockCosmosChain<S> {
}

/// Queries the chain for a given path and height.
pub async fn query(
&self,
path: impl Into<Path> + Send,
height: &Height,
) -> (Vec<u8>, CommitmentProofBytes) {
let request = RequestQuery {
path: IBC_QUERY_PATH.to_string(),
data: path.into().to_string().into_bytes().into(),
height: TmHeight::try_from(height.revision_height()).unwrap(),
prove: true,
};

let mut app = self.app.clone();

let response = match app.call(AbciRequest::Query(request)).await.unwrap() {
AbciResponse::Query(res) => res,
_ => panic!("unexpected response from query"),
};

let proof = match response.proof {
Some(proof) => proof,
None => panic!("proof not found in query response"),
};

let merkle_proof = convert_tm_to_ics_merkle_proof(&proof);

let commitment_proof = merkle_proof.try_into().unwrap();

(response.value.into(), commitment_proof)
}

/// Queries the chain for a given path and height.
pub fn sync_query(
pub fn query(
&self,
data: Vec<u8>,
path: String,
Expand Down Expand Up @@ -350,7 +337,7 @@ impl<S: ProvableStore + Default + Debug> MockCosmosChain<S> {
.store_client_state(client_state_path, client_state.into())
.unwrap();

let consensus_state_path = ClientConsensusStatePath::new(client_id.clone(), 0, 10);
let consensus_state_path = ClientConsensusStatePath::new(client_id.clone(), 0, 3);

let consensus_state = AnyConsensusState::Tendermint(
TmConsensusState::new(vec![].into(), Time::now(), Hash::None).into(),
Expand Down
41 changes: 2 additions & 39 deletions mocks/src/cosmos/handle.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
use std::fmt::Debug;
use std::str::FromStr;

use basecoin_app::modules::ibc::{AnyConsensusState, IbcContext};
use basecoin_store::context::ProvableStore;
use basecoin_store::impls::RevertibleStore;
use ibc_app_transfer::types::msgs::transfer::MsgTransfer;
use ibc_app_transfer::types::packet::PacketData;
use ibc_app_transfer::types::{Coin, Memo, PrefixedDenom};
use ibc_client_tendermint::types::Header;
use ibc_core::channel::types::timeout::TimeoutHeight;
use ibc_core::client::types::Height;
use ibc_core::commitment_types::commitment::CommitmentProofBytes;
use ibc_core::handler::types::events::IbcEvent;
use ibc_core::host::types::identifiers::{ChainId, ChannelId, PortId};
use ibc_core::host::types::identifiers::ChainId;
use ibc_core::primitives::proto::Any;
use ibc_core::primitives::{Signer, Timestamp};

use super::app::MockCosmosChain;
use crate::relayer::context::ChainContext;
use crate::relayer::handle::Handle;

impl<S: ProvableStore + Debug + Default> Handle for MockCosmosChain<S> {
Expand Down Expand Up @@ -62,7 +55,7 @@ impl<S: ProvableStore + Debug + Default> Handle for MockCosmosChain<S> {
path: String,
height: &Height,
) -> (Vec<u8>, CommitmentProofBytes) {
self.sync_query(data, path, height)
self.query(data, path, height)
}

fn consensus_state_to_any(&self, consensus_state: AnyConsensusState) -> Any {
Expand All @@ -81,33 +74,3 @@ impl<S: ProvableStore + Debug + Default> Handle for MockCosmosChain<S> {
events
}
}

impl<S: ProvableStore + Debug + Default> ChainContext<MockCosmosChain<S>> {
/// Builds a CosmosChain token transfer message; serialized to Any
/// Note: keep the amount value lower than the initial balance of the sender address
pub fn build_token_transfer(
&self,
denom: PrefixedDenom,
sender: Signer,
receiver: Signer,
amount: u64,
) -> MsgTransfer {
let packet_data = PacketData {
token: Coin {
denom,
amount: amount.into(),
},
sender,
receiver,
memo: Memo::from_str("").unwrap(),
};

MsgTransfer {
port_id_on_a: PortId::transfer(),
chan_id_on_a: ChannelId::default(),
packet_data,
timeout_height_on_b: TimeoutHeight::At(Height::new(1, 200).unwrap()),
timeout_timestamp_on_b: Timestamp::none(),
}
}
}
Loading

0 comments on commit b08f238

Please sign in to comment.