Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
argv.server = argvCmd.server || argv.server;
argv.cpu = argvCmd.cpu || argv.cpu;
console.log(argv);
if (!argv.address) {
await logWithoutExit('Usage: node index.js --address=<address> [--name=] [--thread=] [--server=] [--percent=] [--cpu=]');
}
const address = argv.address;
let name = argv.name || '*';
// auto set name
if (name === '*') {
name = [ip.address(), os.platform(), os.arch(), os.release()].join(' ');
Log.w(`auto set name to ${name}`)
}
let thread = parseInt(argv.thread);
const max_thread = os.cpus().length;
if (thread > max_thread) {
Log.w(`thread ${thread} larger than CPU threads ${max_thread}, force thread to ${max_thread}`);
thread = max_thread;
}
if (thread === 0 || !Number.isInteger(thread)) {
thread = max_thread > 1 ? max_thread - 1 : 1;
Log.w(`auto set thread to ${thread}`);
}
if (thread < 0) {
thread = max_thread;
Log.w(`auto set thread to ${thread}`);
}</address>
switchInedx = 0;
}
server = argv.server[switchInedx];
Log.w(`switch to server ${server}`);
} else {
server = argv.server;
}
const percent = parseFloat(argv.percent || 100);
const event = new EventEmitter();
// choose cpu version
let cpu;
if (argv.cpu && CPUType[argv.cpu]) {
Log.w('CPU type given by user, choose ' + argv.cpu + ' version.');
cpu = CPUType[argv.cpu];
} else {
cpu = await autoDetectCPU();
}
if (thread > 128) {
logWithoutExit('Error: thread too large');
} else if (thread <= 0) {
logWithoutExit('Error: thread too small');
} else if (address && address.length !== 44) {
logWithoutExit('Error: address format error');
} else if (name && name.length > 200) {
logWithoutExit('Error: name too long');
} else if (percent < 50 || percent > 100) {
logWithoutExit('Error: percent need between 50 to 100');
} else {
// for special packaging by skypool owners
global.skypool_package = false;
process.env.UV_THREADPOOL_SIZE = 128;
const os = require('os');
const util = require('util');
const ip = require('ip');
const EventEmitter = require('events').EventEmitter;
const Miner = require('./Miner.js');
const Log = require('@nimiq/core').Log;
const setTimeoutPromise = util.promisify(setTimeout);
const CPUType = require('./CPUType');
async function logWithoutExit(text) {
for (;;) {
Log.e(text);
await setTimeoutPromise(2000);
}
}
let miner = null;
let autoRestartInterval = null;
let switchInedx = 0;
const autoDetectCPU = async () => {
const addon = require('./build/Release/detectCPU');
let name = argv.name || '*';
// auto set name
if (name === '*') {
name = [ip.address(), os.platform(), os.arch(), os.release()].join(' ');
Log.w(`auto set name to ${name}`)
}
let thread = parseInt(argv.thread);
const max_thread = os.cpus().length;
if (thread > max_thread) {
Log.w(`thread ${thread} larger than CPU threads ${max_thread}, force thread to ${max_thread}`);
thread = max_thread;
}
if (thread === 0 || !Number.isInteger(thread)) {
thread = max_thread > 1 ? max_thread - 1 : 1;
Log.w(`auto set thread to ${thread}`);
}
if (thread < 0) {
thread = max_thread;
Log.w(`auto set thread to ${thread}`);
}
// switch server
let server;
if (typeof argv.server === 'object') {
if (switchInedx >= argv.server.length) {
switchInedx = 0;
}
server = argv.server[switchInedx];
Log.w(`switch to server ${server}`);
} else {
server = argv.server;
const startNonce4096 = taskObject.workrange4096queue.shift();
// if this task run out
if (startNonce4096 === undefined) {
this._taskQueue.shift();
continue;
}
// if this task run to threshold, pull next task
if (taskObject.workrange4096queue.length === this._workrange4096pullThreshold) {
this._pull();
}
const startNonce = startNonce4096 * RANGE;
const endNonce = startNonce + RANGE;
percentTime = new Date();
const result = await this._multiMine(new Nimiq.SerialBuffer(taskObject.header), this._compact, startNonce, endNonce);
percentDuration = new Date() - percentTime;
if (result && taskObject.timeNonce36 === this._timeNonce36) {
Log.i(Miner, `mined a share, timeNonce36 ${taskObject.timeNonce36}, nonce ${result.nonce}`);
this._push(
taskObject.timeNonce36,
result.nonce,
taskObject.index,
);
}
this._currHashrate += RANGE;
if (this._percentX > 0) {
await setTimeoutPromise(percentDuration * this._percentX);
}
+ /* hashAlgo */ 1
+ hash.byteLength
+ /* hashCount */ 1
+ /* timeout */ 4;
const buffer = new Nimiq.SerialBuffer(bufferSize);
sender.serialize(buffer);
recipient.serialize(buffer);
buffer.writeUint8(hashAlgo);
buffer.write(hash);
buffer.writeUint8(hashCount);
buffer.writeUint32(timeout);
recipient = Nimiq.Address.CONTRACT_CREATION;
const recipientType = Nimiq.Account.Type.HTLC;
const flags = Nimiq.Transaction.Flag.CONTRACT_CREATION;
return new Nimiq.ExtendedTransaction(sender, Nimiq.Account.Type.BASIC, recipient, recipientType,
value, 0, $.blockchain.height + 1, flags, buffer);
}
function generateHtlcTransaction(sender, recipient, hash, value, timeout) {
const hashAlgo = Nimiq.Hash.Algorithm.SHA256
const hashCount = 1
value = Nimiq.Policy.coinsToSatoshis(value)
timeout = $.blockchain.height + timeout
const bufferSize = sender.serializedSize
+ recipient.serializedSize
+ /* hashAlgo */ 1
+ hash.byteLength
+ /* hashCount */ 1
+ /* timeout */ 4;
const buffer = new Nimiq.SerialBuffer(bufferSize);
sender.serialize(buffer);
recipient.serialize(buffer);
buffer.writeUint8(hashAlgo);
buffer.write(hash);
buffer.writeUint8(hashCount);
buffer.writeUint32(timeout);
recipient = Nimiq.Address.CONTRACT_CREATION;
const recipientType = Nimiq.Account.Type.HTLC;
const flags = Nimiq.Transaction.Flag.CONTRACT_CREATION;
return new Nimiq.ExtendedTransaction(sender, Nimiq.Account.Type.BASIC, recipient, recipientType,
value, 0, $.blockchain.height + 1, flags, buffer);
}
function generateHtlcTransaction(sender, recipient, hash, value, timeout) {
const hashAlgo = Nimiq.Hash.Algorithm.SHA256
const hashCount = 1
value = Nimiq.Policy.coinsToSatoshis(value)
timeout = $.blockchain.height + timeout
const bufferSize = sender.serializedSize
+ recipient.serializedSize
+ /* hashAlgo */ 1
+ hash.byteLength
+ /* hashCount */ 1
+ /* timeout */ 4;
const buffer = new Nimiq.SerialBuffer(bufferSize);
sender.serialize(buffer);
recipient.serialize(buffer);
buffer.writeUint8(hashAlgo);
buffer.write(hash);
buffer.writeUint8(hashCount);
buffer.writeUint32(timeout);
const util = require('util');
const os = require('os');
const atob = require('atob');
const WebSocketClient = require('websocket').client;
const Nimiq = require('@nimiq/core');
const Log = Nimiq.Log;
const BigNumber = Nimiq.BigNumber;
let NodeNative;
const P = require('./Protocol.js');
const setTimeoutPromise = util.promisify(setTimeout);
const RANGE = 4096;
const WORKLOADS_PER_THREAD_PC = 75; // 1 thread is 75 * 4096 nonces
const DIFFICULT_PER_THREAD_PC = 2; // 1 thread is 2 difficulty, one share equal to 2*thread*Math.pow(2 ,16) Hashes, 2*thread*65536
class Miner {
constructor(server, address, name, threads, percent, event, cpu) {
/** miner metadata */
this._version = 8;
this._platform = [os.platform(), os.arch(), os.release()].join(' ');
const bufferSize = sender.serializedSize
+ recipient.serializedSize
+ /* hashAlgo */ 1
+ hash.byteLength
+ /* hashCount */ 1
+ /* timeout */ 4;
const buffer = new Nimiq.SerialBuffer(bufferSize);
sender.serialize(buffer);
recipient.serialize(buffer);
buffer.writeUint8(hashAlgo);
buffer.write(hash);
buffer.writeUint8(hashCount);
buffer.writeUint32(timeout);
recipient = Nimiq.Address.CONTRACT_CREATION;
const recipientType = Nimiq.Account.Type.HTLC;
const flags = Nimiq.Transaction.Flag.CONTRACT_CREATION;
return new Nimiq.ExtendedTransaction(sender, Nimiq.Account.Type.BASIC, recipient, recipientType,
value, 0, $.blockchain.height + 1, flags, buffer);
}