Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit c6a7509

Browse files
authored
Add constant-delay worker (#22)
* Add constant-delay worker * Fix docs * Less delay to compensate for 20-chains * Update README and change block-delay flag name
1 parent 9ecabec commit c6a7509

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ Usage: chainweb-mining-client [--info] [--long-info] [-v|--version] [--license]
6060
[-c|--thread-count ARG]
6161
[--generate-key | --no-generate-key]
6262
[-l|--log-level error|warn|info|debug]
63-
[-w|--worker cpu|external|simulation|stratum]
63+
[-w|--worker cpu|external|simulation|stratum|constant-delay]
6464
[--external-worker-cmd ARG] [--stratum-port ARG]
6565
[--stratum-interface ARG]
6666
[--stratum-difficulty ARG] [-s|--stratum-rate ARG]
67+
[--constant-delay-block-time ARG]
6768
6869
Kadena Chainweb Mining Client
6970
@@ -98,7 +99,7 @@ Available options:
9899
-l,--log-level error|warn|info|debug
99100
Level at which log messages are written to the
100101
console
101-
-w,--worker cpu|external|simulation|stratum
102+
-w,--worker cpu|external|simulation|stratum|constant-delay
102103
The type of mining worker that is used
103104
--external-worker-cmd ARG
104105
command that is used to call an external worker. When
@@ -114,6 +115,8 @@ Available options:
114115
leading zeros).
115116
-s,--stratum-rate ARG Rate (in milliseconds) at which a stratum worker
116117
thread emits jobs.
118+
--constant-delay-block-time ARG
119+
time at which a constant-delay worker emits blocks
117120
118121
Configurations are loaded in order from the following sources:
119122
1. Configuration files from locations provided through --config-file options

chainweb-mining-client.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ executable chainweb-mining-client
5252
Target
5353
Utils
5454
Worker
55+
Worker.ConstantDelay
5556
Worker.CPU
5657
Worker.External
5758
Worker.Simulation

main/Main.hs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import Text.Read (Read(..), readListPrecDefault)
8686
import Utils
8787
import Logger
8888
import Worker
89+
import Worker.ConstantDelay
8990
import Worker.CPU
9091
import Worker.External
9192
import Worker.Simulation
@@ -203,6 +204,7 @@ data WorkerConfig
203204
| ExternalWorker
204205
| SimulationWorker
205206
| StratumWorker
207+
| ConstantDelayWorker
206208
deriving (Show, Eq, Ord, Generic)
207209
deriving anyclass (Hashable)
208210

@@ -220,13 +222,15 @@ workerConfigToText CpuWorker = "cpu"
220222
workerConfigToText ExternalWorker = "external"
221223
workerConfigToText SimulationWorker = "simulation"
222224
workerConfigToText StratumWorker = "stratum"
225+
workerConfigToText ConstantDelayWorker = "constant-delay"
223226

224227
workerConfigFromText :: MonadThrow m => T.Text -> m WorkerConfig
225228
workerConfigFromText t = case T.toCaseFold t of
226229
"cpu" -> return CpuWorker
227230
"external" -> return ExternalWorker
228231
"simulation" -> return SimulationWorker
229232
"stratum" -> return StratumWorker
233+
"constant-delay" -> return ConstantDelayWorker
230234
_ -> throwM $ FromTextException $ "unknown worker configuraton: " <> t
231235

232236
-- -------------------------------------------------------------------------- --
@@ -252,6 +256,7 @@ data Config = Config
252256
, _configStratumInterface :: !HostPreference
253257
, _configStratumDifficulty :: !Stratum.StratumDifficulty
254258
, _configStratumRate :: !Natural
259+
, _configConstantDelayBlockTime :: !Natural
255260
}
256261
deriving (Show, Eq, Ord, Generic)
257262

@@ -274,6 +279,7 @@ defaultConfig = Config
274279
, _configStratumInterface = "*"
275280
, _configStratumDifficulty = Stratum.WorkDifficulty
276281
, _configStratumRate = 1000
282+
, _configConstantDelayBlockTime = 30
277283
}
278284

279285
instance ToJSON Config where
@@ -293,6 +299,7 @@ instance ToJSON Config where
293299
, "stratumInterface" .= _configStratumInterface c
294300
, "stratumDifficulty" .= _configStratumDifficulty c
295301
, "stratumRate" .= _configStratumRate c
302+
, "constantDelayBlockTime" .= _configConstantDelayBlockTime c
296303
]
297304

