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

add cron fields #64

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
target: wasm32-unknown-unknown
toolchain: nightly
override: true
- run: cargo b --all --release
- run: cargo t --all --release
- run: cargo b --all
- run: cargo t --all

fmt:
name: Rustfmt
Expand All @@ -38,7 +38,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
args: --all --check

clippy:
name: Clippy
Expand All @@ -55,4 +55,4 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
args: --all -- -D clippy::all
17 changes: 8 additions & 9 deletions atomic-exec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Actor {
}

let msgs = rt.transaction(|st: &mut State, rt| {
st.modify_atomic_exec(rt.store(), &exec_id, &actors, |entry| {
st.modify_atomic_exec(rt.store(), exec_id, actors, |entry| {
// Record the pre-commitment
entry.insert(from.to_string().unwrap(), params.commit);

Expand Down Expand Up @@ -135,13 +135,12 @@ impl Actor {

// Remove the atomic execution entry
rt.transaction(|st: &mut State, rt| {
st.rm_atomic_exec(rt.store(), &exec_id, &actors)
.map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
"failed to remove atomic exec from registry",
)
})
st.rm_atomic_exec(rt.store(), exec_id, actors).map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
"failed to remove atomic exec from registry",
)
})
})?;

Ok(true)
Expand Down Expand Up @@ -180,7 +179,7 @@ impl Actor {
}

let msg = rt.transaction(|st: &mut State, rt| {
st.modify_atomic_exec(rt.store(), &exec_id, &actors, |entry| {
st.modify_atomic_exec(rt.store(), exec_id, actors, |entry| {
// Remove the pre-commitment
entry.remove_entry(&from.to_string().unwrap());

Expand Down
4 changes: 2 additions & 2 deletions gateway/src/cross.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl CrossMsgs {
}

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

/// Appends a cross-message to cross-msgs
Expand Down Expand Up @@ -211,7 +211,7 @@ pub(crate) fn distribute_crossmsg_fee(
fee: TokenAmount,
) -> Result<(), ActorError> {
if !fee.is_zero() {
rt.send(&subnet_actor, SUBNET_ACTOR_REWARD_METHOD, None, fee)?;
rt.send(subnet_actor, SUBNET_ACTOR_REWARD_METHOD, None, fee)?;
}
Ok(())
}
Expand Down
41 changes: 19 additions & 22 deletions gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Actor {
fn constructor(rt: &mut impl Runtime, params: ConstructorParams) -> Result<(), ActorError> {
rt.validate_immediate_caller_is(std::iter::once(&INIT_ACTOR_ADDR))?;

let st = State::new(rt.store(), params).map_err(|e| {
let st = State::new(rt.store(), params, rt.curr_epoch()).map_err(|e| {
cryptoAtwill marked this conversation as resolved.
Show resolved Hide resolved
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
"Failed to create SCA actor state",
Expand Down Expand Up @@ -328,30 +328,27 @@ impl Actor {

// commit cross-message in checkpoint to either execute them or
// queue them for propagation if there are cross-msgs availble.
match commit.cross_msgs() {
Some(cross_msg) => {
// if tcid not default it means cross-msgs are being propagated.
if cross_msg.msgs_cid != TCid::default() {
st.store_bottomup_msg(rt.store(), cross_msg).map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
"error storing bottom_up messages from checkpoint",
)
})?;
}

// release circulating supply
sub.release_supply(&cross_msg.value).map_err(|e| {
if let Some(cross_msg) = commit.cross_msgs() {
// if tcid not default it means cross-msgs are being propagated.
if cross_msg.msgs_cid != TCid::default() {
st.store_bottomup_msg(rt.store(), cross_msg).map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
"error releasing circulating supply",
"error storing bottom_up messages from checkpoint",
)
})?;

// distribute fee
fee = cross_msg.fee.clone();
}
None => {}

// release circulating supply
sub.release_supply(&cross_msg.value).map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
"error releasing circulating supply",
)
})?;

// distribute fee
fee = cross_msg.fee.clone();
}

// append new checkpoint to the list of childs
Expand Down Expand Up @@ -466,7 +463,7 @@ impl Actor {
rt.transaction(|st: &mut State, rt| {
let fee = &CROSS_MSG_FEE;
// collect fees
st.collect_cross_fee(&mut value, &fee)?;
st.collect_cross_fee(&mut value, fee)?;

// Create release message
let r_msg = CrossMsg {
Expand All @@ -486,7 +483,7 @@ impl Actor {
};

// Commit bottom-up message.
st.commit_bottomup_msg(rt.store(), &r_msg, &fee, rt.curr_epoch())
st.commit_bottomup_msg(rt.store(), &r_msg, fee, rt.curr_epoch())
.map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
Expand Down
12 changes: 11 additions & 1 deletion gateway/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,22 @@ pub struct State {
pub bottomup_msg_meta: TCid<TAmt<CrossMsgMeta, CROSSMSG_AMT_BITWIDTH>>,
pub applied_bottomup_nonce: u64,
pub applied_topdown_nonce: u64,
/// The epoch that this actor is deployed
cryptoAtwill marked this conversation as resolved.
Show resolved Hide resolved
pub genesis_epoch: ChainEpoch,
/// How often cron checkpoints will be submitted by validator in the child subnet
pub cron_period: ChainEpoch,
}

lazy_static! {
static ref MIN_SUBNET_COLLATERAL: TokenAmount = TokenAmount::from_atto(MIN_COLLATERAL_AMOUNT);
}

impl State {
pub fn new<BS: Blockstore>(store: &BS, params: ConstructorParams) -> anyhow::Result<State> {
pub fn new<BS: Blockstore>(
store: &BS,
params: ConstructorParams,
current_epoch: ChainEpoch,
) -> anyhow::Result<State> {
Ok(State {
network_name: SubnetID::from_str(&params.network_name)?,
total_subnets: Default::default(),
Expand All @@ -73,6 +81,8 @@ impl State {
// We first increase to the subsequent and then execute for bottom-up messages
applied_bottomup_nonce: MAX_NONCE,
applied_topdown_nonce: Default::default(),
genesis_epoch: current_epoch,
cryptoAtwill marked this conversation as resolved.
Show resolved Hide resolved
cron_period: params.cron_period,
})
}

Expand Down
12 changes: 12 additions & 0 deletions gateway/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use fvm_shared::address::Address;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::econ::TokenAmount;
use ipc_sdk::subnet_id::SubnetID;
use ipc_sdk::ValidatorSet;
use multihash::MultihashDigest;
use primitives::CodeType;

use crate::checkpoint::{Checkpoint, CrossMsgMeta};
use crate::cross::CrossMsg;
use crate::StorableMsg;

/// ID used in the builtin-actors bundle manifest
pub const MANIFEST_ID: &str = "ipc_gateway";
Expand All @@ -30,6 +32,7 @@ pub type CrossMsgArray<'bs, BS> = Array<'bs, CrossMsg, BS>;
pub struct ConstructorParams {
pub network_name: String,
pub checkpoint_period: ChainEpoch,
pub cron_period: ChainEpoch,
}

#[derive(Serialize_tuple, Deserialize_tuple, Clone)]
Expand Down Expand Up @@ -99,6 +102,13 @@ impl PostBoxItem {
}
}

#[derive(Clone, Debug, Serialize_tuple, Deserialize_tuple, PartialEq, Eq)]
cryptoAtwill marked this conversation as resolved.
Show resolved Hide resolved
pub struct CronCheckpoint {
pub epoch: ChainEpoch,
pub membership: ValidatorSet,
pub top_down_msgs: Vec<StorableMsg>,
}

#[cfg(test)]
mod tests {
use crate::ConstructorParams;
Expand All @@ -109,6 +119,7 @@ mod tests {
let p = ConstructorParams {
network_name: "/root".to_string(),
checkpoint_period: 100,
cron_period: 20,
};
let bytes = fil_actors_runtime::util::cbor::serialize(&p, "").unwrap();
let serialized = base64::encode(bytes.bytes());
Expand All @@ -120,5 +131,6 @@ mod tests {

assert_eq!(p.network_name, deserialized.network_name);
assert_eq!(p.checkpoint_period, deserialized.checkpoint_period);
assert_eq!(p.cron_period, deserialized.cron_period);
}
}
9 changes: 9 additions & 0 deletions gateway/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use fvm_ipld_encoding::RawBytes;
use fvm_shared::address::Address;
use fvm_shared::bigint::bigint_ser::BigIntDe;
use fvm_shared::bigint::Zero;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::MethodNum;
Expand All @@ -46,6 +47,7 @@ lazy_static! {
Address::new_bls(&[1; fvm_shared::address::BLS_PUB_LEN]).unwrap();
pub static ref ACTOR: Address = Address::new_actor("actor".as_bytes());
pub static ref SIG_TYPES: Vec<Cid> = vec![*ACCOUNT_ACTOR_CODE_ID, *MULTISIG_ACTOR_CODE_ID];
pub static ref DEFAULT_CRON_PERIOD: ChainEpoch = 20;
}

pub fn new_runtime() -> MockRuntime {
Expand Down Expand Up @@ -83,6 +85,7 @@ impl Harness {
let params = ConstructorParams {
network_name: self.net_name.to_string(),
checkpoint_period: 10,
cron_period: *DEFAULT_CRON_PERIOD,
};
rt.set_caller(*INIT_ACTOR_CODE_ID, INIT_ACTOR_ADDR);
rt.call::<Actor>(
Expand All @@ -93,7 +96,11 @@ impl Harness {
}

pub fn construct_and_verify(&self, rt: &mut MockRuntime) {
let chain_epoch = 10;
rt.set_epoch(chain_epoch);

self.construct(rt);

let st: State = rt.get_state();
let store = &rt.store;

Expand All @@ -106,6 +113,8 @@ impl Harness {
assert_eq!(st.check_period, DEFAULT_CHECKPOINT_PERIOD);
assert_eq!(st.applied_bottomup_nonce, MAX_NONCE);
assert_eq!(st.bottomup_msg_meta.cid(), empty_bottomup_array);
assert_eq!(st.genesis_epoch, chain_epoch);
assert_eq!(st.cron_period, *DEFAULT_CRON_PERIOD);
verify_empty_map(rt, st.subnets.cid());
verify_empty_map(rt, st.checkpoints.cid());
verify_empty_map(rt, st.check_msg_registry.cid());
Expand Down
59 changes: 59 additions & 0 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use fvm_ipld_encoding::tuple::{Deserialize_tuple, Serialize_tuple};
use fvm_shared::address::Address;
use fvm_shared::econ::TokenAmount;

pub mod address;
pub mod error;
pub mod subnet_id;
Expand All @@ -6,3 +10,58 @@ pub mod account {
/// Public key account actor method.
pub const PUBKEY_ADDRESS_METHOD: u64 = 2;
}

#[derive(Clone, Debug, Serialize_tuple, Deserialize_tuple, PartialEq, Eq)]
pub struct Validator {
pub addr: Address,
pub net_addr: String,
// voting power for the validator determined by its stake in the
// network.
pub weight: TokenAmount,
}

#[derive(Clone, Default, Debug, Serialize_tuple, Deserialize_tuple, PartialEq, Eq)]
pub struct ValidatorSet {
validators: Vec<Validator>,
// sequence number that uniquely identifies a validator set
configuration_number: u64,
}

impl ValidatorSet {
pub fn validators(&self) -> &Vec<Validator> {
&self.validators
}

pub fn validators_mut(&mut self) -> &mut Vec<Validator> {
&mut self.validators
}

pub fn config_number(&self) -> u64 {
self.configuration_number
}

/// Push a new validator to the validator set.
pub fn push(&mut self, val: Validator) {
self.validators.push(val);
// update the config_number with every update
// we allow config_number to overflow if that scenario ever comes.
self.configuration_number += 1;
}

/// Remove a validator from validator set by address
pub fn rm(&mut self, val: &Address) {
self.validators.retain(|x| x.addr != *val);
// update the config_number with every update
// we allow config_number to overflow if that scenario ever comes.
self.configuration_number += 1;
}

pub fn update_weight(&mut self, val: &Address, weight: &TokenAmount) {
self.validators_mut()
.iter_mut()
.filter(|x| x.addr == *val)
.for_each(|x| x.weight = weight.clone());

self.configuration_number += 1;
}
}
4 changes: 2 additions & 2 deletions subnet-actor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl SubnetActor for Actor {
// complex and fair policies to incentivize certain behaviors.
// we may even have a default one for IPC.
let div = {
if st.validator_set.validators().len() == 0 {
if st.validator_set.validators().is_empty() {
return Err(actor_error!(illegal_state, "no validators in subnet"));
};
match BigInt::from_usize(st.validator_set.validators().len()) {
Expand All @@ -351,7 +351,7 @@ impl SubnetActor for Actor {
}
};
let rew_amount = amount.div_floor(div);
for v in st.validator_set.validators().into_iter() {
for v in st.validator_set.validators().iter() {
rt.send(&v.addr, METHOD_SEND, None, rew_amount.clone())?;
}
Ok(None)
Expand Down
5 changes: 3 additions & 2 deletions subnet-actor/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use fvm_shared::bigint::Zero;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::econ::TokenAmount;
use ipc_gateway::{Checkpoint, SubnetID, DEFAULT_CHECKPOINT_PERIOD, MIN_COLLATERAL_AMOUNT};
use ipc_sdk::{Validator, ValidatorSet};
use lazy_static::lazy_static;
use num::rational::Ratio;
use num::BigInt;
Expand Down Expand Up @@ -78,7 +79,7 @@ impl State {
checkpoints: TCid::new_hamt(store)?,
stake: TCid::new_hamt(store)?,
window_checks: TCid::new_hamt(store)?,
validator_set: ValidatorSet::new(),
validator_set: ValidatorSet::default(),
};

Ok(state)
Expand Down Expand Up @@ -391,7 +392,7 @@ impl Default for State {
checkpoints: TCid::default(),
stake: TCid::default(),
window_checks: TCid::default(),
validator_set: ValidatorSet::new(),
validator_set: ValidatorSet::default(),
min_validators: 0,
}
}
Expand Down
Loading