From 1c9cfd5df240849c433f1bbffb83e261f095560a Mon Sep 17 00:00:00 2001 From: FiveMovesAhead Date: Tue, 14 May 2024 13:48:43 +0800 Subject: [PATCH] Verify challenge exists in submit algorithm --- tig-protocol/src/submit_algorithm.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tig-protocol/src/submit_algorithm.rs b/tig-protocol/src/submit_algorithm.rs index 1f9576d3..f4f6be2f 100644 --- a/tig-protocol/src/submit_algorithm.rs +++ b/tig-protocol/src/submit_algorithm.rs @@ -10,6 +10,7 @@ pub(crate) async fn execute( details: &AlgorithmDetails, code: &String, ) -> ProtocolResult { + 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( Ok(algorithm_id) } +async fn verify_challenge_exists( + 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( ctx: &mut T, details: &AlgorithmDetails,