11#![ cfg_attr( not( feature = "std" ) , no_std) ]
2- /// A runtime module template with necessary imports
32
4- /// Feel free to remove or edit this file as needed.
5- /// If you change the name of this file, make sure to update its references in runtime/src/lib.rs
6- /// If you remove this file, you can remove those references
3+ /******************************************************************************
4+ A runtime module template with necessary imports
5+ ******************************************************************************/
76
8-
9- /// For more guidance on Substrate modules, see the example module
10- /// https://github.com/paritytech/substrate/blob/master/srml/example/src/lib.rs
117use sp_std:: prelude:: * ;
128use sp_std:: fmt:: Debug ;
139use sp_std:: collections:: btree_map:: BTreeMap ;
1410use frame_support:: {
1511 decl_module,
16- decl_storage,
12+ decl_storage,
1713 decl_event,
1814 decl_error,
1915 debug:: native,
@@ -72,10 +68,12 @@ use sp_io::hashing::blake2_256;
7268use rand_chacha:: { rand_core:: { RngCore , SeedableRng } , ChaChaRng } ;
7369use ed25519:: { Public , Signature } ;
7470
75- /// The module's configuration trait.
71+ /******************************************************************************
72+ The module's configuration trait
73+ ******************************************************************************/
7674pub trait Trait : system:: Trait {
7775 type Event : From < Event < Self > > + Into < <Self as system:: Trait >:: Event > ;
78- type Hash :
76+ type Hash :
7977 Parameter + Member + MaybeSerializeDeserialize + Debug + MaybeDisplay + SimpleBitOps
8078 + Default + Copy + CheckEqual + sp_std:: hash:: Hash + AsRef < [ u8 ] > + AsMut < [ u8 ] > ;
8179 type Randomness : Randomness < <Self as system:: Trait >:: Hash > ;
@@ -104,37 +102,37 @@ pub trait Trait: system::Trait{
104102}
105103
106104
107-
108- // Events in order of expected incidence
105+ /******************************************************************************
106+ Events
107+ ******************************************************************************/
109108decl_event ! (
110109 pub enum Event <T >{
111- /// New feed registered
110+ /// New data feed registered
112111 NewFeed ( T :: FeedId ) ,
113- /// New hosting plan
112+ /// New hosting plan by publisher for selected feed (many possible plans per feed)
114113 NewPlan ( T :: PlanId ) ,
115- //const data = [encoderID, hosterID, selectedPlan.feed, contractID, contract.ranges]
116- /// A new contract is created between publisher, encoder, and hoster
114+ /// A new contract between publisher, encoder, and hoster (many contracts per plan)
117115 NewContract ( T :: EncoderId , T :: HosterId , T :: FeedId , T :: ContractId , ContractRanges ) ,
118- /// Hosting ( contract) started
116+ /// Hosting contract started
119117 HostingStarted ( T :: ContractId ) ,
120118 /// New proof-of-storage challenge
121- ProofOfStorageChallenge ( T :: ChallengeId ) ,
122- /// Proof of storage confirmed
119+ NewProofOfStorageChallenge ( T :: ChallengeId ) ,
120+ /// Proof-of- storage confirmed
123121 ProofOfStorageConfirmed ( T :: AttestorId , T :: ChallengeId ) ,
124- /// Proof of storage not confirmed
122+ /// Proof-of- storage not confirmed
125123 ProofOfStorageFailed ( T :: ChallengeId ) ,
126124 /// Attestation of retrievability requested
127- ProofOfRetrievabilityAttestation ( T :: ChallengeId ) ,
125+ newAttestation ( T :: ChallengeId ) ,
128126 /// Proof of retrievability confirmed
129- ProofOfRetrievabilityConfirmed ( T :: ChallengeId ) ,
127+ AttestationReportConfirmed ( T :: ChallengeId ) ,
130128 /// Data serving not verified
131- ProofOfRetrievabilityFailed ( T :: ChallengeId ) ,
129+ AttestationReportFailed ( T :: ChallengeId ) ,
132130 }
133131) ;
134132
135133decl_error ! {
136134 pub enum Error for Module <T : Trait > {
137-
135+
138136 }
139137}
140138
@@ -153,7 +151,18 @@ struct User<T: Trait> {
153151 address : T :: AccountId
154152}
155153
156- #[ derive( Decode , PartialEq , Eq , Encode , Clone , Copy , RuntimeDebug ) ]
154+ type FeedKey = Public ;
155+
156+ #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
157+ struct Feed < T : Trait > {
158+ id : T :: FeedId ,
159+ publickey : FeedKey ,
160+ meta : TreeRoot
161+ }
162+
163+ type Ranges < T : Trait > = Vec < ( T :: ChunkIndex , T :: ChunkIndex ) > ;
164+
165+ #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
157166pub struct ParentHashInRoot {
158167 hash : H256 ,
159168 hash_number : u64 ,
@@ -173,17 +182,6 @@ pub struct TreeHashPayload {
173182 children : Vec < ParentHashInRoot >
174183}
175184
176- type FeedKey = Public ;
177-
178- #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
179- struct Feed < T : Trait > {
180- id : T :: FeedId ,
181- publickey : FeedKey ,
182- meta : TreeRoot
183- }
184-
185- type Ranges < T : Trait > = Vec < ( T :: ChunkIndex , T :: ChunkIndex ) > ;
186-
187185#[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
188186struct Plan < T : Trait > {
189187 id : T :: PlanId ,
@@ -208,6 +206,13 @@ struct Challenge<T: Trait> {
208206 chunks : Vec < T :: ChunkIndex >
209207}
210208
209+ #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
210+ pub struct Proof {
211+ index : u64 ,
212+ nodes : Vec < Node > ,
213+ signature : Option < Signature >
214+ }
215+
211216#[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
212217struct Attestation < T : Trait > {
213218 id : T :: AttestationId ,
@@ -216,37 +221,46 @@ struct Attestation<T: Trait> {
216221 chunks : Vec < T :: ChunkIndex >
217222}
218223
219- // storage items/db
224+ #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
225+ pub struct Report {
226+ location : u8 ,
227+ latency : Option < u8 >
228+ }
229+
230+ /******************************************************************************
231+ Storage items/db
232+ ******************************************************************************/
220233decl_storage ! {
221234 trait Store for Module <T : Trait > as DatVerify {
222- // public/api
235+ // PUBLIC/API
223236 pub GetFeedByID : map hasher( twox_64_concat) T :: FeedId => Feed <T >;
224237 pub GetUserByID : map hasher( twox_64_concat) T :: UserId => User <T >;
225238 pub GetContractByID : map hasher( twox_64_concat) T :: ContractId => Contract <T >;
226239 pub GetChallengeByID : map hasher( twox_64_concat) T :: ChallengeId => Challenge <T >;
227240 pub GetPlanByID : map hasher( twox_64_concat) T :: PlanId => Plan <T >;
228241 pub GetAttestationByID : map hasher( twox_64_concat) T :: AttestationId => Attestation <T >;
229- // internally required storage
242+ // INETRNALLY REQUIRED STORAGE
230243 pub GetNextFeedID : T :: FeedId ;
231244 pub GetNextUserID : T :: UserId ;
232245 pub GetNextContractID : T :: ContractId ;
233246 pub GetNextChallengeID : T :: ChallengeId ;
234247 pub GetNextPlanID : T :: PlanId ;
235248 pub GetNextAttestationID : T :: AttestationId ;
236249 pub Nonce : u64 ;
237- // lookups (created as neccesary)
250+ // LOOKUPS (created as neccesary)
238251 pub GetIDByUser : map hasher( twox_64_concat) User <T > => T :: UserId ;
239- // role array
252+ // ROLES ARRAY
240253 pub Roles : double_map hasher( twox_64_concat) Role , hasher( twox_64_concat) T :: UserId => RoleValue ;
241254 }
242255}
243256
244- //externally dispatchable functions/calls
245- //(including on_finalize, on_initialize)
257+ /******************************************************************************
258+ External functions (including on_finalize, on_initialize)
259+ ******************************************************************************/
246260decl_module ! {
247261 pub struct Module <T : Trait > for enum Call where origin: T :: Origin {
248262 type Error = Error <T >;
249-
263+
250264 fn deposit_event( ) = default ;
251265
252266 fn new_user( origin) {
@@ -328,19 +342,86 @@ decl_module!{
328342 plan_id = x;
329343 x = x+1 ;
330344 } ) ;
331- Self :: try_new_contract ( None , None , Some ( plan_id) ) ;
345+ Self :: make_new_contract ( None , None , Some ( plan_id) ) ;
332346 Self :: deposit_event( RawEvent :: NewFeed ( feed_id) ) ;
333347 Self :: deposit_event( RawEvent :: NewPlan ( plan_id) ) ;
334348 }
335349
336- fn
350+ fn encoding_done( origin, T :: ContractId ) {
351+ let user_address = ensure_signed( origin) ?;
352+ // DB.contractsEncoded.push(contractID)
353+ }
354+
355+ fn hosting_starts( origin, T :: ContractId ) {
356+ let user_address = ensure_signed( origin) ?;
357+ // DB.contractsHosted.push(contractID)
358+ // const HostingStarted = { event: { data: [contractID], method: 'HostingStarted' } }
359+ // handlers.forEach(handler => handler([HostingStarted]))
360+ }
361+
362+ fn request_proof_of_storage_challenge( origin, T :: ContractId ) {
363+ let user_address = ensure_signed( origin) ?;
364+ let contract = <GetContractByID >:: Get ( & ContractId ) ;
365+ let ranges = contract. ranges;
366+ /*
367+ const ranges = DB.contracts[contractID - 1].ranges // [ [0, 3], [5, 7] ]
368+ const chunks = ranges.map(range => getRandomInt(range[0], range[1] + 1))
369+ const challenge = { contract: contractID, chunks }
370+ const challengeID = DB.challenges.push(challenge)
371+ challenge.id = challengeID
372+ // emit events
373+ const newChallenge = { event: { data: [challengeID], method: 'NewProofOfStorageChallenge' } }
374+ handlers.forEach(handler => handler([newChallenge]))
375+ */
376+ }
377+
378+ fn submit_proof_of_storage( origin, T :: ChallengeId , proof: Proof ) {
379+ let user_address = ensure_signed( origin) ?;
380+ let challenge = <GetChallengeByID >:: Get ( & ChallengeId ) ;
381+ /*
382+ const challenge = DB.challenges[challengeID - 1]
383+ const isValid = validateProof(proof, challenge)
384+ let proofValidation
385+ const data = [challengeID]
386+ console.log('Submitting Proof Of Storage Challenge with ID:', challengeID)
387+ if (isValid) proofValidation = { event: { data, method: 'ProofOfStorageConfirmed' } }
388+ else proofValidation = { event: { data: [challengeID], method: 'ProofOfStorageFailed' } }
389+ // emit events
390+ handlers.forEach(handler => handler([proofValidation]))
391+ */
392+ }
393+
394+ fn request_attestation( origin, T :: ContractId ) {
395+ let user_address = ensure_signed( origin) ?;
396+ /*
397+ const [ attestorID ] = getRandom(DB.attestors)
398+ const attestation = { contract: contractID , attestor: attestorID }
399+ const attestationID = DB.attestations.push(attestation)
400+ attestation.id = attestationID
401+ const PoRChallenge = { event: { data: [attestationID], method: 'newAttestation' } }
402+ handlers.forEach(handler => handler([PoRChallenge]))
403+ */
404+ }
405+
406+ fn submit_attestation_report( origin, T :: AttestationId , report: Report ) {
407+ let user_address = ensure_signed( origin) ?;
408+ /*
409+ console.log('Submitting Proof Of Retrievability Attestation with ID:', attestationID)
410+ // emit events
411+ if (report) PoR = { event: { data: [attestationID], method: 'AttestationReportConfirmed' } }
412+ else PoR = { event: { data: [attestationID], method: 'AttestationReportFailed' } }
413+ handlers.forEach(handler => handler([PoR]))
414+ */
415+ }
337416 }
338417}
339418
340- //pallet internal functions
419+ /******************************************************************************
420+ Internal functions
421+ ******************************************************************************/
341422impl < T : Trait > Module < T > {
342423
343- fn try_new_contract (
424+ fn make_new_contract (
344425 encoder_option : Option < T :: EncoderId > ,
345426 hoster_option : Option < T :: HosterId > ,
346427 plan_option : Option < T :: PlanId >
@@ -352,7 +433,7 @@ impl<T: Trait> Module<T> {
352433 ( Some ( encoder_id) , Some ( hoster_id) , Some ( plan_id) ) => {
353434 <GetNextContractID < T > >:: mutate ( |x|{
354435 let plan = <GetPlanByID < T > >:: get ( plan_id) ;
355- let new_contract = Contract < T > {
436+ let new_contract = Contract {
356437 id : x,
357438 plan : plan_id,
358439 ranges : plan. ranges ,
@@ -363,17 +444,17 @@ impl<T: Trait> Module<T> {
363444 x = x+1 ;
364445 } ) ;
365446 } ,
366- ( None , None , Some ( plan_id) ) => {
447+ ( None , None , Some ( plan_id) ) => { // Condition: if planID && encoders available & hosters available
367448 // todo get random
368- Self :: try_new_contract ( random_encoder_option, random_hoster_option, plan_option) ;
449+ Self :: make_new_contract ( random_encoder_option, random_hoster_option, plan_option) ;
369450 } ,
370- ( None , Some ( hoster_id) , None ) => {
451+ ( None , Some ( hoster_id) , None ) => { //Condition: if hosterID && encoders available & plans available
371452 // todo get random
372- Self :: try_new_contract ( random_encoder_option, hoster_option, random_plan_option) ;
453+ Self :: make_new_contract ( random_encoder_option, hoster_option, random_plan_option) ;
373454 } ,
374- ( Some ( encoder_id) , None , None ) => {
455+ ( Some ( encoder_id) , None , None ) => { //Condition: if encoderID && hosters available & plans available
375456 // todo get random
376- Self :: try_new_contract ( encoder_option, random_hoster_option, random_plan_option) ;
457+ Self :: make_new_contract ( encoder_option, random_hoster_option, random_plan_option) ;
377458 } ,
378459 ( _, _, _) => ( )
379460 }
@@ -384,7 +465,7 @@ impl<T: Trait> Module<T> {
384465 ( rng. next_u32 ( ) % ( max as u32 + 1 ) ) as usize
385466 }
386467
387-
468+
388469 /// Pick an item at pseudo-random from the slice, given the `rng`. `None` iff the slice is empty.
389470 fn pick_item < ' a , R : RngCore , E > ( rng : & mut R , items : & ' a [ E ] ) -> Option < & ' a E > {
390471 if items. is_empty ( ) {
@@ -403,7 +484,7 @@ impl<T: Trait> Module<T> {
403484
404485 fn Get_random_of_role ( influence : & [ u8 ] , role : & Role , count : u32 ) -> Vec < u64 > {
405486 let members : Vec < u64 > = <roles < T > >:: iter_prefix ( role) . filter_map ( |x|{
406-
487+
407488 } ) . collect ( ) ;
408489 match members. len ( ) {
409490 0 => members,
@@ -424,4 +505,4 @@ impl<T: Trait> Module<T> {
424505 }
425506 }
426507
427- }
508+ }
0 commit comments