Updated ethash
This commit is contained in:
48
Godeps/_workspace/src/github.com/ethereum/ethash/ethash.go
generated
vendored
48
Godeps/_workspace/src/github.com/ethereum/ethash/ethash.go
generated
vendored
@@ -34,13 +34,12 @@ import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
"github.com/ethereum/go-ethereum/pow"
|
||||
)
|
||||
|
||||
var minDifficulty = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
|
||||
|
||||
var powlogger = logger.NewLogger("POW")
|
||||
|
||||
type ParamsAndCache struct {
|
||||
params *C.ethash_params
|
||||
cache *C.ethash_cache
|
||||
@@ -92,10 +91,13 @@ func makeParamsAndCache(chainManager pow.ChainManager, blockNum uint64) (*Params
|
||||
return nil, err
|
||||
}
|
||||
|
||||
powlogger.Infoln("Making Cache")
|
||||
glog.V(logger.Info).Infoln("Making cache")
|
||||
start := time.Now()
|
||||
C.ethash_mkcache(paramsAndCache.cache, paramsAndCache.params, (*C.uint8_t)(unsafe.Pointer(&seedHash[0])))
|
||||
powlogger.Infoln("Took:", time.Since(start))
|
||||
C.ethash_mkcache(paramsAndCache.cache, paramsAndCache.params, (*C.ethash_blockhash_t)(unsafe.Pointer(&seedHash[0])))
|
||||
|
||||
if glog.V(logger.Info) {
|
||||
glog.Infoln("Took:", time.Since(start))
|
||||
}
|
||||
|
||||
return paramsAndCache, nil
|
||||
}
|
||||
@@ -131,9 +133,9 @@ func makeDAG(p *ParamsAndCache) *DAG {
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
powlogger.Infof("... still generating DAG (%v) ...\n", time.Since(tstart).Seconds())
|
||||
glog.V(logger.Info).Infof("... still generating DAG (%v) ...\n", time.Since(tstart).Seconds())
|
||||
case str := <-donech:
|
||||
powlogger.Infof("... %s ...\n", str)
|
||||
glog.V(logger.Info).Infof("... %s ...\n", str)
|
||||
break done
|
||||
}
|
||||
}
|
||||
@@ -193,30 +195,30 @@ func (pow *Ethash) UpdateDAG() {
|
||||
pow.paramsAndCache = paramsAndCache
|
||||
path := path.Join("/", "tmp", "dag")
|
||||
pow.dag = nil
|
||||
powlogger.Infoln("Retrieving DAG")
|
||||
glog.V(logger.Info).Infoln("Retrieving DAG")
|
||||
start := time.Now()
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
powlogger.Infof("No DAG found. Generating new DAG in '%s' (this takes a while)...\n", path)
|
||||
glog.V(logger.Info).Infof("No DAG found. Generating new DAG in '%s' (this takes a while)...\n", path)
|
||||
pow.dag = makeDAG(paramsAndCache)
|
||||
file = pow.writeDagToDisk(pow.dag, thisEpoch)
|
||||
pow.dag.file = true
|
||||
} else {
|
||||
data, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
powlogger.Infof("DAG load err: %v\n", err)
|
||||
glog.V(logger.Info).Infof("DAG load err: %v\n", err)
|
||||
}
|
||||
|
||||
if len(data) < 8 {
|
||||
powlogger.Infof("DAG in '%s' is less than 8 bytes, it must be corrupted. Generating new DAG (this takes a while)...\n", path)
|
||||
glog.V(logger.Info).Infof("DAG in '%s' is less than 8 bytes, it must be corrupted. Generating new DAG (this takes a while)...\n", path)
|
||||
pow.dag = makeDAG(paramsAndCache)
|
||||
file = pow.writeDagToDisk(pow.dag, thisEpoch)
|
||||
pow.dag.file = true
|
||||
} else {
|
||||
dataEpoch := binary.BigEndian.Uint64(data[0:8])
|
||||
if dataEpoch < thisEpoch {
|
||||
powlogger.Infof("DAG in '%s' is stale. Generating new DAG (this takes a while)...\n", path)
|
||||
glog.V(logger.Info).Infof("DAG in '%s' is stale. Generating new DAG (this takes a while)...\n", path)
|
||||
pow.dag = makeDAG(paramsAndCache)
|
||||
file = pow.writeDagToDisk(pow.dag, thisEpoch)
|
||||
pow.dag.file = true
|
||||
@@ -224,7 +226,7 @@ func (pow *Ethash) UpdateDAG() {
|
||||
// FIXME
|
||||
panic(fmt.Errorf("Saved DAG in '%s' reports to be from future epoch %v (current epoch is %v)\n", path, dataEpoch, thisEpoch))
|
||||
} else if len(data) != (int(paramsAndCache.params.full_size) + 8) {
|
||||
powlogger.Infof("DAG in '%s' is corrupted. Generating new DAG (this takes a while)...\n", path)
|
||||
glog.V(logger.Info).Infof("DAG in '%s' is corrupted. Generating new DAG (this takes a while)...\n", path)
|
||||
pow.dag = makeDAG(paramsAndCache)
|
||||
file = pow.writeDagToDisk(pow.dag, thisEpoch)
|
||||
pow.dag.file = true
|
||||
@@ -238,7 +240,7 @@ func (pow *Ethash) UpdateDAG() {
|
||||
}
|
||||
}
|
||||
}
|
||||
powlogger.Infoln("Took:", time.Since(start))
|
||||
glog.V(logger.Info).Infoln("Took:", time.Since(start))
|
||||
|
||||
file.Close()
|
||||
}
|
||||
@@ -319,7 +321,7 @@ func (pow *Ethash) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte
|
||||
start := time.Now().UnixNano()
|
||||
|
||||
nonce := uint64(r.Int63())
|
||||
cMiningHash := (*C.uint8_t)(unsafe.Pointer(&miningHash[0]))
|
||||
cMiningHash := (*C.ethash_blockhash_t)(unsafe.Pointer(&miningHash[0]))
|
||||
target := new(big.Int).Div(minDifficulty, diff)
|
||||
|
||||
var ret C.ethash_return_value
|
||||
@@ -336,11 +338,11 @@ func (pow *Ethash) Search(block pow.Block, stop <-chan struct{}) (uint64, []byte
|
||||
pow.HashRate = int64(hashes)
|
||||
|
||||
C.ethash_full(&ret, pow.dag.dag, pow.dag.paramsAndCache.params, cMiningHash, C.uint64_t(nonce))
|
||||
result := common.Bytes2Big(C.GoBytes(unsafe.Pointer(&ret.result[0]), C.int(32)))
|
||||
result := common.Bytes2Big(C.GoBytes(unsafe.Pointer(&ret.result), C.int(32)))
|
||||
|
||||
// TODO: disagrees with the spec https://github.com/ethereum/wiki/wiki/Ethash#mining
|
||||
if result.Cmp(target) <= 0 {
|
||||
mixDigest := C.GoBytes(unsafe.Pointer(&ret.mix_hash[0]), C.int(32))
|
||||
mixDigest := C.GoBytes(unsafe.Pointer(&ret.mix_hash), C.int(32))
|
||||
seedHash, err := GetSeedHash(block.NumberU64()) // This seedhash is useless
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -366,14 +368,14 @@ func (pow *Ethash) Verify(block pow.Block) bool {
|
||||
func (pow *Ethash) verify(hash common.Hash, mixDigest common.Hash, difficulty *big.Int, blockNum uint64, nonce uint64) bool {
|
||||
// Make sure the block num is valid
|
||||
if blockNum >= epochLength*2048 {
|
||||
powlogger.Infoln(fmt.Sprintf("Block number exceeds limit, invalid (value is %v, limit is %v)",
|
||||
glog.V(logger.Info).Infoln(fmt.Sprintf("Block number exceeds limit, invalid (value is %v, limit is %v)",
|
||||
blockNum, epochLength*2048))
|
||||
return false
|
||||
}
|
||||
|
||||
// First check: make sure header, mixDigest, nonce are correct without hitting the cache
|
||||
// This is to prevent DOS attacks
|
||||
chash := (*C.uint8_t)(unsafe.Pointer(&hash[0]))
|
||||
chash := (*C.ethash_blockhash_t)(unsafe.Pointer(&hash[0]))
|
||||
cnonce := C.uint64_t(nonce)
|
||||
target := new(big.Int).Div(minDifficulty, difficulty)
|
||||
|
||||
@@ -387,7 +389,7 @@ func (pow *Ethash) verify(hash common.Hash, mixDigest common.Hash, difficulty *b
|
||||
// If we can't make the params for some reason, this block is invalid
|
||||
pAc, err = makeParamsAndCache(pow.chainManager, blockNum+1)
|
||||
if err != nil {
|
||||
powlogger.Infoln("big fucking eror", err)
|
||||
glog.V(logger.Info).Infoln("big fucking eror", err)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
@@ -401,7 +403,7 @@ func (pow *Ethash) verify(hash common.Hash, mixDigest common.Hash, difficulty *b
|
||||
|
||||
C.ethash_light(ret, pAc.cache, pAc.params, chash, cnonce)
|
||||
|
||||
result := common.Bytes2Big(C.GoBytes(unsafe.Pointer(&ret.result[0]), C.int(32)))
|
||||
result := common.Bytes2Big(C.GoBytes(unsafe.Pointer(&ret.result), C.int(32)))
|
||||
return result.Cmp(target) <= 0
|
||||
}
|
||||
|
||||
@@ -417,7 +419,7 @@ func (pow *Ethash) FullHash(nonce uint64, miningHash []byte) []byte {
|
||||
pow.UpdateDAG()
|
||||
pow.dagMutex.Lock()
|
||||
defer pow.dagMutex.Unlock()
|
||||
cMiningHash := (*C.uint8_t)(unsafe.Pointer(&miningHash[0]))
|
||||
cMiningHash := (*C.ethash_blockhash_t)(unsafe.Pointer(&miningHash[0]))
|
||||
cnonce := C.uint64_t(nonce)
|
||||
ret := new(C.ethash_return_value)
|
||||
// pow.hash is the output/return of ethash_full
|
||||
@@ -427,7 +429,7 @@ func (pow *Ethash) FullHash(nonce uint64, miningHash []byte) []byte {
|
||||
}
|
||||
|
||||
func (pow *Ethash) LightHash(nonce uint64, miningHash []byte) []byte {
|
||||
cMiningHash := (*C.uint8_t)(unsafe.Pointer(&miningHash[0]))
|
||||
cMiningHash := (*C.ethash_blockhash_t)(unsafe.Pointer(&miningHash[0]))
|
||||
cnonce := C.uint64_t(nonce)
|
||||
ret := new(C.ethash_return_value)
|
||||
C.ethash_light(ret, pow.paramsAndCache.cache, pow.paramsAndCache.params, cMiningHash, cnonce)
|
||||
|
Reference in New Issue
Block a user