diff --git a/README.md b/README.md index bc1e9e4..1a7e580 100644 --- a/README.md +++ b/README.md @@ -60,10 +60,11 @@ Usage: chainweb-mining-client [--info] [--long-info] [-v|--version] [--license] [-c|--thread-count ARG] [--generate-key | --no-generate-key] [-l|--log-level error|warn|info|debug] - [-w|--worker cpu|external|simulation|stratum] + [-w|--worker cpu|external|simulation|stratum|constant-delay] [--external-worker-cmd ARG] [--stratum-port ARG] [--stratum-interface ARG] [--stratum-difficulty ARG] [-s|--stratum-rate ARG] + [--constant-delay-block-time ARG] Kadena Chainweb Mining Client @@ -98,7 +99,7 @@ Available options: -l,--log-level error|warn|info|debug Level at which log messages are written to the console - -w,--worker cpu|external|simulation|stratum + -w,--worker cpu|external|simulation|stratum|constant-delay The type of mining worker that is used --external-worker-cmd ARG command that is used to call an external worker. When @@ -114,6 +115,8 @@ Available options: leading zeros). -s,--stratum-rate ARG Rate (in milliseconds) at which a stratum worker thread emits jobs. + --constant-delay-block-time ARG + time at which a constant-delay worker emits blocks Configurations are loaded in order from the following sources: 1. Configuration files from locations provided through --config-file options diff --git a/chainweb-mining-client.cabal b/chainweb-mining-client.cabal index 3d4fab7..27b7ee4 100644 --- a/chainweb-mining-client.cabal +++ b/chainweb-mining-client.cabal @@ -52,6 +52,7 @@ executable chainweb-mining-client Target Utils Worker + Worker.ConstantDelay Worker.CPU Worker.External Worker.Simulation diff --git a/main/Main.hs b/main/Main.hs index ab12f80..05933a4 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -86,6 +86,7 @@ import Text.Read (Read(..), readListPrecDefault) import Utils import Logger import Worker +import Worker.ConstantDelay import Worker.CPU import Worker.External import Worker.Simulation @@ -203,6 +204,7 @@ data WorkerConfig | ExternalWorker | SimulationWorker | StratumWorker + | ConstantDelayWorker deriving (Show, Eq, Ord, Generic) deriving anyclass (Hashable) @@ -220,6 +222,7 @@ workerConfigToText CpuWorker = "cpu" workerConfigToText ExternalWorker = "external" workerConfigToText SimulationWorker = "simulation" workerConfigToText StratumWorker = "stratum" +workerConfigToText ConstantDelayWorker = "constant-delay" workerConfigFromText :: MonadThrow m => T.Text -> m WorkerConfig workerConfigFromText t = case T.toCaseFold t of @@ -227,6 +230,7 @@ workerConfigFromText t = case T.toCaseFold t of "external" -> return ExternalWorker "simulation" -> return SimulationWorker "stratum" -> return StratumWorker + "constant-delay" -> return ConstantDelayWorker _ -> throwM $ FromTextException $ "unknown worker configuraton: " <> t -- -------------------------------------------------------------------------- -- @@ -252,6 +256,7 @@ data Config = Config , _configStratumInterface :: !HostPreference , _configStratumDifficulty :: !Stratum.StratumDifficulty , _configStratumRate :: !Natural + , _configConstantDelayBlockTime :: !Natural } deriving (Show, Eq, Ord, Generic) @@ -274,6 +279,7 @@ defaultConfig = Config , _configStratumInterface = "*" , _configStratumDifficulty = Stratum.WorkDifficulty , _configStratumRate = 1000 + , _configConstantDelayBlockTime = 30 } instance ToJSON Config where @@ -293,6 +299,7 @@ instance ToJSON Config where , "stratumInterface" .= _configStratumInterface c , "stratumDifficulty" .= _configStratumDifficulty c , "stratumRate" .= _configStratumRate c + , "constantDelayBlockTime" .= _configConstantDelayBlockTime c ] instance FromJSON (Config -> Config) where @@ -312,6 +319,7 @@ instance FromJSON (Config -> Config) where <*< configStratumInterface ..: "stratumInterface" % o <*< configStratumDifficulty ..: "stratumDifficulty" % o <*< configStratumRate ..: "stratumRate" % o + <*< configConstantDelayBlockTime ..: "constantDelayBlockTime" % o where parseLogLevel = withText "LogLevel" $ return . logLevelFromText @@ -358,7 +366,7 @@ parseConfig = id % short 'w' <> long "worker" <> help "The type of mining worker that is used" - <> metavar "cpu|external|simulation|stratum" + <> metavar "cpu|external|simulation|stratum|constant-delay" <*< configExternalWorkerCommand .:: option (textReader $ Right . T.unpack) % long "external-worker-cmd" <> help "command that is used to call an external worker. When the command is called the target value is added as last parameter to the command line." @@ -375,6 +383,9 @@ parseConfig = id % short 's' <> long "stratum-rate" <> help "Rate (in milliseconds) at which a stratum worker thread emits jobs." + <*< configConstantDelayBlockTime .:: option auto + % long "constant-delay-block-time" + <> help "time at which a constant-delay worker emits blocks" -- -------------------------------------------------------------------------- -- -- HTTP Retry Logic @@ -537,8 +548,8 @@ getJob conf ver mgr = do postSolved :: Config -> ChainwebVersion -> Logger -> HTTP.Manager -> Work -> IO () postSolved conf ver logger mgr (Work bytes) = retryHttp logger $ do logg Info "post solved worked" - (void $ HTTP.httpLbs req mgr) - `catch` \(e@(HTTP.HttpExceptionRequest _ _)) -> do + void (HTTP.httpLbs req mgr) + `catch` \e@(HTTP.HttpExceptionRequest _ _) -> do logg Error $ "failed to submit solved work: " <> sshow e return () where @@ -817,6 +828,8 @@ run conf logger = do SimulationWorker -> do rng <- MWC.createSystemRandom f $ \l -> simulationWorker l rng workerRate + ConstantDelayWorker -> do + f $ \l -> constantDelayWorker l (_configConstantDelayBlockTime conf) ExternalWorker -> f $ \l -> externalWorker l (_configExternalWorkerCommand conf) CpuWorker -> f $ cpuWorker @Blake2s_256 StratumWorker -> Stratum.withStratumServer diff --git a/src/Worker/ConstantDelay.hs b/src/Worker/ConstantDelay.hs new file mode 100644 index 0000000..230b73d --- /dev/null +++ b/src/Worker/ConstantDelay.hs @@ -0,0 +1,46 @@ +{-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE NumericUnderscores #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} + +-- | +-- Module: Worker.ConstantDelay +-- Copyright: Copyright © 2022 Kadena LLC. +-- License: MIT +-- Maintainer: Edmund Noble +-- Stability: experimental +-- +-- Simulation Mining Worker +-- +-- A fake mining worker that is not actually doing any work. +-- It returns the work bytes unchanged after a constant delay has passed. +-- +module Worker.ConstantDelay (constantDelayWorker) where + +import Control.Concurrent (threadDelay) + +import qualified Data.Text as T + +import Numeric.Natural + +import System.LogLevel + +-- internal modules + +import Logger +import Worker + +-- -------------------------------------------------------------------------- -- +-- Simulation Mining Worker + +-- | A fake mining worker that is not actually doing any work. It returns +-- the work bytes unchanged after a configured time delay passes. +-- +constantDelayWorker :: Logger -> Natural -> Worker +constantDelayWorker logger delay _nonce _target work = do + logg Info $ "solve time (seconds): " <> T.pack (show delay) + threadDelay ((1_000000 * fromIntegral delay) `div` 20) + return work + where + logg = writeLog logger + diff --git a/src/Worker/Simulation.hs b/src/Worker/Simulation.hs index 8d286fe..e540afb 100644 --- a/src/Worker/Simulation.hs +++ b/src/Worker/Simulation.hs @@ -1,7 +1,6 @@ {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} -{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NumericUnderscores #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-}