diff options
author | obscuren <geffobscura@gmail.com> | 2015-02-21 01:13:46 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-02-21 01:13:46 +0800 |
commit | bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca (patch) | |
tree | 46ab5943fd5e26198067aeec4a44287452eb2a32 /pow/ezp/pow.go | |
parent | 771bfe9e78f9952002a71cccc8d41c8c544fdfcb (diff) | |
parent | d586a633ff005ac01c9f1eb33552d147cf6c883e (diff) | |
download | dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.gz dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.bz2 dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.lz dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.xz dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.zst dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.zip |
Merge branch 'release/0.9.0'
Diffstat (limited to 'pow/ezp/pow.go')
-rw-r--r-- | pow/ezp/pow.go | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/pow/ezp/pow.go b/pow/ezp/pow.go index bfe3ea098..540381243 100644 --- a/pow/ezp/pow.go +++ b/pow/ezp/pow.go @@ -6,10 +6,10 @@ import ( "time" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/pow" - "github.com/obscuren/sha3" ) var powlogger = logger.NewLogger("POW") @@ -21,7 +21,7 @@ type EasyPow struct { } func New() *EasyPow { - return &EasyPow{turbo: true} + return &EasyPow{turbo: false} } func (pow *EasyPow) GetHashrate() int64 { @@ -35,31 +35,38 @@ func (pow *EasyPow) Turbo(on bool) { func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) []byte { r := rand.New(rand.NewSource(time.Now().UnixNano())) hash := block.HashNoNonce() - diff := block.Diff() - i := int64(0) + diff := block.Difficulty() + //i := int64(0) + // TODO fix offset + i := rand.Int63() + starti := i start := time.Now().UnixNano() - t := time.Now() + + defer func() { pow.HashRate = 0 }() + + // Make sure stop is empty +empty: + for { + select { + case <-stop: + default: + break empty + } + } for { select { case <-stop: - powlogger.Infoln("Breaking from mining") - pow.HashRate = 0 return nil default: i++ - if time.Since(t) > (1 * time.Second) { - elapsed := time.Now().UnixNano() - start - hashes := ((float64(1e9) / float64(elapsed)) * float64(i)) / 1000 - pow.HashRate = int64(hashes) - powlogger.Infoln("Hashing @", pow.HashRate, "khash") - - t = time.Now() - } + elapsed := time.Now().UnixNano() - start + hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000 + pow.HashRate = int64(hashes) sha := crypto.Sha3(big.NewInt(r.Int63()).Bytes()) - if pow.verify(hash, diff, sha) { + if verify(hash, diff, sha) { return sha } } @@ -72,18 +79,22 @@ func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) []byte { return nil } -func (pow *EasyPow) verify(hash []byte, diff *big.Int, nonce []byte) bool { +func (pow *EasyPow) Verify(block pow.Block) bool { + return Verify(block) +} + +func verify(hash []byte, diff *big.Int, nonce []byte) bool { sha := sha3.NewKeccak256() d := append(hash, nonce...) sha.Write(d) verification := new(big.Int).Div(ethutil.BigPow(2, 256), diff) - res := ethutil.U256(ethutil.BigD(sha.Sum(nil))) + res := ethutil.BigD(sha.Sum(nil)) return res.Cmp(verification) <= 0 } -func (pow *EasyPow) Verify(block pow.Block) bool { - return pow.verify(block.HashNoNonce(), block.Diff(), block.N()) +func Verify(block pow.Block) bool { + return verify(block.HashNoNonce(), block.Difficulty(), block.N()) } |