@@ -62,6 +62,7 @@ import Oracle.Validate.Types
62
62
( AValidationResult (.. )
63
63
, Validate
64
64
, Validated
65
+ , hoistValidate
65
66
, liftMaybe
66
67
, runValidate
67
68
)
@@ -86,63 +87,63 @@ import User.Types
86
87
)
87
88
import Validation (Validation )
88
89
90
+ type ValidateWithContext m a =
91
+ Validate UpdateTestRunFailure (WithContext m ) a
92
+
89
93
withExpectedState
90
94
:: (FromJSON Maybe (TestRunState phase ), Monad m )
91
95
=> TokenId
92
96
-> TestRun
93
- -> (TestRunState phase -> WithContext m result )
94
- -> WithContext m ( Maybe result )
97
+ -> (TestRunState phase -> ValidateWithContext m result )
98
+ -> ValidateWithContext m result
95
99
withExpectedState 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
112
113
113
114
withResolvedTestRun
114
115
:: Monad m
115
116
=> TokenId
116
117
-> TestRunId
117
- -> (TestRun -> WithContext m ( Maybe a ) )
118
- -> WithContext m ( Maybe a )
118
+ -> (TestRun -> ValidateWithContext m a )
119
+ -> ValidateWithContext m a
119
120
withResolvedTestRun 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
124
127
Nothing -> False
125
128
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
130
133
131
134
updateTestRunState
132
135
:: (Monad m , FromJSON Maybe (TestRunState phase ))
133
136
=> TokenId
134
137
-> TestRunId
135
138
-> ( TestRun
136
139
-> TestRunState phase
137
- -> WithContext m ( AValidationResult UpdateTestRunFailure a )
140
+ -> ValidateWithContext m a
138
141
)
139
142
-> WithContext m (AValidationResult UpdateTestRunFailure a )
140
143
updateTestRunState 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
146
147
147
148
agentCmd
148
149
:: MonadIO m
@@ -347,29 +348,27 @@ signAndSubmitAnUpdate
347
348
-> key
348
349
-> old
349
350
-> new
350
- -> WithContext
351
- m
352
- (AValidationResult UpdateTestRunFailure (WithTxHash new ))
351
+ -> ValidateWithContext m (WithTxHash new )
353
352
signAndSubmitAnUpdate validate tokenId wallet testRun oldState newState = do
354
353
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
373
372
374
373
reportCommand
375
374
:: Monad m
@@ -379,12 +378,9 @@ reportCommand
379
378
-> TestRunState RunningT
380
379
-> Duration
381
380
-> URL
382
- -> WithContext
381
+ -> ValidateWithContext
383
382
m
384
- ( AValidationResult
385
- UpdateTestRunFailure
386
- (WithTxHash (TestRunState DoneT ))
387
- )
383
+ (WithTxHash (TestRunState DoneT ))
388
384
reportCommand tokenId wallet testRun oldState duration url =
389
385
signAndSubmitAnUpdate
390
386
validateToDoneUpdate
@@ -401,12 +397,9 @@ rejectCommand
401
397
-> TestRun
402
398
-> TestRunState PendingT
403
399
-> [TestRunRejection ]
404
- -> WithContext
400
+ -> ValidateWithContext
405
401
m
406
- ( AValidationResult
407
- UpdateTestRunFailure
408
- (WithTxHash (TestRunState DoneT ))
409
- )
402
+ (WithTxHash (TestRunState DoneT ))
410
403
rejectCommand tokenId wallet testRun testRunState reason =
411
404
signAndSubmitAnUpdate
412
405
validateToDoneUpdate
@@ -422,12 +415,9 @@ acceptCommand
422
415
-> Wallet
423
416
-> TestRun
424
417
-> TestRunState PendingT
425
- -> WithContext
418
+ -> ValidateWithContext
426
419
m
427
- ( AValidationResult
428
- UpdateTestRunFailure
429
- (WithTxHash (TestRunState RunningT ))
430
- )
420
+ (WithTxHash (TestRunState RunningT ))
431
421
acceptCommand tokenId wallet testRun testRunState =
432
422
signAndSubmitAnUpdate
433
423
validateToRunningUpdate
0 commit comments