Skip to content

Commit

Permalink
Add option to disable V increasing
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Jul 2, 2024
1 parent 538f7d5 commit f51a799
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
41 changes: 31 additions & 10 deletions crates/phactory/src/system/gk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ where
GatekeeperEvent::_RepairV
| GatekeeperEvent::_PhalaLaunched
| GatekeeperEvent::_UnrespFix => unreachable!(),
GatekeeperEvent::SetStaticV { .. } => {
// Handled by ComputingEconomics
}
}
}

Expand Down Expand Up @@ -1178,14 +1181,19 @@ impl<MsgChan: MessageChannel<Signer = Sr25519Signer>> ComputingEconomics<MsgChan
GatekeeperEvent::_RepairV
| GatekeeperEvent::_PhalaLaunched
| GatekeeperEvent::_UnrespFix => unreachable!(),
GatekeeperEvent::SetStaticV { enabled } => {
if origin.is_pallet() {
self.tokenomic_params.set_static_v(enabled);
}
}
}
}

pub fn update_tokenomic_parameters(
&mut self,
params: phala_types::messaging::TokenomicParameters,
) {
self.tokenomic_params = params.into();
self.tokenomic_params.update(params);
info!(
target: "gk_computing",
"Tokenomic parameter updated: {:#?}",
Expand Down Expand Up @@ -1348,13 +1356,16 @@ mod tokenomic {
#[serde(with = "serde_fp")]
payout_ration: FixedPoint,
pub heartbeat_window: u32,
/// The V won't increase on IDLE if set to true
#[serde(default)]
static_v: bool,
}

impl From<TokenomicParameters> for Params {
fn from(params: TokenomicParameters) -> Self {
impl Params {
pub fn update(&mut self, params: TokenomicParameters) {
let treasury_ration = FixedPoint::from_bits(params.treasury_ratio);
let payout_ration = fp!(1) - treasury_ration;
Params {
*self = Params {
rho: FixedPoint::from_bits(params.rho),
slash_rate: FixedPoint::from_bits(params.slash_rate),
budget_per_block: FixedPoint::from_bits(params.budget_per_block),
Expand All @@ -1364,7 +1375,12 @@ mod tokenomic {
treasury_ration,
payout_ration,
heartbeat_window: params.heartbeat_window,
}
static_v: self.static_v,
};
}

pub fn set_static_v(&mut self, enabled: bool) {
self.static_v = enabled;
}
}

Expand All @@ -1379,6 +1395,7 @@ mod tokenomic {
treasury_ration: fp!(0.2),
payout_ration: fp!(0.8),
heartbeat_window: 10,
static_v: false,
}
}

Expand All @@ -1393,6 +1410,9 @@ mod tokenomic {

/// case1: Idle, no event
pub fn update_v_idle(&mut self, params: &Params) {
if params.static_v {
return;
}
let cost_idle = params.cost_k * self.p_bench + params.cost_b;
let perf_multiplier = if self.p_bench == fp!(0) {
fp!(1)
Expand All @@ -1418,7 +1438,7 @@ mod tokenomic {
if sum_share == fp!(0) {
return NO_UPDATE;
}
if self.v_deductible == fp!(0) {
if !params.static_v && self.v_deductible == fp!(0) {
return NO_UPDATE;
}
if block_number <= self.v_update_block {
Expand All @@ -1434,10 +1454,11 @@ mod tokenomic {
let actual_payout = budget * params.payout_ration;
let actual_treasury = budget * params.treasury_ration;

let actual_v_deduct = self.v_deductible.clamp(fp!(0), actual_payout);
self.v -= actual_v_deduct;

self.v_deductible = fp!(0);
if !params.static_v {
let actual_v_deduct = self.v_deductible.clamp(fp!(0), actual_payout);
self.v -= actual_v_deduct;
self.v_deductible = fp!(0);
}
self.v_update_at = now_ms;
self.v_update_block = block_number;

Expand Down
1 change: 1 addition & 0 deletions crates/phala-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ pub mod messaging {
/// Fix the payout duration problem in unresponsive state.
/// Dropped in Phala. The index is reserved here for Khala+pruntime-v0 compatibility.
_UnrespFix,
SetStaticV { enabled: bool },
}

impl GatekeeperEvent {
Expand Down
15 changes: 15 additions & 0 deletions pallets/phala/src/compute/computation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ pub mod pallet {
nonce: u64,
budget: u128,
},
StaticVChanged {
enabled: bool,
},
}

#[pallet::error]
Expand Down Expand Up @@ -632,6 +635,18 @@ pub mod pallet {
ContractAccount::<T>::put(account_id);
Ok(())
}

/// Enable static V.
///
/// When enabled, the V of a worker doesn't increase on IDLE.
#[pallet::call_index(9)]
#[pallet::weight(Weight::from_parts(1, 0))]
pub fn set_static_v(origin: OriginFor<T>, enabled: bool) -> DispatchResult {
T::GovernanceOrigin::ensure_origin(origin)?;
Self::push_message(GatekeeperEvent::SetStaticV { enabled });
Self::deposit_event(Event::<T>::StaticVChanged { enabled });
Ok(())
}
}

#[pallet::hooks]
Expand Down

0 comments on commit f51a799

Please sign in to comment.