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

Commit

Permalink
update submit checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoAtwill committed Oct 17, 2023
1 parent e9d0c6b commit 3a42972
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

52 changes: 41 additions & 11 deletions ipc/provider/src/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,47 @@ impl<T: BottomUpCheckpointRelayer + Send + Sync + 'static> BottomUpCheckpointMan

/// Submit the checkpoint from the target validator address
pub async fn submit_checkpoint(&self, validator: &Address) -> Result<()> {
self.submit_last_epoch(validator).await?;
self.submit_next_epoch(validator).await
}

async fn next_submission_height(&self) -> Result<ChainEpoch> {
let last_checkpoint_epoch = self
.parent_handler
.last_bottom_up_checkpoint_height(&self.metadata.child.id)
.await
.map_err(|e| {
anyhow!("cannot obtain the last bottom up checkpoint height due to: {e:}")
})?;
Ok(last_checkpoint_epoch + self.checkpoint_period())
}

async fn submit_last_epoch(&self, validator: &Address) -> Result<()> {
let subnet = &self.metadata.child.id;
if self
.child_handler
.has_submitted_in_last_checkpoint_height(subnet, validator)
.await?
{
return Ok(());
}

let height = self
.child_handler
.last_bottom_up_checkpoint_height(subnet)
.await?;
let bundle = self.child_handler.checkpoint_bundle_at(height).await?;
log::debug!("bottom up bundle: {bundle:?}");

self.parent_handler
.submit_checkpoint(validator, bundle)
.await
.map_err(|e| anyhow!("cannot submit bottom up checkpoint due to: {e:}"))?;

Ok(())
}

async fn submit_next_epoch(&self, validator: &Address) -> Result<()> {
let next_submission_height = self.next_submission_height().await?;
let current_height = self.child_handler.current_epoch().await?;

Expand All @@ -121,15 +162,4 @@ impl<T: BottomUpCheckpointRelayer + Send + Sync + 'static> BottomUpCheckpointMan

Ok(())
}

async fn next_submission_height(&self) -> Result<ChainEpoch> {
let last_checkpoint_epoch = self
.parent_handler
.last_bottom_up_checkpoint_height(&self.metadata.child.id)
.await
.map_err(|e| {
anyhow!("cannot obtain the last bottom up checkpoint height due to: {e:}")
})?;
Ok(last_checkpoint_epoch + self.checkpoint_period())
}
}
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 @@ -841,6 +841,23 @@ impl BottomUpCheckpointRelayer for EthSubnetManager {
Ok(epoch as ChainEpoch)
}

async fn has_submitted_in_last_checkpoint_height(
&self,
subnet_id: &SubnetID,
submitter: &Address,
) -> Result<bool> {
let address = contract_address_from_subnet(subnet_id)?;
let contract = subnet_actor_getter_facet::SubnetActorGetterFacet::new(
address,
Arc::new(self.ipc_contract_info.provider.clone()),
);
let addr = payload_to_evm_address(submitter.payload())?;
Ok(contract
.has_submitted_in_last_bottom_up_checkpoint_height(addr)
.call()
.await?)
}

async fn checkpoint_period(&self, subnet_id: &SubnetID) -> anyhow::Result<ChainEpoch> {
let address = contract_address_from_subnet(subnet_id)?;
let contract = subnet_actor_getter_facet::SubnetActorGetterFacet::new(
Expand Down
6 changes: 6 additions & 0 deletions ipc/provider/src/manager/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ pub trait BottomUpCheckpointRelayer: Send + Sync {
) -> Result<()>;
/// The last confirmed/submitted checkpoint height.
async fn last_bottom_up_checkpoint_height(&self, subnet_id: &SubnetID) -> Result<ChainEpoch>;
/// Check if the submitter has already submitted in the `last_bottom_up_checkpoint_height`
async fn has_submitted_in_last_checkpoint_height(
&self,
subnet_id: &SubnetID,
submitter: &Address,
) -> Result<bool>;
/// Get the checkpoint period, i.e the number of blocks to submit bottom up checkpoints.
async fn checkpoint_period(&self, subnet_id: &SubnetID) -> Result<ChainEpoch>;
/// Get the checkpoint at a specific height. If it does not exist, it will through error.
Expand Down

0 comments on commit 3a42972

Please sign in to comment.