Verify challenge exists in submit algorithm

This commit is contained in:
FiveMovesAhead 2024-05-14 13:48:43 +08:00
parent 3dd21b3588
commit 1c9cfd5df2

View File

@ -10,6 +10,7 @@ pub(crate) async fn execute<T: Context>(
details: &AlgorithmDetails,
code: &String,
) -> ProtocolResult<String> {
verify_challenge_exists(ctx, details).await?;
verify_algorithm_name_is_unique(ctx, details).await?;
verify_submission_fee(ctx, player, details).await?;
let algorithm_id = ctx
@ -19,6 +20,23 @@ pub(crate) async fn execute<T: Context>(
Ok(algorithm_id)
}
async fn verify_challenge_exists<T: Context>(
ctx: &mut T,
details: &AlgorithmDetails,
) -> ProtocolResult<()> {
if ctx
.get_challenges(ChallengesFilter::Id(details.challenge_id.clone()), None)
.await
.unwrap_or_else(|e| panic!("get_challenges error: {:?}", e))
.is_empty()
{
return Err(ProtocolError::InvalidChallenge {
challenge_id: details.challenge_id.clone(),
});
}
Ok(())
}
async fn verify_algorithm_name_is_unique<T: Context>(
ctx: &mut T,
details: &AlgorithmDetails,