Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
align topdown and validator changes query pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoAtwill committed Nov 27, 2023
1 parent 54eadef commit 3702a88
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
19 changes: 3 additions & 16 deletions ipc/cli/src/commands/crossmsg/topdown_cross.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: MIT
//! List top down cross messages
use anyhow::anyhow;
use std::fmt::Debug;
use std::str::FromStr;

Expand All @@ -27,19 +26,9 @@ impl CommandLineHandler for ListTopdownMsgs {
let provider = get_ipc_provider(global)?;
let subnet = SubnetID::from_str(&arguments.subnet)?;

let hash = if let Some(hash) = &arguments.block_hash {
hex::decode(hash)?
} else {
let parent = subnet
.parent()
.ok_or_else(|| anyhow!("subnet has not parent"))?;
let hash = provider.get_block_hash(&parent, arguments.epoch).await?;
hash.block_hash
};
let msgs = provider
.get_top_down_msgs(&subnet, arguments.epoch, &hash)
.await?;
for msg in msgs {
let result = provider.get_top_down_msgs(&subnet, arguments.epoch).await?;
println!("block hash: {}", hex::encode(result.block_hash));
for msg in result.value {
println!(
"from: {}, to: {}, value: {}, nonce: {}, fee: {} ",
msg.msg.from.to_string()?,
Expand All @@ -61,8 +50,6 @@ pub(crate) struct ListTopdownMsgsArgs {
pub subnet: String,
#[arg(long, short, help = "Include topdown messages of this epoch")]
pub epoch: ChainEpoch,
#[arg(long, short, help = "The block hash to query until")]
pub block_hash: Option<String>,
}

pub(crate) struct LatestParentFinality;
Expand Down
7 changes: 2 additions & 5 deletions ipc/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,17 +649,14 @@ impl IpcProvider {
&self,
subnet: &SubnetID,
epoch: ChainEpoch,
block_hash: &[u8],
) -> anyhow::Result<Vec<CrossMsg>> {
) -> anyhow::Result<TopDownQueryPayload<Vec<CrossMsg>>> {
let parent = subnet.parent().ok_or_else(|| anyhow!("no parent found"))?;
let conn = match self.connection(&parent) {
None => return Err(anyhow!("target parent subnet not found")),
Some(conn) => conn,
};

conn.manager()
.get_top_down_msgs(subnet, epoch, block_hash)
.await
conn.manager().get_top_down_msgs(subnet, epoch).await
}

pub async fn get_block_hash(
Expand Down
38 changes: 19 additions & 19 deletions ipc/provider/src/manager/evm/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,17 @@ impl TopDownFinalityQuery for EthSubnetManager {
&self,
subnet_id: &SubnetID,
epoch: ChainEpoch,
block_hash: &[u8],
) -> Result<Vec<CrossMsg>> {
if block_hash.len() != 32 {
return Err(anyhow!("invalid block hash len"));
}

) -> Result<TopDownQueryPayload<Vec<CrossMsg>>> {
let gateway_contract = gateway_manager_facet::GatewayManagerFacet::new(
self.ipc_contract_info.gateway_addr,
Arc::new(self.ipc_contract_info.provider.clone()),
);

let topic1 = contract_address_from_subnet(subnet_id)?;
log::debug!(
"getting top down messages for subnet: {:?} with topic 1: {}, block hash equals: {}",
"getting top down messages for subnet: {:?} with topic 1: {}",
subnet_id,
topic1,
hex::encode(block_hash),
);

let ev = gateway_contract
Expand All @@ -136,22 +130,28 @@ impl TopDownFinalityQuery for EthSubnetManager {
.topic1(topic1);

let mut messages = vec![];
let mut hash = None;
for (event, meta) in query_with_meta(ev, gateway_contract.client()).await? {
let msg = CrossMsg::try_from(event.message)?;
log::debug!("received message: {:?} and meta: {:?}", msg, meta);

if meta.block_hash.0 != block_hash {
return Err(anyhow!(
"block hash not equal, input: {}, received: {}",
hex::encode(block_hash),
hex::encode(meta.block_hash.0)
));
if let Some(h) = hash {
if h != meta.block_hash {
return Err(anyhow!("block hash not equal"));
}
} else {
hash = Some(meta.block_hash);
}

messages.push(msg);
messages.push(CrossMsg::try_from(event.message)?);
}

Ok(messages)
let block_hash = if let Some(h) = hash {
h.0.to_vec()
} else {
self.get_block_hash(epoch).await?.block_hash
};
Ok(TopDownQueryPayload {
value: messages,
block_hash,
})
}

async fn get_block_hash(&self, height: ChainEpoch) -> Result<GetBlockHashResult> {
Expand Down
3 changes: 1 addition & 2 deletions ipc/provider/src/manager/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ pub trait TopDownFinalityQuery: Send + Sync {
&self,
subnet_id: &SubnetID,
epoch: ChainEpoch,
block_hash: &[u8],
) -> Result<Vec<CrossMsg>>;
) -> Result<TopDownQueryPayload<Vec<CrossMsg>>>;
/// Get the block hash
async fn get_block_hash(&self, height: ChainEpoch) -> Result<GetBlockHashResult>;
/// Get the validator change set from start to end block.
Expand Down

0 comments on commit 3702a88

Please sign in to comment.