|
| 1 | +{- |
| 2 | +This is a full automated oracle process that can be used in the CLI. |
| 3 | +The process |
| 4 | +- respects a maximum number of requests to process in one run. |
| 5 | +- poll the MPFS system to get new requests. (get token) |
| 6 | +- batches requests and calls MPFS to include them in the token state. |
| 7 | +-} |
| 8 | + |
| 9 | +module Oracle.Process |
| 10 | + ( ProcessOptions (..) |
| 11 | + , oracleProcess |
| 12 | + , parseArgs |
| 13 | + ) where |
| 14 | + |
| 15 | +import Cli (Command (GetToken), WithValidation (..), cmd) |
| 16 | +import Control.Concurrent (threadDelay) |
| 17 | +import Control.Monad (forM_) |
| 18 | +import Control.Monad.Catch (MonadMask) |
| 19 | +import Control.Monad.IO.Class (MonadIO (..)) |
| 20 | +import Core.Context (WithContext, withContext) |
| 21 | +import Core.Options (tokenIdOption, walletOption) |
| 22 | +import Core.Types.Basic (RequestRefId, TokenId) |
| 23 | +import Core.Types.MPFS (MPFSClient (..), mpfsClientOption) |
| 24 | +import Core.Types.Wallet (Wallet) |
| 25 | +import Data.Functor (void) |
| 26 | +import GitHub (Auth) |
| 27 | +import MPFS.API (mpfsClient) |
| 28 | +import OptEnvConf |
| 29 | +import Options (githubAuthOption) |
| 30 | +import Oracle.Token.Cli |
| 31 | + ( TokenCommand (..) |
| 32 | + , TokenUpdateFailure |
| 33 | + , tokenCmdCore |
| 34 | + ) |
| 35 | +import Oracle.Types |
| 36 | + ( Token (tokenRequests) |
| 37 | + , requestZooRefId |
| 38 | + ) |
| 39 | +import Oracle.Validate.Types (AValidationResult (..), Validated (..)) |
| 40 | +import Paths_anti (version) |
| 41 | +import Validation (mkValidation) |
| 42 | + |
| 43 | +parseArgs :: IO ProcessOptions |
| 44 | +parseArgs = |
| 45 | + runParser |
| 46 | + version |
| 47 | + "anithesis-oracle-process - Automated antithesis oracle process" |
| 48 | + processOptionsParser |
| 49 | + |
| 50 | +data ProcessOptions = ProcessOptions |
| 51 | + { poAuth :: Auth |
| 52 | + , poMaxRequestsPerBatch :: Int |
| 53 | + , poPollIntervalSeconds :: Int |
| 54 | + , poWallet :: Wallet |
| 55 | + , poTokenId :: TokenId |
| 56 | + , poMPFSClient :: MPFSClient |
| 57 | + } |
| 58 | + |
| 59 | +processOptionsParser :: Parser ProcessOptions |
| 60 | +processOptionsParser = |
| 61 | + ProcessOptions |
| 62 | + <$> githubAuthOption |
| 63 | + <*> maxRequestsPerBatchOption |
| 64 | + <*> pollIntervalOption |
| 65 | + <*> walletOption |
| 66 | + <*> tokenIdOption |
| 67 | + <*> mpfsClientOption |
| 68 | + |
| 69 | +maxRequestsPerBatchOption :: Parser Int |
| 70 | +maxRequestsPerBatchOption = |
| 71 | + setting |
| 72 | + [ long "max-requests-per-batch" |
| 73 | + , metavar "INT" |
| 74 | + , help "Maximum number of requests to include in one batch" |
| 75 | + , env "MAX_REQUESTS_PER_BATCH" |
| 76 | + , reader auto |
| 77 | + , option |
| 78 | + , value 5 |
| 79 | + ] |
| 80 | + |
| 81 | +pollIntervalOption :: Parser Int |
| 82 | +pollIntervalOption = |
| 83 | + setting |
| 84 | + [ long "poll-interval" |
| 85 | + , metavar "SECONDS" |
| 86 | + , help "Interval in seconds between polling for new requests" |
| 87 | + , env "POLL_INTERVAL_SECONDS" |
| 88 | + , reader auto |
| 89 | + , option |
| 90 | + , value 30 |
| 91 | + ] |
| 92 | + |
| 93 | +oracleProcess :: ProcessOptions -> IO () |
| 94 | +oracleProcess = \case |
| 95 | + opts@ProcessOptions{poAuth, poMPFSClient} -> do |
| 96 | + let MPFSClient{runMPFS, submitTx} = poMPFSClient |
| 97 | + runMPFS |
| 98 | + $ withContext |
| 99 | + mpfsClient |
| 100 | + (mkValidation poAuth) |
| 101 | + submitTx |
| 102 | + $ processServer opts |
| 103 | + |
| 104 | +processServer |
| 105 | + :: forall m |
| 106 | + . (MonadIO m, MonadMask m) |
| 107 | + => ProcessOptions |
| 108 | + -> WithContext m () |
| 109 | +processServer opts@ProcessOptions{poPollIntervalSeconds} = do |
| 110 | + liftIO $ putStrLn "Starting oracle process server..." |
| 111 | + let loop :: WithContext m () = do |
| 112 | + liftIO $ putStrLn "Polling for new requests..." |
| 113 | + reqIds <- liftIO $ poll opts |
| 114 | + if null reqIds |
| 115 | + then do |
| 116 | + liftIO |
| 117 | + $ putStrLn |
| 118 | + $ "No new requests found. Sleeping for " |
| 119 | + ++ show poPollIntervalSeconds |
| 120 | + ++ " seconds..." |
| 121 | + liftIO $ threadDelay (poPollIntervalSeconds * 1000000) |
| 122 | + loop |
| 123 | + else do |
| 124 | + liftIO |
| 125 | + $ putStrLn |
| 126 | + $ "Found " |
| 127 | + ++ show (length reqIds) |
| 128 | + ++ " new requests. Processing..." |
| 129 | + let batches = batch opts reqIds |
| 130 | + forM_ batches $ \batchReqIds -> do |
| 131 | + liftIO |
| 132 | + $ putStrLn |
| 133 | + $ "Submitting batch of " |
| 134 | + ++ show (length batchReqIds) |
| 135 | + ++ " requests..." |
| 136 | + result <- submit opts batchReqIds |
| 137 | + case result of |
| 138 | + ValidationFailure err -> |
| 139 | + liftIO |
| 140 | + $ putStrLn |
| 141 | + $ "Failed to submit batch: " ++ show err |
| 142 | + ValidationSuccess txHash -> |
| 143 | + liftIO |
| 144 | + $ putStrLn |
| 145 | + $ "Successfully submitted batch with tx hash: " |
| 146 | + ++ show txHash |
| 147 | + loop |
| 148 | + loop |
| 149 | +poll :: ProcessOptions -> IO [RequestRefId] |
| 150 | +poll ProcessOptions{poTokenId, poMPFSClient, poAuth} = do |
| 151 | + result <- cmd (GetToken poAuth poMPFSClient poTokenId) |
| 152 | + case result of |
| 153 | + ValidationFailure err -> error $ "Failed to get token: " ++ show err |
| 154 | + ValidationSuccess token -> pure |
| 155 | + $ fmap (requestZooRefId . request) |
| 156 | + $ flip filter (tokenRequests token) |
| 157 | + $ \(WithValidation v _) -> case v of |
| 158 | + ValidationFailure _err -> False |
| 159 | + ValidationSuccess Validated -> True |
| 160 | + |
| 161 | +batch :: ProcessOptions -> [RequestRefId] -> [[RequestRefId]] |
| 162 | +batch ProcessOptions{poMaxRequestsPerBatch} = go |
| 163 | + where |
| 164 | + go [] = [] |
| 165 | + go xs = take poMaxRequestsPerBatch xs : go (drop poMaxRequestsPerBatch xs) |
| 166 | + |
| 167 | +submit |
| 168 | + :: (MonadIO m, MonadMask m) |
| 169 | + => ProcessOptions |
| 170 | + -> [RequestRefId] |
| 171 | + -> WithContext |
| 172 | + m |
| 173 | + ( AValidationResult |
| 174 | + TokenUpdateFailure |
| 175 | + () |
| 176 | + ) |
| 177 | +submit ProcessOptions{poWallet, poTokenId} reqIds = do |
| 178 | + void <$> tokenCmdCore (UpdateToken poTokenId poWallet reqIds) |
0 commit comments