mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-24 06:25:55 +00:00
Parallel miner
This commit is contained in:
parent
635b793e14
commit
28c5e50a87
@ -2,7 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
nativeerrors "errors"
|
nativeerrors "errors"
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"runtime"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -42,39 +44,43 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, targetBlocksPerSecond
|
|||||||
templatesLoop(client, miningAddr, errChan)
|
templatesLoop(client, miningAddr, errChan)
|
||||||
})
|
})
|
||||||
|
|
||||||
spawn("blocksLoop", func() {
|
for c := 0; c < (runtime.NumCPU()/2)+1; c++ {
|
||||||
const windowSize = 10
|
c := c
|
||||||
var expectedDurationForWindow time.Duration
|
spawn(fmt.Sprintf("blocksLoop %d", c), func() {
|
||||||
var windowExpectedEndTime time.Time
|
const windowSize = 10
|
||||||
hasBlockRateTarget := targetBlocksPerSecond != 0
|
var expectedDurationForWindow time.Duration
|
||||||
if hasBlockRateTarget {
|
var windowExpectedEndTime time.Time
|
||||||
expectedDurationForWindow = time.Duration(float64(windowSize)/targetBlocksPerSecond) * time.Second
|
hasBlockRateTarget := targetBlocksPerSecond != 0
|
||||||
windowExpectedEndTime = time.Now().Add(expectedDurationForWindow)
|
|
||||||
}
|
|
||||||
blockInWindowIndex := 0
|
|
||||||
|
|
||||||
sleepTime := 0 * time.Second
|
|
||||||
|
|
||||||
for {
|
|
||||||
foundBlockChan <- mineNextBlock(mineWhenNotSynced)
|
|
||||||
|
|
||||||
if hasBlockRateTarget {
|
if hasBlockRateTarget {
|
||||||
blockInWindowIndex++
|
expectedDurationForWindow = time.Duration(float64(windowSize)/targetBlocksPerSecond) * time.Second
|
||||||
if blockInWindowIndex == windowSize-1 {
|
windowExpectedEndTime = time.Now().Add(expectedDurationForWindow)
|
||||||
deviation := windowExpectedEndTime.Sub(time.Now())
|
|
||||||
if deviation > 0 {
|
|
||||||
sleepTime = deviation / windowSize
|
|
||||||
log.Infof("Finished to mine %d blocks %s earlier than expected. Setting the miner "+
|
|
||||||
"to sleep %s between blocks to compensate",
|
|
||||||
windowSize, deviation, sleepTime)
|
|
||||||
}
|
|
||||||
blockInWindowIndex = 0
|
|
||||||
windowExpectedEndTime = time.Now().Add(expectedDurationForWindow)
|
|
||||||
}
|
|
||||||
time.Sleep(sleepTime)
|
|
||||||
}
|
}
|
||||||
}
|
blockInWindowIndex := 0
|
||||||
})
|
|
||||||
|
sleepTime := 0 * time.Second
|
||||||
|
|
||||||
|
for {
|
||||||
|
foundBlockChan <- mineNextBlock(mineWhenNotSynced)
|
||||||
|
|
||||||
|
if hasBlockRateTarget {
|
||||||
|
blockInWindowIndex++
|
||||||
|
if blockInWindowIndex == windowSize-1 {
|
||||||
|
deviation := windowExpectedEndTime.Sub(time.Now())
|
||||||
|
if deviation > 0 {
|
||||||
|
sleepTime = deviation / windowSize
|
||||||
|
log.Infof("cpu: %d Finished to mine %d blocks %s earlier than expected. Setting the miner "+
|
||||||
|
"to sleep %s between blocks to compensate",
|
||||||
|
c, windowSize, deviation, sleepTime)
|
||||||
|
}
|
||||||
|
blockInWindowIndex = 0
|
||||||
|
windowExpectedEndTime = time.Now().Add(expectedDurationForWindow)
|
||||||
|
}
|
||||||
|
time.Sleep(sleepTime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
spawn("handleFoundBlock", func() {
|
spawn("handleFoundBlock", func() {
|
||||||
for i := uint64(0); numberOfBlocks == 0 || i < numberOfBlocks; i++ {
|
for i := uint64(0); numberOfBlocks == 0 || i < numberOfBlocks; i++ {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user