@@ -44,7 +44,12 @@ use frame_system::{
4444 ensure_root,
4545 RawOrigin
4646} ;
47- use codec:: { Encode , Decode , Codec } ;
47+ use codec:: {
48+ Encode ,
49+ Decode ,
50+ Codec ,
51+ EncodeLike
52+ } ;
4853use sp_core:: {
4954 ed25519,
5055 H256 ,
@@ -79,25 +84,25 @@ pub trait Trait: system::Trait{
7984 + Default + Copy + CheckEqual + sp_std:: hash:: Hash + AsRef < [ u8 ] > + AsMut < [ u8 ] > ;
8085 type Randomness : Randomness < <Self as system:: Trait >:: Hash > ;
8186 //ID TYPES---
82- type FeedId : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
87+ type FeedId : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
8388 MaybeSerializeDeserialize + Debug ;
84- type UserId : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
89+ type UserId : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
8590 MaybeSerializeDeserialize + Debug ;
86- type ContractId : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
91+ type ContractId : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
8792 MaybeSerializeDeserialize + Debug ;
88- type ChallengeId : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
93+ type ChallengeId : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
8994 MaybeSerializeDeserialize + Debug ;
90- type PlanId : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
95+ type PlanId : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
9196 MaybeSerializeDeserialize + Debug ;
92- type AttestorId : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
97+ type AttestorId : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
9398 MaybeSerializeDeserialize + Debug ;
94- type AttestationId : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
99+ type AttestationId : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
95100 MaybeSerializeDeserialize + Debug ;
96- type HosterId : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
101+ type HosterId : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
97102 MaybeSerializeDeserialize + Debug ;
98- type EncoderId : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
103+ type EncoderId : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
99104 MaybeSerializeDeserialize + Debug ;
100- type ChunkIndex : Parameter + Member + AtLeast32Bit + BaseArithmetic + Codec + Default + Copy +
105+ type ChunkIndex : Parameter + Member + AtLeast32Bit + BaseArithmetic + EncodeLike < u32 > + Codec + Default + Copy +
101106 MaybeSerializeDeserialize + Debug ;
102107 // ---
103108}
@@ -300,7 +305,7 @@ impl Node {
300305 //calculate the root hash
301306 fn get_orphan_indeces ( highest_index : u64 ) -> Vec < u64 > {
302307 let mut indeces : Vec < u64 > = Vec :: new ( ) ;
303- for i in 0 ..Self :: highest_at_index ( highest_index) +1 {
308+ for i in 0 ..Self :: highest_at_index ( highest_index) +1 {
304309 match Self :: top_index_at_height ( i, highest_index) {
305310 Some ( expr) => if expr % 2 == 0 {
306311 indeces. push ( expr) ;
@@ -359,7 +364,7 @@ decl_storage! {
359364 pub GetNextAttestationID : T :: AttestationId ;
360365 pub Nonce : u64 ;
361366 // LOOKUPS (created as neccesary)
362- pub GetIDByUser : map hasher( twox_64_concat) User < T > => T :: UserId ;
367+ pub GetIDByUser : map hasher( twox_64_concat) T :: AccountId => Option < T :: UserId > ;
363368 // ROLES ARRAY
364369 pub Roles : double_map hasher( twox_64_concat) Role , hasher( twox_64_concat) T :: UserId => RoleValue ;
365370 }
@@ -370,138 +375,151 @@ decl_storage! {
370375******************************************************************************/
371376decl_module ! {
372377 pub struct Module <T : Trait > for enum Call where origin: T :: Origin {
373-
374378 fn deposit_event( ) = default ;
375379
376-
377380 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
378381 fn new_user( origin) {
379382 let user_address = ensure_signed( origin) ?;
380- <GetIDByUser >:: exists( & user_address) ;
381- <GetNextUserID >:: mutate( |x|{
382- let new_user = User {
383- id: x,
384- address: user_address
385- } ;
386- <GetUserByID >:: insert( x, new_user) ;
387- x = x+1 ;
388- } ) ;
383+ if <GetIDByUser <T >>:: contains_key( & user_address) {
384+ // some err
385+ } else {
386+ let mut x = <GetNextUserID <T >>:: get( ) ;
387+ let new_user = User {
388+ id: x. clone( ) ,
389+ address: user_address. clone( )
390+ } ;
391+ <GetUserByID <T >>:: insert( x, new_user) ;
392+ <GetIDByUser <T >>:: insert( & user_address, x. clone( ) ) ;
393+ <GetNextUserID <T >>:: put( x+One :: one( ) ) ;
394+ }
389395 }
390396
391397
392398 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
393399 fn register_encoder( origin) {
394400 let user_address = ensure_signed( origin) ?;
395- let user_id = <GetUserByID >:: Get ( & user_address) ;
396- <Roles >:: insert( Role :: Encoder , user_id, Some ( 0 ) ) ;
401+ if let Some ( user_id) = <GetIDByUser <T >>:: get( & user_address) {
402+ <Roles <T >>:: insert( Role :: Encoder , user_id, RoleValue :: Some ( 0 ) ) ;
403+ }
397404 }
398405
399406 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
400407 fn register_hoster( origin) {
401408 let user_address = ensure_signed( origin) ?;
402- let user_id = <GetUserByID >:: Get ( & user_address) ;
403- <Roles >:: insert( Role :: Hoster , user_id, Some ( 0 ) ) ;
409+ if let Some ( user_id) = <GetIDByUser <T >>:: get( & user_address) {
410+ <Roles <T >>:: insert( Role :: Hoster , user_id, RoleValue :: Some ( 0 ) ) ;
411+ }
404412 }
405413
406414 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
407415 fn register_attestor( origin) {
408416 let user_address = ensure_signed( origin) ?;
409- let user_id = <GetUserByID >:: Get ( & user_address) ;
410- <Roles >:: insert( Role :: Attestor , user_id, Some ( 0 ) ) ;
417+ if let Some ( user_id) = <GetIDByUser <T >>:: get( & user_address) {
418+ <Roles <T >>:: insert( Role :: Attestor , user_id, RoleValue :: Some ( 0 ) ) ;
419+ }
411420 }
412421
413422 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
414423 fn unregister_encoder( origin) {
415424 let user_address = ensure_signed( origin) ?;
416- let user_id = <GetUserByID >:: Get ( & user_address) ;
417- <Roles >:: insert( Role :: Encoder , user_id, None ) ;
425+ if let Some ( user_id) = <GetIDByUser <T >>:: get( & user_address) {
426+ <Roles <T >>:: insert( Role :: Encoder , user_id, RoleValue :: None ) ;
427+ }
418428 }
419429
420430 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
421431 fn unregister_hoster( origin) {
422432 let user_address = ensure_signed( origin) ?;
423- let user_id = <GetUserByID >:: Get ( & user_address) ;
424- <Roles >:: insert( Role :: Hoster , user_id, None ) ;
433+ if let Some ( user_id) = <GetIDByUser <T >>:: get( & user_address) {
434+ <Roles <T >>:: insert( Role :: Hoster , user_id, RoleValue :: None ) ;
435+ }
425436 }
426437
427438 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
428439 fn unregister_attestor( origin) {
429440 let user_address = ensure_signed( origin) ?;
430- let user_id = <GetUserByID >:: Get ( & user_address) ;
431- <Roles >:: insert( Role :: Attestor , user_id, None ) ;
441+ if let Some ( user_id) = <GetIDByUser <T >>:: get( & user_address) {
442+ <Roles <T >>:: insert( Role :: Attestor , user_id, RoleValue :: None ) ;
443+ }
432444 }
433445
434446 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
435- fn publish_feed_and_plan( origin, merkle_root: ( Public , TreeHashPayload , H512 ) , plan : Plan ) {
447+ fn publish_feed_and_plan( origin, merkle_root: ( Public , TreeHashPayload , H512 ) , ranges : Ranges < T :: ChunkIndex > ) {
436448 let user_address = ensure_signed( origin) ?;
437- let user_id = <GetUserByID > :: Get ( & user_address) ;
438- let mut feed_id;
439- let mut plan_id;
449+ if let Some ( user_id) = <GetIDByUser < T >> :: get ( & user_address) {
450+ let mut feed_id : T :: FeedId ;
451+ let mut plan_id : T :: PlanId ;
440452 //TODO -- currently re-registering existing feeds creates a new FeedID, should lookup first.
441- <GetNextFeedID > :: mutate ( |x| {
453+ let next_feed_id = <GetNextFeedID < T >> :: get ( ) ;
442454 let new_feed = Feed {
443- id: x ,
455+ id: next_feed_id . clone ( ) ,
444456 publickey: merkle_root. 0 ,
445457 meta: TreeRoot {
446458 signature: merkle_root. 2 ,
447- ...merkle_root. 1
459+ hash_type: merkle_root. 1 . hash_type,
460+ children: merkle_root. 1 . children
448461 }
449462 } ;
450- <GetFeedByID >:: insert( x, new_feed) ;
451- feed_id = x;
452- x = x+1 ;
453- } ) ;
454- <GetNextPlanID >:: mutate( |x|{
455- let new_plan = Plan {
456- id: x,
463+ <GetFeedByID <T >>:: insert( next_feed_id, new_feed. clone( ) ) ;
464+ feed_id = next_feed_id. clone( ) ;
465+ <GetNextFeedID <T >>:: put( next_feed_id+One :: one( ) ) ;
466+ let next_plan_id = <GetNextPlanID <T >>:: get( ) ;
467+ let new_plan = Plan :: <T > {
468+ id: next_plan_id. clone( ) ,
457469 publisher: user_id,
458470 feed: feed_id,
459- ranges: plan . ranges
471+ ranges: ranges
460472 } ;
461- <GetPlanByID >:: insert( x, new_plan) ;
462- plan_id = x;
463- x = x+1 ;
464- } ) ;
465- Self :: make_new_contract( None , None , Some ( plan_id) ) ;
473+ <GetPlanByID <T >>:: insert( next_plan_id, new_plan. clone( ) ) ;
474+ plan_id = next_plan_id. clone( ) ;
475+ <GetNextPlanID <T >>:: put( next_plan_id+One :: one( ) ) ;
476+ Self :: make_new_contract( None , None , Some ( plan_id. clone( ) ) ) ;
466477 Self :: deposit_event( RawEvent :: NewFeed ( feed_id) ) ;
467- Self :: deposit_event( RawEvent :: NewPlan ( plan_id) ) ;
478+ Self :: deposit_event( RawEvent :: NewPlan ( plan_id. clone( ) ) ) ;
479+ } else {
480+ //some err
481+ }
468482 }
469483
470484 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
471- fn encoding_done( origin, T :: ContractId ) {
485+ fn encoding_done( origin, contract_id : T :: ContractId ) {
472486 let user_address = ensure_signed( origin) ?;
473487 // DB.contractsEncoded.push(contractID)
474488 }
475489
476490 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
477- fn hosting_starts( origin, T :: ContractId ) {
491+ fn hosting_starts( origin, contract_id : T :: ContractId ) {
478492 let user_address = ensure_signed( origin) ?;
479493 // DB.contractsHosted.push(contractID)
480494 // const HostingStarted = { event: { data: [contractID], method: 'HostingStarted' } }
481495 // handlers.forEach(handler => handler([HostingStarted]))
496+ Self :: deposit_event( RawEvent :: HostingStarted ( contract_id) ) ;
482497 }
483498
484499 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
485- fn request_proof_of_storage_challenge( origin, T :: ContractId ) {
500+ fn request_proof_of_storage_challenge( origin, contract_id : T :: ContractId ) {
486501 let user_address = ensure_signed( origin) ?;
487- let contract = <GetContractByID >:: Get ( & ContractId ) ;
488- let ranges = contract. ranges;
489- /*
490- const ranges = DB.contracts[contractID - 1].ranges // [ [0, 3], [5, 7] ]
491- const chunks = ranges.map(range => getRandomInt(range[0], range[1] + 1))
492- const challenge = { contract: contractID, chunks }
493- const challengeID = DB.challenges.push(challenge)
494- challenge.id = challengeID
495- // emit events
496- const newChallenge = { event: { data: [challengeID], method: 'NewProofOfStorageChallenge' } }
497- handlers.forEach(handler => handler([newChallenge]))
498- */
502+ if let Some ( contract) = <GetContractByID <T >>:: get( & contract_id) {
503+ let ranges = contract. ranges;
504+ /*
505+ const ranges = DB.contracts[contractID - 1].ranges // [ [0, 3], [5, 7] ]
506+ const chunks = ranges.map(range => getRandomInt(range[0], range[1] + 1))
507+ const challenge = { contract: contractID, chunks }
508+ const challengeID = DB.challenges.push(challenge)
509+ challenge.id = challengeID
510+ // emit events
511+ const newChallenge = { event: { data: [challengeID], method: 'NewProofOfStorageChallenge' } }
512+ handlers.forEach(handler => handler([newChallenge]))
513+ */
514+ } else {
515+ //some err
516+ }
499517 }
500518
501519 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
502- fn submit_proof_of_storage( origin, T :: ChallengeId , proof: Proof ) {
520+ fn submit_proof_of_storage( origin, challenge_id : T :: ChallengeId , proof: Proof ) {
503521 let user_address = ensure_signed( origin) ?;
504- let challenge = <GetChallengeByID > :: Get ( & ChallengeId ) ;
522+ if let Some ( challenge) = <GetChallengeByID < T >> :: get ( & challenge_id ) {
505523 /*
506524 const challenge = DB.challenges[challengeID - 1]
507525 const isValid = validateProof(proof, challenge)
@@ -513,10 +531,11 @@ decl_module!{
513531 // emit events
514532 handlers.forEach(handler => handler([proofValidation]))
515533 */
534+ }
516535 }
517536
518537 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
519- fn request_attestation( origin, T :: ContractId ) {
538+ fn request_attestation( origin, contract_id : T :: ContractId ) {
520539 let user_address = ensure_signed( origin) ?;
521540 /*
522541 const [ attestorID ] = getRandom(DB.attestors)
@@ -529,7 +548,7 @@ decl_module!{
529548 }
530549
531550 #[ weight = ( 100000 , Operational , Pays :: No ) ] //todo weight
532- fn submit_attestation_report( origin, T :: AttestationId , report: Report ) {
551+ fn submit_attestation_report( origin, attestation_id : T :: AttestationId , report: Report ) {
533552 let user_address = ensure_signed( origin) ?;
534553 /*
535554 console.log('Submitting Proof Of Retrievability Attestation with ID:', attestationID)
@@ -557,19 +576,18 @@ impl<T: Trait> Module<T> {
557576 let mut random_plan_option = None ;
558577 match ( encoder_option, hoster_option, plan_option) {
559578 ( Some ( encoder_id) , Some ( hoster_id) , Some ( plan_id) ) => {
560- <GetNextContractID < T > >:: mutate ( |x| {
579+ let x = <GetNextContractID < T > >:: get ( ) ;
561580 if let Some ( plan) = <GetPlanByID < T > >:: get ( plan_id) {
562- let new_contract = Contract {
581+ let new_contract = Contract :: < T > {
563582 id : x. clone ( ) ,
564583 plan : plan_id,
565584 ranges : plan. ranges ,
566585 encoder : encoder_id,
567586 hoster : hoster_id
568587 } ;
569- <GetContractByID < T > >:: insert ( x. into ( ) , new_contract) ;
570- * x = * x +One :: one ( ) ;
588+ <GetContractByID < T > >:: insert ( x, new_contract) ;
589+ < GetNextContractID < T > > :: put ( x +One :: one ( ) ) ;
571590 } ;
572- } ) ;
573591 } ,
574592 ( None , None , Some ( plan_id) ) => { // Condition: if planID && encoders available & hosters available
575593 // todo get random
@@ -610,13 +628,13 @@ impl<T: Trait> Module<T> {
610628 // ---
611629
612630 fn unique_nonce ( ) -> u64 {
613- let nonce = <Nonce >:: get ( ) ;
631+ let nonce : u64 = <Nonce >:: get ( ) ;
614632 <Nonce >:: put ( nonce+1 ) ;
615633 nonce
616634 }
617635
618636 /*
619- fn Get_random_of_role (influence: &[u8], role: &Role, count: u32) -> Vec<u64> {
637+ fn get_random_of_role (influence: &[u8], role: &Role, count: u32) -> Vec<u64> {
620638 let members : Vec<u64> = <Roles<T>>::iter_prefix(role).filter_map(|x|{
621639
622640 }).collect();
0 commit comments