Skip to content
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
40 changes: 40 additions & 0 deletions pallets/online-profile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ pub mod pallet {
pub(super) type AuthorizedForceExitAccounts<T: Config> =
StorageValue<_, Vec<T::AccountId>, ValueQuery>;

/// Per-stash 自定义收租钱包(spec 410)
/// 若不存在(矿工未配置),则默认租金走 stash 本账户。
#[pallet::storage]
#[pallet::getter(fn stash_rent_receiver)]
pub(super) type StashRentReceiver<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, T::AccountId>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(block_number: T::BlockNumber) -> Weight {
Expand Down Expand Up @@ -1447,6 +1454,30 @@ pub mod pallet {
Self::deposit_event(Event::SpecificDateCleared(machine_id, date_days));
Ok(().into())
}

/// 矿工设置独立收租钱包(spec 410)
/// 传 None 恢复默认(租金走 stash 账户)。仅 stash 本人可调用。
/// Weight: 1 write + 1 event ~ 140_000
#[pallet::call_index(27)]
#[pallet::weight(frame_support::weights::Weight::from_parts(140_000, 0))]
pub fn set_rent_receiver(
origin: OriginFor<T>,
receiver: Option<T::AccountId>,
) -> DispatchResultWithPostInfo {
let stash = ensure_signed(origin)?;
// spec 410: 防止把租金发到全零地址(实际上等于烧钱,UX 陷阱)
if let Some(ref r) = receiver {
let zero = T::AccountId::decode(&mut &[0u8; 32][..])
.map_err(|_| Error::<T>::InvalidRentReceiver)?;
ensure!(r != &zero, Error::<T>::InvalidRentReceiver);
}
match receiver.as_ref() {
Some(r) => StashRentReceiver::<T>::insert(&stash, r),
None => StashRentReceiver::<T>::remove(&stash),
}
Self::deposit_event(Event::RentReceiverChanged(stash, receiver));
Ok(().into())
}
}

#[pallet::event]
Expand Down Expand Up @@ -1493,6 +1524,8 @@ pub mod pallet {
// machine_id, date_days (since epoch)
SpecificDateScheduleSet(MachineId, u32),
SpecificDateCleared(MachineId, u32),
// spec 410: 矿工设置独立收租钱包;Some(addr)=切换,None=恢复默认(stash 收)
RentReceiverChanged(T::AccountId, Option<T::AccountId>),
}

#[pallet::error]
Expand Down Expand Up @@ -1535,6 +1568,8 @@ pub mod pallet {
RentalTooShort,
/// 请求时段不在机器允许出租的时段内
OutOfRentalSchedule,
/// spec 410: receiver 地址非法(如全零)
InvalidRentReceiver,
}
}

Expand All @@ -1545,6 +1580,11 @@ impl<T: Config> Pallet<T> {
T::DbcPrice::get_dbc_amount_by_value(online_stake_params.reonline_stake)
}

/// spec 410: 获取 stash 的实际收租账户(未配置则回退到 stash 本身)
pub fn effective_rent_receiver(stash: &T::AccountId) -> T::AccountId {
Self::stash_rent_receiver(stash).unwrap_or_else(|| stash.clone())
}

