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

Commit

Permalink
Merge pull request #266 from consensus-shipyard/integrate_diamond
Browse files Browse the repository at this point in the history
Integrate diamond
  • Loading branch information
adlrocha authored Jul 27, 2023
2 parents 00a07f3 + 70bb43f commit 8122281
Show file tree
Hide file tree
Showing 25 changed files with 55,401 additions and 54,140 deletions.
34,943 changes: 0 additions & 34,943 deletions contracts/Gateway.json

This file was deleted.

11,900 changes: 11,900 additions & 0 deletions contracts/GatewayGetterFacet.json

Large diffs are not rendered by default.

8,061 changes: 8,061 additions & 0 deletions contracts/GatewayManagerFacet.json

Large diffs are not rendered by default.

14,291 changes: 14,291 additions & 0 deletions contracts/GatewayRouterFacet.json

Large diffs are not rendered by default.

17,869 changes: 0 additions & 17,869 deletions contracts/SubnetActor.json

This file was deleted.

7,259 changes: 7,259 additions & 0 deletions contracts/SubnetActorGetterFacet.json

Large diffs are not rendered by default.

12,629 changes: 12,629 additions & 0 deletions contracts/SubnetActorManagerFacet.json

Large diffs are not rendered by default.

1,073 changes: 544 additions & 529 deletions contracts/SubnetRegistry.json

Large diffs are not rendered by default.

138 changes: 137 additions & 1 deletion src/checkpoint/bottomup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ use crate::checkpoint::{CheckpointManager, CheckpointMetadata, CheckpointQuery};
use crate::config::Subnet;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use base64::Engine;
use fvm_shared::address::Address;
use fvm_shared::clock::ChainEpoch;
use ipc_gateway::checkpoint::BatchCrossMsgs;
use ipc_sdk::subnet_id::SubnetID;
use num_traits::ToPrimitive;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use std::fmt::{Display, Formatter};

/// Native bottom up checkpoint struct independent of chain specific implementations.
Expand Down Expand Up @@ -70,7 +74,10 @@ impl<P: BottomUpHandler, C: BottomUpHandler> BottomUpManager<P, C> {
parent_handler: P,
child_handler: C,
) -> Result<Self> {
let period = parent_handler.checkpoint_period(&child.id).await?;
let period = parent_handler
.checkpoint_period(&child.id)
.await
.map_err(|e| anyhow!("cannot get bottom up checkpoint period: {e}"))?;
Ok(Self {
metadata: CheckpointMetadata {
parent,
Expand Down Expand Up @@ -177,3 +184,132 @@ impl<P: BottomUpHandler, C: BottomUpHandler> CheckpointManager for BottomUpManag
Ok(true)
}
}

// Serialization related

/// A helper struct to serialize struct to json.
///
/// Most of the types should have no need to use this struct. But some types that are shared between
/// actor, which are using cbor tuple serialization and json rpc response. We are using this wrapper
/// to handle convert to json instead.
#[derive(Debug)]
struct SerializeToJson<T>(pub T);

impl Serialize for NativeBottomUpCheckpoint {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let encoding_optional_bytes = |optional: &Option<Vec<u8>>| {
if let Some(p) = optional {
base64::engine::general_purpose::STANDARD.encode(p)
} else {
String::from("")
}
};

let NativeBottomUpCheckpoint {
source,
proof,
epoch,
prev_check,
children,
cross_msgs,
sig,
} = self;

let source = source.to_string();

let proof = encoding_optional_bytes(proof);
let prev_check = encoding_optional_bytes(prev_check);

let children = children
.iter()
.map(|c| {
let source = c.source.to_string();
let checks = c
.checks
.iter()
.map(|bytes| base64::engine::general_purpose::STANDARD.encode(bytes))
.collect::<Vec<_>>();
serde_json::json!({
"source": source,
"checks": checks,
})
})
.collect::<Vec<_>>();

let cross_msgs = SerializeToJson(cross_msgs);

let mut state = serializer.serialize_struct("CheckData", 7)?;
state.serialize_field("source", &source)?;
state.serialize_field("proof", &proof)?;
state.serialize_field("epoch", epoch)?;
state.serialize_field("prev_check", &prev_check)?;
state.serialize_field("children", &children)?;
state.serialize_field("cross_msgs", &cross_msgs)?;
state.serialize_field(
"sig",
&base64::engine::general_purpose::STANDARD.encode(sig),
)?;

state.end()
}
}

impl<'a> Serialize for SerializeToJson<&'a BatchCrossMsgs> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let BatchCrossMsgs { cross_msgs, fee } = self.0;

let mut state = serializer.serialize_struct("BatchCrossMsgs", 2)?;
state.serialize_field("fee", fee)?;

if let Some(cross_msgs) = cross_msgs {
let vs = cross_msgs.iter().map(|c| {
serde_json::json!({
"from": c.msg.from.to_string().unwrap(), // safe to unwrap
"to": c.msg.to.to_string().unwrap(), // safe to unwrap
"method": c.msg.method,
"params": base64::engine::general_purpose::STANDARD.encode(c.msg.params.bytes()),
"value": c.msg.value.atto().to_u64().unwrap_or_default(),
"nonce": c.msg.nonce,
})
})
.collect::<Vec<_>>();
state.serialize_field("cross_msgs", &vs)?;
} else {
state.serialize_field::<Vec<serde_json::Value>>("cross_msgs", &vec![])?;
};

state.end()
}
}

