Dagger improvements
This commit is contained in:
parent
d2b3071b4d
commit
d895f83136
56
dagger.go
56
dagger.go
@ -14,7 +14,33 @@ type Dagger struct {
|
|||||||
xn *big.Int
|
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 {
|
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)
|
dag.hash = big.NewInt(0)
|
||||||
|
|
||||||
obj := BigPow(2, 256)
|
obj := BigPow(2, 256)
|
||||||
@ -22,23 +48,25 @@ func (dag *Dagger) Search(diff *big.Int) *big.Int {
|
|||||||
|
|
||||||
fmt.Println("diff", diff, "< objective", obj)
|
fmt.Println("diff", diff, "< objective", obj)
|
||||||
|
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
Found = false
|
||||||
rnd := big.NewInt(r.Int63())
|
resChan := make(chan int64, 3)
|
||||||
fmt.Println("init rnd =", rnd)
|
var res int64
|
||||||
|
|
||||||
for i := 0; i < 1000; i++ {
|
for k := 0; k < amountOfRoutines; k++ {
|
||||||
if dag.Eval(rnd).Cmp(obj) < 0 {
|
go dag.Find(obj, resChan)
|
||||||
fmt.Println("Found result! nonce = ", rnd)
|
|
||||||
|
|
||||||
return rnd
|
|
||||||
} else {
|
|
||||||
fmt.Println("Not found :( nonce = ", rnd)
|
|
||||||
}
|
|
||||||
|
|
||||||
rnd = rnd.Add(rnd, big.NewInt(1))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return big.NewInt(0)
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("\n")
|
||||||
|
|
||||||
|
return big.NewInt(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DaggerVerify(hash, diff, nonce *big.Int) bool {
|
func DaggerVerify(hash, diff, nonce *big.Int) bool {
|
||||||
|
17
dagger_test.go
Normal file
17
dagger_test.go
Normal file
@ -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
ethereum.go
13
ethereum.go
@ -11,10 +11,10 @@ import (
|
|||||||
|
|
||||||
const Debug = true
|
const Debug = true
|
||||||
|
|
||||||
var StartDBQueryInterface bool
|
var StartConsole bool
|
||||||
var StartMining bool
|
var StartMining bool
|
||||||
func Init() {
|
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.BoolVar(&StartMining, "mine", false, "start dagger mining")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@ -42,12 +42,13 @@ func main() {
|
|||||||
|
|
||||||
Init()
|
Init()
|
||||||
|
|
||||||
if StartDBQueryInterface {
|
if StartConsole {
|
||||||
dbInterface := NewDBInterface()
|
console := NewConsole()
|
||||||
dbInterface.Start()
|
console.Start()
|
||||||
} else if StartMining {
|
} else if StartMining {
|
||||||
dagger := &Dagger{}
|
dagger := &Dagger{}
|
||||||
dagger.Search(BigPow(2, 36))
|
res := dagger.Search(BigPow(2, 36))
|
||||||
|
fmt.Println("nonce =", res)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("[DBUG]: Starting Ethereum")
|
fmt.Println("[DBUG]: Starting Ethereum")
|
||||||
server, err := NewServer()
|
server, err := NewServer()
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
/*
|
||||||
import (
|
import (
|
||||||
_"fmt"
|
_"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
@ -72,4 +73,4 @@ func TestVm(t *testing.T) {
|
|||||||
bm := NewBlockManager()
|
bm := NewBlockManager()
|
||||||
bm.ProcessBlock( block )
|
bm.ProcessBlock( block )
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user