Skip to content

Commit b1cc9cd

Browse files
author
Gustav Simonsson
committed
Integrate new ethash API and change geth makedag cmd
1 parent 50659f4 commit b1cc9cd

File tree

9 files changed

+41
-27
lines changed

9 files changed

+41
-27
lines changed

cmd/geth/main.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
"io/ioutil"
2828
"os"
2929
"path"
30+
"path/filepath"
3031
"runtime"
3132
"strconv"
33+
"strings"
3234
"time"
3335

3436
"github.com/codegangsta/cli"
@@ -601,12 +603,32 @@ func dump(ctx *cli.Context) {
601603
}
602604

603605
func makedag(ctx *cli.Context) {
604-
chain, _, _ := utils.GetChain(ctx)
605-
pow := ethash.New(chain)
606-
fmt.Println("making cache")
607-
pow.UpdateCache(0, true)
608-
fmt.Println("making DAG")
609-
pow.UpdateDAG()
606+
args := ctx.Args()
607+
wrongArgs := func() {
608+
utils.Fatalf(`Usage: geth makedag <block number> <outputdir>`)
609+
}
610+
switch {
611+
case len(args) == 2:
612+
blockNum, err := strconv.ParseUint(args[0], 0, 64)
613+
dir := args[1]
614+
if err != nil {
615+
wrongArgs()
616+
} else {
617+
dir = filepath.Clean(dir)
618+
// seems to require a trailing slash
619+
if !strings.HasSuffix(dir, "/") {
620+
dir = dir + "/"
621+
}
622+
_, err = ioutil.ReadDir(dir)
623+
if err != nil {
624+
utils.Fatalf("Can't find dir")
625+
}
626+
fmt.Println("making DAG, this could take awhile...")
627+
ethash.MakeDAG(blockNum, dir)
628+
}
629+
default:
630+
wrongArgs()
631+
}
610632
}
611633

