Skip to content

Commit

Permalink
Add constant-delay worker (#22)
Browse files Browse the repository at this point in the history
* Add constant-delay worker

* Fix docs

* Less delay to compensate for 20-chains

* Update README and change block-delay flag name
  • Loading branch information
edmundnoble authored Dec 14, 2022
1 parent 9ecabec commit c6a7509
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions chainweb-mining-client.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ executable chainweb-mining-client
Target
Utils
Worker
Worker.ConstantDelay
Worker.CPU
Worker.External
Worker.Simulation
Expand Down
19 changes: 16 additions & 3 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -203,6 +204,7 @@ data WorkerConfig
| ExternalWorker
| SimulationWorker
| StratumWorker
| ConstantDelayWorker
deriving (Show, Eq, Ord, Generic)
deriving anyclass (Hashable)

Expand All @@ -220,13 +222,15 @@ 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
"cpu" -> return CpuWorker
"external" -> return ExternalWorker
"simulation" -> return SimulationWorker
"stratum" -> return StratumWorker
"constant-delay" -> return ConstantDelayWorker
_ -> throwM $ FromTextException $ "unknown worker configuraton: " <> t

-- -------------------------------------------------------------------------- --
Expand All @@ -252,6 +256,7 @@ data Config = Config
, _configStratumInterface :: !HostPreference
, _configStratumDifficulty :: !Stratum.StratumDifficulty
, _configStratumRate :: !Natural
, _configConstantDelayBlockTime :: !Natural
}
deriving (Show, Eq, Ord, Generic)

Expand All @@ -274,6 +279,7 @@ defaultConfig = Config
, _configStratumInterface = "*"
, _configStratumDifficulty = Stratum.WorkDifficulty
, _configStratumRate = 1000
, _configConstantDelayBlockTime = 30
}

instance ToJSON Config where
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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."
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions src/Worker/ConstantDelay.hs
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
-- 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

1 change: 0 additions & 1 deletion src/Worker/Simulation.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand Down

0 comments on commit c6a7509

Please sign in to comment.