298305
instance FromJSON (Config -> Config) where
@@ -312,6 +319,7 @@ instance FromJSON (Config -> Config) where
312319
<*< configStratumInterface ..: "stratumInterface" % o
313320
<*< configStratumDifficulty ..: "stratumDifficulty" % o
314321
<*< configStratumRate ..: "stratumRate" % o
322+
<*< configConstantDelayBlockTime ..: "constantDelayBlockTime" % o
315323
where
316324
parseLogLevel = withText "LogLevel" $ return . logLevelFromText
317325

@@ -358,7 +366,7 @@ parseConfig = id
358366
% short 'w'
359367
<> long "worker"
360368
<> help "The type of mining worker that is used"
361-
<> metavar "cpu|external|simulation|stratum"
369+
<> metavar "cpu|external|simulation|stratum|constant-delay"
362370
<*< configExternalWorkerCommand .:: option (textReader $ Right . T.unpack)
363371
% long "external-worker-cmd"
364372
<> 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
375383
% short 's'
376384
<> long "stratum-rate"
377385
<> help "Rate (in milliseconds) at which a stratum worker thread emits jobs."
386+
<*< configConstantDelayBlockTime .:: option auto
387+
% long "constant-delay-block-time"
388+
<> help "time at which a constant-delay worker emits blocks"
378389

379390
-- -------------------------------------------------------------------------- --
380391
-- HTTP Retry Logic
@@ -537,8 +548,8 @@ getJob conf ver mgr = do
537548
postSolved :: Config -> ChainwebVersion -> Logger -> HTTP.Manager -> Work -> IO ()
538549
postSolved conf ver logger mgr (Work bytes) = retryHttp logger $ do
539550
logg Info "post solved worked"
540-
(void $ HTTP.httpLbs req mgr)
541-
`catch` \(e@(HTTP.HttpExceptionRequest _ _)) -> do
551+
void (HTTP.httpLbs req mgr)
552+
`catch` \e@(HTTP.HttpExceptionRequest _ _) -> do
542553
logg Error $ "failed to submit solved work: " <> sshow e
543554
return ()
544555
where
@@ -817,6 +828,8 @@ run conf logger = do
817828
SimulationWorker -> do
818829
rng <- MWC.createSystemRandom
819830
f $ \l -> simulationWorker l rng workerRate
831+
ConstantDelayWorker -> do
832+
f $ \l -> constantDelayWorker l (_configConstantDelayBlockTime conf)
820833
ExternalWorker -> f $ \l -> externalWorker l (_configExternalWorkerCommand conf)
821834
CpuWorker -> f $ cpuWorker @Blake2s_256
822835
StratumWorker -> Stratum.withStratumServer

src/Worker/ConstantDelay.hs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{-# LANGUAGE DerivingStrategies #-}
2+
{-# LANGUAGE NumericUnderscores #-}
3+
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
5+
6+
-- |
7+
-- Module: Worker.ConstantDelay
8+
-- Copyright: Copyright © 2022 Kadena LLC.
9+
-- License: MIT
10+
-- Maintainer: Edmund Noble <edmund@kadena.io>
11+
-- Stability: experimental
12+
--
13+
-- Simulation Mining Worker
14+
--
15+
-- A fake mining worker that is not actually doing any work.
16+
-- It returns the work bytes unchanged after a constant delay has passed.
17+
--
18+
module Worker.ConstantDelay (constantDelayWorker) where
19+
20+
import Control.Concurrent (threadDelay)
21+
22+
import qualified Data.Text as T
23+
24+
import Numeric.Natural
25+
26+
import System.LogLevel
27+
28+
-- internal modules
29+
30+
import Logger
31+
import Worker
32+
33+
-- -------------------------------------------------------------------------- --
34+
-- Simulation Mining Worker
35+
36+
-- | A fake mining worker that is not actually doing any work. It returns
37+
-- the work bytes unchanged after a configured time delay passes.
38+
--
39+
constantDelayWorker :: Logger -> Natural -> Worker
40+
constantDelayWorker logger delay _nonce _target work = do
41+
logg Info $ "solve time (seconds): " <> T.pack (show delay)
42+
threadDelay ((1_000000 * fromIntegral delay) `div` 20)
43+
return work
44+
where
45+
logg = writeLog logger
46+

src/Worker/Simulation.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{-# LANGUAGE DeriveGeneric #-}
22
{-# LANGUAGE DerivingStrategies #-}
33
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4-
{-# LANGUAGE LambdaCase #-}
54
{-# LANGUAGE NumericUnderscores #-}
65
{-# LANGUAGE OverloadedStrings #-}
76
{-# LANGUAGE ScopedTypeVariables #-}

0 commit comments

Comments
 (0)