Skip to content

Commit

Permalink
feat(wip): complete the coding of packet test between two ckb endpoin…
Browse files Browse the repository at this point in the history
…ts, the next is debugging the relayer monitor logic
  • Loading branch information
liyukun committed Aug 23, 2023
1 parent b15acee commit 5e4fac9
Show file tree
Hide file tree
Showing 28 changed files with 560 additions and 251 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

6 changes: 4 additions & 2 deletions crates/relayer/src/chain/ckb4ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ impl Ckb4IbcChain {
let channel_code_hash = self.get_converter()?.get_channel_code_hash();
let client_id = self
.config
.lc_client_type_args(*self.counterparty_client_type.borrow())?;
.lc_client_type_hash(*self.counterparty_client_type.borrow())?;
let channel_args = ChannelArgs {
client_id,
client_id: client_id.into(),
open: is_open,
channel_id: get_channel_number(channel_id)?,
port_id: convert_port_id_to_array(port_id)?,
Expand Down Expand Up @@ -621,6 +621,8 @@ impl ChainEndpoint for Ckb4IbcChain {
&ScriptGroup {
script: Script::from(&self.tx_assembler_address()?),
group_type: ScriptGroupType::Lock,
// TODO: here should be more indices in case of more than one Secp256k1 cells
// have been filled in the transaction
input_indices: vec![last_input_idx],
output_indices: vec![],
},
Expand Down
8 changes: 4 additions & 4 deletions crates/relayer/src/chain/ckb4ibc/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ impl Ckb4IbcEventMonitor {
let connection_code_hash = get_script_hash(&self.config.connection_type_args);
let client_id = self
.config
.lc_client_type_args(self.counterparty_client_type)
.lc_client_type_hash(self.counterparty_client_type)
.map_err(|e| Error::collect_events_failed(e.to_string()))?;
let script = Script::new_builder()
.code_hash(connection_code_hash)
.hash_type(ScriptHashType::Type.into())
.args(client_id.as_slice().pack())
.args(client_id.as_bytes().pack())
.build();
let key = get_search_key(script);
let (ibc_connection_cell, tx_hash) = self
Expand Down Expand Up @@ -202,10 +202,10 @@ impl Ckb4IbcEventMonitor {
async fn fetch_channel_events(&self) -> Result<EventBatch> {
let client_id = self
.config
.lc_client_type_args(self.counterparty_client_type)
.lc_client_type_hash(self.counterparty_client_type)
.map_err(|e| Error::collect_events_failed(e.to_string()))?;
let channel_args = ChannelArgs {
client_id,
client_id: client_id.into(),
open: false,
channel_id: Default::default(),
port_id: Default::default(),
Expand Down
3 changes: 2 additions & 1 deletion crates/relayer/src/chain/ckb4ibc/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ pub fn get_connection_lock_script(
let mut client_cell_type_args = vec![];
if let Some(client_id) = client_id {
let client_type = config.lc_client_type(&client_id)?;
client_cell_type_args.append(&mut config.lc_client_type_args(client_type)?.to_vec());
client_cell_type_args
.append(&mut config.lc_client_type_hash(client_type)?.as_bytes().to_vec());
}
let script = Script::new_builder()
.code_hash(get_script_hash(&config.connection_type_args))
Expand Down
41 changes: 34 additions & 7 deletions crates/relayer/src/config/ckb4ibc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use ckb_types::H256;
use ckb_types::{
core::ScriptHashType,
packed::Script,
prelude::{Builder, Entity, Pack, Unpack},
H256,
};
use ibc_relayer_types::core::{
ics02_client::client_type::ClientType,
ics24_host::identifier::{ChainId, ClientId},
Expand Down Expand Up @@ -26,6 +31,7 @@ pub struct ChainConfig {
pub key_name: String,
pub store_prefix: String,

pub client_code_hash: H256,
pub connection_type_args: H256,
pub channel_type_args: H256,
pub packet_type_args: H256,
Expand All @@ -43,14 +49,16 @@ impl ChainConfig {
.onchain_light_clients
.iter()
.find_map(|(_, v)| {
if hex::encode(&v.client_cell_type_args) == client_id.as_str() {
let client_type_hash =
calc_type_hash(&self.client_code_hash, &v.client_cell_type_args);
if hex::encode(client_type_hash) == client_id.as_str() {
Some(v.chain_id.clone())
} else {
None
}
})
.ok_or(Error::other_error(format!(
"config.toml missing {client_id}"
"config.toml missing client_id {client_id}"
)))?;
Ok(chain_id)
}
Expand All @@ -60,7 +68,9 @@ impl ChainConfig {
.onchain_light_clients
.iter()
.find_map(|(k, v)| {
if hex::encode(&v.client_cell_type_args) == client_id {
let client_type_hash =
calc_type_hash(&self.client_code_hash, &v.client_cell_type_args);
if hex::encode(client_type_hash) == client_id {
Some(*k)
} else {
None
Expand All @@ -74,19 +84,26 @@ impl ChainConfig {

pub fn lc_client_id(&self, client_type: ClientType) -> Result<ClientId, Error> {
let client_type_args = self.lc_client_type_args(client_type)?;
let client_id = hex::encode(client_type_args).parse().unwrap();
let client_type_hash = calc_type_hash(&self.client_code_hash, &client_type_args);
let client_id = hex::encode(client_type_hash).parse().unwrap();
Ok(client_id)
}

pub fn lc_client_type_args(&self, client_type: ClientType) -> Result<[u8; 32], Error> {
pub fn lc_client_type_args(&self, client_type: ClientType) -> Result<H256, Error> {
let (_, item) = self
.onchain_light_clients
.iter()
.find(|(v, _)| **v == client_type)
.ok_or(Error::other_error(format!(
"config.toml missing client_type {client_type}"
)))?;
Ok(item.client_cell_type_args.clone().into())
Ok(item.client_cell_type_args.clone())
}

pub fn lc_client_type_hash(&self, client_type: ClientType) -> Result<H256, Error> {
let client_type_args = self.lc_client_type_args(client_type)?;
let client_type_hash = calc_type_hash(&self.client_code_hash, &client_type_args);
Ok(client_type_hash)
}
}

Expand All @@ -101,3 +118,13 @@ fn light_client_serialize<S: serde::Serializer>(
.try_for_each(|(k, v)| map.serialize_entry(k.as_str(), v))?;
map.end()
}

fn calc_type_hash(client_code_hash: &H256, client_type_args: &H256) -> H256 {
let client_type_hash = Script::new_builder()
.code_hash(client_code_hash.pack())
.hash_type(ScriptHashType::Type.into())
.args(client_type_args.as_bytes().pack())
.build()
.calc_script_hash();
client_type_hash.unpack()
}
10 changes: 4 additions & 6 deletions crates/relayer/tests/config/fixtures/relayer_conf_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ address_type = { derivation = 'cosmos' }

[chains.packet_filter]
policy = 'allow'
list = [
['ica*', '*'],
['transfer', 'channel-0'],
]
list = [['ica*', '*'], ['transfer', 'channel-0']]

[[chains]]
id = 'chain_B'
Expand All @@ -68,7 +65,7 @@ initial_checkpoint = "0xa179cbd497b112acb057039601a75e2daafae994aa5f01d6e1a1d6f8
key_name = 'relayer_eth_wallet'
rpc_addr_pool = [
"https://www.lightclientdata.org",
"https://beacon-nd-995-871-887.p2pify.com/c9dce41bab3e120f541e4ffb748efa60"
"https://beacon-nd-995-871-887.p2pify.com/c9dce41bab3e120f541e4ffb748efa60",
]
rpc_port = 8545
[chains.forks]
Expand Down Expand Up @@ -105,9 +102,10 @@ ckb_rpc = "http://127.0.0.1:8114"
ckb_indexer_rpc = "http://127.0.0.1:8114"
key_name = "relayer_ckb_wallet"
store_prefix = "forcerelay"
client_code_hash = "0x00000000000000000000000000000000000000000000000000545950455f4944"
connection_type_args = "0xf49ce32397c6741998b04d7548c5ed372007424daf67ee5bfadaefec3c865781"
channel_type_args = "0xfbe09e8ff3e5f3d0fab7cc7431feed2131846184d356a9626639f55e7f471846"
packet_type_args = "0xad8bca6ff76ad676bb7eb35882faf259cb6ff50be8ce9c0b9d6f51728ec54fab"
[chains.onchain_light_clients]
Ckb4Ibc = { chain_id = "ckb4ibc-1", client_id = "29866e133f707f070459b905065294ab1a7b70bea200952a080f849319ae6202", client_cell_type_args = "0x29866e133f707f070459b905065294ab1a7b70bea200952a080f849319ae6202" }
Axon = { chain_id = "axon-0", client_id = "07-axon-x", client_cell_type_args = "0x29866e133f707f070459b905065294ab1a7b70bea200952a080f849319ae6202" }
Axon = { chain_id = "axon-0", client_id = "07-axon-x", client_cell_type_args = "0x29866e133f707f070459b905065294ab1a7b70bea200952a080f849319ae6202" }
2 changes: 1 addition & 1 deletion tools/ckb4ibc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ log = "0.4.19"
toml_edit = "0.19.14"
lazy_static = "1.4.0"

forcerelay-ckb-sdk = { git = "https://github.com/synapseweb3/forcerelay-ckb-sdk", rev = "1737ef3" }
forcerelay-ckb-sdk = { git = "https://github.com/synapseweb3/forcerelay-ckb-sdk", rev = "264bcf7" }
Binary file modified tools/ckb4ibc-test/contracts/ics-channel
Binary file not shown.
Binary file modified tools/ckb4ibc-test/contracts/ics-connection
Binary file not shown.
Binary file modified tools/ckb4ibc-test/contracts/ics-packet
Binary file not shown.
6 changes: 2 additions & 4 deletions tools/ckb4ibc-test/contracts/version
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Download here

all-contracts-in-debug-mode
all-contracts

commit: 8dc3c92

<https://github.com/synapseweb3/ibc-ckb-contracts/actions/runs/5925648798>
<https://github.com/synapseweb3/ibc-ckb-contracts/actions/runs/5948286236>
14 changes: 7 additions & 7 deletions tools/ckb4ibc-test/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use ckb_types::{h256, H256};
pub const CONNECTION_CODE_HASH: H256 =
h256!("0xcf6e0c0148123081af1deda0ef162d39cfdfe1ea6565d3689009c1f3562a5e82");
pub const CHANNEL_CODE_HASH: H256 =
h256!("0xbc5049d6328c889a4adab53bf3c40379539db5145d61cfa4f86f61c77a962258");
h256!("0x903e6d9cebe749c92c36c509a797ab422c6d2023d0e77d07de646b6079f7cdbb");

pub const CONNECTION_TYPE_ARGS: H256 =
pub const CONNECTION_TYPE_ARGS: H256 =
h256!("0xf49ce32397c6741998b04d7548c5ed372007424daf67ee5bfadaefec3c865781");
pub const CHANNEL_TYPE_ARGS: H256 =
h256!("0xb407c3b93dee611b2e65248254c28012a8d227c53803e5842d4a81934179adfc");
pub const PACKET_TYPE_ARGS: H256 =
h256!("0x63b3d51df3884cc649135a51ad2a1ae1a8c2dfeca37c8b16220b26716fb3b4c4");
pub const CHANNEL_TYPE_ARGS: H256 =
h256!("0xc0cc3728ad52ed82eb2ea6d418e773b8410faa14caa96714f6d8b653165c0e0b");
pub const PACKET_TYPE_ARGS: H256 =
h256!("0x395dff439222ac7146022147b846f19586a319034ddc0b9038395e2fcf352292");
pub const CLIENT_TYPE_ARGS: H256 =
h256!("0x7ede7d98985de2f464e737b8e177ede186c50d3d584d1bd9b2399330c2187e61");
h256!("0xd3d01069e55e5653f80c9018470a4ee7f7b1a408cf191b6272808d9b6a5c1dbf");
93 changes: 45 additions & 48 deletions tools/ckb4ibc-test/src/framework/utils/ckb.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::consts::{CHANNEL_CODE_HASH, CLIENT_TYPE_ARGS, CONNECTION_CODE_HASH};
use crate::generator::GENESIS_TXHASH;
use crate::generator::{calc_script_hash, GENESIS_TXHASH};
use crate::rpc_client::RpcClient;

use anyhow::{anyhow, Result};
Expand All @@ -9,6 +9,7 @@ use ckb_ics_axon::handler::{IbcChannel, IbcConnections};
use ckb_ics_axon::object::State;
use ckb_ics_axon::ChannelArgs;
use ckb_jsonrpc_types::{Deserialize, JsonBytes, Status};
use ckb_sdk::constants::TYPE_ID_CODE_HASH;
use ckb_sdk::rpc::ckb_light_client::{ScriptType, SearchKey};
use ckb_sdk::*;
use ckb_types::core::ScriptHashType;
Expand Down Expand Up @@ -296,30 +297,30 @@ fn check_and_wait_ckb_transaction(hash: H256, port: u32) {
}
}

fn get_test_client_id() -> H256 {
calc_script_hash(&TYPE_ID_CODE_HASH, CLIENT_TYPE_ARGS.as_bytes())
}

pub fn fetch_ibc_connections(port: u32) -> IbcConnections {
let client = get_client(port);
let rpc_client = get_client(port);
let search_key = SearchKey {
script: Script::new_builder()
.code_hash(CONNECTION_CODE_HASH.pack())
.args(get_test_client_id().as_bytes().pack())
.hash_type(ScriptHashType::Type.into())
.build()
.into(),
script_type: ScriptType::Lock,
filter: None,
with_data: None,
group_by_transaction: None,
script_search_mode: None,
};
let mut loop_count = 0;
loop {
let search_connection_cell = client.fetch_live_cells(
SearchKey {
script: Script::new_builder()
.code_hash(CONNECTION_CODE_HASH.pack())
.args("".pack()) // FIXME: use prefix search
.hash_type(ScriptHashType::Type.into())
.build()
.into(),
script_type: ScriptType::Lock,
filter: None,
with_data: None,
group_by_transaction: None,
script_search_mode: None,
},
1,
None,
);
let cells = wait_task(search_connection_cell).unwrap();
let cells = wait_task(rpc_client.fetch_live_cells(search_key.clone(), 1, None)).unwrap();
if let Some(connection_cell) = cells.objects.into_iter().next() {
let tx = wait_task(client.get_transaction(&connection_cell.out_point.tx_hash))
let tx = wait_task(rpc_client.get_transaction(&connection_cell.out_point.tx_hash))
.unwrap()
.unwrap()
.transaction
Expand Down Expand Up @@ -355,35 +356,31 @@ fn channel_id_to_u16(channel_id: &ChannelId) -> u16 {

pub fn fetch_ibc_channel_cell(port: u32, port_id: [u8; 32], channel_id: &ChannelId) -> IbcChannel {
let rpc_client = get_client(port);
let search_key = SearchKey {
script: Script::new_builder()
.code_hash(CHANNEL_CODE_HASH.pack())
.args(
ChannelArgs {
client_id: get_test_client_id().into(),
open: true,
channel_id: channel_id_to_u16(channel_id),
port_id,
}
.to_args()
.pack(),
)
.hash_type(ScriptHashType::Type.into())
.build()
.into(),
script_type: ScriptType::Lock,
filter: None,
with_data: None,
group_by_transaction: None,
script_search_mode: None,
};
let mut loop_count = 0;
loop {
let search_channel_cell = rpc_client.fetch_live_cells(
SearchKey {
script: Script::new_builder()
.code_hash(CHANNEL_CODE_HASH.pack())
.args(
ChannelArgs {
client_id: CLIENT_TYPE_ARGS.into(),
open: true,
channel_id: channel_id_to_u16(channel_id),
port_id,
}
.to_args()
.pack(),
)
.hash_type(ScriptHashType::Type.into())
.build()
.into(),
script_type: ScriptType::Lock,
filter: None,
with_data: None,
group_by_transaction: None,
script_search_mode: None,
},
1,
None,
);
let cells = wait_task(search_channel_cell).unwrap();
let cells = wait_task(rpc_client.fetch_live_cells(search_key.clone(), 1, None)).unwrap();
if let Some(channel_cell) = cells.objects.first() {
let tx_hash = &channel_cell.out_point.tx_hash;
let tx = wait_task(rpc_client.get_transaction(tx_hash))
Expand Down
Loading

0 comments on commit 5e4fac9

Please sign in to comment.