Skip to content

Commit 76e0d52

Browse files
committed
fix timeouts (?)
1 parent 400ef6f commit 76e0d52

File tree

15 files changed

+155
-65
lines changed

15 files changed

+155
-65
lines changed

Cargo.lock

+13-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ members = [
5050
"utils/subxt/generated",
5151
"utils/parachain-node",
5252
"utils/parachain-node/runtime",
53-
"utils/calldata-compression",
53+
"utils/compress",
5454
# "utils/simnode"
5555

5656
"hyperspace/ethereum/evm-indexer"

config/ethereum-local.toml

+12-12
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,42 @@ name = "ethereum-client"
77
client_id = "07-tendermint-0"
88
channel_whitelist = []
99
commitment_prefix = "696263"
10-
diamond_address = "0xa99cd5fb8b1742a9cee7b73f06adeeffe3877512"
11-
tendermint_address = "0x08563ca7c14985101fb012374e7d0c0b4210553b"
12-
ics20_transfer_bank_address = "0xb8964e5938e1b1898bfbcfc1299cdb8f2e592bbf"
13-
ics20_bank_address = "0x3f98a1c3022628a11aa27d0c315472b319a0e669"
10+
diamond_address = "0xf4c29c09b793ec0832580f0291e25dda4bf7fc92"
11+
tendermint_address = "0xb9a11502bfdddf44eaf78745af8f509e4902fa24"
12+
ics20_transfer_bank_address = "0xe9ed9766bd7c85cc9914be8c890600517bf73a47"
13+
ics20_bank_address = "0x70654c67b997d277c8b589d0a3ded77f5966cf46"
1414
diamond_facets = [
1515
[
1616
"IBCClient",
17-
"0xbc9098af79b4a35defef8a16098b94454849a540",
17+
"0xa0d94305e4508bc3db169248968707421f9f5d4a",
1818
],
1919
[
2020
"IBCConnection",
21-
"0x3a105a567a677cfb049d96b4103ed08ad66c80da",
21+
"0x774e3038f91732f689d8cda201857d891105d73f",
2222
],
2323
[
2424
"IBCChannelHandshake",
25-
"0xcf4210c1f5e1558f58300fea9b8398ab74f8900a",
25+
"0x13a43fd4982cd2e18ac572c90fc2c3d058cfa9c2",
2626
],
2727
[
2828
"IBCPacket",
29-
"0x481796b2a6cf91a68e18b42dedc98c035734043f",
29+
"0x2ab116a110ea6eacb6fc28b76d57719d68648e26",
3030
],
3131
[
3232
"IBCQuerier",
33-
"0xec509da1cb1284f6b3899fcf33c3f208dd4e1f72",
33+
"0x414e2dbd344a507d0a85e284111efdd3cb8405b9",
3434
],
3535
[
3636
"DiamondCutFacet",
37-
"0xa1dfcc1c574eace5458900a3690f9c559dc98478",
37+
"0x3ea11052b9f9ac3147fd665903895594b2867831",
3838
],
3939
[
4040
"DiamondLoupeFacet",
41-
"0xa702cb28df2fb6642c46c32e8ab8d826a21083aa",
41+
"0xd06efe520063a4a1036d4f0d70b87b80919da7ea",
4242
],
4343
[
4444
"OwnershipFacet",
45-
"0x2de2ce2858096a4a3e0ce01cab750293b51d4b0a",
45+
"0x5a7746c9d20797f91d6f397a32a27869a36861cd",
4646
],
4747
]
4848
client_type = "07-tendermint"

hyperspace/core/src/packets.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,12 @@ pub async fn query_ready_and_timed_out_packets(
235235
Error::Custom(format!("Packet height not found for packet {packet:?}"))
236236
})?;
237237

238-
239-
log::info!("sink_height = {sink_height:?}, timeout_height = {:?}", packet.timeout_height);
238+
log::info!("sink_height = {sink_height:?}, timeout_height = {:?}, timeout_timestamp = {}, seq = {}", packet.timeout_height, packet.timeout_timestamp, packet.sequence);
240239

