1+ // This file is part of Substrate.
2+ // Copyright (C) Auguth Research Foundation, India.
3+ // SPDX-License-Identifier: Apache-2.0
4+
5+ // Licensed under the Apache License, Version 2.0 (the "License");
6+ // you may not use this file except in compliance with the License.
7+ // You may obtain a copy of the License at
8+ //
9+ // http://www.apache.org/licenses/LICENSE-2.0
10+ //
11+ // Unless required by applicable law or agreed to in writing, software
12+ // distributed under the License is distributed on an "AS IS" BASIS,
13+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ // See the License for the specific language governing permissions and
15+ // limitations under the License.
16+ //
17+ // This file is utilized for Proof of Contract Stake Protocol (PoCS).
18+ //
19+
20+ // Module Imports
21+ pub mod chain_ext;
22+
23+
24+ // Imports
125use crate :: {
226Config , Error , Event , OriginFor , Pallet as Contracts , DelegateInfoMap , StakeInfoMap , ValidatorInfoMap ,
327} ;
@@ -10,13 +34,24 @@ use sp_runtime::{
1034use sp_std:: prelude:: * ;
1135use pallet_staking:: { Pallet as Staking , ValidatorPrefs , Bonded } ;
1236
37+ /// The minimum reputation required to participate in staking contracts.
1338pub const MIN_REPUTATION : u32 = 3 ;
39+
40+ /// The minimum number of delegates required for a validator to be eligible.
1441pub const MIN_DELEGATES : u32 = 10 ;
15- pub const REPUTATION_FACTOR : u32 = 1 ;
16- pub const INITIAL_STAKE_SCORE : u128 = 0 ;
1742
18- pub mod chain_ext;
43+ /// The fixed unit used for incrementing reputation and initializing it during instantiation.
44+ pub const REPUTATION_FACTOR : u32 = 1 ;
45+
46+ /// The initial stake score, set to zero for contract constructor purposes.
47+ pub const INITIAL_STAKE_SCORE : u128 = 0 ;
1948
49+
50+ /// Represents the delegation details of a deployed contract.
51+ /// It includes:
52+ /// - The owner of the contract.
53+ /// - The validator account i.e., contract to which the contract is delegated.
54+ /// - The block number when the delegation was set.
2055#[ derive( Encode , Decode , Clone , PartialEq , Eq , RuntimeDebug , TypeInfo , MaxEncodedLen ) ]
2156#[ scale_info( skip_type_params( T ) ) ]
2257pub struct DelegateInfo < T : Config > {
@@ -26,42 +61,52 @@ pub struct DelegateInfo<T: Config> {
2661}
2762
2863impl < T : Config > DelegateInfo < T > {
29-
64+ /// Returns the owner `AccountId` of the contract associated with this `DelegateInfo`.
3065 pub fn owner ( & self ) -> T :: AccountId {
3166 self . owner . clone ( )
3267 }
3368
69+ /// Returns the `AccountId` of the validator to whom the contract is delegated.
3470 pub fn delegate_to ( & self ) -> T :: AccountId {
3571 self . delegate_to . clone ( )
3672 }
3773
74+ /// Returns the block number when the delegate information was last updated.
3875 pub fn delegate_at ( & self ) -> BlockNumberFor < T > {
3976 self . delegate_at
4077 }
4178
79+ /// Retrieves the `DelegateInfo` for a given contract address.
4280 pub fn get ( contract_addr : & T :: AccountId ) -> Result < DelegateInfo < T > , DispatchError > {
4381 Contracts :: < T > :: get_delegate_info ( contract_addr)
44- . ok_or_else ( || Error :: < T > :: NoStakeExists . into ( ) )
82+ . ok_or_else ( || Error :: < T > :: NoStakeExists . into ( ) )
4583 }
4684
47- fn new ( owner : & T :: AccountId , ) -> Self {
48- Self {
49- owner : owner. clone ( ) ,
85+ /// Creates a new `DelegateInfo` instance where the deployer is both the owner and delegate.
86+ pub fn new ( owner : & T :: AccountId ) -> Self {
87+ Self {
88+ owner : owner. clone ( ) ,
5089 delegate_to : owner. clone ( ) ,
51- delegate_at : frame_system:: Pallet :: < T > :: block_number ( )
52- }
53- }
54-
55- fn update ( & self , delegate : & T :: AccountId ) -> Self {
56- Self {
57- owner : self . owner . clone ( ) ,
58- delegate_to : delegate. clone ( ) ,
59- delegate_at : frame_system:: Pallet :: < T > :: block_number ( )
60- }
61- }
90+ delegate_at : frame_system:: Pallet :: < T > :: block_number ( ) ,
91+ }
92+ }
6293
94+ /// Updates the `delegate_to` field and returns an updated `DelegateInfo` instance.
95+ pub fn update ( & self , delegate : & T :: AccountId ) -> Self {
96+ Self {
97+ owner : self . owner . clone ( ) ,
98+ delegate_to : delegate. clone ( ) ,
99+ delegate_at : frame_system:: Pallet :: < T > :: block_number ( ) ,
100+ }
101+ }
63102}
64103
104+
105+ /// Tracks the gas usage metrics of a contract for staking purposes.
106+ /// It includes:
107+ /// - The reputation score of the contract.
108+ /// - The block height of its most recent usage.
109+ /// - The stake score associated with the contract.
65110#[ derive( Encode , Decode , Clone , PartialEq , Eq , RuntimeDebug , TypeInfo , MaxEncodedLen ) ]
66111#[ scale_info( skip_type_params( T ) ) ]
67112pub struct StakeInfo < T : Config > {
@@ -72,23 +117,28 @@ pub struct StakeInfo<T: Config> {
72117
73118impl < T : Config > StakeInfo < T > {
74119
120+ /// Returns the stake score of a contract's `StakeInfo`.
75121 pub fn stake_score ( & self ) -> u128 {
76122 self . stake_score
77123 }
78124
125+ /// Returns the reputation score of a contract's `StakeInfo`.
79126 pub fn reputation ( & self ) -> u32 {
80127 self . reputation
81128 }
82129
130+ /// Returns the block height of the most recent interaction with the contract.
83131 pub fn blockheight ( & self ) -> BlockNumberFor < T > {
84132 self . blockheight
85133 }
86134
135+ /// Retrieves the `StakeInfo` of an instantiated contract.
87136 pub fn get ( contract_addr : & T :: AccountId ) -> Result < StakeInfo < T > , DispatchError > {
88137 Contracts :: < T > :: get_stake_info ( contract_addr)
89138 . ok_or_else ( || Error :: < T > :: NoStakeExists . into ( ) )
90139 }
91140
141+ /// Creates a mock `StakeInfo` instance for testing with a given stake score and reputation.
92142 pub fn mock_stake ( stake_score : u128 , reputation : u32 ) -> Self {
93143 Self {
94144 reputation : reputation,
@@ -97,6 +147,7 @@ impl<T: Config> StakeInfo<T>{
97147 }
98148 }
99149
150+ /// Creates a new `StakeInfo` instance using predefined constants for instantiation.
100151fn new ( ) -> Self {
101152Self {
102153reputation : REPUTATION_FACTOR ,
@@ -105,6 +156,7 @@ impl<T: Config> StakeInfo<T>{
105156}
106157}
107158
159+ /// Resets the stake score in `StakeInfo` to zero, updates the block number, and retains the reputation.
108160fn reset ( & self ) -> Self {
109161Self {
110162reputation : self . reputation ,
@@ -113,6 +165,7 @@ impl<T: Config> StakeInfo<T>{
113165}
114166}
115167
168+ /// Updates the stake score based on gas usage provided and adjusts reputation if the block height has changed.
116169 fn update ( & self , gas : & u64 ) -> Self {
117170 let current_block_height = <frame_system:: Pallet < T > >:: block_number ( ) ;
118171 let current_reputation = self . reputation ;
@@ -227,6 +280,14 @@ impl<T: Config> StakeRequest<T>{
227280 if StakeInfoMap :: < T > :: contains_key ( & contract_addr) {
228281 StakeInfoMap :: < T > :: remove ( & contract_addr) ;
229282 }
283+ if DelegateInfoMap :: < T > :: contains_key ( & contract_addr) {
284+ let delegate_info = <DelegateInfo < T > >:: get ( & contract_addr) . unwrap ( ) ;
285+ let delegate_to = delegate_info. delegate_to ( ) ;
286+ if delegate_to != delegate_info. owner ( ) {
287+ <DelegateRequest < T > >:: decrement ( & delegate_to) ;
288+ }
289+ DelegateInfoMap :: < T > :: remove ( & contract_addr) ;
290+ }
230291 }
231292
232293 fn nominate ( owner : & T :: AccountId , validator : & T :: AccountId ) -> Result < ( ) , DispatchError > {
0 commit comments