Skip to content

Commit b80a662

Browse files
author
Joshua Mir
committed
attestation completeion and challenge failure
1 parent b626ae4 commit b80a662

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

pallet-datdot/src/lib.rs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ decl_event!(
340340
NewPin(u64, u64),
341341
Attest(AccountId, Attestation),
342342
AttestPhase(u64, ChallengeAttestations),
343+
ChallengeSuccess(AccountId, Public),
343344
}
344345
);
345346

@@ -794,21 +795,27 @@ decl_module!{
794795
native::info!("OnFinalize; Challenge: {:#?}", challenge);
795796
native::info!("OnFinalize; Dat: {:#?}", dat);
796797
let deadline = challenge.2;
797-
let attesting = <ChallengeAttestors>::contains_key(challenge_index);
798-
if (n == deadline) {
799-
if !attesting {
800-
<SelectedChallenges<T>>::remove(challenge_index);
801-
<ChallengeMap>::remove(challenge_index);
802-
Self::punish_seeder(user.clone());
803-
Self::deposit_event(RawEvent::ChallengeFailed(user, dat));
798+
let attesting = Self::is_attestation_complete(challenge_index);
799+
if (n >= deadline) {
800+
match attesting {
801+
Some(true) => {
802+
<SelectedChallenges<T>>::remove(challenge_index);
803+
<ChallengeMap>::remove(challenge_index);
804+
Self::reward_seeder(user.clone());
805+
Self::deposit_event(RawEvent::ChallengeSuccess(user, dat));
806+
},
807+
_ => {
808+
<SelectedChallenges<T>>::remove(challenge_index);
809+
<ChallengeMap>::remove(challenge_index);
810+
Self::punish_seeder(user.clone());
811+
Self::deposit_event(RawEvent::ChallengeFailed(user, dat));
812+
},
804813
}
805814
} else {
806815
if <RemovedDats>::get().contains(&dat) {
807816
Self::clear_challenge(user, challenge_index);
808817
}
809-
//todo charge fee*
810818
}
811-
//todo - iterate through attestations
812819
}
813820
<RemovedDats>::kill();
814821
}
@@ -830,6 +837,33 @@ impl<T: Trait> Module<T> {
830837
}
831838
}
832839

840+
fn is_attestation_complete(challenge_index: u64) -> Option<bool> {
841+
let state = Self::get_attestors_for(challenge_index);
842+
let mut fail_count = 0;
843+
for expected in state.clone().expected_attestors {
844+
// If any of the expected attestors haven't returned, return None.
845+
if state.completed
846+
.iter()
847+
.map(|x|x.0)
848+
.position(|x| x == expected)
849+
.and_then(|y| state.completed.get(y)
850+
.and_then(|z|{
851+
if z.1.latency.is_none(){
852+
fail_count += 1;
853+
Some(false)
854+
} else {
855+
Some(true)
856+
}
857+
}
858+
))
859+
.is_none(){
860+
return None;
861+
}
862+
}
863+
// else return Some(success status)
864+
Some(fail_count<=(T::AttestorsPerChallenge::get()/2))
865+
}
866+
833867
fn clear_challenge(account: T::AccountId, challenge_index: u64){
834868
let account_index = <ChallengeMap>::get(&challenge_index);
835869
let (_, count) = <SelectedUserIndex<T>>::get(&account);

0 commit comments

Comments
 (0)