Skip to content

Commit

Permalink
Code cleanup and dag generation correct logging
Browse files Browse the repository at this point in the history
  • Loading branch information
wam-rd committed Jan 21, 2024
1 parent 7e8bc86 commit b6f296a
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 507 deletions.
1 change: 1 addition & 0 deletions cmd/karlsenminer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
defaultLogFilename = "karlsenminer.log"
defaultErrLogFilename = "karlsenminer_err.log"
defaultTargetBlockRateRatio = 2.0
hashingAlgoVersion = "fishhash-kls-0.0.1"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion cmd/karlsenminer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {

// Show version at startup.
log.Infof("Version %s", version.Version())
log.Infof("Using KarlsenHash V2")
log.Infof("Using KarlsenHashV2 impl: %s", hashingAlgoVersion)

// Enable http profiling server if requested.
if cfg.Profile != "" {
Expand Down
29 changes: 12 additions & 17 deletions cmd/karlsenminer/mineloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
nativeerrors "errors"
"fmt"
"math/rand"
"sync/atomic"
"time"
Expand All @@ -20,6 +19,7 @@ import (
)

var hashesTried uint64
var dagReady = false

const logHashRateInterval = 10 * time.Second

Expand All @@ -41,7 +41,6 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, targetBlocksPerSecond
})

spawn("blocksLoop", func() {
fmt.Printf("blocksLoop -- log0\n")
const windowSize = 10
hasBlockRateTarget := targetBlocksPerSecond != 0
var windowTicker, blockTicker *time.Ticker
Expand Down Expand Up @@ -74,7 +73,6 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, targetBlocksPerSecond
})

spawn("handleFoundBlock", func() {
fmt.Printf("handleFoundBlock -- log0\n")
for i := uint64(0); numberOfBlocks == 0 || i < numberOfBlocks; i++ {
block := <-foundBlockChan
err := handleFoundBlock(client, block)
Expand All @@ -98,15 +96,13 @@ func mineLoop(client *minerClient, numberOfBlocks uint64, targetBlocksPerSecond

func logHashRate() {
spawn("logHashRate", func() {
fmt.Printf("logHashRate -- log0\n")
//_, state, _ := templatemanager.Get()
//fmt.Printf("logHashRate -- log1\n")
lastCheck := time.Now()
for range time.Tick(logHashRateInterval) {

//fmt.Printf("logHashRate -- log2\n")
//fmt.Printf("logHashRate -- IsContextReady : %t\n", state.IsContextReady())
//fmt.Printf("logHashRate -- IsContextReady : %t\n", state.IsContextReady())
if !dagReady {
log.Infof("Generating DAG, please wait ...")
continue
}

currentHashesTried := atomic.LoadUint64(&hashesTried)
currentTime := time.Now()
Expand Down Expand Up @@ -148,8 +144,10 @@ func handleFoundBlock(client *minerClient, block *externalapi.DomainBlock) error

func mineNextBlock(mineWhenNotSynced bool) *externalapi.DomainBlock {
nonce := rand.Uint64() // Use the global concurrent-safe random source.
//fmt.Printf("mineNextBlock -- log0\n")
for {
if !dagReady {
continue
}
nonce++
//fmt.Printf("mineNextBlock -- log1\n")
// For each nonce we try to build a block from the most up to date
Expand All @@ -176,12 +174,8 @@ func getBlockForMining(mineWhenNotSynced bool) (*externalapi.DomainBlock, *pow.S
const sleepTime = 500 * time.Millisecond
const sleepTimeWhenNotSynced = 5 * time.Second

//fmt.Printf("getBlockForMining -- log0\n")

for {
tryCount++
//fmt.Printf("getBlockForMining -- log1\n")

shouldLog := (tryCount-1)%10 == 0
template, state, isSynced := templatemanager.Get()
if template == nil {
Expand All @@ -206,7 +200,6 @@ func getBlockForMining(mineWhenNotSynced bool) (*externalapi.DomainBlock, *pow.S
func templatesLoop(client *minerClient, miningAddr util.Address, errChan chan error) {
getBlockTemplate := func() {
template, err := client.GetBlockTemplate(miningAddr.String(), "karlsenminer-"+version.Version())
//fmt.Printf("Getting Block template\n")
if nativeerrors.Is(err, router.ErrTimeout) {
log.Warnf("Got timeout while requesting block template from %s: %s", client.Address(), err)
reconnectErr := client.Reconnect()
Expand All @@ -224,8 +217,10 @@ func templatesLoop(client *minerClient, miningAddr util.Address, errChan chan er
errChan <- errors.Wrapf(err, "Error getting block template from %s", client.Address())
return
}
//fmt.Printf("Template SET\n")
err = templatemanager.Set(template)
err = templatemanager.Set(template, backendLog)
// after first template DAG is supposed to be ready
// TODO: refresh dag status in real time
dagReady = true
if err != nil {
errChan <- errors.Wrapf(err, "Error setting block template from %s", client.Address())
return
Expand Down
4 changes: 3 additions & 1 deletion cmd/karlsenminer/templatemanager/templatemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/karlsen-network/karlsend/app/appmessage"
"github.com/karlsen-network/karlsend/domain/consensus/model/externalapi"
"github.com/karlsen-network/karlsend/domain/consensus/utils/pow"
"github.com/karlsen-network/karlsend/infrastructure/logger"
)

var currentTemplate *externalapi.DomainBlock
Expand All @@ -27,14 +28,15 @@ func Get() (*externalapi.DomainBlock, *pow.State, bool) {
}

// Set sets the current template to work on
func Set(template *appmessage.GetBlockTemplateResponseMessage) error {
func Set(template *appmessage.GetBlockTemplateResponseMessage, backendLog *logger.Backend) error {
block, err := appmessage.RPCBlockToDomainBlock(template.Block)
if err != nil {
return err
}
lock.Lock()
defer lock.Unlock()
currentTemplate = block
pow.SetLogger(backendLog, logger.LevelTrace)
currentState = pow.NewState(block.Header.ToMutable(), true)
isSynced = template.IsSynced
return nil
Expand Down
Loading

0 comments on commit b6f296a

Please sign in to comment.