Skip to content

Commit

Permalink
Initial Ravencoin commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JCThePants committed Jun 8, 2021
1 parent 0981bd3 commit 39652fb
Show file tree
Hide file tree
Showing 15 changed files with 532 additions and 202 deletions.
97 changes: 94 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@ ref-stratum-ravencoin
This Reference Stratum is a simple implementation used as a basis for testing, experimentation, and
demonstration purposes. It is not intended for production use.

This project has been developed and tested on [Node v10.17](https://nodejs.org/) and [Ubuntu 16.04](http://releases.ubuntu.com/16.04/)

## Install ##

__NodeJS v10 (Ubuntu)__
```bash
# Optional: uninstall current version
sudo apt-get remove node
sudo apt-get remove nodejs

# Install version 10.x
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install nodejs -y
```

__Dependencies__
```bash
# (ubuntu) build essentials required to compile KawPOW verification
sudo apt-get install build-essentials

# Dependencies may require that you have a Github personal access token to install.
npm config set @mintpond:registry https://npm.pkg.github.com/mintpond
npm config set //npm.pkg.github.com/:_authToken <PERSONAL_ACCESS_TOKEN>
Expand All @@ -15,9 +32,83 @@ npm config set //npm.pkg.github.com/:_authToken <PERSONAL_ACCESS_TOKEN>

__Download from Github__
```bash
git clone https://github.com/MintPond/ref-stratum
git clone https://github.com/MintPond/ref-stratum-ravencoin

# install
cd ref-stratum
cd ref-stratum-ravencoin
npm install
```
```

__Install in a Project__
```bash
npm config set @mintpond:registry https://npm.pkg.github.com/mintpond
npm config set //npm.pkg.github.com/:_authToken <PERSONAL_ACCESS_TOKEN>

npm install @mintpond/ref-stratum-ravencoin --save
```

## Usage ##
The stratum can be used as a module in a pool:
```javascript
const Stratum = require('@mintpond/ref-stratum-ravencoin').Stratum;

class MyStratum extends Stratum {
/* Override */
canAuthorizeWorker(client, callback) {
// implement your own logic
if (client.minerAddress === 'bad') {
// do not authorize worker
callback(null/*error*/, false/*isAuthorized*/);
}
else {
// authorize worker
callback(null/*error*/, true/*isAuthorized*/);
}
}
}

const stratum = new MyStratum({
coinbaseAddress: 'n1W99FwjX2HgBxVN9aYvpn91uhZfi9B8fj', // address that receives block reward
blockBrand: '/@mintpond/ref-stratum/', // Branding string added to every block found
host: "0.0.0.0", // address the stratum will listen on
port: {
number: 3010, // port the stratum will listen on
diff: 10 // stratum difficulty
},
rpc: {
host: '172.16.3.102', // Ravencoin daemon RPC host
port: 17011, // Ravencoin daemon RPC port
user: 'rpcuser', // Ravencoin daemon RPC user
password: "x" // Ravencoin daemon RPC password
},
jobUpdateInterval: 55, // Broadcast job updates every n seconds
blockPollIntervalMs: 250 // Check for new blocks every n milliseconds
});

stratum.on(Stratum.EVENT_SHARE_SUBMITTED, ev => {
console.log(ev.share);
});

stratum.init();
```

### Start Script ###
There is a start script (`start.js`) included which contains further
examples. It can also be run in order to get a Stratum going for test
purposes. You will need to open and modify the config inside before
running it.
```
> node start
```

## Areas of Interest ##
- [ClientReader](libs/class.ClientReader.js) - Handles messages received from a client.
- [ClientWriter](libs/class.ClientWriter.js) - Handles sending messages to a client.
- [Coinbase](libs/class.Coinbase.js) - Creates coinbase transaction.
- [Share](libs/class.Share.js) - Processes and validates shares, creates blocks.
- [Socket](libs/class.Socket.js) - Handles binary JSON serialization and deserialization.
- [algorithm](libs/service.algorithm.js) - Contains KawPOW constants and hash verification.


## Resources ##
- [MintPond Mining Pool](https://mintpond.com/#!/ravencoin) - Ravencoin mining pool.
8 changes: 0 additions & 8 deletions libs/class.Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const
Job = require('./class.Job');

const TIMEOUT = 600;
const EXTRANONCE2_SIZE = 4;


class Client extends EventEmitter {
Expand Down Expand Up @@ -58,7 +57,6 @@ class Client extends EventEmitter {
_._isAuthorized = false;
_._disconnectReason = '';


_._socket.on(TcpSocket.EVENT_MESSAGE_IN, _._onSocketMessageIn.bind(_));
_._socket.on(TcpSocket.EVENT_MALFORMED_MESSAGE, _._onMalformedMessage.bind(_));
_._socket.on(TcpSocket.EVENT_DISCONNECT, _._onDisconnect.bind(_));
Expand Down Expand Up @@ -121,12 +119,6 @@ class Client extends EventEmitter {
*/
get extraNonce1Hex() { return this._extraNonce1Hex; }

/**
* Get the clients assigned extraNonce2 size.
* @returns {number}
*/
get extraNonce2Size() { return EXTRANONCE2_SIZE; }

/**
* Get the client socket.
* @returns {TcpSocket}
Expand Down
73 changes: 67 additions & 6 deletions libs/class.ClientReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
const
precon = require('@mintpond/mint-precon'),
mu = require('@mintpond/mint-utils'),
buffers = require('@mintpond/mint-utils').buffers,
Share = require('./class.Share'),
StratumError = require('./class.StratumError');

const EMPTY_BUFFER = Buffer.alloc(0);


class ClientReader {

Expand Down Expand Up @@ -194,18 +197,18 @@ class ClientReader {
}

const jobIdHex = _._hex(message.params[1]);
const extraNonce2Hex = _._hex(message.params[2]);
const nTimeHex = _._hex(message.params[3]);
const nonceHex = _._hex(message.params[4]);
const nonceBuf = _._toBufferLE(message.params[2]);
const headerHashBuf = _._toBuffer(message.params[3]);
const mixHashBuf = _._toBuffer(message.params[4]);

const share = new Share({
client: _._client,
stratum: _._stratum,
workerName: workerName,
jobIdHex: jobIdHex,
extraNonce2Hex: extraNonce2Hex,
nTimeHex: nTimeHex,
nonceHex: nonceHex
nonceBuf: nonceBuf,
headerHashBuf: headerHashBuf,
mixHashBuf: mixHashBuf
});

const isValid = share.validate();
Expand Down Expand Up @@ -239,6 +242,64 @@ class ClientReader {

return value;
}


_toBufferLE(val) {

let value;

if (Buffer.isBuffer(val)) {
// conversion not needed
value = val;
}
else if (mu.isString(val)) {

if (val.startsWith('0x'))
val = val.substr(2);

try {
// convert hex to LE bytes
value = buffers.hexToLE(val);
}
catch (err) {
value = EMPTY_BUFFER;
}
}
else {
value = EMPTY_BUFFER;
}

return value;
}


_toBuffer(val) {

let value;

if (Buffer.isBuffer(val)) {
// conversion not needed
value = val;
}
else if (mu.isString(val)) {

if (val.startsWith('0x'))
val = val.substr(2);

try {
// convert hex directly to bytes
value = Buffer.from(val, 'hex');
}
catch (err) {
value = EMPTY_BUFFER;
}
}
else {
value = EMPTY_BUFFER;
}

return value;
}
}

module.exports = ClientReader;
39 changes: 13 additions & 26 deletions libs/class.ClientWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const
precon = require('@mintpond/mint-precon'),
mu = require('@mintpond/mint-utils'),
buffers = require('@mintpond/mint-utils').buffers,
Job = require('./class.Job'),
StratumError = require('./class.StratumError');
StratumError = require('./class.StratumError'),
algorithm = require('./service.algorithm');


class ClientWriter {
Expand Down Expand Up @@ -52,18 +54,10 @@ class ClientWriter {
const replyId = args.replyId;
const subscriptionIdHex = _._client.subscriptionIdHex;
const extraNonce1Hex = _._client.extraNonce1Hex;
const extraNonce2Size = _._client.extraNonce2Size;

_._socket.send({
id: replyId,
result: [
[
['mining.set_difficulty', subscriptionIdHex],
['mining.notify', subscriptionIdHex]
],
extraNonce1Hex,
extraNonce2Size
],
result: [subscriptionIdHex, extraNonce1Hex],
error: null
});
}
Expand All @@ -72,35 +66,28 @@ class ClientWriter {
miningNotify(args) {
precon.instanceOf(args.job, Job, 'job');
precon.boolean(args.cleanJobs, 'cleanJobs');
precon.opt_positiveNumber(args.diff, 'diff');
precon.positiveNumber(args.diff, 'diff');

const _ = this;

const job = args.job;
const cleanJobs = args.cleanJobs;
const diff = args.diff;

if (mu.isNumber(diff)) {
_._socket.send({
id: null,
method: 'mining.set_difficulty',
params: [diff]
});
}
const nDiff = diff / algorithm.multiplier;
const targetBuffer = buffers.packUInt256LE(algorithm.diff1 / nDiff);

_._socket.send({
id: null,
method: 'mining.notify',
params: [
/* 0 Job Id */ job.idHex,
/* 1 prevhash */ job.prevBlockId,
/* 2 coinb1 */ job.coinbase.coinbase1Buf.toString('hex'),
/* 3 coinb2 */ job.coinbase.coinbase2Buf.toString('hex'),
/* 4 merkle_branch */ job.merkleTree.branchHexArr,
/* 5 version */ job.versionHex,
/* 6 nbits (diff) */ job.bitsHex,
/* 7 ntime */ job.curTimeHex,
/* 8 clean_jobs */ cleanJobs
/* 1 header hash */ job.getHeaderHashBuf(_._client).toString('hex'),
/* 2 seed hash */ job.seedHashBuf.toString('hex'),
/* 3 min target */ buffers.leToHex(targetBuffer),
/* 4 clean_jobs */ cleanJobs,
/* 4 block height */ job.height,
/* 6 nbits (diff) */ buffers.leToHex(job.bitsBuf)
]
});
}
Expand Down
12 changes: 6 additions & 6 deletions libs/class.Coinbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const
precon = require('@mintpond/mint-precon'),
mu = require('@mintpond/mint-utils'),
buffers = require('@mintpond/mint-utils').buffers,
Client = require('./class.Client'),
Share = require('./class.Share');

const
Expand Down Expand Up @@ -59,21 +60,20 @@ class Coinbase {


/**
* Use information from a share to serialize coinbase.
* Use information from a client to serialize coinbase.
*
* @param share {Share}
* @param client {Client}
* @returns {Buffer}
*/
serialize(share) {
precon.instanceOf(share, Share, 'share');
serialize(client) {
precon.notNull(client, 'client');

const _ = this;
const coinbase1Buf = _.coinbase1Buf;
const coinbase2Buf = _.coinbase2Buf;
return Buffer.concat([
coinbase1Buf,
Buffer.from(share.extraNonce1Hex, 'hex'),
Buffer.from(share.extraNonce2Hex, 'hex'),
Buffer.from(client.extraNonce1Hex),
coinbase2Buf
]);
}
Expand Down
Loading

0 comments on commit 39652fb

Please sign in to comment.