Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
sys-256 committed Aug 13, 2021
1 parent 935ce11 commit 134bfaa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 55 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ To use the miner, download [duino-js.min.js, worker.min.js and hashes.min.js fro
```html
<script src="duino-js.min.js"></script> <!--imports the Duino-JS miner-->
<script>
var username = "Hoiboy19"; //put your username here (e.g. revox, ericddm, snehaislove or Hoiboy19), the default is Hoiboy19.
var rigid = "Duino-JS"; //If you want to change the rig ID, you can change this. If you want to keep using "Duino-JS", you can remove this line.
username = "Hoiboy19"; //put your username here (e.g. revox, ericddm, snehaislove or Hoiboy19), the default is Hoiboy19.
rigid = "Duino-JS"; //If you want to change the rig ID, you can change this. If you want to keep using "Duino-JS", you can remove this line.
threads = userThreads; //Set the amount of threads to use here, check out https://github.com/sys-256/Duino-JS for more options. The default is 1.
startMiner(); //starts the miner
</script>
Expand Down
39 changes: 12 additions & 27 deletions duino-js.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
//puts the amount of threads the user has in a variable
var userThreads = navigator.hardwareConcurrency;
const userThreads = navigator.hardwareConcurrency;

//my username, which it autmattically mines for (no worries, if you specify you own username in a script below the script:src markup, it will use your username)
var username = "Hoiboy19";
let username = "Hoiboy19";

//the standerd rig ID, but if you specify your own in another script, it will you that one
var rigid = "Duino-JS";
let rigid = "Duino-JS";

//sets the threads to 1, but if you specify your own in another script, it will use that one
let threads = 1;

function startMiner ()
{
function startMiner() {
//checks if threads is 1 or bigger, and if it isn't it sets it to 1
if (threads < 1)
{
threads = 1;
}
if (threads < 1) threads = 1;
//checks if threads is 8 or smaller, and if it isn't it sets it to 8 because it won't be profitible when mining with more than 8 threads
if (threads > 8)
{
threads = 8;
}
if (threads > 8) threads = 8;
//checks if threads is smaller then the amount of threads the user has, and if it isn't, it sets threads to the amount of threads the user has
if (threads > userThreads)
{
threads = userThreads;
}
if (threads > userThreads) threads = userThreads;

//sets workerVer to 0
let workerVer = 0;

//creates threads workers (so if threads is 1, it would make 1 worker, and if threads was 12, it would make 12 workers)
for (let workersAmount = 0; workersAmount < threads; workersAmount++)
{
for (let workersAmount = 0; workersAmount < threads; workersAmount++) {
//creates the worker
worker = new Worker("worker.js");
//passes the username and rigid to the worker
worker.postMessage({
username: username,
rigid: rigid,
workerVer: workerVer
});
//sends the username, rigid and the workers version to the worker
worker.postMessage([username, rigid, workerVer]);
//adds 1 to the workerVer
workerVer++;
}
}
}
44 changes: 18 additions & 26 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,47 @@
importScripts("hashes.js");

//when the main script send a message (it passes the username and rigid), this will get executed
onmessage = function (event)
{
onmessage = (event) => {
//gets the username out of the send data and puts it in the variable "username"
var username = event.data.username;
const username = event.data[0];
//gets the rigid out of the send data and puts it in the variable "rigid"
var rigid = event.data.rigid;
const rigid = event.data[1];
//gets the workerVer out of the send data and puts it in the variable "workerVer"
var workerVer = event.data.workerVer;
const workerVer = event.data[2];

//makes a connection with the server
var socket = new WebSocket("wss://server.duinocoin.com:14808/");
let socket = new WebSocket("wss://server.duinocoin.com:14808/");
//executes when the server sends a message
socket.onmessage = function (event)
{
socket.onmessage = (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."))
{
if (event.data.includes("2.")) {
//shows the server version in console
console.log("CPU" + workerVer + ": The server is on version " + event.data);
//shows in the console that it's requesting a new job
console.log("CPU" + workerVer + ": Requesting a new job...\n");
//asks for a new job
socket.send("JOB," + username + ",LOW")
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"))
{
else if (event.data.includes("GOOD")) {
//shows in the console that the share was correct
console.log("CPU" + workerVer + ": and the share was correct!\n");
//shows in the console that it's requesting a new job
console.log("CPU" + workerVer + ": Requesting a new job...\n");
//asks for a new job
socket.send("JOB," + username + ",LOW")
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"))
{
else if (event.data.includes("BAD")) {
//shows in the console that the share was wrong
console.log("CPU" + workerVer + ": and the share was wrong...\n");
//shows in the console that it's requesting a new job
console.log("CPU" + workerVer + ": Requesting a new job...\n");
//asks for a new job
socket.send("JOB," + username + ",LOW")
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
{
else {
//shows in console that it recieved a new job, and shows the contents
console.log("CPU" + workerVer + ": New job recieved! It contains: " + event.data);
//splits the job in multiple pieces
Expand All @@ -58,13 +52,11 @@ onmessage = function (event)
//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++)
{
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)
ducos1 = new Hashes.SHA1().hex(job[0] + result);
//executes if the given job is the same as the calculated hash
if (job[1] === ducos1)
{
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)
Expand All @@ -74,11 +66,11 @@ onmessage = function (event)
//shows the hashrate in the console
console.log("CPU" + workerVer + ": 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 v3.2 by sys-256," + rigid)
socket.send(result + "," + hashrate + ",Duino-JS v3.2 by sys-256," + rigid);
//breaks the script so it stops calculating the other possible hashes
break;
}
}
}
}
}
}

0 comments on commit 134bfaa

Please sign in to comment.