Skip to content

Commit

Permalink
wip: still getting invalid slots sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo committed Sep 27, 2023
1 parent 4b01ca4 commit f961e2c
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 345 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions lib/chain-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ tokio = { version = "1.32.0", default-features = false }
typenum = { version = "1.16.0", default-features = false, features = ["const-generics", "no_std"] }
# TODO: Use a version here
prost = "*"
sha2 = "0.10.6"
chrono = { version = "0.4.26", default-features = false, features = ["alloc"] }
15 changes: 15 additions & 0 deletions lib/chain-utils/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ impl<C: ChainSpec> Chain for Evm<C> {
}
}

fn query_latest_timestamp(&self) -> impl Future<Output = i64> + '_ {
async move {
self.beacon_api_client
.finality_update()
.await
.unwrap()
.data
.attested_header
.execution
.timestamp
.try_into()
.unwrap()
}
}

fn self_client_state(
&self,
beacon_height: Height,
Expand Down
32 changes: 31 additions & 1 deletion lib/chain-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use std::{
hash::Hash,
};

use chrono::{DateTime, Utc};
use futures::{Future, Stream};
use serde::{Deserialize, Serialize};
use unionlabs::{
ethereum::{H256, U256},
ethereum_consts_traits::ChainSpec,
events::IbcEvent,
ibc::{
core::client::height::{Height, IsHeight},
Expand Down Expand Up @@ -42,7 +44,7 @@ pub trait Chain {
+ ClientState<Height = Self::Height>;
type SelfConsensusState: Debug + Clone + PartialEq + Serialize + for<'de> Deserialize<'de>;

type Header: Debug + Clone + PartialEq + Serialize + for<'de> Deserialize<'de>;
type Header: Header + Debug + Clone + PartialEq + Serialize + for<'de> Deserialize<'de>;

// this is just Height
type Height: IsHeight;
Expand All @@ -56,6 +58,8 @@ pub trait Chain {

fn query_latest_height(&self) -> impl Future<Output = Self::Height> + '_;

fn query_latest_timestamp(&self) -> impl Future<Output = i64> + '_;

/// The client state on this chain at the specified `Height`.
fn self_client_state(
&self,
Expand Down Expand Up @@ -153,6 +157,32 @@ where
}
}

pub trait Header {
fn timestamp(&self) -> u64;
}

impl<C: ChainSpec> Header for wasm::header::Header<ethereum::header::Header<C>> {
fn timestamp(&self) -> u64 {
self.data
.consensus_update
.attested_header
.execution
.timestamp
}
}

impl Header for cometbls::header::Header {
fn timestamp(&self) -> u64 {
self.signed_header
.header
.time
.seconds
.inner()
.try_into()
.unwrap()
}
}

macro_rules! chain_client_id {
(
#[ty = $Ty:ident]
Expand Down
124 changes: 89 additions & 35 deletions lib/chain-utils/src/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ use futures::{stream, Future, FutureExt, Stream, StreamExt, TryFutureExt, TryStr
use prost::Message;
use protos::ibc::core::channel::v1::QueryPacketAcknowledgementRequest;
use serde::{Deserialize, Serialize};
use sha2::Digest;
use tendermint_rpc::{
event::EventData, query::EventType, Client, SubscriptionClient, WebSocketClient,
WebSocketClientUrl,
error::ResponseSubdetail,
event::EventData,
query::{Condition, EventType, Operand, Query},
Client, SubscriptionClient, WebSocketClient, WebSocketClientUrl,
};
use tokio::select;
use unionlabs::{
ethereum::H256,
events::{IbcEvent, TryFromTendermintEventError},
Expand Down Expand Up @@ -68,36 +72,35 @@ impl Chain for Union {
async move {
let height = self
.tm_client
.latest_commit()
.latest_block()
// .latest_commit()
.await
.unwrap()
.signed_header
.block
// .signed_header
.header
.height
.value();

loop {
if self
.tm_client
.latest_commit()
.await
.unwrap()
.signed_header
.header
.height
.value()
> height
{
break;
}

tokio::time::sleep(std::time::Duration::from_secs(2)).await;
}

self.make_height(height)
}
}

fn query_latest_timestamp(&self) -> impl Future<Output = i64> + '_ {
async move {
self.tm_client
.latest_block()
// .latest_commit()
.await
.unwrap()
.block
// .signed_header
.header
.time
.unix_timestamp()
}
}

fn self_client_state(
&self,
height: Height,
Expand Down Expand Up @@ -281,7 +284,7 @@ impl Union {
pub async fn broadcast_tx_commit(
&self,
messages: impl IntoIterator<Item = protos::google::protobuf::Any>,
) -> tendermint_rpc::endpoint::broadcast::tx_commit::Response {
) {
use protos::cosmos::tx;

let account = self.account_info_of_signer(&self.signer).await;
Expand Down Expand Up @@ -314,6 +317,7 @@ impl Union {
.to_vec(),
fee: Some(tx::v1beta1::Fee {
amount: vec![protos::cosmos::base::v1beta1::Coin {
// TODO: This needs to be configurable
denom: "stake".to_string(),
amount: "1".to_string(),
}],
Expand All @@ -340,24 +344,74 @@ impl Union {
signatures: [alice_signature].to_vec(),
};

let response = self
.tm_client
.broadcast_tx_commit(tx_raw.encode_to_vec())
.await
.unwrap();
let tx_raw_bytes = tx_raw.encode_to_vec();

tracing::info!(check_tx_code = ?response.check_tx.code, check_tx_log = %response.check_tx.log);
tracing::info!(deliver_tx_code = ?response.tx_result.code, deliver_tx_log = %response.tx_result.log);
let tx_hash = hex::encode_upper(sha2::Sha256::new().chain_update(&tx_raw_bytes).finalize());

if response.check_tx.code.is_err() {
panic!("check_tx failed: {:?}", response.check_tx)
let query = Query {
event_type: Some(EventType::Tx),
conditions: [Condition::eq(
"tx.hash".to_string(),
Operand::String(tx_hash.clone()),
)]
.into(),
};

let mut subscription = self.tm_client.subscribe(query.clone()).await.unwrap();

let tx = loop {
if let Ok(_) = self.tm_client.tx(tx_hash.parse().unwrap(), false).await {
// TODO: Log an error if this is unsuccessful
let _ = self.tm_client.unsubscribe(query).await;
return;
}

// dbg!(maybe_tx);

let response_result = self.tm_client.broadcast_tx_sync(tx_raw_bytes.clone()).await;

dbg!(&response_result);

let response = response_result.unwrap();

assert_eq!(tx_hash, response.hash.to_string());

dbg!(&tx_hash);

tracing::info!(check_tx_code = ?response.code, check_tx_log = %response.log);
// tracing::info!(deliver_tx_code = ?response.tx_result.code, deliver_tx_log = %response.tx_result.log);

if response.code.is_err() {
panic!("check_tx failed: {:?}", response)
};

// let tx = subscription
// .inspect(|x| {
// dbg!(x);
// })
// .next()
// .await
// .unwrap()
// .unwrap();

select! {
_ = tokio::time::sleep(std::time::Duration::from_secs(30)) => {
tracing::warn!("tx not submitted, trying again");
}
Some(res) = subscription.next() => {
match res {
Ok(ok) => break ok,
Err(err) => panic!("{err}"),
}
}
}
};

if response.tx_result.code.is_err() {
panic!("tx_result failed: {:?}", response.tx_result)
let EventData::Tx { tx_result } = tx.data else {
panic!()
};

response
dbg!(tx_result);
}

#[must_use]
Expand Down
5 changes: 2 additions & 3 deletions pr-desc.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Lots of big changes! (Apologies for the 100k+ diff)
## `unionlabs`

- Make IBC message types generic over some of their field types (mainly `Height`, `ClientId`, and `ClientType`)

- This allows for more type checking within the new relayer code - instead of `MsgConnectionOpenTry`, you have the following:

```rust
Expand All @@ -18,9 +19,7 @@ Lots of big changes! (Apologies for the 100k+ diff)
<L::Counterparty as LightClient>::ClientId,
HeightOf<ChainOf<L::Counterparty>>,
HeightOf<ChainOf<L>>,
>
>
```

Requiring that the values passed in are from the correct source.


8 changes: 2 additions & 6 deletions voyager-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@
"preset_base": "minimal",
"cometbls_client_address": "0x83428c7db9815f482a39a1715684dcf755021997",
"ibc_handler_address": "0xeda338e4dc46038493b885327842fd3e301cab39",
"ics20_transfer_bank_address": "0xee9170abfbf9421ad6dd07f6bdec9d89f2b581e0",
"ics20_bank_address": "0xf8f7758fbcefd546eaeff7de24aff666b6228e73",
"signer": {
"raw": "0x4e9444a6efd6d42725a250b650a781da2737ea308c839eaccb0f7f3dbd2fea77"
},
"eth_rpc_api": "ws://localhost:8546",
"eth_beacon_rpc_api": "http://localhost:9596",
"wasm_code_id": "0x702876237efab8e1dc3b6438df44de5cc0516b5f863d74dfe0115ff982d1b153"
"eth_beacon_rpc_api": "http://localhost:9596"
},
"union-devnet": {
"chain_type": "union",
"signer": {
"raw": "0xaa820fa947beb242032a41b6dc9a8b9c37d8f5fbcda0966b1ec80335b10a7d6f"
},
"ws_url": "ws://localhost:26657/websocket",
"wasm_code_id": "0x702876237efab8e1dc3b6438df44de5cc0516b5f863d74dfe0115ff982d1b153",
"prover_endpoint": "https://galois-devnet.cryptware.io:443",
"dump_path": "dump",
"grpc_url": "http://localhost:9090"
}
}
}
}
Loading

0 comments on commit f961e2c

Please sign in to comment.