612634
func version(c *cli.Context) {

cmd/utils/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func GetChain(ctx *cli.Context) (*core.ChainManager, common.Database, common.Dat
316316

317317
eventMux := new(event.TypeMux)
318318
chainManager := core.NewChainManager(blockDb, stateDb, eventMux)
319-
pow := ethash.New(chainManager)
319+
pow := ethash.New()
320320
txPool := core.NewTxPool(eventMux, chainManager.State, chainManager.GasLimit)
321321
blockProcessor := core.NewBlockProcessor(stateDb, extraDb, pow, txPool, chainManager, eventMux)
322322
chainManager.SetProcessor(blockProcessor)

core/chain_makers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
// So we can generate blocks easily
1515
type FakePow struct{}
1616

17-
func (f FakePow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte, []byte) {
18-
return 0, nil, nil
17+
func (f FakePow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte) {
18+
return 0, nil
1919
}
2020
func (f FakePow) Verify(block pow.Block) bool { return true }
2121
func (f FakePow) GetHashrate() int64 { return 0 }

eth/backend.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func New(config *Config) (*Ethereum, error) {
220220

221221
eth.chainManager = core.NewChainManager(blockDb, stateDb, eth.EventMux())
222222
eth.downloader = downloader.New(eth.chainManager.HasBlock, eth.chainManager.GetBlock)
223-
eth.pow = ethash.New(eth.chainManager)
223+
eth.pow = ethash.New()
224224
eth.txPool = core.NewTxPool(eth.EventMux(), eth.chainManager.State, eth.chainManager.GasLimit)
225225
eth.blockProcessor = core.NewBlockProcessor(stateDb, extraDb, eth.pow, eth.txPool, eth.chainManager, eth.EventMux())
226226
eth.chainManager.SetProcessor(eth.blockProcessor)
@@ -318,7 +318,6 @@ func (s *Ethereum) PeersInfo() (peersinfo []*PeerInfo) {
318318

319319
func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) {
320320
s.chainManager.ResetWithGenesisBlock(gb)
321-
s.pow.UpdateCache(0, true)
322321
}
323322

324323
func (s *Ethereum) StartMining() error {

miner/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (self *CpuMiner) mine(block *types.Block) {
8585
self.chMu.Unlock()
8686

8787
// Mine
88-
nonce, mixDigest, _ := self.pow.Search(block, self.quitCurrentOp)
88+
nonce, mixDigest := self.pow.Search(block, self.quitCurrentOp)
8989
if nonce != 0 {
9090
block.SetNonce(nonce)
9191
block.Header().MixDigest = common.BytesToHash(mixDigest)

miner/miner.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package miner
33
import (
44
"math/big"
55

6-
"github.com/ethereum/ethash"
76
"github.com/ethereum/go-ethereum/common"
87
"github.com/ethereum/go-ethereum/core"
98
"github.com/ethereum/go-ethereum/core/state"
@@ -41,13 +40,7 @@ func (self *Miner) Mining() bool {
4140
func (self *Miner) Start(coinbase common.Address) {
4241
self.mining = true
4342
self.worker.coinbase = coinbase
44-
45-
if self.threads > 0 {
46-
self.pow.(*ethash.Ethash).UpdateDAG()
47-
}
48-
4943
self.worker.start()
50-
5144
self.worker.commitNewWork()
5245
}
5346

pow/dagger/dagger.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"math/rand"
77
"time"
88

9-
"github.com/ethereum/go-ethereum/crypto/sha3"
109
"github.com/ethereum/go-ethereum/common"
10+
"github.com/ethereum/go-ethereum/crypto/sha3"
1111
"github.com/ethereum/go-ethereum/logger"
1212
)
1313

@@ -44,7 +44,7 @@ func (dag *Dagger) Find(obj *big.Int, resChan chan int64) {
4444
resChan <- 0
4545
}
4646

47-
func (dag *Dagger) Search(hash, diff *big.Int) ([]byte, []byte, []byte) {
47+
func (dag *Dagger) Search(hash, diff *big.Int) (uint64, []byte) {
4848
// TODO fix multi threading. Somehow it results in the wrong nonce
4949
amountOfRoutines := 1
5050

@@ -69,7 +69,7 @@ func (dag *Dagger) Search(hash, diff *big.Int) ([]byte, []byte, []byte) {
6969
}
7070
}
7171

72-
return big.NewInt(res).Bytes(), nil, nil
72+
return uint64(res), nil
7373
}
7474

7575
func (dag *Dagger) Verify(hash, diff, nonce *big.Int) bool {

pow/ezp/pow.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (pow *EasyPow) Turbo(on bool) {
3232
pow.turbo = on
3333
}
3434

35-
func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte, []byte) {
35+
func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte) {
3636
r := rand.New(rand.NewSource(time.Now().UnixNano()))
3737
hash := block.HashNoNonce()
3838
diff := block.Difficulty()
@@ -57,7 +57,7 @@ empty:
5757
for {
5858
select {
5959
case <-stop:
60-
return 0, nil, nil
60+
return 0, nil
6161
default:
6262
i++
6363

@@ -67,7 +67,7 @@ empty:
6767

6868
sha := uint64(r.Int63())
6969
if verify(hash, diff, sha) {
70-
return sha, nil, nil
70+
return sha, nil
7171
}
7272
}
7373

@@ -76,7 +76,7 @@ empty:
7676
}
7777
}
7878

79-
return 0, nil, nil
79+
return 0, nil
8080
}
8181

8282
func (pow *EasyPow) Verify(block pow.Block) bool {

pow/pow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package pow
22

33
type PoW interface {
4-
Search(block Block, stop <-chan struct{}) (uint64, []byte, []byte)
4+
Search(block Block, stop <-chan struct{}) (uint64, []byte)
55
Verify(block Block) bool
66
GetHashrate() int64
77
Turbo(bool)

0 commit comments

Comments
 (0)