241240
if packet.timed_out(&sink_timestamp, sink_height) {
242241
source.common_state().ignored_timeouted_sequences.lock().await.insert(
243242
packet.sequence.0
244243
);
245-
return Ok(None)
246-
/*
247244
timeout_packets_count.fetch_add(1, Ordering::SeqCst);
248245
// so we know this packet has timed out on the sink, we need to find the maximum
249246
// consensus state height at which we can generate a non-membership proof of the
@@ -253,6 +250,7 @@ pub async fn query_ready_and_timed_out_packets(
253250
&**source,
254251
&**sink,
255252
source_height,
253+
source_timestamp,
256254
sink_height,
257255
sink_timestamp,
258256
latest_sink_height_on_source,
@@ -296,7 +294,6 @@ pub async fn query_ready_and_timed_out_packets(
296294
)
297295
.await?;
298296
return Ok(Some(Left(msg)))
299-
*/
300297
} else {
301298
log::trace!(target: "hyperspace", "The packet has not timed out yet: {:?}", packet);
302299
}
@@ -369,6 +366,7 @@ pub async fn query_ready_and_timed_out_packets(
369366

370367
let msg = construct_recv_message(&**source, &**sink, packet, proof_height).await?;
371368
Ok(Some(Right(msg)))
369+
// return Ok(None)
372370
});
373371
}
374372
}
@@ -470,8 +468,8 @@ pub async fn query_ready_and_timed_out_packets(
470468
log::trace!(target: "hyperspace", "Using proof height: {}", proof_height);
471469
proof_height
472470
} else {
473-
log::trace!(target: "hyperspace", "Skipping acknowledgement for packet {:?} as no proof height could be found", packet);
474-
return Ok(None)
471+
log::trace!(target: "hyperspace", "Skipping acknowledgement for packet {:?}
472+
as no proof height could be found", packet); return Ok(None)
475473
};
476474

477475
if !verify_delay_passed(

hyperspace/core/src/packets/utils.rs

+59-14
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ use ibc::{
3636
};
3737
use ibc_proto::google::protobuf::Any;
3838
use primitives::{find_suitable_proof_height_for_client, Chain};
39-
use std::time::Duration;
39+
use std::{ops::Sub, time::Duration};
4040
use tendermint_proto::Protobuf;
4141

4242
#[allow(clippy::too_many_arguments)]
4343
pub async fn get_timeout_proof_height(
4444
source: &impl Chain,
4545
sink: &impl Chain,
4646
source_height: Height,
47+
source_timestamp: Timestamp,
4748
sink_height: Height,
4849
sink_timestamp: Timestamp,
4950
latest_client_height_on_source: Height,
@@ -74,17 +75,44 @@ pub async fn get_timeout_proof_height(
7475
// bound for where to start our search
7576
// We offset the sink height when this packet was created with the approximate number of
7677
// blocks contained in the difference in timestamp at packet creation until timeout
78+
79+
/*
80+
1. Calculate packet creation time on A.
81+
2. Calculate time difference between the current time and the packet creation time (dTa)
82+
3. Calculate the number of blocks contained in dTb (nTb)
83+
4. Calculate the height of the packet on B (Hb) by subtracting nTb from the current height of B (Tb = Hb - nTb)
84+
5. Calculate timeout block on B (TOb) by adding the timeout duration (dTO) in B blocks to Hb
85+
*/
86+
7787
let timeout_ns = packet.timeout_timestamp.nanoseconds();
7888
let sink_ns = sink_timestamp.nanoseconds();
7989
if timeout_ns > sink_ns {
8090
return None
8191
}
82-
let period = sink_ns.saturating_sub(timeout_ns);
83-
let period = Duration::from_nanos(period);
84-
let timeout_height = (sink_height.revision_height -
85-
calculate_block_delay(period, sink.expected_block_time()))
86-
.saturating_sub(1);
87-
let start_height = Height::new(sink_height.revision_number, timeout_height);
92+
93+
let packet_lifetime_blocks_on_a =
94+
source_height.revision_height.saturating_sub(packet_creation_height);
95+
let packet_timestamp = (source_timestamp -
96+
source.expected_block_time() * packet_lifetime_blocks_on_a as u32)
97+
.ok()?;
98+
let timeout_timestamp_relative = Duration::from_nanos(
99+
packet
100+
.timeout_timestamp
101+
.nanoseconds()
102+
.saturating_sub(packet_timestamp.nanoseconds()),
103+
);
104+
let packet_lifetime_timestamp =
105+
source.expected_block_time() * (packet_lifetime_blocks_on_a as u32);
106+
let packet_lifetime_blocks_on_b = (packet_lifetime_timestamp.as_nanos() /
107+
sink.expected_block_time().as_nanos()) as u64;
108+
let packet_height_on_b =
109+
sink_height.revision_height.saturating_sub(packet_lifetime_blocks_on_b);
110+
let timeout_block_on_b = (packet_height_on_b +
111+
(timeout_timestamp_relative.as_nanos() / sink.expected_block_time().as_nanos())
112+
as u64)
113+
.saturating_sub(1);
114+
115+
let start_height = Height::new(sink_height.revision_number, timeout_block_on_b);
88116
find_suitable_proof_height_for_client(
89117
sink,
90118
source,
@@ -105,13 +133,30 @@ pub async fn get_timeout_proof_height(
105133
if timeout_ns > sink_ns {
106134
return None
107135
}
108-
let period = sink_ns.saturating_sub(timeout_ns);
109-
let period = Duration::from_nanos(period);
110-
let timeout_height = (sink_height.revision_height -
111-
calculate_block_delay(period, sink.expected_block_time()))
112-
.saturating_sub(1);
113-
let start_height =
114-
Height::new(sink_height.revision_number, timeout_height).min(packet.timeout_height);
136+
137+
let packet_lifetime_blocks_on_a =
138+
source_height.revision_height.saturating_sub(packet_creation_height);
139+
let packet_timestamp = (source_timestamp -
140+
source.expected_block_time() * packet_lifetime_blocks_on_a as u32)
141+
.ok()?;
142+
let timeout_timestamp_relative = Duration::from_nanos(
143+
packet
144+
.timeout_timestamp
145+
.nanoseconds()
146+
.saturating_sub(packet_timestamp.nanoseconds()),
147+
);
148+
let packet_lifetime_timestamp =
149+
source.expected_block_time() * (packet_lifetime_blocks_on_a as u32);
150+
let packet_lifetime_blocks_on_b = (packet_lifetime_timestamp.as_nanos() /
151+
sink.expected_block_time().as_nanos()) as u64;
152+
let packet_height_on_b =
153+
sink_height.revision_height.saturating_sub(packet_lifetime_blocks_on_b);
154+
let timeout_block_on_b = (packet_height_on_b +
155+
(timeout_timestamp_relative.as_nanos() / sink.expected_block_time().as_nanos())
156+
as u64)
157+
.saturating_sub(1);
158+
let start_height = Height::new(sink_height.revision_number, timeout_block_on_b)
159+
.min(packet.timeout_height);
115160

116161
find_suitable_proof_height_for_client(
117162
sink,

hyperspace/ethereum/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ itertools = "0.11.0"
5959

6060
redis = "0.22"
6161
sqlx = { version = "0.6", features = [ "runtime-tokio-native-tls" , "postgres" ] }
62+
ics23 = { git = "https://github.com/cosmos/ics23", rev = "74ce807b7be39a7e0afb4e2efb8e28a57965f57b", default-features = false }
6263

6364
[dev-dependencies]
6465
toml = "0.7.3"
6566
git2 = "0.17.1"
6667
tendermint = { git = "https://github.com/informalsystems/tendermint-rs", rev = "e81f7bf23d63ffbcd242381d1ce5e35da3515ff1", default-features = false }
6768

6869
[features]
69-
default = ["testnet", "no_beacon"]
70+
default = ["testnet"]
7071
testnet = [
7172
"sync-committee-primitives/testnet",
7273
"sync-committee-verifier/testnet",

hyperspace/ethereum/evm-indexer/bin/indexer.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async fn main() {
5454

5555
loop {
5656
sync_chain(&rpc, &db, &mut config, &mut indexed_blocks).await;
57-
sleep(Duration::from_millis(500)).await;
57+
sleep(Duration::from_millis(50)).await;
5858
}
5959
} else {
6060
db.delete_indexed_blocks().await.unwrap();
@@ -177,6 +177,9 @@ async fn sync_chain(
177177
)
178178
.await;
179179

180+
// for number in missing_blocks_chunk.into_iter() {
181+
// indexed_blocks.insert(*number);
182+
// }
180183
for block in db_blocks.into_iter() {
181184
indexed_blocks.insert(block.number);
182185
}

hyperspace/ethereum/src/chain.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ use ibc::{
3333
conn_open_init::MsgConnectionOpenInit, conn_open_try::MsgConnectionOpenTry,
3434
},
3535
ics04_channel::msgs as channel_msgs,
36-
ics23_commitment::commitment::{CommitmentProofBytes, CommitmentRoot},
36+
ics23_commitment::{
37+
commitment::{CommitmentProofBytes, CommitmentRoot},
38+
merkle::MerkleProof,
39+
},
3740
ics24_host::identifier::{ChainId, ClientId},
3841
},
3942
proofs::ConsensusProof,
@@ -45,11 +48,14 @@ use ics07_tendermint::{
4548
client_state::ClientState,
4649
consensus_state::ConsensusState,
4750
};
51+
use ics23::commitment_proof::Proof;
4852
use icsxx_ethereum::{
4953
abi::EthereumClientAbi::EthereumClientPrimitivesConsensusStateProof, utils::keccak256,
5054
};
5155
use log::{error, info};
52-
use pallet_ibc::light_clients::{AnyClientMessage, AnyClientState, AnyConsensusState};
56+
use pallet_ibc::light_clients::{
57+
AnyClientMessage, AnyClientState, AnyConsensusState, HostFunctionsManager,
58+
};
5359
use primitives::{
5460
mock::LocalClientTypes, Chain, CommonClientState, IbcProvider, LightClientSync,
5561
MisbehaviourHandler,
@@ -1346,6 +1352,33 @@ impl Chain for EthereumClient {
13461352
)
13471353
.expect("proof can't be empty"),
13481354
);
1355+
let cs_proof = msg.proofs.client_proof().as_ref().expect("no proof").as_bytes();
1356+
use ibc_proto::ibc::core::commitment::v1::MerkleProof as RawMerkleProof;
1357+
let merkle_proof: MerkleProof<HostFunctionsManager> = RawMerkleProof::try_from(
1358+
CommitmentProofBytes::try_from(cs_proof.to_vec()).unwrap(),
1359+
)
1360+
.unwrap()
1361+
.into();
1362+
merkle_proof.clone().proofs.iter_mut().enumerate().for_each(|(i, proof)| {
1363+
proof.proof.as_mut().map(|proof| match proof {
1364+
Proof::Exist(p) =>
1365+
if i == 0 {
1366+
p.value.clear();
1367+
},
1368+
Proof::Nonexist(_) => {
1369+
unimplemented!()
1370+
},
1371+
Proof::Batch(batch) => {
1372+
todo!("1")
1373+
},
1374+
Proof::Compressed(compressed) => {
1375+
todo!("2")
1376+
},
1377+
});
1378+
});
1379+
let new_raw_proof = RawMerkleProof::from(merkle_proof.clone());
1380+
let new_proof = CommitmentProofBytes::try_from(new_raw_proof).unwrap();
1381+
msg.proofs.client_proof = Some(new_proof);
13491382

13501383
let mut token = msg_connection_open_ack_token(msg)?;
13511384
let Token::Tuple(ref mut tokens) = token else {

0 commit comments

Comments
 (0)