Parallel miner

This commit is contained in:
Kaspa Profiler 2021-04-13 12:48:22 +03:00
parent 635b793e14
commit 28c5e50a87

View File

@ -2,7 +2,9 @@ package main
import (
nativeerrors "errors"
"fmt"
"math/rand"
"runtime"
"sync/atomic"
"time"
@ -42,7 +44,9 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, targetBlocksPerSecond
templatesLoop(client, miningAddr, errChan)
})
spawn("blocksLoop", func() {
for c := 0; c < (runtime.NumCPU()/2)+1; c++ {
c := c
spawn(fmt.Sprintf("blocksLoop %d", c), func() {
const windowSize = 10
var expectedDurationForWindow time.Duration
var windowExpectedEndTime time.Time
@ -64,9 +68,9 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, targetBlocksPerSecond
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 "+
log.Infof("cpu: %d Finished to mine %d blocks %s earlier than expected. Setting the miner "+
"to sleep %s between blocks to compensate",
windowSize, deviation, sleepTime)
c, windowSize, deviation, sleepTime)
}
blockInWindowIndex = 0
windowExpectedEndTime = time.Now().Add(expectedDurationForWindow)
@ -76,6 +80,8 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, targetBlocksPerSecond
}
})
}
spawn("handleFoundBlock", func() {
for i := uint64(0); numberOfBlocks == 0 || i < numberOfBlocks; i++ {
block := <-foundBlockChan