aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/ethereum/ethash/ethash.go
blob: 32d3f02641c6505270eb8ec8fdeccd3088ab1b92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package ethash

/*
#cgo CFLAGS: -std=gnu99 -Wall
#include "libethash/ethash.h"
#include "libethash/util.c"
#include "libethash/internal.c"
#include "libethash/sha3.c"
*/
import "C"

import (
    "bytes"
    "encoding/binary"
    "log"
    "math/big"
    "math/rand"
    "sync"
    "time"
    "unsafe"

    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/pow"
)

var powlogger = logger.NewLogger("POW")

type DAG struct {
    SeedBlockNum uint64
    dag          unsafe.Pointer // full GB of memory for dag
}

type ParamsAndCache struct {
    params       *C.ethash_params
    cache        *C.ethash_cache
    SeedBlockNum uint64
}

type Ethash struct {
    turbo          bool
    HashRate       int64
    chainManager   pow.ChainManager
    dag            *DAG
    paramsAndCache *ParamsAndCache
    nextdag        unsafe.Pointer
    ret            *C.ethash_return_value
    dagMutex       *sync.Mutex
    cacheMutex     *sync.Mutex
}

func parseNonce(nonce []byte) (uint64, error) {
    nonceBuf := bytes.NewBuffer(nonce)
    nonceInt, err := binary.ReadUvarint(nonceBuf)
    if err != nil {
        return 0, err
    }
    return nonceInt, nil
}

const epochLength uint64 = 30000

func getSeedBlockNum(blockNum uint64) uint64 {
    var seedBlockNum uint64 = 0
    if blockNum >= 2*epochLength {
        seedBlockNum = ((blockNum / epochLength) - 1) * epochLength
    }
    return seedBlockNum
}

func makeParamsAndCache(chainManager pow.ChainManager, blockNum uint64) *ParamsAndCache {
    seedBlockNum := getSeedBlockNum(blockNum)
    paramsAndCache := &ParamsAndCache{
        params:       new(C.ethash_params),
        cache:        new(C.ethash_cache),
        SeedBlockNum: seedBlockNum,
    }
    C.ethash_params_init(paramsAndCache.params, C.uint32_t(seedBlockNum))
    paramsAndCache.cache.mem = C.malloc(paramsAndCache.params.cache_size)
    seedHash := chainManager.GetBlockByNumber(seedBlockNum).Header().Hash()
    log.Println("Params", paramsAndCache.params)

    log.Println("Making Cache")
    start := time.Now()
    C.ethash_mkcache(paramsAndCache.cache, paramsAndCache.params, (*C.uint8_t)((unsafe.Pointer)(&seedHash[0])))
    log.Println("Took:", time.Since(start))
    return paramsAndCache
}

func (pow *Ethash) updateCache() {
    pow.cacheMutex.Lock()
    seedNum := getSeedBlockNum(pow.chainManager.CurrentBlock().NumberU64())
    if pow.paramsAndCache.SeedBlockNum != seedNum {
        pow.paramsAndCache = makeParamsAndCache(pow.chainManager, pow.chainManager.CurrentBlock().NumberU64())
    }
    pow.cacheMutex.Unlock()
}

func makeDAG(p *ParamsAndCache) *DAG {
    d := &DAG{
        dag:          C.malloc(p.params.full_size),
        SeedBlockNum: p.SeedBlockNum,
    }
    C.ethash_compute_full_data(d.dag, p.params, p.cache)
    return d
}

func (pow *Ethash) updateDAG() {
    pow.cacheMutex.Lock()
    pow.dagMutex.Lock()

    seedNum := getSeedBlockNum(pow.chainManager.CurrentBlock().NumberU64())
    if pow.dag == nil || pow.dag.SeedBlockNum != seedNum {
        pow.dag = nil
        log.Println("Making Dag")
        start := time.Now()
        pow.dag = makeDAG(pow.paramsAndCache)
        log.Println("Took:", time.Since(start))
    }

    pow.dagMutex.Unlock()
    pow.cacheMutex.Unlock()
}

func New(chainManager pow.ChainManager) *Ethash {
    return &Ethash{
        turbo:          false,
        paramsAndCache: makeParamsAndCache(chainManager, chainManager.CurrentBlock().NumberU64()),
        chainManager:   chainManager,
        dag:            nil,
        ret:            new(C.ethash_return_value),
        cacheMutex:     new(sync.Mutex),
        dagMutex:       new(sync.Mutex),
    }
}

func (pow *Ethash) DAGSize() uint64 {
    return uint64(pow.paramsAndCache.params.full_size)
}

func (pow *Ethash) CacheSize() uint64 {
    return uint64(pow.paramsAndCache.params.cache_size)
}

func (pow *Ethash) GetSeedHash(blockNum uint64) []byte {
    return pow.chainManager.GetBlockByNumber(getSeedBlockNum(blockNum)).Header().Hash()
}

func (pow *Ethash) Stop() {
    pow.cacheMutex.Lock()
    pow.dagMutex.Lock()
    if pow.paramsAndCache.cache != nil {
        C.free(pow.paramsAndCache.cache.mem)
    }
    if pow.dag != nil {
        C.free(pow.dag.dag)
    }
    pow.dagMutex.Unlock()
    pow.cacheMutex.Unlock()
}

