op-geth/Godeps/_workspace/src/github.com/ethereum/ethash/ethash.go

377 lines
10 KiB
Go
Raw Normal View History

2015-02-28 19:58:37 +00:00
package ethash
/*
2015-05-05 05:50:04 +00:00
#include "src/libethash/internal.h"
int ethashGoCallback_cgo(unsigned);
2015-02-28 19:58:37 +00:00
*/
import "C"
import (
2015-05-05 05:50:04 +00:00
"errors"
2015-03-14 15:42:05 +00:00
"fmt"
"io/ioutil"
2015-02-28 19:58:37 +00:00
"math/big"
"math/rand"
"os"
2015-05-05 05:50:04 +00:00
"os/user"
"path/filepath"
"runtime"
2015-02-28 19:58:37 +00:00
"sync"
"sync/atomic"
2015-02-28 19:58:37 +00:00
"time"
"unsafe"
2015-03-16 10:27:38 +00:00
"github.com/ethereum/go-ethereum/common"
2015-03-20 10:19:12 +00:00
"github.com/ethereum/go-ethereum/crypto"
2015-02-28 19:58:37 +00:00
"github.com/ethereum/go-ethereum/logger"
2015-04-07 11:17:27 +00:00
"github.com/ethereum/go-ethereum/logger/glog"
2015-02-28 19:58:37 +00:00
"github.com/ethereum/go-ethereum/pow"
)
2015-05-05 05:50:04 +00:00
var (
minDifficulty = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
sharedLight = new(Light)
)
2015-02-28 19:58:37 +00:00
2015-05-05 05:50:04 +00:00
const (
epochLength uint64 = 30000
cacheSizeForTesting C.uint64_t = 1024
dagSizeForTesting C.uint64_t = 1024 * 32
)
2015-02-28 19:58:37 +00:00
2015-05-05 05:50:04 +00:00
var DefaultDir = defaultDir()
2015-02-28 19:58:37 +00:00
2015-05-05 05:50:04 +00:00
func defaultDir() string {
home := os.Getenv("HOME")
if user, err := user.Current(); err == nil {
home = user.HomeDir
}
2015-05-05 05:50:04 +00:00
if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Ethash")
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
return filepath.Join(home, ".ethash")
}
2015-04-07 11:17:27 +00:00
2015-05-05 05:50:04 +00:00
// cache wraps an ethash_light_t with some metadata
// and automatic memory management.
type cache struct {
epoch uint64
test bool
2015-05-05 05:50:04 +00:00
gen sync.Once // ensures cache is only generated once.
ptr *C.struct_ethash_light
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
// generate creates the actual cache. it can be called from multiple
// goroutines. the first call will generate the cache, subsequent
// calls wait until it is generated.
func (cache *cache) generate() {
cache.gen.Do(func() {
started := time.Now()
seedHash := makeSeedHash(cache.epoch)
glog.V(logger.Debug).Infof("Generating cache for epoch %d (%x)", cache.epoch, seedHash)
size := C.ethash_get_cachesize(C.uint64_t(cache.epoch * epochLength))
if cache.test {
size = cacheSizeForTesting
2015-03-14 15:42:05 +00:00
}
2015-05-05 05:50:04 +00:00
cache.ptr = C.ethash_light_new_internal(size, (*C.ethash_h256_t)(unsafe.Pointer(&seedHash[0])))
runtime.SetFinalizer(cache, freeCache)
glog.V(logger.Debug).Infof("Done generating cache for epoch %d, it took %v", cache.epoch, time.Since(started))
})
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
func freeCache(cache *cache) {
C.ethash_light_delete(cache.ptr)
cache.ptr = nil
}
2015-03-14 15:42:05 +00:00
2015-05-05 05:50:04 +00:00
// Light implements the Verify half of the proof of work.
// It uses a small in-memory cache to verify the nonces
// found by Full.
type Light struct {
test bool // if set use a smaller cache size
mu sync.Mutex // protects current
current *cache // last cache which was generated.
// TODO: keep multiple caches.
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
// Verify checks whether the block's nonce is valid.
func (l *Light) Verify(block pow.Block) bool {
// TODO: do ethash_quick_verify before getCache in order
// to prevent DOS attacks.
2015-06-24 05:17:21 +00:00
blockNum := block.NumberU64()
2015-05-05 05:50:04 +00:00
if blockNum >= epochLength*2048 {
glog.V(logger.Debug).Infof("block number %d too high, limit is %d", epochLength*2048)
return false
}
2015-06-24 05:17:21 +00:00
difficulty := block.Difficulty()
/* Cannot happen if block header diff is validated prior to PoW, but can
happen if PoW is checked first due to parallel PoW checking.
We could check the minimum valid difficulty but for SoC we avoid (duplicating)
Ethereum protocol consensus rules here which are not in scope of Ethash
*/
if difficulty.Cmp(common.Big0) == 0 {
glog.V(logger.Debug).Infof("invalid block difficulty")
return false
}
cache := l.getCache(blockNum)
dagSize := C.ethash_get_datasize(C.uint64_t(blockNum))
if l.test {
dagSize = dagSizeForTesting
}
2015-05-05 05:50:04 +00:00
// Recompute the hash using the cache.
hash := hashToH256(block.HashNoNonce())
ret := C.ethash_light_compute_internal(cache.ptr, dagSize, hash, C.uint64_t(block.Nonce()))
if !ret.success {
return false
}
2015-06-16 10:06:06 +00:00
// avoid mixdigest malleability as it's not included in a block's "hashNononce"
2015-06-16 22:56:25 +00:00
if block.MixDigest() != h256ToHash(ret.mix_hash) {
2015-06-16 10:06:06 +00:00
return false
}
2015-05-05 05:50:04 +00:00
// Make sure cache is live until after the C call.
// This is important because a GC might happen and execute
// the finalizer before the call completes.
_ = cache
// The actual check.
target := new(big.Int).Div(minDifficulty, difficulty)
return h256ToHash(ret.result).Big().Cmp(target) <= 0
}
2015-05-06 15:38:28 +00:00
func h256ToHash(in C.ethash_h256_t) common.Hash {
2015-05-05 05:50:04 +00:00
return *(*common.Hash)(unsafe.Pointer(&in.b))
}
2015-05-06 15:38:28 +00:00
func hashToH256(in common.Hash) C.ethash_h256_t {
return C.ethash_h256_t{b: *(*[32]C.uint8_t)(unsafe.Pointer(&in[0]))}
}
2015-05-05 05:50:04 +00:00
func (l *Light) getCache(blockNum uint64) *cache {
var c *cache
epoch := blockNum / epochLength
// Update or reuse the last cache.
l.mu.Lock()
if l.current != nil && l.current.epoch == epoch {
c = l.current
} else {
c = &cache{epoch: epoch, test: l.test}
l.current = c
2015-03-14 15:42:05 +00:00
}
2015-05-05 05:50:04 +00:00
l.mu.Unlock()
// Wait for the cache to finish generating.
c.generate()
return c
}
2015-02-28 19:58:37 +00:00
2015-05-05 05:50:04 +00:00
// dag wraps an ethash_full_t with some metadata
// and automatic memory management.
type dag struct {
epoch uint64
test bool
dir string
2015-05-05 05:50:04 +00:00
gen sync.Once // ensures DAG is only generated once.
ptr *C.struct_ethash_full
}
2015-03-14 15:42:05 +00:00
2015-05-05 05:50:04 +00:00
// generate creates the actual DAG. it can be called from multiple
// goroutines. the first call will generate the DAG, subsequent
// calls wait until it is generated.
func (d *dag) generate() {
d.gen.Do(func() {
var (
started = time.Now()
seedHash = makeSeedHash(d.epoch)
blockNum = C.uint64_t(d.epoch * epochLength)
cacheSize = C.ethash_get_cachesize(blockNum)
dagSize = C.ethash_get_datasize(blockNum)
)
if d.test {
cacheSize = cacheSizeForTesting
dagSize = dagSizeForTesting
2015-03-14 15:42:05 +00:00
}
2015-05-05 05:50:04 +00:00
if d.dir == "" {
d.dir = DefaultDir
}
2015-05-05 05:50:04 +00:00
glog.V(logger.Info).Infof("Generating DAG for epoch %d (%x)", d.epoch, seedHash)
// Generate a temporary cache.
// TODO: this could share the cache with Light
cache := C.ethash_light_new_internal(cacheSize, (*C.ethash_h256_t)(unsafe.Pointer(&seedHash[0])))
defer C.ethash_light_delete(cache)
// Generate the actual DAG.
d.ptr = C.ethash_full_new_internal(
C.CString(d.dir),
hashToH256(seedHash),
dagSize,
cache,
(C.ethash_callback_t)(unsafe.Pointer(C.ethashGoCallback_cgo)),
)
if d.ptr == nil {
panic("ethash_full_new IO or memory error")
}
runtime.SetFinalizer(d, freeDAG)
glog.V(logger.Info).Infof("Done generating DAG for epoch %d, it took %v", d.epoch, time.Since(started))
})
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
func freeDAG(h *dag) {
C.ethash_full_delete(h.ptr)
h.ptr = nil
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
//export ethashGoCallback
func ethashGoCallback(percent C.unsigned) C.int {
glog.V(logger.Info).Infof("Still generating DAG: %d%%", percent)
return 0
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
// MakeDAG pre-generates a DAG file for the given block number in the
// given directory. If dir is the empty string, the default directory
// is used.
func MakeDAG(blockNum uint64, dir string) error {
d := &dag{epoch: blockNum / epochLength, dir: dir}
2015-03-14 15:42:05 +00:00
if blockNum >= epochLength*2048 {
2015-05-05 05:50:04 +00:00
return fmt.Errorf("block number too high, limit is %d", epochLength*2048)
2015-03-14 15:42:05 +00:00
}
2015-05-05 05:50:04 +00:00
d.generate()
if d.ptr == nil {
return errors.New("failed")
2015-03-14 15:42:05 +00:00
}
2015-05-05 05:50:04 +00:00
return nil
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
// Full implements the Search half of the proof of work.
type Full struct {
Dir string // use this to specify a non-default DAG directory
2015-05-05 05:50:04 +00:00
test bool // if set use a smaller DAG size
turbo bool
hashRate int32
2015-05-05 05:50:04 +00:00
mu sync.Mutex // protects dag
current *dag // current full DAG
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
func (pow *Full) getDAG(blockNum uint64) (d *dag) {
epoch := blockNum / epochLength
pow.mu.Lock()
if pow.current != nil && pow.current.epoch == epoch {
d = pow.current
} else {
d = &dag{epoch: epoch, test: pow.test, dir: pow.Dir}
pow.current = d
}
pow.mu.Unlock()
// wait for it to finish generating.
d.generate()
return d
}
2015-02-28 19:58:37 +00:00
2015-05-05 05:50:04 +00:00
func (pow *Full) Search(block pow.Block, stop <-chan struct{}) (nonce uint64, mixDigest []byte) {
dag := pow.getDAG(block.NumberU64())
2015-02-28 19:58:37 +00:00
r := rand.New(rand.NewSource(time.Now().UnixNano()))
diff := block.Difficulty()
2015-02-28 19:58:37 +00:00
i := int64(0)
starti := i
2015-02-28 19:58:37 +00:00
start := time.Now().UnixNano()
previousHashrate := int32(0)
2015-02-28 19:58:37 +00:00
2015-05-05 05:50:04 +00:00
nonce = uint64(r.Int63())
hash := hashToH256(block.HashNoNonce())
2015-03-14 15:42:05 +00:00
target := new(big.Int).Div(minDifficulty, diff)
2015-02-28 19:58:37 +00:00
for {
select {
case <-stop:
atomic.AddInt32(&pow.hashRate, -previousHashrate)
2015-05-05 05:50:04 +00:00
return 0, nil
2015-02-28 19:58:37 +00:00
default:
i++
// we don't have to update hash rate on every nonce, so update after
// first nonce check and then after 2^X nonces
if i == 2 || ((i % (1 << 16)) == 0) {
elapsed := time.Now().UnixNano() - start
hashes := (float64(1e9) / float64(elapsed)) * float64(i-starti)
hashrateDiff := int32(hashes) - previousHashrate
previousHashrate = int32(hashes)
atomic.AddInt32(&pow.hashRate, hashrateDiff)
}
2015-02-28 19:58:37 +00:00
2015-05-05 05:50:04 +00:00
ret := C.ethash_full_compute(dag.ptr, hash, C.uint64_t(nonce))
result := h256ToHash(ret.result).Big()
2015-03-14 15:42:05 +00:00
// TODO: disagrees with the spec https://github.com/ethereum/wiki/wiki/Ethash#mining
2015-05-05 05:50:04 +00:00
if ret.success && result.Cmp(target) <= 0 {
mixDigest = C.GoBytes(unsafe.Pointer(&ret.mix_hash), C.int(32))
atomic.AddInt32(&pow.hashRate, -previousHashrate)
2015-05-05 05:50:04 +00:00
return nonce, mixDigest
2015-02-28 19:58:37 +00:00
}
nonce += 1
}
if !pow.turbo {
time.Sleep(20 * time.Microsecond)
}
}
}
2015-05-05 05:50:04 +00:00
func (pow *Full) GetHashrate() int64 {
return int64(atomic.LoadInt32(&pow.hashRate))
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
func (pow *Full) Turbo(on bool) {
// TODO: this needs to use an atomic operation.
pow.turbo = on
}
2015-05-05 05:50:04 +00:00
// Ethash combines block verification with Light and
// nonce searching with Full into a single proof of work.
type Ethash struct {
*Light
*Full
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
// New creates an instance of the proof of work.
// A single instance of Light is shared across all instances
// created with New.
func New() *Ethash {
return &Ethash{sharedLight, &Full{turbo: true}}
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
// NewForTesting creates a proof of work for use in unit tests.
// It uses a smaller DAG and cache size to keep test times low.
// DAG files are stored in a temporary directory.
//
// Nonces found by a testing instance are not verifiable with a
// regular-size cache.
func NewForTesting() (*Ethash, error) {
dir, err := ioutil.TempDir("", "ethash-test")
if err != nil {
return nil, err
}
return &Ethash{&Light{test: true}, &Full{Dir: dir, test: true}}, nil
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
func GetSeedHash(blockNum uint64) ([]byte, error) {
if blockNum >= epochLength*2048 {
return nil, fmt.Errorf("block number too high, limit is %d", epochLength*2048)
}
sh := makeSeedHash(blockNum / epochLength)
return sh[:], nil
2015-02-28 19:58:37 +00:00
}
2015-05-05 05:50:04 +00:00
func makeSeedHash(epoch uint64) (sh common.Hash) {
for ; epoch > 0; epoch-- {
sh = crypto.Sha3Hash(sh[:])
}
return sh
2015-02-28 19:58:37 +00:00
}