worker: release ethash

parent af3767e5
Pipeline #17917581 passed with stages
in 7 minutes and 23 seconds
......@@ -5,10 +5,8 @@
[![Build status](https://ci.appveyor.com/api/projects/status/6bl4w08cpa6163kx?svg=true)](https://ci.appveyor.com/project/JakobGillich/blockforge)
[![Snap Status](https://build.snapcraft.io/badge/jgillich/blockforge.svg)](https://build.snapcraft.io/user/jgillich/blockforge)
BlockForge is a next generation miner for cryptocurrencies.
Easy to use, multi algo and open source.
Current state: Under development.
BlockForge is a next generation miner for cryptocurrencies. Easy to use, multi algo and open source.
Alpha quality software under heavy development, use with caution.
## Usage
......
......@@ -40,11 +40,13 @@ func DiffToTarget(diff float32) *big.Int {
binary.LittleEndian.PutUint64(t[k*4:], m)
}
reverse := make([]byte, 32)
for i := 0; i < 32; i++ {
reverse[31-i] = t[i]
// reverse
for i := 0; i < len(t)/2; i++ {
j := len(t) - i - 1
t[i], t[j] = t[j], t[i]
}
return new(big.Int).SetBytes(reverse)
return new(big.Int).SetBytes(t)
}
func (work *Work) Verify(hash *Ethash, nonce uint64) (bool, error) {
......@@ -71,7 +73,8 @@ func (work *Work) VerifySend(hash *Ethash, nonce uint64, results chan<- Share) (
}
func (work *Work) VerifyRange(hash *Ethash, start uint64, size uint64, results chan<- Share) error {
for i := start + work.ExtraNonce; i < start+size+work.ExtraNonce; i++ {
end := start + size + work.ExtraNonce
for i := start + work.ExtraNonce; i < end; i++ {
if _, err := work.VerifySend(hash, i, results); err != nil {
return err
}
......
......@@ -244,6 +244,8 @@ func (stratum *Ethash) loop() {
continue
}
log.Infof("job difficulty %v", params[0])
stratum.target = ethash.DiffToTarget(params[0])
case "mining.set_extranonce":
var params []string
......@@ -303,7 +305,7 @@ func (stratum *Ethash) submit(share ethash.Share) {
stratum.protoErr(err)
return
}
log.Infof("submitting share %+v", params)
log.Infof("submitting share %v", string(params))
stratum.conn.putMessage(&message{
Id: 1,
Method: "mining.submit",
......
......@@ -8,7 +8,6 @@ import (
"math/big"
"math/rand"
"strings"
"sync"
"time"
metrics "github.com/armon/go-metrics"
......@@ -28,7 +27,6 @@ type Ethash struct {
hash *ethash.Ethash
seedhash string
lock sync.RWMutex
metrics *metrics.Metrics
}
......@@ -60,6 +58,8 @@ func (worker *Ethash) Start() error {
defer close(workChannels[index])
}
var hash *ethash.Ethash
for work := range worker.Work {
if worker.seedhash != work.Seedhash {
worker.seedhash = work.Seedhash
......@@ -72,12 +72,13 @@ func (worker *Ethash) Start() error {
for i := 0; i < totalThreads; i++ {
close(workChannels[i])
workChannels[i] = make(chan *ethash.Work, 1)
if hash != nil {
hash.Release()
}
}
log.Info("DAG is being initialized, this may take a while")
worker.lock.Lock()
worker.hash, err = ethash.NewEthash(seedhash)
worker.lock.Unlock()
hash, err = ethash.NewEthash(seedhash)
if err != nil {
return err
}
......@@ -86,7 +87,7 @@ func (worker *Ethash) Start() error {
for cpuIndex, conf := range worker.config.Processors {
for i := 0; i < conf.Threads; i++ {
key := []string{"cpu", fmt.Sprintf("%v", cpuIndex), fmt.Sprintf("%v", i)}
go worker.thread(key, workChannels[len(worker.config.CLDevices)+i])
go worker.thread(key, hash, workChannels[len(worker.config.CLDevices)+i])
}
}
......@@ -111,25 +112,28 @@ func (worker *Ethash) Start() error {
return nil
}
func (worker *Ethash) thread(key []string, workChan chan *ethash.Work) {
func (worker *Ethash) thread(key []string, hash *ethash.Ethash, workChan chan *ethash.Work) {
work := <-workChan
var ok bool
nonce := uint64(worker.rand.Uint32())
stepping := uint64(128 * 1024)
for {
select {
case work, ok = <-workChan:
if !ok {
return
}
nonce = uint64(worker.rand.Uint32())
default:
start := time.Now()
worker.lock.RLock()
if err := work.VerifyRange(worker.hash, uint64(worker.rand.Uint32()), 10*1024, worker.Shares); err != nil {
if err := work.VerifyRange(hash, nonce, stepping, worker.Shares); err != nil {
workerError(err)
}
worker.lock.RUnlock()
worker.metrics.IncrCounter(key, float32(10*1024/time.Since(start).Seconds()))
nonce += stepping
worker.metrics.IncrCounter(key, float32(float64(stepping)/time.Since(start).Seconds()))
}
}
}
......@@ -157,7 +161,6 @@ func (worker *Ethash) clThread(key []string, cl *ethashCL, workChan chan *ethash
default:
start := time.Now()
worker.lock.RLock()
startNonce := uint64(worker.rand.Uint32())
if err := cl.Run(work.ExtraNonce+startNonce, results); err != nil {
workerError(err)
......@@ -168,7 +171,6 @@ func (worker *Ethash) clThread(key []string, cl *ethashCL, workChan chan *ethash
Nonce: startNonce + uint64(results[1]),
}
}
worker.lock.RUnlock()
worker.metrics.IncrCounter(key, float32(10*1024/time.Since(start).Seconds()))
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment