Skip to content

Commit

Permalink
Made miner look nice and work with the new server patch
Browse files Browse the repository at this point in the history
  • Loading branch information
encloinc committed Jan 27, 2019
1 parent 11cc714 commit 6000965
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 23 deletions.
41 changes: 41 additions & 0 deletions difficultyAlg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const difficulty = 0;

function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}

function checkHash(difficulty, hash){

const hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']

let preKey = ''

for(let i = 0; i <= Math.floor((difficulty - 1)/16); i++){

preKey += '0'
}

if (difficulty % 16 != 0){

for (let i = 0; i < (16 - (difficulty % 16)); i++){

if (hash.startsWith(preKey + hex[i])){

return true;
}
}

return false;

}else{

return hash.startsWith(preKey)
}

}

console.log(new Date().getTime())

console.log(checkHash(32, '00a'))
90 changes: 67 additions & 23 deletions miner.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
const request = require('request');
const crypto = require('crypto');
const fs = require('fs')
require('http').createServer().listen(3000)

var colors = require('colors');
const figlet = require('figlet');

const config = JSON.parse(fs.readFileSync('config.json', 'utf8'))
console.log(config.username)

function checkHash(difficulty, hash){

const hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']

let preKey = ''

for(let i = 0; i <= Math.floor((difficulty - 1)/16); i++){

preKey += '0'
}

if (difficulty % 16 != 0){

for (let i = 0; i < (16 - (difficulty % 16)); i++){

if (hash.startsWith(preKey + hex[i])){

return true;
}
}

return false;

}else{

return hash.startsWith(preKey)
}

}
function hasher(s){

return new Promise(function(resolve, reject){
Expand Down Expand Up @@ -65,68 +93,75 @@ function get(url){
}

let hashCount = 0;
let hashLast = Date.now();
let minerKey = null;
let salt = null;
let lastSecond = null;


async function loop(){
let serverConfig = null;
let e = null;


while (true){
if(hashLast + 5000 < Date.now()){

console.log(`HASHRATE`.bgBlue.black + ` ${hashCount/5000}KHS\n`)
hashLast = Date.now()
hashCount = 0;
}

if (lastSecond + config.checkTimeout < Date.now()){

lastSecond = Date.now();

console.log(`# Checking block key integrity at ${Date.now()}ms`)
console.log(`NOTICE`.bgWhite.black + ` Checking block difficulty integrity at ` + `${Date.now()}ms`.underline +'\n')

serverConfig = await get(`${config.mattcoinApiUrl}/config`)

if (minerKey != serverConfig.key){
if (minerDifficulty != serverConfig.difficulty){

minerKey = serverConfig.key;
minerDifficulty = serverConfig.difficulty;

salt = serverConfig.salt

console.log(`# Block key integrity nullified! New key ${minerKey} registered.`)
console.log(`WARNING`.bgYellow.black + ` Block hash nullified! New difficulty ${minerDifficulty} registered.\n`)

}
}

let hashObject = await hasher(salt);

let check = checkHash(minerDifficulty, hashObject.hash);

hashCount++;

if(hashObject.hash.startsWith(minerKey)){
if(check){

console.log(`# Found hash that satisfies block key`)
console.log(`SUCCESS`.bgGreen.black + ` Found hash that satisfies block difficulty:\n`)

console.log(hashObject.hash)
console.log(hashObject.hash.underline + '\n')

console.log(`# Attempting hash verification`)
console.log(`NOTICE`.bgWhite.black + ` Attempting hash verification\n`)

e = await post(`${config.mattcoinApiUrl}/submission`, {'value': hashObject.val, 'user': config.username} );

serverConfig = await get(`${config.mattcoinApiUrl}/config`)

if(e.response){

console.log(`# Authored block ${serverConfig.blockCount - 1} with success!`)
console.log(`SUCCESS`.bgGreen.black + ` Authored block ${serverConfig.blockCount - 1} with success!\n`)

minerKey = serverConfig.key;
minerDifficulty = serverConfig.difficulty;

salt = serverConfig.salt



}else{

console.log(`# An error occured with the provided hash, mining has continued.`)
console.log(`ERROR`.bgRed.black + ` An error occured with the provided hash, mining has continued.\n`)

minerKey = serverConfig.key;
minerDifficulty = serverConfig.difficulty;

salt = serverConfig.salt

Expand All @@ -140,13 +175,22 @@ async function loop(){
}


figlet('Mattcoin Miner', function(err, data) {
if (err) {
console.dir(err);
return;
}
console.log(data.yellow)
console.log('Created by EncloCreations'.bgYellow.black)
console.log(`Mining as ${config.username}`.bgWhite.black +'\n')

get(`${config.mattcoinApiUrl}/config`).then(function(serverConfig){
get(`${config.mattcoinApiUrl}/config`).then(function(serverConfig){

minerKey = serverConfig.key;
minerDifficulty = serverConfig.difficulty;

console.log(`Starting miner with key ${minerKey} as first block key.`)
lastSecond = Date.now()
loop()
console.log(`NOTICE`.bgWhite.black + ` Starting miner with difficulty of ${minerDifficulty}\n`)
lastSecond = Date.now()
loop()

})
})
});
23 changes: 23 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
},
"homepage": "https://github.com/mtcscratch/miner#readme",
"dependencies": {
"colors": "^1.3.3",
"figlet": "^1.2.1",
"pretty-cli": "0.0.14",
"request": "^2.88.0"
}
}

0 comments on commit 6000965

Please sign in to comment.