Skip to content

Commit

Permalink
Merge pull request #68 from alephium/update-job-timestamp
Browse files Browse the repository at this point in the history
Add a function to update the job timestamp
  • Loading branch information
polarker authored Sep 5, 2024
2 parents 8748445 + 894be18 commit 80d252e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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)
}
})

0 comments on commit 80d252e

Please sign in to comment.