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,8 +102,9 @@ 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 > where
111110 <T as Trait >:: EncoderId ,
@@ -115,33 +114,32 @@ decl_event!(
115114 <T as Trait >:: PlanId ,
116115 <T as Trait >:: ChallengeId
117116 {
118- /// New feed registered
119- NewFeed ( FeedId ) ,
120- /// New hosting plan
121- NewPlan ( PlanId ) ,
122- //const data = [encoderID, hosterID, selectedPlan.feed, contractID, contract.ranges]
123- /// A new contract is created between publisher, encoder, and hoster
124- NewContract ( EncoderId , HosterId , FeedId , ContractId , ContractRanges ) ,
125- /// Hosting (contract) started
126- HostingStarted ( ContractId ) ,
117+ /// New data feed registered
118+ NewFeed ( T :: FeedId ) ,
119+ /// New hosting plan by publisher for selected feed (many possible plans per feed)
120+ NewPlan ( T :: PlanId ) ,
121+ /// A new contract between publisher, encoder, and hoster (many contracts per plan)
122+ NewContract ( T :: EncoderId , T :: HosterId , T :: FeedId , T :: ContractId , ContractRanges ) ,
123+ /// Hosting contract started
124+ HostingStarted ( T :: ContractId ) ,
127125 /// New proof-of-storage challenge
128- ProofOfStorageChallenge ( ChallengeId ) ,
129- /// Proof of storage confirmed
130- ProofOfStorageConfirmed ( AttestorId , ChallengeId ) ,
131- /// Proof of storage not confirmed
132- ProofOfStorageFailed ( ChallengeId ) ,
126+ NewProofOfStorageChallenge ( T :: ChallengeId ) ,
127+ /// Proof-of- storage confirmed
128+ ProofOfStorageConfirmed ( T :: AttestorId , T :: ChallengeId ) ,
129+ /// Proof-of- storage not confirmed
130+ ProofOfStorageFailed ( T :: ChallengeId ) ,
133131 /// Attestation of retrievability requested
134- ProofOfRetrievabilityAttestation ( ChallengeId ) ,
132+ newAttestation ( T :: ChallengeId ) ,
135133 /// Proof of retrievability confirmed
136- ProofOfRetrievabilityConfirmed ( ChallengeId ) ,
134+ AttestationReportConfirmed ( T :: ChallengeId ) ,
137135 /// Data serving not verified
138- ProofOfRetrievabilityFailed ( ChallengeId ) ,
136+ AttestationReportFailed ( T :: ChallengeId ) ,
139137 }
140138) ;
141139
142140decl_error ! {
143141 pub enum Error for Module <T : Trait > {
144-
142+
145143 }
146144}
147145
@@ -160,7 +158,18 @@ struct User<T: Trait> {
160158 address : T :: AccountId
161159}
162160
163- #[ derive( Decode , PartialEq , Eq , Encode , Clone , Copy , RuntimeDebug ) ]
161+ type FeedKey = Public ;
162+
163+ #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
164+ struct Feed < T : Trait > {
165+ id : T :: FeedId ,
166+ publickey : FeedKey ,
167+ meta : TreeRoot
168+ }
169+
170+ type Ranges < T : Trait > = Vec < ( T :: ChunkIndex , T :: ChunkIndex ) > ;
171+
172+ #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
164173pub struct ParentHashInRoot {
165174 hash : H256 ,
166175 hash_number : u64 ,
@@ -180,17 +189,6 @@ pub struct TreeHashPayload {
180189 children : Vec < ParentHashInRoot >
181190}
182191
183- type FeedKey = Public ;
184-
185- #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
186- struct Feed < T : Trait > {
187- id : T :: FeedId ,
188- publickey : FeedKey ,
189- meta : TreeRoot
190- }
191-
192- type Ranges < T : Trait > = Vec < ( T :: ChunkIndex , T :: ChunkIndex ) > ;
193-
194192#[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
195193struct Plan < T : Trait > {
196194 id : T :: PlanId ,
@@ -215,6 +213,13 @@ struct Challenge<T: Trait> {
215213 chunks : Vec < T :: ChunkIndex >
216214}
217215
216+ #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
217+ pub struct Proof {
218+ index : u64 ,
219+ nodes : Vec < Node > ,
220+ signature : Option < Signature >
221+ }
222+
218223#[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
219224struct Attestation < T : Trait > {
220225 id : T :: AttestationId ,
@@ -223,37 +228,46 @@ struct Attestation<T: Trait> {
223228 chunks : Vec < T :: ChunkIndex >
224229}
225230
226- // storage items/db
231+ #[ derive( Decode , PartialEq , Eq , Encode , Clone , RuntimeDebug ) ]
232+ pub struct Report {
233+ location : u8 ,
234+ latency : Option < u8 >
235+ }
236+
237+ /******************************************************************************
238+ Storage items/db
239+ ******************************************************************************/
227240decl_storage ! {
228241 trait Store for Module <T : Trait > as DatVerify {
229- // public/api
242+ // PUBLIC/API
230243 pub GetFeedByID : map hasher( twox_64_concat) T :: FeedId => Option <Feed <T >>;
231244 pub GetUserByID : map hasher( twox_64_concat) T :: UserId => Option <User <T >>;
232245 pub GetContractByID : map hasher( twox_64_concat) T :: ContractId => Option <Contract <T >>;
233246 pub GetChallengeByID : map hasher( twox_64_concat) T :: ChallengeId => Option <Challenge <T >>;
234247 pub GetPlanByID : map hasher( twox_64_concat) T :: PlanId => Option <Plan <T >>;
235248 pub GetAttestationByID : map hasher( twox_64_concat) T :: AttestationId => Option <Attestation <T >>;
236- // internally required storage
249+ // INTERNALLY REQUIRED STORAGE
237250 pub GetNextFeedID : T :: FeedId ;
238251 pub GetNextUserID : T :: UserId ;
239252 pub GetNextContractID : T :: ContractId ;
240253 pub GetNextChallengeID : T :: ChallengeId ;
241254 pub GetNextPlanID : T :: PlanId ;
242255 pub GetNextAttestationID : T :: AttestationId ;
243256 pub Nonce : u64 ;
244- // lookups (created as neccesary)
257+ // LOOKUPS (created as neccesary)
245258 pub GetIDByUser : map hasher( twox_64_concat) User <T > => T :: UserId ;
246- // role array
259+ // ROLES ARRAY
247260 pub Roles : double_map hasher( twox_64_concat) Role , hasher( twox_64_concat) T :: UserId => RoleValue ;
248261 }
249262}
250263
251- //externally dispatchable functions/calls
252- //(including on_finalize, on_initialize)
264+ /******************************************************************************
265+ External functions (including on_finalize, on_initialize)
266+ ******************************************************************************/
253267decl_module ! {
254268 pub struct Module <T : Trait > for enum Call where origin: T :: Origin {
255269 type Error = Error <T >;
256-
270+
257271 fn deposit_event( ) = default ;
258272
259273 fn new_user( origin) {
@@ -335,19 +349,86 @@ decl_module!{
335349 plan_id = x;
336350 x = x+1 ;
337351 } ) ;
338- Self :: try_new_contract ( None , None , Some ( plan_id) ) ;
352+ Self :: make_new_contract ( None , None , Some ( plan_id) ) ;
339353 Self :: deposit_event( RawEvent :: NewFeed ( feed_id) ) ;
340354 Self :: deposit_event( RawEvent :: NewPlan ( plan_id) ) ;
341355 }
342356
343- fn
357+ fn encoding_done( origin, T :: ContractId ) {
358+ let user_address = ensure_signed( origin) ?;
359+ // DB.contractsEncoded.push(contractID)
360+ }
361+
362+ fn hosting_starts( origin, T :: ContractId ) {
363+ let user_address = ensure_signed( origin) ?;
364+ // DB.contractsHosted.push(contractID)
365+ // const HostingStarted = { event: { data: [contractID], method: 'HostingStarted' } }
366+ // handlers.forEach(handler => handler([HostingStarted]))
367+ }
368+
369+ fn request_proof_of_storage_challenge( origin, T :: ContractId ) {
370+ let user_address = ensure_signed( origin) ?;
371+ let contract = <GetContractByID >:: Get ( & ContractId ) ;
372+ let ranges = contract. ranges;
373+ /*
374+ const ranges = DB.contracts[contractID - 1].ranges // [ [0, 3], [5, 7] ]
375+ const chunks = ranges.map(range => getRandomInt(range[0], range[1] + 1))
376+ const challenge = { contract: contractID, chunks }
377+ const challengeID = DB.challenges.push(challenge)
378+ challenge.id = challengeID
379+ // emit events
380+ const newChallenge = { event: { data: [challengeID], method: 'NewProofOfStorageChallenge' } }
381+ handlers.forEach(handler => handler([newChallenge]))
382+ */
383+ }
384+
385+ fn submit_proof_of_storage( origin, T :: ChallengeId , proof: Proof ) {
386+ let user_address = ensure_signed( origin) ?;
387+ let challenge = <GetChallengeByID >:: Get ( & ChallengeId ) ;
388+ /*
389+ const challenge = DB.challenges[challengeID - 1]
390+ const isValid = validateProof(proof, challenge)
391+ let proofValidation
392+ const data = [challengeID]
393+ console.log('Submitting Proof Of Storage Challenge with ID:', challengeID)
394+ if (isValid) proofValidation = { event: { data, method: 'ProofOfStorageConfirmed' } }
395+ else proofValidation = { event: { data: [challengeID], method: 'ProofOfStorageFailed' } }
396+ // emit events
397+ handlers.forEach(handler => handler([proofValidation]))
398+ */
399+ }
400+
401+ fn request_attestation( origin, T :: ContractId ) {
402+ let user_address = ensure_signed( origin) ?;
403+ /*
404+ const [ attestorID ] = getRandom(DB.attestors)
405+ const attestation = { contract: contractID , attestor: attestorID }
406+ const attestationID = DB.attestations.push(attestation)
407+ attestation.id = attestationID
408+ const PoRChallenge = { event: { data: [attestationID], method: 'newAttestation' } }
409+ handlers.forEach(handler => handler([PoRChallenge]))
410+ */
411+ }
412+
413+ fn submit_attestation_report( origin, T :: AttestationId , report: Report ) {
414+ let user_address = ensure_signed( origin) ?;
415+ /*
416+ console.log('Submitting Proof Of Retrievability Attestation with ID:', attestationID)
417+ // emit events
418+ if (report) PoR = { event: { data: [attestationID], method: 'AttestationReportConfirmed' } }
419+ else PoR = { event: { data: [attestationID], method: 'AttestationReportFailed' } }
420+ handlers.forEach(handler => handler([PoR]))
421+ */
422+ }
344423 }
345424}
346425
347- //pallet internal functions
426+ /******************************************************************************
427+ Internal functions
428+ ******************************************************************************/
348429impl < T : Trait > Module < T > {
349430
350- fn try_new_contract (
431+ fn make_new_contract (
351432 encoder_option : Option < T :: EncoderId > ,
352433 hoster_option : Option < T :: HosterId > ,
353434 plan_option : Option < T :: PlanId >
@@ -359,7 +440,7 @@ impl<T: Trait> Module<T> {
359440 ( Some ( encoder_id) , Some ( hoster_id) , Some ( plan_id) ) => {
360441 <GetNextContractID < T > >:: mutate ( |x|{
361442 let plan = <GetPlanByID < T > >:: get ( plan_id) ;
362- let new_contract = Contract < T > {
443+ let new_contract = Contract {
363444 id : x,
364445 plan : plan_id,
365446 ranges : plan. ranges ,
@@ -370,17 +451,17 @@ impl<T: Trait> Module<T> {
370451 x = x+1 ;
371452 } ) ;
372453 } ,
373- ( None , None , Some ( plan_id) ) => {
454+ ( None , None , Some ( plan_id) ) => { // Condition: if planID && encoders available & hosters available
374455 // todo get random
375- Self :: try_new_contract ( random_encoder_option, random_hoster_option, plan_option) ;
456+ Self :: make_new_contract ( random_encoder_option, random_hoster_option, plan_option) ;
376457 } ,
377- ( None , Some ( hoster_id) , None ) => {
458+ ( None , Some ( hoster_id) , None ) => { //Condition: if hosterID && encoders available & plans available
378459 // todo get random
379- Self :: try_new_contract ( random_encoder_option, hoster_option, random_plan_option) ;
460+ Self :: make_new_contract ( random_encoder_option, hoster_option, random_plan_option) ;
380461 } ,
381- ( Some ( encoder_id) , None , None ) => {
462+ ( Some ( encoder_id) , None , None ) => { //Condition: if encoderID && hosters available & plans available
382463 // todo get random
383- Self :: try_new_contract ( encoder_option, random_hoster_option, random_plan_option) ;
464+ Self :: make_new_contract ( encoder_option, random_hoster_option, random_plan_option) ;
384465 } ,
385466 ( _, _, _) => ( )
386467 }
@@ -391,7 +472,7 @@ impl<T: Trait> Module<T> {
391472 ( rng. next_u32 ( ) % ( max as u32 + 1 ) ) as usize
392473 }
393474
394-
475+
395476 /// Pick an item at pseudo-random from the slice, given the `rng`. `None` iff the slice is empty.
396477 fn pick_item < ' a , R : RngCore , E > ( rng : & mut R , items : & ' a [ E ] ) -> Option < & ' a E > {
397478 if items. is_empty ( ) {
@@ -410,7 +491,7 @@ impl<T: Trait> Module<T> {
410491
411492 fn Get_random_of_role ( influence : & [ u8 ] , role : & Role , count : u32 ) -> Vec < u64 > {
412493 let members : Vec < u64 > = <roles < T > >:: iter_prefix ( role) . filter_map ( |x|{
413-
494+
414495 } ) . collect ( ) ;
415496 match members. len ( ) {
416497 0 => members,
@@ -431,4 +512,4 @@ impl<T: Trait> Module<T> {
431512 }
432513 }
433514
434- }
515+ }
0 commit comments