#[cfg(test)]
mod tests {
use crate::checkpoint::{NativeBottomUpCheckpoint, NativeChildCheck};
use ipc_gateway::checkpoint::BatchCrossMsgs;
use ipc_sdk::subnet_id::SubnetID;

#[test]
fn test_serialization() {
let root = SubnetID::new_root(123);
let cp = NativeBottomUpCheckpoint {
source: root.clone(),
proof: Some(vec![1, 2, 3]),
epoch: 100,
prev_check: Some(vec![2, 3, 4]),
children: vec![NativeChildCheck {
source: root,
checks: vec![vec![1, 2, 3]],
}],
cross_msgs: BatchCrossMsgs::default(),
sig: vec![1, 2, 3],
};
let v = serde_json::to_string(&cp);
assert!(v.is_ok());
}
}
1 change: 1 addition & 0 deletions src/checkpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ async fn submit_till_current_epoch(manager: &dyn CheckpointManager) -> Result<()
}

let period = manager.checkpoint_period();
log::debug!("checkpoint period: {period} for manager: {manager}");

let last_executed_epoch = manager
.last_executed_epoch()
Expand Down
7 changes: 5 additions & 2 deletions src/checkpoint/topdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: MIT
use crate::checkpoint::{CheckpointManager, CheckpointMetadata, CheckpointQuery};
use crate::config::Subnet;
use anyhow::Result;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use fvm_shared::address::Address;
use fvm_shared::clock::ChainEpoch;
Expand Down Expand Up @@ -45,7 +45,10 @@ impl<P: TopDownHandler, C: TopDownHandler> TopDownManager<P, C> {
parent_handler: P,
child_handler: C,
) -> Result<Self> {
let period = child_handler.checkpoint_period(&child.id).await?;
let period = child_handler
.checkpoint_period(&child.id)
.await
.map_err(|e| anyhow!("cannot get bottom up checkpoint period: {e}"))?;
Ok(Self {
metadata: CheckpointMetadata {
parent,
Expand Down
1 change: 0 additions & 1 deletion src/cli/commands/checkpoint/list_checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ impl CommandLineHandler for ListBottomUpCheckpoints {
.await?;

for c in checkpoints.iter() {
let c = &c["data"];
log::info!(
"epoch {} - prev_check={}, cross_msgs={}, child_checks={}",
c["epoch"],
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ pub mod jsonrpc;
pub mod lotus;
pub mod manager;
pub mod sdk;
mod serialization;
pub mod server;
Loading

0 comments on commit 8122281

Please sign in to comment.