Skip to content

Commit

Permalink
Dagger improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
obscuren committed Jan 9, 2014
1 parent d2b3071 commit d895f83
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
52 changes: 40 additions & 12 deletions dagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,59 @@ type Dagger struct {
xn *big.Int
}

var Found bool
func (dag *Dagger) Find(obj *big.Int, resChan chan int64) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))

for i := 0; i < 1000; i++ {
rnd := r.Int63()

if dag.Eval(big.NewInt(rnd)).Cmp(obj) < 0 {
// Post back result on the channel
resChan <- rnd
// Notify other threads we've found a valid nonce
Found = true
} else {
fmt.Printf(".")
}

// Break out if found
if Found { break }
}

resChan <- 0
}

func (dag *Dagger) Search(diff *big.Int) *big.Int {
// TODO fix multi threading. Somehow it results in the wrong nonce
amountOfRoutines := 1

dag.hash = big.NewInt(0)

obj := BigPow(2, 256)
obj = obj.Div(obj, diff)

fmt.Println("diff", diff, "< objective", obj)

r := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd := big.NewInt(r.Int63())
fmt.Println("init rnd =", rnd)
Found = false
resChan := make(chan int64, 3)
var res int64

for i := 0; i < 1000; i++ {
if dag.Eval(rnd).Cmp(obj) < 0 {
fmt.Println("Found result! nonce = ", rnd)
for k := 0; k < amountOfRoutines; k++ {
go dag.Find(obj, resChan)
}

return rnd
} else {
fmt.Println("Not found :( nonce = ", rnd)
// Wait for each go routine to finish
for k := 0; k < amountOfRoutines; k++ {
// Get the result from the channel. 0 = quit
if r := <- resChan; r != 0 {
res = r
}

rnd = rnd.Add(rnd, big.NewInt(1))
}

return big.NewInt(0)
fmt.Println("\n")

return big.NewInt(res)
}

func DaggerVerify(hash, diff, nonce *big.Int) bool {
Expand Down
17 changes: 17 additions & 0 deletions dagger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"testing"
"math/big"
)

func BenchmarkDaggerSearch(b *testing.B) {
hash := big.NewInt(0)
diff := BigPow(2, 36)
o := big.NewInt(0) // nonce doesn't matter. We're only testing against speed, not validity

// Reset timer so the big generation isn't included in the benchmark
b.ResetTimer()
// Validate
DaggerVerify(hash, diff, o)
}
13 changes: 7 additions & 6 deletions ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

const Debug = true

var StartDBQueryInterface bool
var StartConsole bool
var StartMining bool
func Init() {
flag.BoolVar(&StartDBQueryInterface, "c", false, "console interface")
flag.BoolVar(&StartConsole, "c", false, "debug and testing console")
flag.BoolVar(&StartMining, "mine", false, "start dagger mining")

flag.Parse()
Expand Down Expand Up @@ -42,12 +42,13 @@ func main() {

Init()

if StartDBQueryInterface {
dbInterface := NewDBInterface()
dbInterface.Start()
if StartConsole {
console := NewConsole()
console.Start()
} else if StartMining {
dagger := &Dagger{}
dagger.Search(BigPow(2, 36))
res := dagger.Search(BigPow(2, 36))
fmt.Println("nonce =", res)
} else {
fmt.Println("[DBUG]: Starting Ethereum")
server, err := NewServer()
Expand Down
3 changes: 2 additions & 1 deletion vm_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package main

/*
import (
_"fmt"
"testing"
Expand Down Expand Up @@ -72,4 +73,4 @@ func TestVm(t *testing.T) {
bm := NewBlockManager()
bm.ProcessBlock( block )
}

*/

0 comments on commit d895f83

Please sign in to comment.