aboutsummaryrefslogtreecommitdiffstats
path: root/dagger.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-01-09 23:19:16 +0800
committerobscuren <geffobscura@gmail.com>2014-01-09 23:19:16 +0800
commitd895f83136eccc7e440792fdb45fd4d40876d82e (patch)
treeac774926bdb194a3571e83ef99b9eaae0f655cd9 /dagger.go
parentd2b3071b4d820b9530b3de891078970ea036767c (diff)
downloadgo-tangerine-d895f83136eccc7e440792fdb45fd4d40876d82e.tar
go-tangerine-d895f83136eccc7e440792fdb45fd4d40876d82e.tar.gz
go-tangerine-d895f83136eccc7e440792fdb45fd4d40876d82e.tar.bz2
go-tangerine-d895f83136eccc7e440792fdb45fd4d40876d82e.tar.lz
go-tangerine-d895f83136eccc7e440792fdb45fd4d40876d82e.tar.xz
go-tangerine-d895f83136eccc7e440792fdb45fd4d40876d82e.tar.zst
go-tangerine-d895f83136eccc7e440792fdb45fd4d40876d82e.zip
Dagger improvements
Diffstat (limited to 'dagger.go')
-rw-r--r--dagger.go52
1 files changed, 40 insertions, 12 deletions
diff --git a/dagger.go b/dagger.go
index b161ef733..9fef78a36 100644
--- a/dagger.go
+++ b/dagger.go
@@ -14,7 +14,33 @@ 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)
@@ -22,23 +48,25 @@ func (dag *Dagger) Search(diff *big.Int) *big.Int {
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 {