mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-07 06:36:46 +00:00
Add TargetBlocksPerSecond for kaspaminer (#1385)
Co-authored-by: Svarog <feanorr@gmail.com>
This commit is contained in:
parent
09e1a73340
commit
0f2d0d45b5
@ -30,12 +30,13 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type configFlags struct {
|
type configFlags struct {
|
||||||
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
|
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
|
||||||
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
|
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
|
||||||
MiningAddr string `long:"miningaddr" description:"Address to mine 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."`
|
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."`
|
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"`
|
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
|
config.NetworkFlags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func main() {
|
|||||||
|
|
||||||
doneChan := make(chan struct{})
|
doneChan := make(chan struct{})
|
||||||
spawn("mineLoop", func() {
|
spawn("mineLoop", func() {
|
||||||
err = mineLoop(client, cfg.NumberOfBlocks, cfg.MineWhenNotSynced, miningAddr)
|
err = mineLoop(client, cfg.NumberOfBlocks, cfg.TargetBlocksPerSecond, cfg.MineWhenNotSynced, miningAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(errors.Wrap(err, "error in mine loop"))
|
panic(errors.Wrap(err, "error in mine loop"))
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ var hashesTried uint64
|
|||||||
|
|
||||||
const logHashRateInterval = 10 * time.Second
|
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 {
|
miningAddr util.Address) error {
|
||||||
|
|
||||||
errChan := make(chan error)
|
errChan := make(chan error)
|
||||||
@ -33,7 +33,18 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, mineWhenNotSynced bool
|
|||||||
|
|
||||||
doneChan := make(chan struct{})
|
doneChan := make(chan struct{})
|
||||||
spawn("mineLoop-internalLoop", func() {
|
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++ {
|
for i := uint64(0); numberOfBlocks == 0 || i < numberOfBlocks; i++ {
|
||||||
|
|
||||||
foundBlock := make(chan *externalapi.DomainBlock)
|
foundBlock := make(chan *externalapi.DomainBlock)
|
||||||
mineNextBlock(client, miningAddr, foundBlock, mineWhenNotSynced, templateStopChan, errChan)
|
mineNextBlock(client, miningAddr, foundBlock, mineWhenNotSynced, templateStopChan, errChan)
|
||||||
block := <-foundBlock
|
block := <-foundBlock
|
||||||
@ -42,6 +53,20 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, mineWhenNotSynced bool
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- err
|
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{}{}
|
doneChan <- struct{}{}
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user