Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add contract deployment script and configs #345

Merged
merged 7 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ibc-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ concurrency:
jobs:
ibc-test:
runs-on: ubuntu-20.04
timeout-minutes: 60
timeout-minutes: 75
env:
SRC_DIR: ${{ github.workspace }}/ibc-test-src
AXON_COMMIT: a088cf83d29b8658a7d2ae96a74a53bffb743f03
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ target/
# Ignore chain's data
data

# Ignore debug persistence file
debug

# Ignore Python artifacts
.mypy_cache/
__pycache__/
Expand All @@ -31,4 +34,4 @@ mc.log
# Ignore native MMR storage
ckb_mmr_storage*/

**/ckb-dev*
**/ckb-dev*
3 changes: 2 additions & 1 deletion Cargo.lock

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

9 changes: 8 additions & 1 deletion crates/relayer-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ pub fn restore_key(
ChainType::Ckb => "ckb",
ChainType::Ckb4Ibc => "ckb4ibc",
};
let address_type = match config.r#type() {
ChainType::CosmosSdk => &config.cosmos().address_type,
ChainType::Eth => todo!(),
ChainType::Axon => &AddressType::Axon,
ChainType::Ckb | ChainType::Ckb4Ibc => &AddressType::Ckb,
};

let key_pair = {
let mut keyring = KeyRing::new_secp256k1(Store::Test, account_prefix, config.id())?;

Expand All @@ -284,7 +291,7 @@ pub fn restore_key(
let key_pair = Secp256k1KeyPair::from_mnemonic(
&mnemonic_content,
hdpath,
&config.cosmos().address_type,
address_type,
keyring.account_prefix(),
)?;

Expand Down
62 changes: 42 additions & 20 deletions crates/relayer-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "ibc-relayer-types"
version = "0.23.0"
edition = "2021"
license = "Apache-2.0"
readme = "README.md"
keywords = ["blockchain", "consensus", "cosmos", "ibc", "tendermint"]
repository = "https://github.com/informalsystems/hermes"
authors = ["Informal Systems <[email protected]>"]
name = "ibc-relayer-types"
version = "0.23.0"
edition = "2021"
license = "Apache-2.0"
readme = "README.md"
keywords = ["blockchain", "consensus", "cosmos", "ibc", "tendermint"]
repository = "https://github.com/informalsystems/hermes"
authors = ["Informal Systems <[email protected]>"]
rust-version = "1.65"
description = """
description = """
Implementation of the Inter-Blockchain Communication Protocol (IBC).
This crate comprises the main data structures and on-chain logic.
"""
Expand All @@ -28,31 +28,46 @@ mocks = ["tendermint-testgen", "clock", "std"]
[dependencies]
# Proto definitions for all IBC-related interfaces, e.g., connections or channels.
ibc-proto = { version = "0.28.0", default-features = false }
ics23 = { version = "=0.9.0", default-features = false, features = ["host-functions"] }
ics23 = { version = "=0.9.0", default-features = false, features = [
"host-functions",
] }
time = { version = ">=0.3.0, <0.3.21", default-features = false }
serde_derive = { version = "1.0.104", default-features = false }
serde = { version = "1.0", default-features = false }
serde_json = { version = "1", default-features = false }
erased-serde = { version = "0.3", default-features = false, features = ["alloc"] }
erased-serde = { version = "0.3", default-features = false, features = [
"alloc",
] }
prost = { version = "0.11", default-features = false }
bytes = { version = "1.4.0", default-features = false }
safe-regex = { version = "0.2.5", default-features = false }
subtle-encoding = { version = "0.5", default-features = false }
flex-error = { version = "0.4.4", default-features = false }
derive_more = { version = "0.99.17", default-features = false, features = ["from", "into", "display"] }
derive_more = { version = "0.99.17", default-features = false, features = [
"from",
"into",
"display",
] }
uint = { version = "0.9", default-features = false }
itertools = { version = "0.10.3", default-features = false, features = ["use_alloc"] }
primitive-types = { version = "0.12.1", default-features = false, features = ["serde_no_std"] }
itertools = { version = "0.10.3", default-features = false, features = [
"use_alloc",
] }
primitive-types = { version = "0.12.1", default-features = false, features = [
"serde_no_std",
] }
dyn-clone = "1.0.8"
num-rational = "0.4.1"
eth2_ssz_types = { git = "https://github.com/synapseweb3/lighthouse", rev = "2c246d6" }
bls = { git = "https://github.com/synapseweb3/lighthouse", rev = "2c246d6" }
tree_hash = { git = "https://github.com/synapseweb3/lighthouse", rev = "2c246d6" }
eth2_ssz_types = { git = "https://github.com/synapseweb3/lighthouse", rev = "2c246d6" }
bls = { git = "https://github.com/synapseweb3/lighthouse", rev = "2c246d6" }
tree_hash = { git = "https://github.com/synapseweb3/lighthouse", rev = "2c246d6" }
tree_hash_derive = { git = "https://github.com/synapseweb3/lighthouse", rev = "2c246d6" }
thiserror = "1.0"
ethereum-types = "0.14.1"
hex = "0.4"
axon-tools = { git = "https://github.com/axonweb3/axon-tools.git", branch = "main", version = "0.1.1", features = ["impl-serde", "proof"] }
axon-tools = { git = "https://github.com/axonweb3/axon-tools.git", rev = "2e21582", version = "0.1.1", features = [
"impl-serde",
"proof",
] }
strum = { version = "0.24.1", features = ["derive"] }

[dependencies.tendermint]
Expand All @@ -76,7 +91,14 @@ default-features = false
[dev-dependencies]
env_logger = "0.10.0"
tracing = { version = "0.1.36", default-features = false }
tracing-subscriber = { version = "0.3.14", features = ["fmt", "env-filter", "json"] }
tracing-subscriber = { version = "0.3.14", features = [
"fmt",
"env-filter",
"json",
] }
test-log = { version = "0.2.10", features = ["trace"] }
tendermint-rpc = { version = "0.30.0", features = ["http-client", "websocket-client"] }
tendermint-rpc = { version = "0.30.0", features = [
"http-client",
"websocket-client",
] }
tendermint-testgen = { version = "0.30.0" } # Needed for generating (synthetic) light blocks.
5 changes: 4 additions & 1 deletion crates/relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ tree_hash = { git = "https://github.com/synapseweb3/lighthouse", rev = "2c246d6"

eth_light_client_in_ckb-verification = { version = "0.2.1", git = "https://github.com/synapseweb3/eth-light-client-in-ckb", tag = "v0.2.1" }
eth_light_client_in_ckb-prover = { version = "0.2.1", git = "https://github.com/synapseweb3/eth-light-client-in-ckb", tag = "v0.2.1" }
axon-tools = { git = "https://github.com/axonweb3/axon-tools.git", branch = "main", version = "0.1.1", features = ["impl-serde", "proof"] }
axon-tools = { git = "https://github.com/axonweb3/axon-tools.git", rev = "2e21582", version = "0.1.1", features = [
"impl-serde",
"proof",
] }

subtle-encoding = "0.5"
humantime-serde = "1.1.1"
Expand Down
15 changes: 13 additions & 2 deletions crates/relayer/src/chain/axon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use axon_tools::types::{AxonBlock, Proof as AxonProof, ValidatorExtend};
use eth2_types::Hash256;
use k256::ecdsa::SigningKey;
use rlp::Encodable;
use tracing::warn;
use tracing::{debug, warn};

use crate::{
account::Balance,
Expand Down Expand Up @@ -1204,11 +1204,17 @@ impl AxonChain {
height,
)
.unwrap();
let debug_content = generate_debug_content(&block, &state_root, &block_proof, &validators);

// check the validation of Axon block
axon_tools::verify_proof(block, state_root, &mut validators, block_proof).map_err(
|err| {
let err_msg = format!("unverified axon block, err: {:?}", err);
std::fs::write(
format!("./debug/axon_block_{block_number}.log"),
debug_content,
)
.unwrap();
let err_msg = format!("unverified axon block #{block_number}, err: {:?}", err);
Error::rpc_response(err_msg)
},
)?;
Expand Down Expand Up @@ -1380,6 +1386,11 @@ impl AxonChain {
.into_iter()
.map(Into::into)
.map(|log| OwnableIBCHandlerEvents::decode_log(&log));
debug!(
"Axon received '{}' with events of {}",
message.type_url.as_str(),
events.len()
);
match message.type_url.as_str() {
create_client::TYPE_URL => {
events.find(|event| matches!(event, Ok(CreateClientFilter(_))))
Expand Down
16 changes: 15 additions & 1 deletion crates/relayer/src/chain/axon/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use axon_tools::types::{AxonBlock, Proof as AxonProof};
use axon_tools::types::{AxonBlock, Proof as AxonProof, ValidatorExtend};
use ckb_ics_axon::proof::{
Log as CkbLog, ObjectProof, TransactionReceipt as CkbTransactionReceipt,
};
Expand Down Expand Up @@ -497,3 +497,17 @@ pub fn ibc_event_from_ibc_handler_event(
tx_hash,
}))
}

pub fn generate_debug_content(
block: &AxonBlock,
state_root: &H256,
block_proof: &AxonProof,
validators: &Vec<ValidatorExtend>,
) -> String {
let block = serde_json::to_string_pretty(block).unwrap();
let validators = serde_json::to_string_pretty(validators).unwrap();
let state_root = hex::encode(state_root);
let block_proof = serde_json::to_string_pretty(block_proof).unwrap();
let content = format!("[block]\n{block}\n[validators]\n{validators}\n[state_root]\n{state_root}\n[block_proof]\n{block_proof}");
content
}
48 changes: 29 additions & 19 deletions crates/relayer/src/chain/ckb4ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ckb_ics_axon::handler::{IbcChannel, IbcConnections, IbcPacket, PacketStatus}
use ckb_ics_axon::message::Envelope;
use ckb_ics_axon::object::Ordering;
use ckb_ics_axon::{ChannelArgs, PacketArgs};
use ckb_jsonrpc_types::{JsonBytes, Status, TransactionView};
use ckb_jsonrpc_types::{Status, TransactionView};
use ckb_sdk::constants::TYPE_ID_CODE_HASH;
use ckb_sdk::traits::SecpCkbRawKeySigner;
use ckb_sdk::unlock::{ScriptSigner, SecpSighashScriptSigner};
Expand Down Expand Up @@ -369,13 +369,17 @@ impl Ckb4IbcChain {
.build();
let capacity: u64 = cell.output.capacity.into();
let client_id = hex::encode(cell.output.lock.args.into_bytes());
resps.push((tx, cell_input, capacity, client_id));
if let Ok(client_type) = self.config.lc_client_type(&client_id) {
resps.push((tx, cell_input, capacity, client_type));
} else {
warn!("skip local missing client_id found on-chain: {client_id}");
}
}
Ok(resps)
});
let mut cache = self.connection_cache.borrow_mut();
let prefix = self.query_commitment_prefix()?;
for (transaction, cell_input, capacity, client_id) in self.rt.block_on(future)? {
for (transaction, cell_input, capacity, client_type) in self.rt.block_on(future)? {
let tx = transaction
.expect("empty transaction response")
.transaction
Expand All @@ -387,7 +391,6 @@ impl Ckb4IbcChain {
}
};
let (connections, ibc_connection) = extract_connections_from_tx(tx, &prefix)?;
let client_type = self.config.lc_client_type(&client_id)?;
cache.insert(
client_type,
(ibc_connection, cell_input, capacity, connections),
Expand Down Expand Up @@ -506,7 +509,7 @@ impl ChainEndpoint for Ckb4IbcChain {
) in &config.onchain_light_clients
{
let client_cell = rt.block_on(rpc_client.search_cell_by_typescript(
&TYPE_ID_CODE_HASH.pack(),
&config.client_code_hash.pack(),
&client_cell_type_args.as_bytes().to_owned(),
))?;
let Some(cell) = client_cell else {
Expand Down Expand Up @@ -710,6 +713,13 @@ impl ChainEndpoint for Ckb4IbcChain {
match response {
Ok(height) => {
if let Some(event) = events.get(i).unwrap().clone() {
if let IbcEvent::CreateClient(e) = &event {
let client_type = e.0.client_type;
info!(
"the counterparty client type of Ckb4Ibc is set as {client_type}"
);
self.sync_counterparty_client_type(client_type);
}
let tx_hash: [u8; 32] = tx_hashes.get(i).unwrap().clone().into();
let ibc_event_with_height = IbcEventWithHeight {
event,
Expand Down Expand Up @@ -968,32 +978,32 @@ impl ChainEndpoint for Ckb4IbcChain {

fn query_connection_channels(
&self,
_request: QueryConnectionChannelsRequest,
request: QueryConnectionChannelsRequest,
) -> Result<Vec<IdentifiedChannelEnd>, Error> {
self.query_channels(QueryChannelsRequest { pagination: None })
let connection_channels = self
.query_channels(QueryChannelsRequest { pagination: None })?
.into_iter()
.filter(|channel| {
channel
.channel_end
.connection_hops
.contains(&request.connection_id)
})
.collect();
Ok(connection_channels)
}

fn query_channels(
&self,
request: QueryChannelsRequest,
_request: QueryChannelsRequest,
) -> Result<Vec<IdentifiedChannelEnd>, Error> {
let channel_code_hash = self.get_converter()?.get_channel_code_hash();
let script = Script::new_builder()
.code_hash(channel_code_hash)
.args("".pack())
.hash_type(ScriptHashType::Type.into())
.build();
let search_key = get_prefix_search_key(script);
let (limit, index) = {
if let Some(pagination) = request.pagination {
(pagination.limit as u32, pagination.offset as u32)
} else {
(u32::MAX, 0)
}
};
let json_bytes = JsonBytes::from_vec(index.to_be_bytes().to_vec());
let cursor = Some(json_bytes);
let cells_rpc_result = self.rpc_client.fetch_live_cells(search_key, limit, cursor);
let cells_rpc_result = self.rpc_client.fetch_live_cells(search_key, u32::MAX, None);
let txs_rpc_result = self
.rt
.block_on(cells_rpc_result)?
Expand Down
31 changes: 27 additions & 4 deletions crates/relayer/src/chain/ckb4ibc/message/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use ckb_ics_axon::message::{Envelope, MsgType};
use ckb_ics_axon::{
handler::IbcConnections,
message::{Envelope, MsgType},
};
use ckb_types::packed::BytesOpt;
use ibc_relayer_types::{
clients::{
ics07_axon::client_state::AXON_CLIENT_STATE_TYPE_URL,
Expand All @@ -13,9 +17,12 @@ use ibc_relayer_types::{
Height,
};

use super::{CkbTxInfo, MsgToTxConverter};
use super::{CkbTxInfo, MsgToTxConverter, TxBuilder};

use crate::error::Error;
use crate::{
chain::ckb4ibc::utils::{get_connection_lock_script, get_encoded_object},
error::Error,
};

pub fn convert_create_client<C: MsgToTxConverter>(
msg: MsgCreateClient,
Expand All @@ -42,8 +49,24 @@ pub fn convert_create_client<C: MsgToTxConverter>(
)));
}
};
// one light client only matches one unique connections cell on CKB, if not exist, create it
let find_unique_connections = converter.get_ibc_connections(client_id.as_str()).is_ok();
let unsigned_tx = if !find_unique_connections {
tracing::info!("connections_cell for {client_type} isn't detected on CKB, create one");
let empty_ibc_connections = get_encoded_object(&IbcConnections::default());
let connections_lock_script =
get_connection_lock_script(converter.get_config(), Some(client_id.to_string()))?;

let packed_tx = TxBuilder::default()
.output(connections_lock_script, empty_ibc_connections.data)
.witness(BytesOpt::default(), empty_ibc_connections.witness)
.build();
Some(packed_tx)
} else {
None
};
Ok(CkbTxInfo {
unsigned_tx: None,
unsigned_tx,
envelope: Envelope {
msg_type: MsgType::MsgClientCreate,
content: vec![],
Expand Down
Loading
Loading