From 709abc680e1427c50c8a990c1c49a7c5a43dc784 Mon Sep 17 00:00:00 2001 From: aeryz Date: Wed, 24 Jul 2024 21:39:24 +0300 Subject: [PATCH] chore: god near connopen works!! Signed-off-by: aeryz --- Cargo.lock | 1 + poc-relayer/Cargo.toml | 1 + poc-relayer/src/main.rs | 195 ++++++++++++++++++++++++++++++---------- 3 files changed, 150 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c10d53ec5..b131c988d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7510,6 +7510,7 @@ dependencies = [ "near-primitives-core 0.23.0", "near-verifier", "num-bigint 0.4.4", + "prost 0.12.6", "protos", "serde", "serde_json", diff --git a/poc-relayer/Cargo.toml b/poc-relayer/Cargo.toml index 3612918b74..9d02b58cf8 100644 --- a/poc-relayer/Cargo.toml +++ b/poc-relayer/Cargo.toml @@ -20,6 +20,7 @@ borsh = { workspace = true, features = ["borsh-deriv cometbft-rpc = { workspace = true } tendermint-rpc = { workspace = true, features = ["http-client", "websocket-client", "default"] } chain-utils = { workspace = true } +prost = { workspace = true } protos.workspace = true hex.workspace = true ibc-vm-rs.workspace = true diff --git a/poc-relayer/src/main.rs b/poc-relayer/src/main.rs index 5d305f85d4..8f36aca4b6 100644 --- a/poc-relayer/src/main.rs +++ b/poc-relayer/src/main.rs @@ -22,6 +22,7 @@ use near_primitives::{ views::{BlockHeaderInnerLiteView, LightClientBlockView, QueryRequest}, }; use num_bigint::BigUint; +use prost::Message; use protos::union::galois::api::v3::union_prover_api_client; use serde::{Deserialize, Serialize}; use tendermint_rpc::{Client as _, WebSocketClientUrl}; @@ -362,7 +363,27 @@ impl Union { .unwrap() .into_inner(); - query_result.value + match ty { + AbciQueryType::State => query_result.value, + AbciQueryType::Proof => { + let proof = protos::ibc::core::commitment::v1::MerkleProof { + proofs: query_result + .proof_ops + .unwrap() + .ops + .into_iter() + .map(|op| { + ::decode( + op.data.as_slice(), + ) + .unwrap() + }) + .collect::>(), + }; + + proof.encode_to_vec() + } + } } async fn connection_open_try>( @@ -426,6 +447,39 @@ impl Union { .await; } + async fn connection_open_confirm( + &self, + connection_id: &str, + proof_ack: Vec, + proof_height: u64, + ) { + self.union + .signers() + .with(|signer| async { + let msg = protos::ibc::core::connection::v1::MsgConnectionOpenConfirm { + connection_id: connection_id.to_string(), + proof_ack, + proof_height: Some( + Height { + revision_number: 0, + revision_height: proof_height, + } + .into(), + ), + signer: signer.to_string(), + }; + + let tx_hash = self + .union + .broadcast_tx_commit(signer, [mk_any(&msg)]) + .await + .unwrap(); + + println!("[ + ] ConnectionOpenConfirm on Union: {tx_hash}"); + }) + .await; + } + async fn update_client>(&self, client_id: &str, header: T) { self.union .signers() @@ -530,6 +584,33 @@ impl Near { self.send_tx("connection_open_init", init).await; } + async fn connection_open_ack( + &self, + connection_id: &str, + counterparty_connection_id: &str, + connection_end_proof: Vec, + proof_height: Height, + ) { + #[derive(serde::Serialize)] + pub struct ConnectionOpenAck { + pub connection_id: String, + pub version: Version, + pub counterparty_connection_id: String, + pub connection_end_proof: Vec, + pub proof_height: Height, + } + + let ack = ConnectionOpenAck { + connection_id: connection_id.to_string(), + version: DEFAULT_IBC_VERSION[0].clone(), + counterparty_connection_id: counterparty_connection_id.to_string(), + connection_end_proof, + proof_height, + }; + + self.send_tx("connection_open_ack", ack).await; + } + async fn next_light_header(&self, trusted_height: u64) -> near::header::Header { let block = self .rpc @@ -797,9 +878,10 @@ impl Near { #[tokio::main] async fn main() { - let near_lc = "08-wasm-1"; + let near_lc = "08-wasm-0"; let cometbls_lc = "cometbls-1"; let near_connection = "connection-1"; + let union_connection = "connection-0"; let near = Near::new(); let union = Union::new().await; @@ -814,54 +896,33 @@ async fn main() { .await; let cs = near.self_client_state().await; - // union - // .create_client( - // wasm::client_state::ClientState { - // data: cs.clone(), - // checksum: hex::decode( - // "88ec41b1142e2895fe8b1260897ed7734670412381322694e32b81348a441a94", - // ) - // .unwrap() - // .try_into() - // .unwrap(), - // latest_height: Height { - // revision_number: 0, - // revision_height: cs.latest_height, - // }, - // }, - // wasm::consensus_state::ConsensusState { - // data: near.self_consensus_state(cs.latest_height + 1).await, - // }, - // ) - // .await; - - // near.connection_open_init(cometbls_lc, near_lc).await; - let header = near.next_light_header(cs.latest_height).await; - // union.update_client(near_lc, header.clone()).await; - - sleep(Duration::from_secs(3)).await; - - let latest_height = union.union.query_latest_height().await.unwrap(); - let light_header = union - .next_light_header(union_client_state.latest_height, latest_height) + union + .create_client( + wasm::client_state::ClientState { + data: cs.clone(), + checksum: hex::decode( + "88ec41b1142e2895fe8b1260897ed7734670412381322694e32b81348a441a94", + ) + .unwrap() + .try_into() + .unwrap(), + latest_height: Height { + revision_number: 0, + revision_height: cs.latest_height, + }, + }, + wasm::consensus_state::ConsensusState { + data: near.self_consensus_state(cs.latest_height + 1).await, + }, + ) .await; - near.update_client(cometbls_lc, light_header.clone()).await; - panic!(); - - sleep(Duration::from_secs(2)).await; + near.connection_open_init(cometbls_lc, near_lc).await; + let header = near.next_light_header(cs.latest_height).await; + let current_near_lc_height = header.new_state.inner_lite.height; + union.update_client(near_lc, header.clone()).await; - // let latest_height = union.union.query_latest_height().await.unwrap(); - // let client_state = - // Any::>::decode_as::( - // &union - // .fetch_abci_query( - // latest_height.revision_height, - // AbciQueryType::State, - // format!("clients/{near_lc}/clientState").as_str(), - // ) - // .await, - // ).unwrap(); + sleep(Duration::from_secs(3)).await; let (proof_init, _) = near .state_proof( @@ -906,12 +967,52 @@ async fn main() { ) .await; + sleep(Duration::from_secs(6)).await; + let latest_height = union.union.query_latest_height().await.unwrap(); let light_header = union .next_light_header(union_client_state.latest_height, latest_height) .await; near.update_client(cometbls_lc, light_header.clone()).await; + + let proof_try = union + .fetch_abci_query( + light_header.signed_header.height.inner() as u64, + AbciQueryType::Proof, + &format!("connections/{union_connection}"), + ) + .await; + + near.connection_open_ack( + near_connection, + union_connection, + proof_try, + Height { + revision_number: 1, + revision_height: light_header.signed_header.height.inner() as u64, + }, + ) + .await; + + sleep(Duration::from_secs(2)).await; + let header = near.next_light_header(current_near_lc_height).await; + union.update_client(near_lc, header.clone()).await; + + let (proof_ack, _) = near + .state_proof( + header.new_state.inner_lite.height - 1, + &format!("connections/{}", near_connection), + ) + .await; + + union + .connection_open_confirm( + union_connection, + proof_ack, + header.new_state.inner_lite.height - 1, + ) + .await; } pub fn convert_block_producers(