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

Refactor SubnetID and add ChainID to root #109

Merged
merged 8 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion atomic-exec/examples/fungible-token/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ use fvm_shared::{address::Address, econ::TokenAmount, error::ExitCode, MethodNum
use ipc_atomic_execution::AtomicExecID;
use ipc_atomic_execution_primitives::{AtomicExecRegistry, AtomicInputID};
use ipc_gateway::{CrossMsg, IPCAddress, StorableMsg, SubnetID};
use ipc_sdk::subnet_id::ROOTNET_ID;
use num_traits::Zero;
use std::collections::HashMap;

lazy_static::lazy_static! {
static ref ROOTNET_ID: SubnetID = SubnetID::new(123, vec![]);
}

#[test]
fn test_constructor() {
let runtime = &mut construct_runtime();
Expand Down
5 changes: 4 additions & 1 deletion atomic-exec/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ mod tests {
use super::*;
use fvm_ipld_blockstore::MemoryBlockstore;
use ipc_gateway::{IPCAddress, SubnetID};
use ipc_sdk::subnet_id::ROOTNET_ID;

lazy_static::lazy_static! {
static ref ROOTNET_ID: SubnetID = SubnetID::new(123, vec![]);
}

#[test]
fn state_works() {
Expand Down
5 changes: 2 additions & 3 deletions atomic-exec/tests/actor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use fvm_ipld_encoding::{ipld_block::IpldBlock, RawBytes};
use fvm_shared::{address::Address, econ::TokenAmount, error::ExitCode, MethodNum};
use ipc_atomic_execution::{Actor, AtomicExecID, ConstructorParams, Method, PreCommitParams};
use ipc_gateway::{ApplyMsgParams, CrossMsg, IPCAddress, StorableMsg, SubnetID};
use ipc_sdk::subnet_id::ROOTNET_ID;

#[test]
fn test_pre_commit_wrong_origin() {
Expand Down Expand Up @@ -81,12 +80,12 @@ lazy_static::lazy_static! {
ipc_gateway_address: *IPC_GATEWAY_ADDR,
};

static ref COORD_ACTOR: IPCAddress = IPCAddress::new(&ROOTNET_ID, &Address::new_id(1)).unwrap();
pub static ref ROOTNET_ID: SubnetID = SubnetID::new(123, vec![]);

static ref COORD_ACTOR: IPCAddress = IPCAddress::new(&ROOTNET_ID, &Address::new_id(1)).unwrap();
static ref SUBNET_A: SubnetID = SubnetID::new_from_parent(&ROOTNET_ID, Address::new_id('A' as u64));
static ref ACTOR_A1: IPCAddress = IPCAddress::new(&SUBNET_A, &Address::new_id(1)).unwrap();
static ref ACTOR_A2: IPCAddress = IPCAddress::new(&SUBNET_A, &Address::new_id(2)).unwrap();

static ref SUBNET_B: SubnetID = SubnetID::new_from_parent(&ROOTNET_ID, Address::new_id('B' as u64));
static ref ACTOR_B1: IPCAddress = IPCAddress::new(&SUBNET_B, &Address::new_id(1)).unwrap();
static ref ACTOR_B2: IPCAddress = IPCAddress::new(&SUBNET_B, &Address::new_id(2)).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion gateway/src/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ mod tests {

#[test]
fn test_serialization() {
let mut checkpoint = BottomUpCheckpoint::new(SubnetID::from_str("/root").unwrap(), 10);
let mut checkpoint = BottomUpCheckpoint::new(SubnetID::from_str("/r123").unwrap(), 10);
checkpoint.data.prev_check = TCid::from(
Cid::from_str("bafy2bzacecnamqgqmifpluoeldx7zzglxcljo6oja4vrmtj7432rphldpdmm2")
.unwrap(),
Expand Down
18 changes: 9 additions & 9 deletions gateway/src/cross.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use fvm_shared::METHOD_SEND;
use ipc_sdk::address::IPCAddress;
use ipc_sdk::subnet_id::SubnetID;
use serde_tuple::{Deserialize_tuple, Serialize_tuple};
use std::path::Path;

/// StorableMsg stores all the relevant information required
/// to execute cross-messages.
Expand Down Expand Up @@ -150,8 +149,8 @@ pub fn is_bottomup(from: &SubnetID, to: &SubnetID) -> bool {
Some((ind, _)) => ind,
None => return false,
};
let a = from.to_string();
Path::new(&a).components().count() - 1 > index
// more children than the common parent
from.children_as_ref().len() > index
}

#[derive(PartialEq, Eq, Clone, Debug, Default, Serialize_tuple, Deserialize_tuple)]
Expand Down Expand Up @@ -224,13 +223,14 @@ mod tests {

#[test]
fn test_is_bottomup() {
bottom_up("/root/t01", "/root/t01/t02", false);
bottom_up("/root/t01", "/root", true);
bottom_up("/root/t01", "/root/t01/t02", false);
bottom_up("/root/t01", "/root/t02/t02", true);
bottom_up("/root/t01/t02", "/root/t01/t02", false);
bottom_up("/root/t01/t02", "/root/t01/t02/t03", false);
bottom_up("/r123/f01", "/r123/f01/f02", false);
bottom_up("/r123/f01", "/r123", true);
bottom_up("/r123/f01", "/r123/f01/f02", false);
bottom_up("/r123/f01", "/r123/f02/f02", true);
bottom_up("/r123/f01/f02", "/r123/f01/f02", false);
bottom_up("/r123/f01/f02", "/r123/f01/f02/f03", false);
}

fn bottom_up(a: &str, b: &str, res: bool) {
assert_eq!(
is_bottomup(
Expand Down
5 changes: 2 additions & 3 deletions gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use fvm_shared::METHOD_SEND;
use fvm_shared::{MethodNum, METHOD_CONSTRUCTOR};
pub use ipc_sdk::address::IPCAddress;
pub use ipc_sdk::subnet_id::SubnetID;
use ipc_sdk::subnet_id::ROOTNET_ID;
use ipc_sdk::ValidatorSet;
use lazy_static::lazy_static;
use num_derive::FromPrimitive;
Expand Down Expand Up @@ -84,7 +83,7 @@ impl Actor {
)
})?;
// the root doesn't need to be explicitly initialized
if st.network_name == *ROOTNET_ID {
if st.network_name.is_root() {
st.init_gateway(rt.store(), 0)?;
}
rt.create(&st)?;
Expand Down Expand Up @@ -700,7 +699,7 @@ impl Actor {
// TODO: Once account abstraction is conveniently supported, there will be
// no need for this initial funding of validators.

if rt.curr_epoch() == 1 && network_name != *ROOTNET_ID {
if rt.curr_epoch() == 1 && !network_name.is_root() {
for v in validator_set.validators().iter() {
rt.send(&v.addr, METHOD_SEND, None, INITIAL_VALIDATOR_FUNDS.clone())?;
}
Expand Down
14 changes: 7 additions & 7 deletions gateway/tests/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ipc_gateway::{
get_topdown_msg, BottomUpCheckpoint, CrossMsg, IPCAddress, PostBoxItem, State, StorableMsg,
TopDownCheckpoint, CROSS_MSG_FEE, INITIAL_VALIDATOR_FUNDS, SUBNET_ACTOR_REWARD_METHOD,
};
use ipc_sdk::subnet_id::{SubnetID, ROOTNET_ID};
use ipc_sdk::subnet_id::SubnetID;
use ipc_sdk::{epoch_key, Validator, ValidatorSet};
use primitives::TCid;
use std::collections::BTreeSet;
Expand Down Expand Up @@ -519,7 +519,7 @@ fn test_send_cross() {
.unwrap();

// top-down
let sub = SubnetID::from_str("/root/t0101/t0101").unwrap();
let sub = SubnetID::from_str("/r123/f0101/f0101").unwrap();
h.send_cross(
&mut rt,
&from,
Expand All @@ -532,7 +532,7 @@ fn test_send_cross() {
&value,
)
.unwrap();
let sub = SubnetID::from_str("/root/t0101/t0101").unwrap();
let sub = SubnetID::from_str("/r123/f0101/f0101").unwrap();
let circ_sup = 2 * &value;
h.send_cross(
&mut rt,
Expand All @@ -546,7 +546,7 @@ fn test_send_cross() {
&circ_sup,
)
.unwrap();
let sub = SubnetID::from_str("/root/t0101/t0101/t01002").unwrap();
let sub = SubnetID::from_str("/r123/f0101/f0101/f01002").unwrap();
let circ_sup = circ_sup.clone() + &value;
h.send_cross(
&mut rt,
Expand All @@ -563,7 +563,7 @@ fn test_send_cross() {

// bottom-up
rt.set_balance(3 * &value);
let sub = SubnetID::from_str("/root/t0102/t0101").unwrap();
let sub = SubnetID::from_str("/r123/f0102/f0101").unwrap();
let zero = TokenAmount::zero();
h.send_cross(
&mut rt,
Expand All @@ -577,7 +577,7 @@ fn test_send_cross() {
&zero,
)
.unwrap();
let sub = SubnetID::from_str("/root/t0102/t0101").unwrap();
let sub = SubnetID::from_str("/r123/f0102/f0101").unwrap();
h.send_cross(
&mut rt,
&from,
Expand All @@ -590,7 +590,7 @@ fn test_send_cross() {
&zero,
)
.unwrap();
let sub = SubnetID::from_str("/root").unwrap();
let sub = SubnetID::from_str("/r123").unwrap();
h.send_cross(
&mut rt,
&from,
Expand Down
2 changes: 1 addition & 1 deletion gateway/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use ipc_gateway::{
StorableMsg, Subnet, SubnetID, TopDownCheckpoint, CROSS_MSG_FEE, DEFAULT_CHECKPOINT_PERIOD,
MIN_COLLATERAL_AMOUNT, SUBNET_ACTOR_REWARD_METHOD,
};
use ipc_sdk::subnet_id::ROOTNET_ID;
use ipc_sdk::ValidatorSet;
use lazy_static::lazy_static;
use primitives::{TCid, TCidContent};
Expand All @@ -44,6 +43,7 @@ lazy_static! {
pub static ref SIG_TYPES: Vec<Cid> = vec![*ACCOUNT_ACTOR_CODE_ID, *MULTISIG_ACTOR_CODE_ID];
pub static ref DEFAULT_TOPDOWN_PERIOD: ChainEpoch = 20;
pub static ref DEFAULT_GENESIS_EPOCH: ChainEpoch = 1;
pub static ref ROOTNET_ID: SubnetID = SubnetID::new(123, vec![]);
}

pub fn new_runtime() -> MockRuntime {
Expand Down
1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ num-traits = "0.2.14"
fvm_ipld_blockstore = "0.1.1"
fvm_ipld_encoding = "0.3.3"
lazy_static = "1.4.0"
fnv = "1.0.7"
serde_tuple = "0.5"
serde = { version = "1.0.136", features = ["derive"] }
fvm_shared = { version = "=3.0.0-alpha.17", default-features = false }
Expand Down
11 changes: 6 additions & 5 deletions sdk/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ impl FromStr for IPCAddress {
#[cfg(test)]
mod tests {
use crate::address::IPCAddress;
use crate::subnet_id::{SubnetID, ROOTNET_ID};
use crate::subnet_id::SubnetID;
use fvm_shared::address::Address;
use std::str::FromStr;
use std::vec;

#[test]
fn test_ipc_address() {
let act = Address::new_id(1001);
let sub_id = SubnetID::new_from_parent(&ROOTNET_ID.clone(), act);
let bls = Address::from_str("t3vvmn62lofvhjd2ugzca6sof2j2ubwok6cj4xxbfzz4yuxfkgobpihhd2thlanmsh3w2ptld2gqkn2jvlss4a").unwrap();
let sub_id = SubnetID::new(123, vec![act]);
let bls = Address::from_str("f3vvmn62lofvhjd2ugzca6sof2j2ubwok6cj4xxbfzz4yuxfkgobpihhd2thlanmsh3w2ptld2gqkn2jvlss4a").unwrap();
let haddr = IPCAddress::new(&sub_id, &bls).unwrap();

let str = haddr.to_string().unwrap();
Expand All @@ -91,7 +92,7 @@ mod tests {

#[test]
fn test_ipc_from_str() {
let sub_id = SubnetID::new_from_parent(&ROOTNET_ID.clone(), Address::new_id(100));
let sub_id = SubnetID::new(123, vec![Address::new_id(100)]);
let addr = IPCAddress::new(&sub_id, &Address::new_id(101)).unwrap();
let st = addr.to_string().unwrap();
let addr_out = IPCAddress::from_str(&st).unwrap();
Expand All @@ -100,7 +101,7 @@ mod tests {

#[test]
fn test_ipc_serialization() {
let sub_id = SubnetID::new_from_parent(&ROOTNET_ID.clone(), Address::new_id(100));
let sub_id = SubnetID::new(123, vec![Address::new_id(100)]);
let addr = IPCAddress::new(&sub_id, &Address::new_id(101)).unwrap();
let st = addr.to_bytes().unwrap();
let addr_out = IPCAddress::from_bytes(&st).unwrap();
Expand Down
19 changes: 1 addition & 18 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
use fil_actors_runtime::fvm_ipld_hamt::BytesKey;
use fvm_ipld_encoding::tuple::{Deserialize_tuple, Serialize_tuple};
use fvm_shared::clock::ChainEpoch;
use fvm_shared::{address::Address, econ::TokenAmount};
use fvm_shared::{
address::{set_current_network, Network},
clock::ChainEpoch,
};
use integer_encoding::VarInt;
use num_traits::cast::FromPrimitive;

pub mod address;
pub mod error;
pub mod subnet_id;

/// Sets the type of network from an environmental variable.
/// This is key to set the right network prefixes on string
/// representation of addresses.
pub fn set_network_from_env() {
let network_raw: u8 = std::env::var("LOTUS_NETWORK")
// default to testnet
.unwrap_or_else(|_| String::from("1"))
.parse()
.unwrap();
let network = Network::from_u8(network_raw).unwrap();
set_current_network(network);
}

/// Encodes the a ChainEpoch as a varInt for its use
/// as a key of a HAMT. This serialization has been
/// tested to be compatible with its go counter-part
Expand Down
Loading