aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-12-10 23:45:16 +0800
committerobscuren <geffobscura@gmail.com>2014-12-10 23:45:16 +0800
commit1b98cbbfa4f587107fa15fccde7d22102ea4b1c0 (patch)
tree367be45e14b7d1a1ff3a62e0fdb949156e9cc71d /core
parent4082c8b61d1e9cc57d9da9b9da5c36ff84895d74 (diff)
downloadgo-tangerine-1b98cbbfa4f587107fa15fccde7d22102ea4b1c0.tar
go-tangerine-1b98cbbfa4f587107fa15fccde7d22102ea4b1c0.tar.gz
go-tangerine-1b98cbbfa4f587107fa15fccde7d22102ea4b1c0.tar.bz2
go-tangerine-1b98cbbfa4f587107fa15fccde7d22102ea4b1c0.tar.lz
go-tangerine-1b98cbbfa4f587107fa15fccde7d22102ea4b1c0.tar.xz
go-tangerine-1b98cbbfa4f587107fa15fccde7d22102ea4b1c0.tar.zst
go-tangerine-1b98cbbfa4f587107fa15fccde7d22102ea4b1c0.zip
Moved pow
Diffstat (limited to 'core')
-rw-r--r--core/block_manager.go8
-rw-r--r--core/dagger.go85
-rw-r--r--core/simple_pow.go1
-rw-r--r--core/types/block.go3
4 files changed, 9 insertions, 88 deletions
diff --git a/core/block_manager.go b/core/block_manager.go
index 4c1cea35a..6f952f9f8 100644
--- a/core/block_manager.go
+++ b/core/block_manager.go
@@ -14,6 +14,8 @@ import (
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/pow"
+ "github.com/ethereum/go-ethereum/pow/ezp"
"github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/wire"
)
@@ -55,7 +57,7 @@ type BlockManager struct {
// non-persistent key/value memory storage
mem map[string]*big.Int
// Proof of work used for validating
- Pow PoW
+ Pow pow.PoW
// The ethereum manager interface
eth EthManager
// The managed states
@@ -78,7 +80,7 @@ type BlockManager struct {
func NewBlockManager(ethereum EthManager) *BlockManager {
sm := &BlockManager{
mem: make(map[string]*big.Int),
- Pow: &EasyPow{},
+ Pow: ezp.New(),
eth: ethereum,
bc: ethereum.ChainManager(),
}
@@ -327,7 +329,7 @@ func (sm *BlockManager) ValidateBlock(block, parent *types.Block) error {
*/
// Verify the nonce of the block. Return an error if it's not valid
- if !sm.Pow.Verify(block.HashNoNonce(), block.Difficulty, block.Nonce) {
+ if !sm.Pow.Verify(block /*block.HashNoNonce(), block.Difficulty, block.Nonce*/) {
return ValidationError("Block's nonce is invalid (= %v)", ethutil.Bytes2Hex(block.Nonce))
}
diff --git a/core/dagger.go b/core/dagger.go
index 8a042b34f..3039d8995 100644
--- a/core/dagger.go
+++ b/core/dagger.go
@@ -6,8 +6,6 @@ import (
"math/rand"
"time"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger"
"github.com/obscuren/sha3"
@@ -15,89 +13,6 @@ import (
var powlogger = logger.NewLogger("POW")
-type PoW interface {
- Search(block *types.Block, stop <-chan struct{}) []byte
- Verify(hash []byte, diff *big.Int, nonce []byte) bool
- GetHashrate() int64
- Turbo(bool)
-}
-
-type EasyPow struct {
- hash *big.Int
- HashRate int64
- turbo bool
-}
-
-func (pow *EasyPow) GetHashrate() int64 {
- return pow.HashRate
-}
-
-func (pow *EasyPow) Turbo(on bool) {
- pow.turbo = on
-}
-
-func (pow *EasyPow) Search(block *types.Block, stop <-chan struct{}) []byte {
- r := rand.New(rand.NewSource(time.Now().UnixNano()))
- hash := block.HashNoNonce()
- diff := block.Difficulty
- i := int64(0)
- start := time.Now().UnixNano()
- t := time.Now()
-
- 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()
- }
-
- sha := crypto.Sha3(big.NewInt(r.Int63()).Bytes())
- if pow.Verify(hash, diff, sha) {
- return sha
- }
- }
-
- if !pow.turbo {
- time.Sleep(20 * time.Microsecond)
- }
- }
-
- return nil
-}
-
-func (pow *EasyPow) 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)))
-
- /*
- fmt.Printf("hash w/o nonce %x\n", hash)
- fmt.Printf("2**256 / %v = %v\n", diff, verification)
- fmt.Printf("%v <= %v\n", res, verification)
- fmt.Printf("vlen: %d rlen: %d\n", len(verification.Bytes()), len(res.Bytes()))
- */
-
- return res.Cmp(verification) <= 0
-}
-
-func (pow *EasyPow) SetHash(hash *big.Int) {
-}
-
type Dagger struct {
hash *big.Int
xn *big.Int
diff --git a/core/simple_pow.go b/core/simple_pow.go
new file mode 100644
index 000000000..9a8bc9592
--- /dev/null
+++ b/core/simple_pow.go
@@ -0,0 +1 @@
+package core
diff --git a/core/types/block.go b/core/types/block.go
index 55ce1fb9b..0108bd586 100644
--- a/core/types/block.go
+++ b/core/types/block.go
@@ -411,3 +411,6 @@ func (self *Block) Size() ethutil.StorageSize {
func (self *Block) RlpData() interface{} {
return self.Value().Val
}
+
+// Implement pow.Block
+func (self *Block) N() []byte { return self.Nonce }