2014-12-10 15:45:16 +00:00
|
|
|
package ezp
|
|
|
|
|
|
|
|
import (
|
2015-03-03 20:04:31 +00:00
|
|
|
"encoding/binary"
|
2014-12-10 15:45:16 +00:00
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2015-01-21 23:25:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/sha3"
|
2015-03-16 10:27:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-12-10 15:45:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/pow"
|
|
|
|
)
|
|
|
|
|
|
|
|
var powlogger = logger.NewLogger("POW")
|
|
|
|
|
|
|
|
type EasyPow struct {
|
|
|
|
hash *big.Int
|
|
|
|
HashRate int64
|
|
|
|
turbo bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *EasyPow {
|
2015-02-20 17:06:45 +00:00
|
|
|
return &EasyPow{turbo: false}
|
2014-12-10 15:45:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pow *EasyPow) GetHashrate() int64 {
|
|
|
|
return pow.HashRate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pow *EasyPow) Turbo(on bool) {
|
|
|
|
pow.turbo = on
|
|
|
|
}
|
|
|
|
|
2015-03-03 20:04:31 +00:00
|
|
|
func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte, []byte) {
|
2014-12-10 15:45:16 +00:00
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
hash := block.HashNoNonce()
|
2014-12-23 12:48:44 +00:00
|
|
|
diff := block.Difficulty()
|
2015-02-09 15:20:34 +00:00
|
|
|
//i := int64(0)
|
|
|
|
// TODO fix offset
|
|
|
|
i := rand.Int63()
|
|
|
|
starti := i
|
2014-12-10 15:45:16 +00:00
|
|
|
start := time.Now().UnixNano()
|
2015-02-09 15:20:34 +00:00
|
|
|
|
2015-02-13 17:30:06 +00:00
|
|
|
defer func() { pow.HashRate = 0 }()
|
|
|
|
|
2015-02-09 15:20:34 +00:00
|
|
|
// Make sure stop is empty
|
|
|
|
empty:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
default:
|
|
|
|
break empty
|
|
|
|
}
|
|
|
|
}
|
2014-12-10 15:45:16 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
2015-03-03 20:04:31 +00:00
|
|
|
return 0, nil, nil
|
2014-12-10 15:45:16 +00:00
|
|
|
default:
|
|
|
|
i++
|
|
|
|
|
2015-02-09 15:20:34 +00:00
|
|
|
elapsed := time.Now().UnixNano() - start
|
|
|
|
hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000
|
|
|
|
pow.HashRate = int64(hashes)
|
2014-12-10 15:45:16 +00:00
|
|
|
|
2015-03-03 20:04:31 +00:00
|
|
|
sha := uint64(r.Int63())
|
2014-12-15 10:37:23 +00:00
|
|
|
if verify(hash, diff, sha) {
|
2015-02-28 19:58:37 +00:00
|
|
|
return sha, nil, nil
|
2014-12-10 15:45:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pow.turbo {
|
|
|
|
time.Sleep(20 * time.Microsecond)
|
|
|
|
}
|
|
|
|
}
|
2015-03-03 20:04:31 +00:00
|
|
|
|
|
|
|
return 0, nil, nil
|
2014-12-10 15:45:16 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 10:37:23 +00:00
|
|
|
func (pow *EasyPow) Verify(block pow.Block) bool {
|
|
|
|
return Verify(block)
|
|
|
|
}
|
|
|
|
|
2015-03-03 20:04:31 +00:00
|
|
|
func verify(hash []byte, diff *big.Int, nonce uint64) bool {
|
2014-12-10 15:45:16 +00:00
|
|
|
sha := sha3.NewKeccak256()
|
|
|
|
|
2015-03-03 20:04:31 +00:00
|
|
|
n := make([]byte, 8)
|
|
|
|
binary.PutUvarint(n, nonce)
|
|
|
|
d := append(hash, n...)
|
2014-12-10 15:45:16 +00:00
|
|
|
sha.Write(d)
|
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
verification := new(big.Int).Div(common.BigPow(2, 256), diff)
|
|
|
|
res := common.BigD(sha.Sum(nil))
|
2014-12-10 15:45:16 +00:00
|
|
|
|
|
|
|
return res.Cmp(verification) <= 0
|
|
|
|
}
|
|
|
|
|
2014-12-15 10:37:23 +00:00
|
|
|
func Verify(block pow.Block) bool {
|
2015-02-27 20:59:33 +00:00
|
|
|
return verify(block.HashNoNonce(), block.Difficulty(), block.Nonce())
|
2014-12-10 15:45:16 +00:00
|
|
|
}
|