diff options
author | Maran <maran.hidskes@gmail.com> | 2014-03-10 18:53:02 +0800 |
---|---|---|
committer | Maran <maran.hidskes@gmail.com> | 2014-03-10 18:53:02 +0800 |
commit | d5efeab8f92509dec3cafcafb36e1856bb084f12 (patch) | |
tree | 1d455afb8824391da15b2f537f17f87fb1678998 /ethchain/dagger.go | |
parent | be543a6d178063a60d404a373ced296ed6d4744c (diff) | |
download | go-tangerine-d5efeab8f92509dec3cafcafb36e1856bb084f12.tar.gz go-tangerine-d5efeab8f92509dec3cafcafb36e1856bb084f12.tar.zst go-tangerine-d5efeab8f92509dec3cafcafb36e1856bb084f12.zip |
Initial smart-miner stuff
Diffstat (limited to 'ethchain/dagger.go')
-rw-r--r-- | ethchain/dagger.go | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/ethchain/dagger.go b/ethchain/dagger.go index 5b4f8b2cd..c33b3c14e 100644 --- a/ethchain/dagger.go +++ b/ethchain/dagger.go @@ -11,7 +11,7 @@ import ( ) type PoW interface { - Search(block *Block) []byte + Search(block *Block, breakChan chan bool) []byte Verify(hash []byte, diff *big.Int, nonce []byte) bool } @@ -19,15 +19,23 @@ type EasyPow struct { hash *big.Int } -func (pow *EasyPow) Search(block *Block) []byte { +func (pow *EasyPow) Search(block *Block, breakChan chan bool) []byte { r := rand.New(rand.NewSource(time.Now().UnixNano())) - hash := block.HashNoNonce() diff := block.Difficulty + for { - sha := ethutil.Sha3Bin(big.NewInt(r.Int63()).Bytes()) - if pow.Verify(hash, diff, sha) { - return sha + select { + case shouldbreak := <-breakChan: + if shouldbreak { + log.Println("Got signal: Breaking out mining.") + return nil + } + default: + sha := ethutil.Sha3Bin(big.NewInt(r.Int63()).Bytes()) + if pow.Verify(hash, diff, sha) { + return sha + } } } @@ -98,9 +106,9 @@ func (dag *Dagger) Search(hash, diff *big.Int) *big.Int { for k := 0; k < amountOfRoutines; k++ { go dag.Find(obj, resChan) - } - // Wait for each go routine to finish + // 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 { |