Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mining protocol #66

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/blockTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var BlockTemplate = module.exports = function BlockTemplate(job, timestamp){
this.targetBlob = job.targetBlob;
this.target = bignum.fromBuffer(this.targetBlob);
this.chainIndex = this.fromGroup * constants.GroupSize + this.toGroup;
this.height = job.height

this.registerSubmit = function(nonce){
if (submits.indexOf(nonce) === -1){
Expand All @@ -49,6 +50,7 @@ var BlockTemplate = module.exports = function BlockTemplate(job, timestamp){
toGroup: this.toGroup,
headerBlob: this.headerBlob.toString('hex'),
txsBlob: emptyTxsBlob,
height: this.height
};
}
return this.jobParams;
Expand Down
1 change: 1 addition & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

module.exports = Object.freeze({
MiningProtocolVersion: 0x01,
JobsMessageType: 0x00,
SubmitResultMessageType: 0x01,
SubmitBlockMessageType: 0x00,
Expand Down
12 changes: 7 additions & 5 deletions lib/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ function MinerClient(instance, logger){

this.submit = function(block, callback){
var blockSize = block.length;
var messageSize = 4 + 1 + blockSize; // encodedBlockSize(4 bytes) + messageType(1 byte)
var msgHeader = Buffer.alloc(9); // encodedMessageSize(4 bytes) + encodedBlockSize(4 bytes) + messageType(1 byte)
msgHeader.writeUInt32BE(messageSize);
msgHeader.writeUInt8(constants.SubmitBlockMessageType, 4);
msgHeader.writeUInt32BE(blockSize, 5);
var msgPrefixSize = 1 + 1 + 4; // version(1 byte) + messageType(1 byte) + encodedBlockSize(4 bytes)
var msgSize = msgPrefixSize + blockSize;
var msgHeader = Buffer.alloc(10); // encodedMessageSize(4 bytes) + msgPrefixSize
msgHeader.writeUInt32BE(msgSize);
msgHeader.writeUInt8(constants.MiningProtocolVersion, 4)
msgHeader.writeUInt8(constants.SubmitBlockMessageType, 5);
msgHeader.writeUInt32BE(blockSize, 6);
var data = Buffer.concat([msgHeader, block]);
client.write(data, callback);
}
Expand Down
13 changes: 10 additions & 3 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var jobParser = new Parser()
.buffer('targetBlob', {
'length': 'targetLength'
})
.uint32('height')
.saveOffset('dataLength');

exports.parseMessage = function(buffer, callback){
Expand All @@ -31,8 +32,12 @@ exports.parseMessage = function(buffer, callback){
callback(null, 0);
}
else {
var messageType = buffer.readUInt8(headerSize);
var startOffset = headerSize + 1; // 1 byte message type
var version = buffer.readUInt8(headerSize); // 1 byte version
polarker marked this conversation as resolved.
Show resolved Hide resolved
if (version !== constants.MiningProtocolVersion) {
throw Error(`Invalid protocol version ${version}, expect ${constants.MiningProtocolVersion}`);
}
var messageType = buffer.readUInt8(headerSize + 1); // 1 byte message type
var startOffset = headerSize + 2;
var endOffset = headerSize + bodyLength;
var message = buffer.slice(startOffset, endOffset);
var payload = parse(messageType, message);
Expand Down Expand Up @@ -72,11 +77,13 @@ function parseJobs(buffer){
function parseSubmitResult(buffer){
var fromGroup = buffer.readUInt32BE();
var toGroup = buffer.readUInt32BE(4);
var result = buffer.readUInt8(8);
var blockHash = buffer.slice(8, 40)
var result = buffer.readUInt8(40);
var succeed = result == 1;
return {
fromGroup: fromGroup,
toGroup: toGroup,
blockHash: blockHash,
succeed: succeed
};
}
13 changes: 7 additions & 6 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,21 @@ var pool = module.exports = function pool(config, logger){
break;
case constants.SubmitResultMessageType:
var result = message.payload;
handleSubmitResult(result.fromGroup, result.toGroup, result.succeed);
handleSubmitResult(result);
break;
default:
logger.error('Invalid message type: ' + message.type);
}
}

function handleSubmitResult(fromGroup, toGroup, succeed){
var chainIndex = chainIndexStr(fromGroup, toGroup);
if (succeed){
logger.info('Submit block succeed for chainIndex: ' + chainIndex);
function handleSubmitResult(result){
var chainIndex = chainIndexStr(result.fromGroup, result.toGroup);
var blockHashHex = result.blockHash.toString('hex');
if (result.succeed){
logger.info(`Submit block ${blockHashHex} succeed for chainIndex ${chainIndex}`);
}
else {
logger.error('Submit block failed for chainIndex: ' + chainIndex);
logger.error(`Submit block ${blockHashHex} failed for chainIndex ${chainIndex}`);
}
}

Expand Down
Loading