Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: power migration for nv24 #4857

Merged
merged 14 commits into from
Oct 9, 2024
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
- [#4865](https://github.com/ChainSafe/forest/issues/4865) Add support for the
`Filecoin.F3IsRunning` RPC method.

- [#4857](https://github.com/ChainSafe/forest/pull/4857) Add support for nv24
(TukTuk).

### Changed

- [#4786](https://github.com/ChainSafe/forest/issues/4786) ubuntu image is
Expand Down
16 changes: 15 additions & 1 deletion src/networks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tracing::warn;
use crate::beacon::{BeaconPoint, BeaconSchedule, DrandBeacon, DrandConfig};
use crate::db::SettingsStore;
use crate::eth::EthChainId;
use crate::shim::clock::{ChainEpoch, EPOCH_DURATION_SECONDS};
use crate::shim::clock::{ChainEpoch, EPOCHS_IN_DAY, EPOCH_DURATION_SECONDS};
use crate::shim::sector::{RegisteredPoStProofV3, RegisteredSealProofV3};
use crate::shim::version::NetworkVersion;
use crate::utils::misc::env::env_or_default;
Expand All @@ -42,6 +42,7 @@ pub const NEWEST_NETWORK_VERSION: NetworkVersion = NetworkVersion::V17;

const ENV_FOREST_BLOCK_DELAY_SECS: &str = "FOREST_BLOCK_DELAY_SECS";
const ENV_FOREST_PROPAGATION_DELAY_SECS: &str = "FOREST_PROPAGATION_DELAY_SECS";
const ENV_PLEDGE_RULE_RAMP: &str = "PLEDGE_RULE_RAMP";
LesnyRumcajs marked this conversation as resolved.
Show resolved Hide resolved

/// Forest builtin `filecoin` network chains. In general only `mainnet` and its
/// chain information should be considered stable.
Expand Down Expand Up @@ -225,6 +226,8 @@ pub struct ChainConfig {
pub policy: Policy,
pub eth_chain_id: EthChainId,
pub breeze_gas_tamping_duration: i64,
// FIP0081 gradually comes into effect over this many epochs.
pub fip0081_ramp_duration_epochs: u64,
}

impl ChainConfig {
Expand All @@ -244,6 +247,8 @@ impl ChainConfig {
policy: make_mainnet_policy!(v13),
eth_chain_id: ETH_CHAIN_ID,
breeze_gas_tamping_duration: BREEZE_GAS_TAMPING_DURATION,
// 1 year on mainnet
fip0081_ramp_duration_epochs: 365 * EPOCHS_IN_DAY as u64,
}
}

Expand All @@ -263,6 +268,8 @@ impl ChainConfig {
policy: make_calibnet_policy!(v13),
eth_chain_id: ETH_CHAIN_ID,
breeze_gas_tamping_duration: BREEZE_GAS_TAMPING_DURATION,
// 3 days on calibnet
fip0081_ramp_duration_epochs: 3 * EPOCHS_IN_DAY as u64,
}
}

Expand All @@ -279,6 +286,8 @@ impl ChainConfig {
policy: make_devnet_policy!(v13),
eth_chain_id: ETH_CHAIN_ID,
breeze_gas_tamping_duration: BREEZE_GAS_TAMPING_DURATION,
// Devnet ramp is 200 epochs in Lotus (subject to change).
fip0081_ramp_duration_epochs: env_or_default(ENV_PLEDGE_RULE_RAMP, 200),
}
}

Expand All @@ -299,6 +308,11 @@ impl ChainConfig {
policy: make_butterfly_policy!(v13),
eth_chain_id: ETH_CHAIN_ID,
breeze_gas_tamping_duration: BREEZE_GAS_TAMPING_DURATION,
// Butterflynet ramp is current set to 365 days in Lotus but this may change.
fip0081_ramp_duration_epochs: env_or_default(
ENV_PLEDGE_RULE_RAMP,
365 * EPOCHS_IN_DAY as u64,
),
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/rpc/methods/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::shim::actors::verifreg::VerifiedRegistryStateExt as _;
use crate::shim::actors::{
market::BalanceTableExt as _,
miner::{MinerStateExt as _, PartitionExt as _},
power::PowerStateExt as _,
};
use crate::shim::address::Payload;
use crate::shim::message::Message;
Expand Down Expand Up @@ -901,8 +902,8 @@ impl RpcMethod<3> for StateMinerInitialPledgeCollateral {
pledge_collateral,
power_smoothed,
&circ_supply.fil_circulating.into(),
0,
0,
power_state.ramp_start_epoch(),
power_state.ramp_duration_epochs(),
)?
.into();

Expand Down
1 change: 1 addition & 0 deletions src/shim/actors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod eam;
pub mod market;
pub mod miner;
pub mod multisig;
pub mod power;
pub mod verifreg;

pub use common::*;
Expand Down
29 changes: 29 additions & 0 deletions src/shim/actors/power.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use crate::shim::clock::ChainEpoch;
use fil_actor_interface::power::State;

pub trait PowerStateExt {
/// `FIP0081` activation epoch. Should be same as `TukTuk` epoch.
fn ramp_start_epoch(&self) -> ChainEpoch;
/// `FIP0081` activation ramp. One year on mainnet, 3 days on calibnet,
/// defaults to 200 epochs on devnet. Only applicable to `v15` (aka `TukTuk`)
/// actors.
fn ramp_duration_epochs(&self) -> u64;
}

impl PowerStateExt for State {
fn ramp_start_epoch(&self) -> ChainEpoch {
match self {
State::V15(st) => st.ramp_start_epoch,
_ => 0,
}
}

fn ramp_duration_epochs(&self) -> u64 {
match self {
State::V15(st) => st.ramp_duration_epochs,
_ => 0,
}
}
}
20 changes: 17 additions & 3 deletions src/state_migration/nv24/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::networks::{ChainConfig, Height};
use crate::shim::{
address::Address,
clock::ChainEpoch,
machine::BuiltinActorManifest,
machine::{BuiltinActor, BuiltinActorManifest},
state_tree::{StateTree, StateTreeVersion},
};
use crate::utils::db::CborStoreExt as _;
Expand All @@ -18,7 +18,7 @@ use cid::Cid;

use fvm_ipld_blockstore::Blockstore;

use super::{system, verifier::Verifier, SystemStateOld};
use super::{power, system, verifier::Verifier, SystemStateOld};
use crate::state_migration::common::{migrators::nil_migrator, StateMigration};

impl<BS: Blockstore> StateMigration<BS> {
Expand All @@ -27,7 +27,7 @@ impl<BS: Blockstore> StateMigration<BS> {
store: &Arc<BS>,
state: &Cid,
new_manifest: &BuiltinActorManifest,
_chain_config: &ChainConfig,
chain_config: &ChainConfig,
) -> anyhow::Result<()> {
let state_tree = StateTree::new_from_root(store.clone(), state)?;
let system_actor = state_tree.get_required_actor(&Address::SYSTEM_ACTOR)?;
Expand All @@ -48,6 +48,20 @@ impl<BS: Blockstore> StateMigration<BS> {
system::system_migrator(new_manifest),
);

let tuktuk_epoch = chain_config
.height_infos
.get(&Height::TukTuk)
.context("no height info for network version NV24")?
.epoch;
self.add_migrator(
current_manifest.get(BuiltinActor::Power)?,
power::power_migrator(
new_manifest.get(BuiltinActor::Power)?,
tuktuk_epoch,
chain_config.fip0081_ramp_duration_epochs,
),
);

Ok(())
}
}
Expand Down
1 change: 1 addition & 0 deletions src/state_migration/nv24/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

//! This module contains the migration logic for the `NV24` upgrade.
mod migration;
mod power;

/// Run migration for `NV24`. This should be the only exported method in this
/// module.
Expand Down
74 changes: 74 additions & 0 deletions src/state_migration/nv24/power.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

//! This module contains the migration logic for the `NV24` upgrade for the
//! Power actor.

use crate::shim::clock::ChainEpoch;
use crate::state_migration::common::{ActorMigration, ActorMigrationInput, ActorMigrationOutput};
use crate::utils::db::CborStoreExt as _;
use cid::Cid;
use fil_actor_power_state::{v14::State as StateV14, v15::State as StateV15};
use fil_actors_shared::v15::builtin::reward::smooth::FilterEstimate as FilterEstimateV15;
use fvm_ipld_blockstore::Blockstore;
use std::sync::Arc;

pub struct PowerMigrator {
new_code_cid: Cid,
tuktuk_epoch: ChainEpoch,
ramp_duration_epochs: u64,
}

pub(in crate::state_migration) fn power_migrator<BS: Blockstore>(
cid: Cid,
tuktuk_epoch: ChainEpoch,
ramp_duration_epochs: u64,
) -> Arc<dyn ActorMigration<BS> + Send + Sync> {
Arc::new(PowerMigrator {
new_code_cid: cid,
tuktuk_epoch,
ramp_duration_epochs,
})
}

// The v15 actor is identical to v14, except for the addition of the `ramp_start_epoch`
// and `ramp_duration_epochs` fields.
impl<BS: Blockstore> ActorMigration<BS> for PowerMigrator {
fn migrate_state(
&self,
store: &BS,
input: ActorMigrationInput,
) -> anyhow::Result<Option<ActorMigrationOutput>> {
let in_state: StateV14 = store.get_cbor_required(&input.head)?;

let out_state = StateV15 {
total_raw_byte_power: in_state.total_raw_byte_power,
total_bytes_committed: in_state.total_bytes_committed,
total_quality_adj_power: in_state.total_quality_adj_power,
total_qa_bytes_committed: in_state.total_qa_bytes_committed,
total_pledge_collateral: in_state.total_pledge_collateral,
this_epoch_raw_byte_power: in_state.this_epoch_raw_byte_power,
this_epoch_quality_adj_power: in_state.this_epoch_quality_adj_power,
this_epoch_pledge_collateral: in_state.this_epoch_pledge_collateral,
this_epoch_qa_power_smoothed: FilterEstimateV15 {
position: in_state.this_epoch_qa_power_smoothed.position,
velocity: in_state.this_epoch_qa_power_smoothed.velocity,
},
miner_count: in_state.miner_count,
miner_above_min_power_count: in_state.miner_above_min_power_count,
ramp_start_epoch: self.tuktuk_epoch,
ramp_duration_epochs: self.ramp_duration_epochs,
cron_event_queue: in_state.cron_event_queue,
first_cron_epoch: in_state.first_cron_epoch,
claims: in_state.claims,
proof_validation_batch: in_state.proof_validation_batch,
};

let new_head = store.put_cbor_default(&out_state)?;

Ok(Some(ActorMigrationOutput {
new_code_cid: self.new_code_cid,
new_head,
}))
}
}
Loading