Skip to content

Commit

Permalink
Merge pull request #8 from sys-256/patch
Browse files Browse the repository at this point in the history
Patch
  • Loading branch information
sys-256 authored Jul 6, 2021
2 parents dd9987a + 6081e5a commit 98cdd57
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 71 deletions.
67 changes: 5 additions & 62 deletions duino-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,9 @@ function startMiner ()
{
//creates the worker
worker = new Worker("worker.js");
//makes a connection with the server
var socket = new WebSocket("wss://server.duinocoin.com:14808/");
//executes when the server sends a message
socket.onmessage = function (event)
{
//this gets executed when the server sends something including "2.", which is the server version which it automattically sends
if (event.data.includes("2."))
{
//shows the server version in console
console.log("The server is on version " + event.data);
//asks for a job
socket.send("JOB," + username + ",LOW")
}
//this gets executed when the server sends something including "GOOD", which means the share was correct
else if (event.data.includes("GOOD"))
{
//shows in the console that the share was correct
console.log(" and the share was correct!\n");
//shows in the console that it's requesting a new job
console.log("Requesting a new job...\n");
//asks for a new job
socket.send("JOB," + username + ",LOW")
}
//this gets executed when the server sends something including "BAD", which means the share was wrong
else if (event.data.includes("BAD"))
{
//shows in the console that the share was wrong
console.log(" and the share was wrong...\n");
//shows in the console that it's requesting a new job
console.log("Requesting a new job...\n");
//asks for a new job
socket.send("JOB," + username + ",LOW")
}
//this gets executed when the server sends something which doesn't agree with the one's above, which means it's probably a job
else
{
//shows in console that it recieved a new job, and shows the contents
console.log("New job recieved! It contains: " + event.data + ", sending the result to the worker.");
//sends the data to the worker
worker.postMessage(event.data)
//looks at the time in milliseconds, and puts it in a variable
var startingTime = performance.now();
//executes when the worker gives the result
worker.onmessage = function(event)
{
//shows in console that it recieved the workers message
console.log("Result recieved!")
//takes the data from the message and puts in the variable "result"
var result = event.data;
//looks at the time in milliseconds, and puts it in a variable
var endingTime = performance.now();
//calculates the time it took to generate the hash, and divides it by 1000 (to convert it from milliseconds to seconds)
var timeDifference = (endingTime - startingTime) / 1000;
//calculates the hashrate with max 2 decimals
var hashrate = (result / timeDifference).toFixed(2);
//shows the hashrate in the console
console.log("The hashrate is " + hashrate + " H/s. Sending the result back to the server...");
//sends the result to the server
socket.send(result + "," + hashrate + ",Duino-JS v1.0 by Hoiboy19," + rigid);
}
}
}
//passes the username and rigid to the worker
worker.postMessage({
username: username,
rigid: rigid
});
}
86 changes: 77 additions & 9 deletions worker.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,86 @@
//imports the sha1 hex library
importScripts("hashes.js");

onmessage = function (job)
//when the main script send a message (it passes the username and rigid), this will get executed
onmessage = function (event)
{
console.log("Worker: Job recieved!");
var job = job.data.split(",");
var difficulty = job[2];
//gets the username out of the send data and puts it in the variable "username"
var username = event.data.username;
//sets usernameRecieved to true
var usernameRecieved = true;
//gets the rigid out of the send data and puts it in the variable "rigid"
var rigid = event.data.rigid;
//sets rigedRecieved to true
var rigidRecieved = true;

for (result=0; result < 100 * difficulty + 1; result++)
//if usernameRecieved and rigidRecieved are both true, this will get executed aka it makes a connection with the server and starts mining
if (usernameRecieved && rigidRecieved)
{
ducos1 = new Hashes.SHA1().hex(job[0] + result)
if (job[1] === ducos1)
//makes a connection with the server
var socket = new WebSocket("wss://server.duinocoin.com:14808/");
//executes when the server sends a message
socket.onmessage = function (event)
{
console.log("Share found! Sending the result (" + result + ") back to the main script!")
postMessage(result);
//this gets executed when the server sends something including "2.", which is the server version which it automattically sends
if (event.data.includes("2."))
{
//shows the server version in console
console.log("The server is on version " + event.data);
//asks for a job
socket.send("JOB," + username + ",LOW")
}
//this gets executed when the server sends something including "GOOD", which means the share was correct
else if (event.data.includes("GOOD"))
{
//shows in the console that the share was correct
console.log(" and the share was correct!\n");
//shows in the console that it's requesting a new job
console.log("Requesting a new job...\n");
//asks for a new job
socket.send("JOB," + username + ",LOW")
}
//this gets executed when the server sends something including "BAD", which means the share was wrong
else if (event.data.includes("BAD"))
{
//shows in the console that the share was wrong
console.log(" and the share was wrong...\n");
//shows in the console that it's requesting a new job
console.log("Requesting a new job...\n");
//asks for a new job
socket.send("JOB," + username + ",LOW")
}
//this gets executed when the server sends something which doesn't agree with the one's above, which means it's probably a job
else
{
//shows in console that it recieved a new job, and shows the contents
console.log("New job recieved! It contains: " + event.data);
//splits the job in multiple pieces
var job = event.data.split(",");
//the difficulty is piece number 2 (counting from 0), and gets selected as a variable
var difficulty = job[2];
//looks at the time in milliseconds, and puts it in a variable
var startingTime = performance.now();
//it starts hashing
for (result=0; result < 100 * difficulty + 1; result++)
{
//makes a variable called "ducos1", and it contains a calculated SHA1 hash for job[0] + the result
ducos1 = new Hashes.SHA1().hex(job[0] + result)
//executes if the given job is the same as the calculated hash
if (job[1] === ducos1)
{
//looks at the time in milliseconds, and puts it in a variable
var endingTime = performance.now();
//calculates the time it took to generate the hash, and divides it by 1000 (to convert it from milliseconds to seconds)
var timeDifference = (endingTime - startingTime) / 1000;
//calculates the hashrate with max 2 decimals
var hashrate = (result / timeDifference).toFixed(2);
//shows the hashrate in the console
console.log("The hashrate is " + hashrate + " H/s. Sending the result back to the server...");
//sends the result to the server
socket.send(result + "," + hashrate + ",Duino-JS v1.0 by Hoiboy19," + rigid)
}
}
}
}
}
}

0 comments on commit 98cdd57

Please sign in to comment.