| 
									
										
										
										
											2014-12-10 16:45:16 +01:00
										 |  |  | package ezp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"math/big" | 
					
						
							|  |  |  | 	"math/rand" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/crypto" | 
					
						
							|  |  |  | 	"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") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type EasyPow struct { | 
					
						
							|  |  |  | 	hash     *big.Int | 
					
						
							|  |  |  | 	HashRate int64 | 
					
						
							|  |  |  | 	turbo    bool | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func New() *EasyPow { | 
					
						
							| 
									
										
										
										
											2015-01-05 19:53:53 +01:00
										 |  |  | 	return &EasyPow{turbo: false} | 
					
						
							| 
									
										
										
										
											2014-12-10 16:45:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (pow *EasyPow) GetHashrate() int64 { | 
					
						
							|  |  |  | 	return pow.HashRate | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (pow *EasyPow) Turbo(on bool) { | 
					
						
							|  |  |  | 	pow.turbo = on | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) []byte { | 
					
						
							|  |  |  | 	r := rand.New(rand.NewSource(time.Now().UnixNano())) | 
					
						
							|  |  |  | 	hash := block.HashNoNonce() | 
					
						
							| 
									
										
										
										
											2014-12-23 13:48:44 +01:00
										 |  |  | 	diff := block.Difficulty() | 
					
						
							| 
									
										
										
										
											2014-12-10 16:45:16 +01:00
										 |  |  | 	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()) | 
					
						
							| 
									
										
										
										
											2014-12-15 11:37:23 +01:00
										 |  |  | 			if verify(hash, diff, sha) { | 
					
						
							| 
									
										
										
										
											2014-12-10 16:45:16 +01:00
										 |  |  | 				return sha | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !pow.turbo { | 
					
						
							|  |  |  | 			time.Sleep(20 * time.Microsecond) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 11:37:23 +01:00
										 |  |  | func (pow *EasyPow) Verify(block pow.Block) bool { | 
					
						
							|  |  |  | 	return Verify(block) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func verify(hash []byte, diff *big.Int, nonce []byte) bool { | 
					
						
							| 
									
										
										
										
											2014-12-10 16:45:16 +01:00
										 |  |  | 	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))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res.Cmp(verification) <= 0 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 11:37:23 +01:00
										 |  |  | func Verify(block pow.Block) bool { | 
					
						
							| 
									
										
										
										
											2014-12-23 13:48:44 +01:00
										 |  |  | 	return verify(block.HashNoNonce(), block.Difficulty(), block.N()) | 
					
						
							| 
									
										
										
										
											2014-12-10 16:45:16 +01:00
										 |  |  | } |