@@ -159,6 +159,12 @@ pub mod pallet {
159159 pub ( super ) type EvmAddress2Account < T : Config > =
160160 StorageMap < _ , Blake2_128Concat , H160 , T :: AccountId > ;
161161
162+ /// 卡主自定义额外加价(USD×10^6 per day per GPU),在系统自动定价基础上叠加
163+ #[ pallet:: storage]
164+ #[ pallet:: getter( fn machine_extra_price) ]
165+ pub ( super ) type MachineExtraPrice < T : Config > =
166+ StorageMap < _ , Blake2_128Concat , MachineId , u64 , ValueQuery > ;
167+
162168 // The current storage version.
163169 #[ pallet:: storage]
164170 #[ pallet:: getter( fn storage_version) ]
@@ -178,6 +184,29 @@ pub mod pallet {
178184 Ok ( ( ) . into ( ) )
179185 }
180186
187+ /// 卡主设置机器额外加价(在系统自动定价基础上叠加)
188+ /// 单位:USD×10^6 per day per GPU,与 get_machine_price 返回值单位一致
189+ /// 设置为 0 表示不额外加价
190+ #[ pallet:: call_index( 10 ) ]
191+ #[ pallet:: weight( frame_support:: weights:: Weight :: from_parts( 10000 , 0 ) ) ]
192+ pub fn set_machine_extra_price (
193+ origin : OriginFor < T > ,
194+ machine_id : MachineId ,
195+ extra_price : u64 ,
196+ ) -> DispatchResultWithPostInfo {
197+ let who = ensure_signed ( origin) ?;
198+ let machine_info = <online_profile:: Pallet < T > >:: machines_info ( & machine_id)
199+ . ok_or ( Error :: < T > :: Unknown ) ?;
200+ // 只有卡主(stash)或控制者(controller)可以设置
201+ ensure ! (
202+ machine_info. machine_stash == who || machine_info. controller == who,
203+ Error :: <T >:: NoPermission
204+ ) ;
205+ MachineExtraPrice :: < T > :: insert ( & machine_id, extra_price) ;
206+ Self :: deposit_event ( Event :: MachineExtraPriceSet ( machine_id, extra_price) ) ;
207+ Ok ( ( ) . into ( ) )
208+ }
209+
181210 /// 用户租用机器(按天租用)
182211 #[ pallet:: call_index( 1 ) ]
183212 #[ pallet:: weight( frame_support:: weights:: Weight :: from_parts( 10000 , 0 ) ) ]
@@ -322,6 +351,8 @@ pub mod pallet {
322351 Relet ( RentOrderId , T :: AccountId , MachineId , u32 , T :: BlockNumber , BalanceOf < T > ) ,
323352
324353 SetEvmAddress ( H160 , T :: AccountId ) ,
354+ // machine_id, extra_price (USD×10^6 per day per GPU)
355+ MachineExtraPriceSet ( MachineId , u64 ) ,
325356 }
326357
327358 #[ pallet:: error]
@@ -348,6 +379,7 @@ pub mod pallet {
348379 MachineNotRented ,
349380 MachineNotFound ,
350381 MoreThanOneRenter ,
382+ NoPermission ,
351383 }
352384}
353385
@@ -389,11 +421,16 @@ impl<T: Config> Pallet<T> {
389421 <generic_func:: Pallet < T > >:: pay_fixed_tx_fee ( renter. clone ( ) )
390422 . map_err ( |_| Error :: < T > :: PayTxFeeFailed ) ?;
391423
392- // 获得machine_price(每天的价格)
424+ // 获得machine_price(每天的价格) = 系统自动定价 + 卡主额外加价
393425 // 根据租用GPU数量计算价格
394- let machine_price =
426+ let system_price =
395427 T :: RTOps :: get_machine_price ( machine_info. calc_point ( ) , rent_gpu_num, gpu_num)
396428 . ok_or ( Error :: < T > :: GetMachinePriceFailed ) ?;
429+ let extra_price_per_gpu = Self :: machine_extra_price ( & machine_id) ;
430+ let extra_price = extra_price_per_gpu
431+ . checked_mul ( rent_gpu_num as u64 )
432+ . unwrap_or ( 0 ) ;
433+ let machine_price = system_price. checked_add ( extra_price) . unwrap_or ( system_price) ;
397434
398435 // 根据租用时长计算rent_fee
399436 let rent_fee_value = machine_price
@@ -496,10 +533,13 @@ impl<T: Config> Pallet<T> {
496533 return Ok ( ( ) . into ( ) )
497534 }
498535
499- // 计算rent_fee
500- let machine_price =
536+ // 计算rent_fee = 系统自动定价 + 卡主额外加价
537+ let system_price =
501538 T :: RTOps :: get_machine_price ( calc_point, gpu_num, machine_info. gpu_num ( ) )
502539 . ok_or ( Error :: < T > :: GetMachinePriceFailed ) ?;
540+ let extra_price = Self :: machine_extra_price ( & machine_id)
541+ . checked_mul ( gpu_num as u64 ) . unwrap_or ( 0 ) ;
542+ let machine_price = system_price. checked_add ( extra_price) . unwrap_or ( system_price) ;
503543 let rent_fee_value = machine_price
504544 . checked_mul ( add_duration. saturated_into :: < u64 > ( ) )
505545 . ok_or ( Error :: < T > :: Overflow ) ?
@@ -865,12 +905,15 @@ impl<T: Config> MachineInfoTrait for Pallet<T> {
865905 let machine_info = <online_profile:: Pallet < T > >:: machines_info ( & machine_id)
866906 . ok_or ( Error :: < T > :: Unknown . as_str ( ) ) ?;
867907
868- let machine_price = T :: RTOps :: get_machine_price (
908+ let system_price = T :: RTOps :: get_machine_price (
869909 machine_info. calc_point ( ) ,
870910 rent_gpu_num,
871911 machine_info. gpu_num ( ) ,
872912 )
873913 . ok_or ( Error :: < T > :: GetMachinePriceFailed ) ?;
914+ let extra_price = Self :: machine_extra_price ( & machine_id)
915+ . checked_mul ( rent_gpu_num as u64 ) . unwrap_or ( 0 ) ;
916+ let machine_price = system_price. checked_add ( extra_price) . unwrap_or ( system_price) ;
874917
875918 // 根据租用时长计算rent_fee
876919 let rent_fee_value = machine_price
@@ -888,12 +931,15 @@ impl<T: Config> MachineInfoTrait for Pallet<T> {
888931 let machine_info = <online_profile:: Pallet < T > >:: machines_info ( & machine_id)
889932 . ok_or ( Error :: < T > :: Unknown . as_str ( ) ) ?;
890933
891- let machine_price = T :: RTOps :: get_machine_price (
934+ let system_price = T :: RTOps :: get_machine_price (
892935 machine_info. calc_point ( ) ,
893936 rent_gpu_num,
894937 machine_info. gpu_num ( ) ,
895938 )
896939 . ok_or ( Error :: < T > :: GetMachinePriceFailed ) ?;
940+ let extra_price = Self :: machine_extra_price ( & machine_id)
941+ . checked_mul ( rent_gpu_num as u64 ) . unwrap_or ( 0 ) ;
942+ let machine_price = system_price. checked_add ( extra_price) . unwrap_or ( system_price) ;
897943
898944 // 根据租用时长计算rent_fee
899945 let rent_fee_value = machine_price
@@ -937,12 +983,15 @@ impl<T: Config> MachineInfoTrait for Pallet<T> {
937983 let machine_info = <online_profile:: Pallet < T > >:: machines_info ( & machine_id)
938984 . ok_or ( Error :: < T > :: Unknown . as_str ( ) ) ?;
939985
940- let machine_price = T :: RTOps :: get_machine_price (
986+ let system_price = T :: RTOps :: get_machine_price (
941987 machine_info. calc_point ( ) ,
942988 rent_gpu_num,
943989 machine_info. gpu_num ( ) ,
944990 )
945991 . ok_or ( Error :: < T > :: GetMachinePriceFailed ) ?;
992+ let extra_price = Self :: machine_extra_price ( & machine_id)
993+ . checked_mul ( rent_gpu_num as u64 ) . unwrap_or ( 0 ) ;
994+ let machine_price = system_price. checked_add ( extra_price) . unwrap_or ( system_price) ;
946995
947996 // 根据租用时长计算rent_fee
948997 let rent_fee_value = machine_price
0 commit comments