pow.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package ezp
  17. import (
  18. "encoding/binary"
  19. "math/big"
  20. "math/rand"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/crypto/sha3"
  24. "github.com/ethereum/go-ethereum/logger"
  25. "github.com/ethereum/go-ethereum/pow"
  26. )
  27. var powlogger = logger.NewLogger("POW")
  28. type EasyPow struct {
  29. hash *big.Int
  30. HashRate int64
  31. turbo bool
  32. }
  33. func New() *EasyPow {
  34. return &EasyPow{turbo: false}
  35. }
  36. func (pow *EasyPow) GetHashrate() int64 {
  37. return pow.HashRate
  38. }
  39. func (pow *EasyPow) Turbo(on bool) {
  40. pow.turbo = on
  41. }
  42. func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte) {
  43. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  44. hash := block.HashNoNonce()
  45. diff := block.Difficulty()
  46. //i := int64(0)
  47. // TODO fix offset
  48. i := rand.Int63()
  49. starti := i
  50. start := time.Now().UnixNano()
  51. defer func() { pow.HashRate = 0 }()
  52. // Make sure stop is empty
  53. empty:
  54. for {
  55. select {
  56. case <-stop:
  57. default:
  58. break empty
  59. }
  60. }
  61. for {
  62. select {
  63. case <-stop:
  64. return 0, nil
  65. default:
  66. i++
  67. elapsed := time.Now().UnixNano() - start
  68. hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000
  69. pow.HashRate = int64(hashes)
  70. sha := uint64(r.Int63())
  71. if verify(hash, diff, sha) {
  72. return sha, nil
  73. }
  74. }
  75. if !pow.turbo {
  76. time.Sleep(20 * time.Microsecond)
  77. }
  78. }
  79. return 0, nil
  80. }
  81. func (pow *EasyPow) Verify(block pow.Block) bool {
  82. return Verify(block)
  83. }
  84. func verify(hash common.Hash, diff *big.Int, nonce uint64) bool {
  85. sha := sha3.NewKeccak256()
  86. n := make([]byte, 8)
  87. binary.PutUvarint(n, nonce)
  88. sha.Write(n)
  89. sha.Write(hash[:])
  90. verification := new(big.Int).Div(common.BigPow(2, 256), diff)
  91. res := common.BigD(sha.Sum(nil))
  92. return res.Cmp(verification) <= 0
  93. }
  94. func Verify(block pow.Block) bool {
  95. return verify(block.HashNoNonce(), block.Difficulty(), block.Nonce())
  96. }