@@ -62,6 +62,7 @@ import Oracle.Validate.Types
6262 ( AValidationResult (.. )
6363 , Validate
6464 , Validated
65+ , hoistValidate
6566 , liftMaybe
6667 , runValidate
6768 )
@@ -86,63 +87,63 @@ import User.Types
8687 )
8788import Validation (Validation )
8889
90+ type ValidateWithContext m a =
91+ Validate UpdateTestRunFailure (WithContext m ) a
92+
8993withExpectedState
9094 :: (FromJSON Maybe (TestRunState phase ), Monad m )
9195 => TokenId
9296 -> TestRun
93- -> (TestRunState phase -> WithContext m result )
94- -> WithContext m ( Maybe result )
97+ -> (TestRunState phase -> ValidateWithContext m result )
98+ -> ValidateWithContext m result
9599withExpectedState tokenId testRun cont = do
96- facts <- withMPFS $ \ mpfs -> mpfsGetTokenFacts mpfs tokenId
97- jsonKeyValue <- toJSON testRun
98- let
99- hasKey (JSObject obj) =
100- any (\ (k, value) -> k == " key" && value == jsonKeyValue) obj
101- hasKey _ = False
102- case facts of
103- JSArray objects -> do
104- case filter hasKey objects of
105- [JSObject object] -> do
106- let value = find (\ (k, _) -> k == " value" ) object
107- case value >>= fromJSON . snd of
108- Nothing -> pure Nothing
109- Just x -> Just <$> cont x
110- _ -> pure Nothing
111- _ -> pure Nothing
100+ facts <- lift
101+ $ fmap parseFacts
102+ $ withMPFS
103+ $ \ mpfs -> mpfsGetTokenFacts mpfs tokenId
104+ jsonKey <- toJSON testRun
105+ jPreviousState <-
106+ liftMaybe UpdateTestRunPreviousStateNotFound
107+ $ find ((== jsonKey) . factKey) facts
108+ value <-
109+ liftMaybe UpdateTestRunWrongPreviousState
110+ $ fromJSON
111+ $ factValue jPreviousState
112+ cont value
112113
113114withResolvedTestRun
114115 :: Monad m
115116 => TokenId
116117 -> TestRunId
117- -> (TestRun -> WithContext m ( Maybe a ) )
118- -> WithContext m ( Maybe a )
118+ -> (TestRun -> ValidateWithContext m a )
119+ -> ValidateWithContext m a
119120withResolvedTestRun tk (TestRunId testRunId) cont = do
120- facts <- withMPFS $ \ mpfs -> mpfsGetTokenFacts mpfs tk
121- let testRuns = parseFacts facts
122- finder :: Fact TestRun JSValue -> Bool
123- finder (Fact key _) = case keyHash key of
121+ facts <- lift
122+ $ fmap parseFacts
123+ $ withMPFS
124+ $ \ mpfs -> mpfsGetTokenFacts mpfs tk
125+ let match :: Fact TestRun JSValue -> Bool
126+ match (Fact key _) = case keyHash key of
124127 Nothing -> False
125128 Just keyId -> keyId == testRunId
126- let mtr = factKey <$> find finder testRuns
127- case mtr of
128- Nothing -> pure Nothing
129- Just testRun -> cont testRun
129+ testRun <-
130+ liftMaybe UpdateTestRunTestRunIdNotResolved
131+ $ factKey <$> find match facts
132+ cont testRun
130133
131134updateTestRunState
132135 :: (Monad m , FromJSON Maybe (TestRunState phase ))
133136 => TokenId
134137 -> TestRunId
135138 -> ( TestRun
136139 -> TestRunState phase
137- -> WithContext m ( AValidationResult UpdateTestRunFailure a )
140+ -> ValidateWithContext m a
138141 )
139142 -> WithContext m (AValidationResult UpdateTestRunFailure a )
140143updateTestRunState tokenId key f = do
141- mResult <- withResolvedTestRun tokenId key $ \ testRun ->
142- withExpectedState tokenId testRun $ f testRun
143- pure $ case mResult of
144- Nothing -> ValidationFailure UpdateTestRunWrongPreviousState
145- Just result -> result
144+ runValidate
145+ $ withResolvedTestRun tokenId key
146+ $ \ testRun -> withExpectedState tokenId testRun $ f testRun
146147
147148agentCmd
148149 :: MonadIO m
@@ -347,29 +348,27 @@ signAndSubmitAnUpdate
347348 -> key
348349 -> old
349350 -> new
350- -> WithContext
351- m
352- (AValidationResult UpdateTestRunFailure (WithTxHash new ))
351+ -> ValidateWithContext m (WithTxHash new )
353352signAndSubmitAnUpdate validate tokenId wallet testRun oldState newState = do
354353 let requester = owner wallet
355- validation <- askValidation $ Just tokenId
356- mconfig <- askConfig tokenId
357- Submission submit <- ($ wallet) <$> askSubmit
358- mpfs <- askMpfs
359- lift $ runValidate $ do
360- Config {configAgent} <-
361- liftMaybe UpdateTestRunConfigNotAvailable mconfig
362- void
363- $ validate validation configAgent requester
364- $ Change (Key testRun)
365- $ Update oldState newState
366- wtx <- lift $ submit $ \ address -> do
367- key <- toJSON testRun
368- oldValue <- toJSON oldState
369- newValue <- toJSON newState
370- mpfsRequestUpdate mpfs address tokenId
371- $ RequestUpdateBody {key, oldValue, newValue}
372- pure $ wtx $> newState
354+ validation <- lift $ askValidation $ Just tokenId
355+ mconfig <- lift $ askConfig tokenId
356+ Submission submit <- lift $ ($ wallet) <$> askSubmit
357+ mpfs <- lift askMpfs
358+ Config {configAgent} <-
359+ liftMaybe UpdateTestRunConfigNotAvailable mconfig
360+ void
361+ $ hoistValidate lift
362+ $ validate validation configAgent requester
363+ $ Change (Key testRun)
364+ $ Update oldState newState
365+ wtx <- lift $ lift $ submit $ \ address -> do
366+ key <- toJSON testRun
367+ oldValue <- toJSON oldState
368+ newValue <- toJSON newState
369+ mpfsRequestUpdate mpfs address tokenId
370+ $ RequestUpdateBody {key, oldValue, newValue}
371+ pure $ wtx $> newState
373372
374373reportCommand
375374 :: Monad m
@@ -379,12 +378,9 @@ reportCommand
379378 -> TestRunState RunningT
380379 -> Duration
381380 -> URL
382- -> WithContext
381+ -> ValidateWithContext
383382 m
384- ( AValidationResult
385- UpdateTestRunFailure
386- (WithTxHash (TestRunState DoneT ))
387- )
383+ (WithTxHash (TestRunState DoneT ))
388384reportCommand tokenId wallet testRun oldState duration url =
389385 signAndSubmitAnUpdate
390386 validateToDoneUpdate
@@ -401,12 +397,9 @@ rejectCommand
401397 -> TestRun
402398 -> TestRunState PendingT
403399 -> [TestRunRejection ]
404- -> WithContext
400+ -> ValidateWithContext
405401 m
406- ( AValidationResult
407- UpdateTestRunFailure
408- (WithTxHash (TestRunState DoneT ))
409- )
402+ (WithTxHash (TestRunState DoneT ))
410403rejectCommand tokenId wallet testRun testRunState reason =
411404 signAndSubmitAnUpdate
412405 validateToDoneUpdate
@@ -422,12 +415,9 @@ acceptCommand
422415 -> Wallet
423416 -> TestRun
424417 -> TestRunState PendingT
425- -> WithContext
418+ -> ValidateWithContext
426419 m
427- ( AValidationResult
428- UpdateTestRunFailure
429- (WithTxHash (TestRunState RunningT ))
430- )
420+ (WithTxHash (TestRunState RunningT ))
431421acceptCommand tokenId wallet testRun testRunState =
432422 signAndSubmitAnUpdate
433423 validateToRunningUpdate
0 commit comments