| 
									
										
										
										
											2014-12-04 10:28:02 +01:00
										 |  |  | package core | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2014-08-21 18:14:41 +02:00
										 |  |  | 	"hash" | 
					
						
							|  |  |  | 	"math/big" | 
					
						
							|  |  |  | 	"math/rand" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-04 10:28:02 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							| 
									
										
										
										
											2014-10-31 12:37:43 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/crypto" | 
					
						
							| 
									
										
										
										
											2014-10-23 15:01:27 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/ethutil" | 
					
						
							| 
									
										
										
										
											2014-10-31 12:56:05 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/logger" | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 	"github.com/obscuren/sha3" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 12:56:05 +01:00
										 |  |  | var powlogger = logger.NewLogger("POW") | 
					
						
							| 
									
										
										
										
											2014-06-23 12:54:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | type PoW interface { | 
					
						
							| 
									
										
										
										
											2014-11-18 16:58:22 +01:00
										 |  |  | 	Search(block *types.Block, stop <-chan struct{}) []byte | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 	Verify(hash []byte, diff *big.Int, nonce []byte) bool | 
					
						
							| 
									
										
										
										
											2014-07-18 12:01:08 +02:00
										 |  |  | 	GetHashrate() int64 | 
					
						
							| 
									
										
										
										
											2014-08-21 18:14:41 +02:00
										 |  |  | 	Turbo(bool) | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type EasyPow struct { | 
					
						
							| 
									
										
										
										
											2014-07-18 12:01:08 +02:00
										 |  |  | 	hash     *big.Int | 
					
						
							|  |  |  | 	HashRate int64 | 
					
						
							| 
									
										
										
										
											2014-08-21 18:14:41 +02:00
										 |  |  | 	turbo    bool | 
					
						
							| 
									
										
										
										
											2014-07-18 12:01:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (pow *EasyPow) GetHashrate() int64 { | 
					
						
							|  |  |  | 	return pow.HashRate | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-21 18:14:41 +02:00
										 |  |  | func (pow *EasyPow) Turbo(on bool) { | 
					
						
							|  |  |  | 	pow.turbo = on | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-18 16:58:22 +01:00
										 |  |  | func (pow *EasyPow) Search(block *types.Block, stop <-chan struct{}) []byte { | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 	r := rand.New(rand.NewSource(time.Now().UnixNano())) | 
					
						
							|  |  |  | 	hash := block.HashNoNonce() | 
					
						
							|  |  |  | 	diff := block.Difficulty | 
					
						
							| 
									
										
										
										
											2014-03-17 10:37:37 +01:00
										 |  |  | 	i := int64(0) | 
					
						
							|  |  |  | 	start := time.Now().UnixNano() | 
					
						
							| 
									
										
										
										
											2014-08-21 20:13:26 +02:00
										 |  |  | 	t := time.Now() | 
					
						
							| 
									
										
										
										
											2014-03-10 11:53:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 	for { | 
					
						
							| 
									
										
										
										
											2014-03-10 11:53:02 +01:00
										 |  |  | 		select { | 
					
						
							| 
									
										
										
										
											2014-10-14 01:58:31 +02:00
										 |  |  | 		case <-stop: | 
					
						
							| 
									
										
										
										
											2014-07-01 15:25:57 +02:00
										 |  |  | 			powlogger.Infoln("Breaking from mining") | 
					
						
							| 
									
										
										
										
											2014-11-07 12:18:48 +01:00
										 |  |  | 			pow.HashRate = 0 | 
					
						
							| 
									
										
										
										
											2014-03-20 11:20:29 +01:00
										 |  |  | 			return nil | 
					
						
							| 
									
										
										
										
											2014-03-10 11:53:02 +01:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2014-03-17 10:37:37 +01:00
										 |  |  | 			i++ | 
					
						
							| 
									
										
										
										
											2014-08-21 20:13:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if time.Since(t) > (1 * time.Second) { | 
					
						
							| 
									
										
										
										
											2014-03-17 10:37:37 +01:00
										 |  |  | 				elapsed := time.Now().UnixNano() - start | 
					
						
							|  |  |  | 				hashes := ((float64(1e9) / float64(elapsed)) * float64(i)) / 1000 | 
					
						
							| 
									
										
										
										
											2014-07-18 12:01:08 +02:00
										 |  |  | 				pow.HashRate = int64(hashes) | 
					
						
							| 
									
										
										
										
											2014-11-07 12:18:48 +01:00
										 |  |  | 				powlogger.Infoln("Hashing @", pow.HashRate, "khash") | 
					
						
							| 
									
										
										
										
											2014-08-21 20:13:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				t = time.Now() | 
					
						
							| 
									
										
										
										
											2014-03-17 10:37:37 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 12:37:43 +01:00
										 |  |  | 			sha := crypto.Sha3(big.NewInt(r.Int63()).Bytes()) | 
					
						
							| 
									
										
										
										
											2014-03-10 11:53:02 +01:00
										 |  |  | 			if pow.Verify(hash, diff, sha) { | 
					
						
							|  |  |  | 				return sha | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-08-21 18:14:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if !pow.turbo { | 
					
						
							| 
									
										
										
										
											2014-08-21 20:13:26 +02:00
										 |  |  | 			time.Sleep(20 * time.Microsecond) | 
					
						
							| 
									
										
										
										
											2014-08-21 18:14:41 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (pow *EasyPow) Verify(hash []byte, diff *big.Int, nonce []byte) bool { | 
					
						
							|  |  |  | 	sha := sha3.NewKeccak256() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	d := append(hash, nonce...) | 
					
						
							|  |  |  | 	sha.Write(d) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 17:22:33 +01:00
										 |  |  | 	verification := new(big.Int).Div(ethutil.BigPow(2, 256), diff) | 
					
						
							|  |  |  | 	res := ethutil.U256(ethutil.BigD(sha.Sum(nil))) | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 17:22:33 +01:00
										 |  |  | 	/* | 
					
						
							|  |  |  | 		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())) | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 17:22:33 +01:00
										 |  |  | 	return res.Cmp(verification) <= 0 | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (pow *EasyPow) SetHash(hash *big.Int) { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Dagger struct { | 
					
						
							|  |  |  | 	hash *big.Int | 
					
						
							|  |  |  | 	xn   *big.Int | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var Found bool | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dag *Dagger) Find(obj *big.Int, resChan chan int64) { | 
					
						
							|  |  |  | 	r := rand.New(rand.NewSource(time.Now().UnixNano())) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i := 0; i < 1000; i++ { | 
					
						
							|  |  |  | 		rnd := r.Int63() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		res := dag.Eval(big.NewInt(rnd)) | 
					
						
							| 
									
										
										
										
											2014-06-23 12:54:10 +01:00
										 |  |  | 		powlogger.Infof("rnd %v\nres %v\nobj %v\n", rnd, res, obj) | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 		if res.Cmp(obj) < 0 { | 
					
						
							|  |  |  | 			// Post back result on the channel | 
					
						
							|  |  |  | 			resChan <- rnd | 
					
						
							|  |  |  | 			// Notify other threads we've found a valid nonce | 
					
						
							|  |  |  | 			Found = true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Break out if found | 
					
						
							|  |  |  | 		if Found { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	resChan <- 0 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dag *Dagger) Search(hash, diff *big.Int) *big.Int { | 
					
						
							|  |  |  | 	// TODO fix multi threading. Somehow it results in the wrong nonce | 
					
						
							|  |  |  | 	amountOfRoutines := 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dag.hash = hash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	obj := ethutil.BigPow(2, 256) | 
					
						
							|  |  |  | 	obj = obj.Div(obj, diff) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Found = false | 
					
						
							|  |  |  | 	resChan := make(chan int64, 3) | 
					
						
							|  |  |  | 	var res int64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for k := 0; k < amountOfRoutines; k++ { | 
					
						
							|  |  |  | 		go dag.Find(obj, resChan) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-10 11:53:02 +01:00
										 |  |  | 		// Wait for each go routine to finish | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-14 23:56:09 +01:00
										 |  |  | 	for k := 0; k < amountOfRoutines; k++ { | 
					
						
							|  |  |  | 		// Get the result from the channel. 0 = quit | 
					
						
							|  |  |  | 		if r := <-resChan; r != 0 { | 
					
						
							|  |  |  | 			res = r | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return big.NewInt(res) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dag *Dagger) Verify(hash, diff, nonce *big.Int) bool { | 
					
						
							|  |  |  | 	dag.hash = hash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	obj := ethutil.BigPow(2, 256) | 
					
						
							|  |  |  | 	obj = obj.Div(obj, diff) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return dag.Eval(nonce).Cmp(obj) < 0 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func DaggerVerify(hash, diff, nonce *big.Int) bool { | 
					
						
							|  |  |  | 	dagger := &Dagger{} | 
					
						
							|  |  |  | 	dagger.hash = hash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	obj := ethutil.BigPow(2, 256) | 
					
						
							|  |  |  | 	obj = obj.Div(obj, diff) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return dagger.Eval(nonce).Cmp(obj) < 0 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dag *Dagger) Node(L uint64, i uint64) *big.Int { | 
					
						
							|  |  |  | 	if L == i { | 
					
						
							|  |  |  | 		return dag.hash | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var m *big.Int | 
					
						
							|  |  |  | 	if L == 9 { | 
					
						
							|  |  |  | 		m = big.NewInt(16) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		m = big.NewInt(3) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sha := sha3.NewKeccak256() | 
					
						
							|  |  |  | 	sha.Reset() | 
					
						
							|  |  |  | 	d := sha3.NewKeccak256() | 
					
						
							|  |  |  | 	b := new(big.Int) | 
					
						
							|  |  |  | 	ret := new(big.Int) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for k := 0; k < int(m.Uint64()); k++ { | 
					
						
							|  |  |  | 		d.Reset() | 
					
						
							|  |  |  | 		d.Write(dag.hash.Bytes()) | 
					
						
							|  |  |  | 		d.Write(dag.xn.Bytes()) | 
					
						
							|  |  |  | 		d.Write(big.NewInt(int64(L)).Bytes()) | 
					
						
							|  |  |  | 		d.Write(big.NewInt(int64(i)).Bytes()) | 
					
						
							|  |  |  | 		d.Write(big.NewInt(int64(k)).Bytes()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		b.SetBytes(Sum(d)) | 
					
						
							|  |  |  | 		pk := b.Uint64() & ((1 << ((L - 1) * 3)) - 1) | 
					
						
							|  |  |  | 		sha.Write(dag.Node(L-1, pk).Bytes()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ret.SetBytes(Sum(sha)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func Sum(sha hash.Hash) []byte { | 
					
						
							|  |  |  | 	//in := make([]byte, 32) | 
					
						
							|  |  |  | 	return sha.Sum(nil) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (dag *Dagger) Eval(N *big.Int) *big.Int { | 
					
						
							|  |  |  | 	pow := ethutil.BigPow(2, 26) | 
					
						
							|  |  |  | 	dag.xn = pow.Div(N, pow) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sha := sha3.NewKeccak256() | 
					
						
							|  |  |  | 	sha.Reset() | 
					
						
							|  |  |  | 	ret := new(big.Int) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for k := 0; k < 4; k++ { | 
					
						
							|  |  |  | 		d := sha3.NewKeccak256() | 
					
						
							|  |  |  | 		b := new(big.Int) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		d.Reset() | 
					
						
							|  |  |  | 		d.Write(dag.hash.Bytes()) | 
					
						
							|  |  |  | 		d.Write(dag.xn.Bytes()) | 
					
						
							|  |  |  | 		d.Write(N.Bytes()) | 
					
						
							|  |  |  | 		d.Write(big.NewInt(int64(k)).Bytes()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		b.SetBytes(Sum(d)) | 
					
						
							|  |  |  | 		pk := (b.Uint64() & 0x1ffffff) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		sha.Write(dag.Node(9, pk).Bytes()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ret.SetBytes(Sum(sha)) | 
					
						
							|  |  |  | } |