From 0f2d0d45b51ec2b61978641d775b4f2863670873 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Sun, 10 Jan 2021 12:44:24 +0200 Subject: [PATCH] Add TargetBlocksPerSecond for kaspaminer (#1385) Co-authored-by: Svarog --- cmd/kaspaminer/config.go | 13 +++++++------ cmd/kaspaminer/main.go | 2 +- cmd/kaspaminer/mineloop.go | 27 ++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cmd/kaspaminer/config.go b/cmd/kaspaminer/config.go index 243dbf22e..68423b5ac 100644 --- a/cmd/kaspaminer/config.go +++ b/cmd/kaspaminer/config.go @@ -30,12 +30,13 @@ var ( ) type configFlags struct { - ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` - RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"` - MiningAddr string `long:"miningaddr" description:"Address to mine to"` - NumberOfBlocks uint64 `short:"n" long:"numblocks" description:"Number of blocks to mine. If omitted, will mine until the process is interrupted."` - MineWhenNotSynced bool `long:"mine-when-not-synced" description:"Mine even if the node is not synced with the rest of the network."` - Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` + ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` + RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"` + MiningAddr string `long:"miningaddr" description:"Address to mine to"` + NumberOfBlocks uint64 `short:"n" long:"numblocks" description:"Number of blocks to mine. If omitted, will mine until the process is interrupted."` + MineWhenNotSynced bool `long:"mine-when-not-synced" description:"Mine even if the node is not synced with the rest of the network."` + Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` + TargetBlocksPerSecond float64 `long:"target-blocks-per-second" description:"Sets a maximum block rate. This flag is for debugging purposes."` config.NetworkFlags } diff --git a/cmd/kaspaminer/main.go b/cmd/kaspaminer/main.go index a4a85fa7a..27729b511 100644 --- a/cmd/kaspaminer/main.go +++ b/cmd/kaspaminer/main.go @@ -48,7 +48,7 @@ func main() { doneChan := make(chan struct{}) spawn("mineLoop", func() { - err = mineLoop(client, cfg.NumberOfBlocks, cfg.MineWhenNotSynced, miningAddr) + err = mineLoop(client, cfg.NumberOfBlocks, cfg.TargetBlocksPerSecond, cfg.MineWhenNotSynced, miningAddr) if err != nil { panic(errors.Wrap(err, "error in mine loop")) } diff --git a/cmd/kaspaminer/mineloop.go b/cmd/kaspaminer/mineloop.go index 2538769e5..97c0cf0d0 100644 --- a/cmd/kaspaminer/mineloop.go +++ b/cmd/kaspaminer/mineloop.go @@ -24,7 +24,7 @@ var hashesTried uint64 const logHashRateInterval = 10 * time.Second -func mineLoop(client *minerClient, numberOfBlocks uint64, mineWhenNotSynced bool, +func mineLoop(client *minerClient, numberOfBlocks uint64, targetBlocksPerSecond float64, mineWhenNotSynced bool, miningAddr util.Address) error { errChan := make(chan error) @@ -33,7 +33,18 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, mineWhenNotSynced bool doneChan := make(chan struct{}) spawn("mineLoop-internalLoop", func() { + const windowSize = 10 + var expectedDurationForWindow time.Duration + var windowExpectedEndTime time.Time + hasBlockRateTarget := targetBlocksPerSecond != 0 + if hasBlockRateTarget { + expectedDurationForWindow = time.Duration(float64(windowSize)/targetBlocksPerSecond) * time.Second + windowExpectedEndTime = time.Now().Add(expectedDurationForWindow) + } + blockInWindowIndex := 0 + for i := uint64(0); numberOfBlocks == 0 || i < numberOfBlocks; i++ { + foundBlock := make(chan *externalapi.DomainBlock) mineNextBlock(client, miningAddr, foundBlock, mineWhenNotSynced, templateStopChan, errChan) block := <-foundBlock @@ -42,6 +53,20 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, mineWhenNotSynced bool if err != nil { errChan <- err } + + if hasBlockRateTarget { + blockInWindowIndex++ + if blockInWindowIndex == windowSize-1 { + deviation := windowExpectedEndTime.Sub(time.Now()) + if deviation > 0 { + log.Infof("Finished to mine %d blocks %s earlier than expected. Sleeping %s to compensate", + windowSize, deviation, deviation) + time.Sleep(deviation) + } + blockInWindowIndex = 0 + } + } + } doneChan <- struct{}{} })