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

Commit

Permalink
Support top down query (#331)
Browse files Browse the repository at this point in the history
* support top down query

* review feedbacks
  • Loading branch information
cryptoAtwill authored Oct 6, 2023
1 parent 804cb5b commit dc011ff
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ipc/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use fvm_shared::{
use ipc_identity::{
EthKeyAddress, EvmKeyStore, KeyStore, KeyStoreConfig, PersistentKeyStore, Wallet,
};
use ipc_sdk::staking::StakingChangeRequest;
use ipc_sdk::{
cross::CrossMsg,
subnet::{ConsensusType, ConstructParams},
Expand Down Expand Up @@ -534,6 +535,33 @@ impl IpcProvider {
conn.manager().chain_head_height().await
}

/// Obtain the genesis epoch of the input subnet.
pub async fn genesis_epoch(&self, subnet: &SubnetID) -> anyhow::Result<ChainEpoch> {
let parent = subnet.parent().ok_or_else(|| anyhow!("no parent found"))?;
let conn = match self.connection(&parent) {
None => return Err(anyhow!("parent subnet config not found")),
Some(conn) => conn,
};
conn.manager().genesis_epoch(subnet).await
}

/// Get the changes in subnet validators. This is fetched from parent.
pub async fn get_validator_changeset(
&self,
subnet: &SubnetID,
start: ChainEpoch,
end: ChainEpoch,
) -> anyhow::Result<Vec<StakingChangeRequest>> {
let conn = match self.connection(subnet) {
None => return Err(anyhow!("target subnet not found")),
Some(conn) => conn,
};

conn.manager()
.get_validator_changeset(subnet, start, end)
.await
}

pub async fn get_top_down_msgs(
&self,
subnet: &SubnetID,
Expand Down Expand Up @@ -572,6 +600,15 @@ impl IpcProvider {

conn.manager().get_chain_id().await
}

pub async fn get_chain_head_height(&self, subnet: &SubnetID) -> anyhow::Result<ChainEpoch> {
let conn = match self.connection(subnet) {
None => return Err(anyhow!("target subnet not found")),
Some(conn) => conn,
};

conn.manager().chain_head_height().await
}
}

/// Lotus JSON keytype format
Expand Down
17 changes: 17 additions & 0 deletions ipc/provider/src/manager/evm/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ struct IPCContractInfo {

#[async_trait]
impl TopDownCheckpointQuery for EthSubnetManager {
async fn genesis_epoch(&self, subnet_id: &SubnetID) -> Result<ChainEpoch> {
let address = contract_address_from_subnet(subnet_id)?;
log::info!("querying genesis epoch in evm subnet contract: {address:}");

let evm_subnet_id = gateway_getter_facet::SubnetID::try_from(subnet_id)?;

let contract = gateway_getter_facet::GatewayGetterFacet::new(
self.ipc_contract_info.gateway_addr,
Arc::new(self.ipc_contract_info.provider.clone()),
);
let (exists, subnet) = contract.get_subnet(evm_subnet_id).call().await?;
if !exists {
return Err(anyhow!("subnet: {} does not exists", subnet_id));
}
Ok(subnet.genesis_epoch.as_u64() as ChainEpoch)
}

async fn chain_head_height(&self) -> Result<ChainEpoch> {
let block = self
.ipc_contract_info
Expand Down
2 changes: 2 additions & 0 deletions ipc/provider/src/manager/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub trait SubnetManager: Send + Sync + TopDownCheckpointQuery {
/// Trait to interact with a subnet to query the necessary information for top down checkpoint.
#[async_trait]
pub trait TopDownCheckpointQuery: Send + Sync {
/// Returns the genesis epoch that the subnet is created in parent network
async fn genesis_epoch(&self, subnet_id: &SubnetID) -> Result<ChainEpoch>;
/// Returns the chain head height
async fn chain_head_height(&self) -> Result<ChainEpoch>;
/// Returns the list of top down messages
Expand Down

0 comments on commit dc011ff

Please sign in to comment.