func (pow *Ethash) Search(block pow.Block, stop <-chan struct{}) ([]byte, []byte, []byte) {
    pow.updateDAG()

    // Not very elegant, multiple mining instances are not supported
    pow.dagMutex.Lock()
    pow.cacheMutex.Lock()
    defer pow.cacheMutex.Unlock()
    defer pow.dagMutex.Unlock()

    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    miningHash := block.HashNoNonce()
    diff := block.Difficulty()
    log.Println("difficulty", diff)
    i := int64(0)
    start := time.Now().UnixNano()
    t := time.Now()

    nonce := uint64(r.Int63())

    for {
        select {
        case <-stop:
            powlogger.Infoln("Breaking from mining")
            pow.HashRate = 0
            pow.dagMutex.Unlock()
            return nil, nil, 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()
            }

            cMiningHash := (*C.uint8_t)(unsafe.Pointer(&miningHash))
            cnonce := C.uint64_t(nonce)
            log.Printf("seed hash, nonce: %x %x\n", miningHash, nonce)
            // pow.hash is the output/return of ethash_full
            C.ethash_full(pow.ret, pow.dag.dag, pow.paramsAndCache.params, cMiningHash, cnonce)
            res := C.ethash_check_difficulty((*C.uint8_t)(&pow.ret.result[0]), (*C.uint8_t)(unsafe.Pointer(&diff.Bytes()[0])))
            if res == 1 {
                mixDigest := C.GoBytes(unsafe.Pointer(&pow.ret.mix_hash[0]), 32)
                // We don't really nead 32 bytes here
                buf := make([]byte, 32)
                binary.PutUvarint(buf, nonce)
                return buf, mixDigest, pow.GetSeedHash(block.NumberU64())
            }
            nonce += 1
        }

        if !pow.turbo {
            time.Sleep(20 * time.Microsecond)
        }
    }
}

func (pow *Ethash) Verify(block pow.Block) bool {
    // Make sure the SeedHash is set correctly
    if bytes.Compare(block.SeedHash(), pow.GetSeedHash(block.NumberU64())) != 0 {
        log.Println("Block had wrong SeedHash")
        log.Println("Expected: ", pow.GetSeedHash(block.NumberU64()))
        log.Println("Actual: ", block.SeedHash())
        return false
    }

    nonceInt, err := parseNonce(block.Nonce())
    if err != nil {
        log.Println("nonce to int err:", err)
        return false
    }
    return pow.verify(block.HashNoNonce(), block.MixDigest(), block.Difficulty(), block.NumberU64(), nonceInt)
}

func (pow *Ethash) verify(hash []byte, mixDigest []byte, difficulty *big.Int, blockNum uint64, nonce uint64) bool {
    // First check: make sure header, mixDigest, nonce are correct without hitting the DAG
    // This is to prevent DOS attacks
    chash := (*C.uint8_t)(unsafe.Pointer(&hash))
    cnonce := C.uint64_t(nonce)
    cmixDigest := (*C.uint8_t)(unsafe.Pointer(&mixDigest))
    cdifficulty := (*C.uint8_t)(unsafe.Pointer(&difficulty.Bytes()[0]))
    if C.ethash_quick_check_difficulty(chash, cnonce, cmixDigest, cdifficulty) != 1 {
        log.Println("Failed to pass quick check.  Are you sure that the mix digest is correct?")
        return false
    }

    var pAc *ParamsAndCache
    // If its an old block (doesn't use the current cache)
    // get the cache for it but don't update (so we don't need the mutex)
    // Otherwise, it's the current block or a future.
    // If current, updateCache will do nothing.
    if getSeedBlockNum(blockNum) < pow.paramsAndCache.SeedBlockNum {
        pAc = makeParamsAndCache(pow.chainManager, blockNum)
    } else {
        pow.updateCache()
        pow.cacheMutex.Lock()
        defer pow.cacheMutex.Unlock()
        pAc = pow.paramsAndCache
    }

    C.ethash_light(pow.ret, pAc.cache, pAc.params, chash, cnonce)
    res := C.ethash_check_difficulty((*C.uint8_t)(unsafe.Pointer(&pow.ret.result[0])), cdifficulty)
    return res == 1
}

func (pow *Ethash) GetHashrate() int64 {
    return pow.HashRate
}

func (pow *Ethash) Turbo(on bool) {
    pow.turbo = on
}

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))
    cnonce := C.uint64_t(nonce)
    log.Println("seed hash, nonce:", miningHash, nonce)
    // pow.hash is the output/return of ethash_full
    C.ethash_full(pow.ret, pow.dag.dag, pow.paramsAndCache.params, cMiningHash, cnonce)
    ghash_full := C.GoBytes(unsafe.Pointer(&pow.ret.result[0]), 32)
    return ghash_full
}

func (pow *Ethash) LightHash(nonce uint64, miningHash []byte) []byte {
    cMiningHash := (*C.uint8_t)(unsafe.Pointer(&miningHash))
    cnonce := C.uint64_t(nonce)
    C.ethash_light(pow.ret, pow.paramsAndCache.cache, pow.paramsAndCache.params, cMiningHash, cnonce)
    ghash_light := C.GoBytes(unsafe.Pointer(&pow.ret.result[0]), 32)
    return ghash_light
}