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

Add a function to update the job timestamp #68

Merged
merged 2 commits into from
Sep 5, 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
10 changes: 10 additions & 0 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ function parseSubmitResult(buffer){
succeed: succeed
};
}

exports.updateJobTimestamp = function(job, updater) {
var headerBlob = job.headerBlob
var encodedTsLength = 8
var encodedTargetLength = 4
var tsOffset = job.headerBlob.length - (encodedTsLength + encodedTargetLength)
var jobTs = Number(headerBlob.readBigUInt64BE(tsOffset))
var newJobTs = updater(jobTs)
headerBlob.writeBigUInt64BE(BigInt(newJobTs), tsOffset)
}
38 changes: 38 additions & 0 deletions test/messagesTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { expect } = require('chai');
const { randomBytes } = require('crypto');
const constants = require('../lib/constants');
const { updateJobTimestamp } = require("../lib/messages")

it('should update the job timestamp', function(){
function randomHeaderBlob(ts) {
var encodedTs = Buffer.alloc(8)
encodedTs.writeBigUInt64BE(BigInt(ts))
return Buffer.concat([
randomBytes(24), // nonce
randomBytes(1), // version
randomBytes(1 + (2 * constants.GroupSize - 1) * 32), // block deps
randomBytes(32), // state hash
randomBytes(32), // txs hash
encodedTs,
randomBytes(4), // target
])
}

for (var i = 1; i <= 10; i += 1) {
var jobTs = Date.now() + (i * 60 * 1000)
var job = { headerBlob: randomHeaderBlob(jobTs) }
var prevHeaderBlob = Buffer.from(job.headerBlob)

updateJobTimestamp(job, _ => jobTs)
expect(job.headerBlob).to.deep.equal(prevHeaderBlob)

var delta = i * 60 * 1000
updateJobTimestamp(job, ts => ts + delta)
expect(job.headerBlob).to.not.deep.equal(prevHeaderBlob)

var tsOffset = job.headerBlob.length - 12
prevHeaderBlob.writeBigUInt64BE(BigInt(jobTs + delta), tsOffset)
expect(job.headerBlob).to.deep.equal(prevHeaderBlob)
expect(Number(job.headerBlob.readBigUInt64BE(tsOffset))).to.equal(jobTs + delta)
}
})
Loading