// ═══════════════════════════════════════════════════════════════
// 分时段出租:校验请求时段是否被机器允许
// ═══════════════════════════════════════════════════════════════
Expand Down
26 changes: 24 additions & 2 deletions pallets/rent-machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ pub mod pallet {
#[pallet::getter(fn rent_fee_pot)]
pub(super) type RentFeePot<T: Config> = StorageValue<_, T::AccountId>;

/// spec 410: 订单创建时快照矿工的 receiver,防止 bait-and-switch。
/// Absent 等价于「用 stash 自己收」(向后兼容:升级前的旧订单无此快照)。
#[pallet::storage]
#[pallet::getter(fn rent_order_receiver)]
pub(super) type RentOrderReceiver<T: Config> =
StorageMap<_, Blake2_128Concat, RentOrderId, T::AccountId>;

#[pallet::type_value]
pub(super) fn MaximumRentalDurationDefault<T: Config>() -> EraIndex {
60
Expand Down Expand Up @@ -232,6 +239,7 @@ pub mod pallet {
&renter,
machine_id.clone(),
machine_info.machine_stash,
rent_id,
rent_info.stake_amount,
)?;

Expand Down Expand Up @@ -434,6 +442,13 @@ impl<T: Config> Pallet<T> {

let rent_id = Self::get_new_rent_id();

// spec 410: 快照矿工当前的独立收租钱包,防止 confirm 窗口内 bait-and-switch
if let Some(snapshot) =
<online_profile::Pallet<T>>::stash_rent_receiver(&machine_info.machine_stash)
{
RentOrderReceiver::<T>::insert(&rent_id, snapshot);
}

let mut machine_rent_order = Self::machine_rent_order(&machine_id);
let rentable_gpu_index = machine_rent_order.gen_rentable_gpu(rent_gpu_num, gpu_num);
ItemList::add_item(&mut machine_rent_order.rent_order, rent_id);
Expand Down Expand Up @@ -536,7 +551,7 @@ impl<T: Config> Pallet<T> {
let user_balance = <T as Config>::Currency::free_balance(&renter);
ensure!(rent_fee < user_balance, Error::<T>::InsufficientValue);

Self::pay_rent_fee(&renter, machine_id.clone(), machine_info.machine_stash, rent_fee)?;
Self::pay_rent_fee(&renter, machine_id.clone(), machine_info.machine_stash, rent_id, rent_fee)?;

// 获取用户租用的结束时间
rent_info.rent_end =
Expand Down Expand Up @@ -596,6 +611,7 @@ impl<T: Config> Pallet<T> {
renter: &T::AccountId,
machine_id: MachineId,
machine_stash: T::AccountId,
rent_id: RentOrderId,
fee_amount: BalanceOf<T>,
) -> DispatchResult {
let rent_fee_pot = Self::rent_fee_pot().ok_or(Error::<T>::UndefinedRentPot)?;
Expand All @@ -605,7 +621,11 @@ impl<T: Config> Pallet<T> {
let fee_to_destroy = destroy_percent * fee_amount;
let fee_to_stash = fee_amount.checked_sub(&fee_to_destroy).ok_or(Error::<T>::Overflow)?;

<T as pallet::Config>::Currency::transfer(renter, &machine_stash, fee_to_stash, KeepAlive)?;
// spec 410: 优先读订单创建时的快照(防 bait-and-switch);升级前旧订单无快照,
// 则回退到矿工当前设置;仍未设置则回退到 stash
let rent_receiver = Self::rent_order_receiver(&rent_id)
.unwrap_or_else(|| <online_profile::Pallet<T>>::effective_rent_receiver(&machine_stash));
<T as pallet::Config>::Currency::transfer(renter, &rent_receiver, fee_to_stash, KeepAlive)?;
<T as pallet::Config>::Currency::transfer(
renter,
&rent_fee_pot,
Expand Down Expand Up @@ -669,6 +689,7 @@ impl<T: Config> Pallet<T> {
MachineRentOrder::<T>::insert(&rent_info.machine_id, machine_rent_order);

RentInfo::<T>::remove(rent_id);
RentOrderReceiver::<T>::remove(rent_id);

T::RTOps::change_machine_status_on_confirm_expired(
&rent_info.machine_id,
Expand Down Expand Up @@ -754,6 +775,7 @@ impl<T: Config> Pallet<T> {
MachineRentOrder::<T>::insert(&rent_info.machine_id, machine_rent_order);

RentInfo::<T>::remove(rent_id);
RentOrderReceiver::<T>::remove(rent_id);
}
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions pallets/rent-machine/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod test_gpu_rental_rules;
pub mod test_online_profile;
pub mod test_rent_individual_gpu;
pub mod test_rent_receiver;
pub mod test_renters;
pub mod test_time_slot_rental;
pub mod tests;
Loading
Loading