Skip to content

Commit

Permalink
Use 'standard' as lint
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Mar 26, 2016
1 parent 8143dd7 commit cfa299b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 65 deletions.
4 changes: 2 additions & 2 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Ethash = require('../index.js')

var ethash = new Ethash()
//make the 1000 cache items with a seed of 0 * 32
// make the 1000 cache items with a seed of 0 * 32
ethash.mkcache(1000, new Buffer(32).fill(0))

var result = ethash.run(new Buffer('test'), new Buffer([0]), 1000)
console.log(result.hash.toString('hex'));
console.log(result.hash.toString('hex'))
19 changes: 9 additions & 10 deletions example/rawExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ const memdown = require('memdown')

Ethash.prototype.verifySubmit = function (number, headerHash, nonce, cb) {
var self = this
console.log(number);
console.log(number)
this.loadEpoc(number, function () {
console.log("EPOC set");
console.log(self.seed.toString('hex'));
var a = self.run(headerHash, new Buffer(nonce, 'hex'));
cb(a.hash);
console.log('EPOC set')
console.log(self.seed.toString('hex'))
var a = self.run(headerHash, new Buffer(nonce, 'hex'))
cb(a.hash)
})
}


var cacheDB = levelup('', {
db: memdown
})

var ethash = new Ethash(cacheDB);
var ethash = new Ethash(cacheDB)

var header = Buffer('0e2887aa1a0668bf8254d1a6ae518927de99e3e5d7f30fd1f16096e2608fe05e', 'hex');
var header = Buffer('0e2887aa1a0668bf8254d1a6ae518927de99e3e5d7f30fd1f16096e2608fe05e', 'hex')

ethash.verifySubmit(35414, header, 'e360b6170c229d15', function (result) {
console.log(result.toString('hex'));
});
console.log(result.toString('hex'))
})
42 changes: 27 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ Ethash.prototype.mkcache = function (cacheSize, seed) {
const n = Math.floor(cacheSize / ethHashUtil.params.HASH_BYTES)
var o = [ethUtil.sha3(seed, 512)]

for (var i = 1; i < n; i++)
var i
for (i = 1; i < n; i++) {
o.push(ethUtil.sha3(o[o.length - 1], 512))
}

for (var _ = 0; _ < ethHashUtil.params.CACHE_ROUNDS; _++) {
for (var i = 0; i < n; i++) {
for (i = 0; i < n; i++) {
var v = o[i].readUInt32LE(0) % n
o[i] = ethUtil.sha3(xor(o[(i - 1 + n) % n], o[v]), 512)
}
}

return this.cache = o
this.cache = o
return this.cache
}

Ethash.prototype.calcDatasetItem = function (i) {
Expand All @@ -53,18 +56,20 @@ Ethash.prototype.run = function (val, nonce, fullSize) {
const mixhashes = Math.floor(ethHashUtil.params.MIX_BYTES / ethHashUtil.params.HASH_BYTES)
var mix = Buffer.concat(Array(mixhashes).fill(s))

for (var i = 0; i < ethHashUtil.params.ACCESSES; i++) {
var i
for (i = 0; i < ethHashUtil.params.ACCESSES; i++) {
var p = ethHashUtil.fnv(i ^ s.readUInt32LE(0), mix.readUInt32LE(i % w * 4)) % Math.floor(n / mixhashes) * mixhashes
var newdata = []
for (var j = 0; j < mixhashes; j++)
for (var j = 0; j < mixhashes; j++) {
newdata.push(this.calcDatasetItem(p + j))
}

newdata = Buffer.concat(newdata)
mix = ethHashUtil.fnvBuffer(mix, newdata)
}

var cmix = new Buffer(mix.length / 4)
for (var i = 0; i < mix.length / 4; i = i + 4) {
for (i = 0; i < mix.length / 4; i = i + 4) {
var a = ethHashUtil.fnv(mix.readUInt32LE(i * 4), mix.readUInt32LE((i + 1) * 4))
var b = ethHashUtil.fnv(a, mix.readUInt32LE((i + 2) * 4))
var c = ethHashUtil.fnv(b, mix.readUInt32LE((i + 3) * 4))
Expand Down Expand Up @@ -95,24 +100,28 @@ Ethash.prototype.loadEpoc = function (number, cb) {
var self = this
const epoc = ethHashUtil.getEpoc(number)

if (this.epoc === epoc)
if (this.epoc === epoc) {
return cb()
}

this.epoc = epoc

// gives the seed the first epoc found
function findLastSeed(epoc, cb2) {
if (epoc === 0)
function findLastSeed (epoc, cb2) {
if (epoc === 0) {
return cb2(ethUtil.zeros(32), 0)
}

self.cacheDB.get(epoc, self.dbOpts, function (err, data) {
if (!err)
if (!err) {
cb2(data.seed, epoc)
else
} else {
findLastSeed(epoc - 1, cb2)
}
})
}

/* eslint-disable handle-callback-err */
self.cacheDB.get(epoc, self.dbOpts, function (err, data) {
if (!data) {
self.cacheSize = ethHashUtil.getCacheSize(epoc)
Expand All @@ -121,7 +130,7 @@ Ethash.prototype.loadEpoc = function (number, cb) {
findLastSeed(epoc, function (seed, foundEpoc) {
self.seed = ethHashUtil.getSeed(seed, foundEpoc, epoc)
var cache = self.mkcache(self.cacheSize, self.seed)
//store the generated cache
// store the generated cache
self.cacheDB.put(epoc, {
cacheSize: self.cacheSize,
fullSize: self.fullSize,
Expand All @@ -140,6 +149,7 @@ Ethash.prototype.loadEpoc = function (number, cb) {
cb()
}
})
/* eslint-enable handle-callback-err */
}

Ethash.prototype._verifyPOW = function (header, cb) {
Expand Down Expand Up @@ -167,16 +177,18 @@ Ethash.prototype.verifyPOW = function (block, cb) {
this._verifyPOW(block.header, function (valid2) {
valid &= valid2

if (!valid)
if (!valid) {
return cb(valid)
}

async.eachSeries(block.uncleHeaders, function (uheader, cb2) {
self._verifyPOW(uheader, function (valid3) {
valid &= valid3
if (!valid)
if (!valid) {
cb2(Boolean(valid))
else
} else {
cb2()
}
})
}, function () {
cb(Boolean(valid))
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "tests"
},
"scripts": {
"lint": "standard",
"test": "node ./tests/"
},
"repository": {
Expand Down Expand Up @@ -35,6 +36,13 @@
"ethereumjs-testing": "0.0.1",
"levelup": "^1.3.0",
"memdown": "^1.1.0",
"standard": "^6.0.8",
"tape": "^4.2.2"
},
"standard": {
"globals": [
"describe",
"it"
]
}
}
12 changes: 6 additions & 6 deletions tests/block.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions tests/ethash.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ const ethUtil = require('ethereumjs-util')
const Header = require('ethereumjs-block/header.js')
const tape = require('tape')
const powTests = require('ethereumjs-testing').tests.powTests.ethash_tests
const async = require('async')

var ethash = new Ethash()
var tests = Object.keys(powTests)

tape('POW tests', function(t) {
tests.forEach(function(key) {
tape('POW tests', function (t) {
tests.forEach(function (key) {
var test = powTests[key]
var header = new Header(new Buffer(test.header, 'hex'))

Expand Down
Loading

0 comments on commit cfa299b

Please sign in to comment.