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

Refactor checkpoints #74

Merged
merged 16 commits into from
Apr 3, 2023
14 changes: 7 additions & 7 deletions gateway/src/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ipc_sdk::subnet_id::SubnetID;
use primitives::{TCid, TLink};
use serde_tuple::{Deserialize_tuple, Serialize_tuple};

use crate::CrossMsgs;
use crate::{CrossMsg, CrossMsgs};

#[derive(PartialEq, Eq, Clone, Debug, Serialize_tuple, Deserialize_tuple)]
pub struct Checkpoint {
Expand Down Expand Up @@ -63,17 +63,17 @@ impl Checkpoint {
}

/// return cross_msg included in the checkpoint.
pub fn cross_msgs(&self) -> Option<&CrossMsgMeta> {
pub fn cross_msgs(&self) -> &Vec<CrossMsg> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there's no need for the ref now that this is a vector.

Suggested change
pub fn cross_msgs(&self) -> &Vec<CrossMsg> {
pub fn cross_msgs(&self) -> Vec<CrossMsg> {

self.data.cross_msgs.as_ref()
}

/// set cross_msg included in the checkpoint.
pub fn set_cross_msgs(&mut self, cm: CrossMsgMeta) {
self.data.cross_msgs = Some(cm)
pub fn set_cross_msgs(&mut self, cm: Vec<CrossMsg>) {
cryptoAtwill marked this conversation as resolved.
Show resolved Hide resolved
self.data.cross_msgs = cm
}

/// return cross_msg included in the checkpoint as mutable reference
pub fn cross_msgs_mut(&mut self) -> Option<&mut CrossMsgMeta> {
pub fn cross_msgs_mut(&mut self) -> &mut Vec<CrossMsg> {
self.data.cross_msgs.as_mut()
}

Expand Down Expand Up @@ -121,7 +121,7 @@ pub struct CheckData {
pub epoch: ChainEpoch,
pub prev_check: TCid<TLink<Checkpoint>>,
pub children: Vec<ChildCheck>,
pub cross_msgs: Option<CrossMsgMeta>,
pub cross_msgs: Vec<CrossMsg>,
}
impl CheckData {
pub fn new(id: SubnetID, epoch: ChainEpoch) -> Self {
Expand All @@ -131,7 +131,7 @@ impl CheckData {
epoch,
prev_check: TCid::default(),
children: Vec::new(),
cross_msgs: None,
cross_msgs: vec![],
}
}
}
Expand Down
52 changes: 30 additions & 22 deletions gateway/src/cron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl CronCheckpoint {
/// - top down messages are sorted by `nonce` in descending order
///
/// Actor will not perform sorting to save gas. Client should do it, actor just check.
fn hash(&self) -> anyhow::Result<HashOutput> {
pub fn hash(&self) -> anyhow::Result<HashOutput> {
// check top down msgs
for i in 1..self.top_down_msgs.len() {
match self.top_down_msgs[i - 1]
Expand Down Expand Up @@ -161,6 +161,17 @@ impl CronSubmission {
}
}

pub fn most_voted_weight<BS: Blockstore>(&self, store: &BS) -> anyhow::Result<TokenAmount> {
// we will only have one entry in the `most_submitted` set if more than 2/3 has reached
if let Some(hash) = &self.most_voted_hash {
Ok(self
.get_submission_weight(store, hash)?
.unwrap_or_else(TokenAmount::zero))
} else {
Ok(TokenAmount::zero())
}
}

pub fn get_submission<BS: Blockstore>(
&self,
store: &BS,
Expand Down Expand Up @@ -213,6 +224,17 @@ impl CronSubmission {
VoteExecutionStatus::ReachingConsensus
}
}

/// Checks if the submitter has already submitted the checkpoint.
pub fn has_submitted<BS: Blockstore>(
&self,
store: &BS,
submitter: &Address,
) -> anyhow::Result<bool> {
let addr_byte_key = BytesKey::from(submitter.to_bytes());
let hamt = self.submitters.load(store)?;
Ok(hamt.contains_key(&addr_byte_key)?)
}
}

/// The status indicating if the voting should be executed
Expand Down Expand Up @@ -328,17 +350,15 @@ impl CronSubmission {
})
}

/// Checks if the submitter has already submitted the checkpoint. Currently used only in
/// tests, but can be used in prod as well.
#[cfg(test)]
fn has_submitted<BS: Blockstore>(
/// Checks if the checkpoint hash has already inserted in the store
fn get_submission_weight<BS: Blockstore>(
&self,
store: &BS,
submitter: &Address,
) -> anyhow::Result<bool> {
let addr_byte_key = BytesKey::from(submitter.to_bytes());
let hamt = self.submitters.load(store)?;
Ok(hamt.contains_key(&addr_byte_key)?)
hash: &HashOutput,
) -> anyhow::Result<Option<TokenAmount>> {
let hamt = self.submission_weights.load(store)?;
let r = hamt.get(&BytesKey::from(hash.as_slice()))?;
Ok(r.cloned())
}

/// Checks if the checkpoint hash has already inserted in the store
Expand All @@ -351,18 +371,6 @@ impl CronSubmission {
let hamt = self.submissions.load(store)?;
Ok(hamt.contains_key(&BytesKey::from(hash.as_slice()))?)
}

/// Checks if the checkpoint hash has already inserted in the store
#[cfg(test)]
fn get_submission_weight<BS: Blockstore>(
&self,
store: &BS,
hash: &HashOutput,
) -> anyhow::Result<Option<TokenAmount>> {
let hamt = self.submission_weights.load(store)?;
let r = hamt.get(&BytesKey::from(hash.as_slice()))?;
Ok(r.cloned())
}
}

#[cfg(test)]
Expand Down
15 changes: 0 additions & 15 deletions gateway/src/cross.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use anyhow::anyhow;
use fil_actors_runtime::runtime::Runtime;
use fil_actors_runtime::ActorError;
use fil_actors_runtime::BURNT_FUNDS_ACTOR_ADDR;
use fvm_ipld_blockstore::MemoryBlockstore;
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_ipld_encoding::RawBytes;
use fvm_shared::address::Address;
Expand All @@ -14,7 +13,6 @@ use fvm_shared::MethodNum;
use fvm_shared::METHOD_SEND;
use ipc_sdk::address::IPCAddress;
use ipc_sdk::subnet_id::SubnetID;
use primitives::{TCid, TLink};
use serde_tuple::{Deserialize_tuple, Serialize_tuple};
use std::path::Path;

Expand Down Expand Up @@ -156,19 +154,6 @@ impl CrossMsgs {
pub fn new() -> Self {
Self::default()
}

pub(crate) fn cid(&self) -> anyhow::Result<TCid<TLink<CrossMsgs>>> {
TCid::new_link(&MemoryBlockstore::new(), self)
}

/// Appends a cross-message to cross-msgs
pub(crate) fn add_msg(&mut self, msg: &CrossMsg) -> bool {
if !self.msgs.contains(msg) {
self.msgs.push(msg.clone());
return true;
}
false
}
}

/// Transaction side-effects from the commitment of a cross-net message. It burns funds
Expand Down
Loading