Skip to content

Commit

Permalink
Add a function to update the job timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Sep 4, 2024
1 parent 2982902 commit 9e01df7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ function parseSubmitResult(buffer){
succeed: succeed
};
}

exports.updateJobTimestamp = function(job, newTimestamp) {
var headerBlob = job.headerBlob
var encodedTsLength = 8
var encodedTargetLength = 4
var tsOffset = job.headerBlob.length - (encodedTsLength + encodedTargetLength)
headerBlob.writeBigUInt64BE(BigInt(newTimestamp), tsOffset)
}
37 changes: 37 additions & 0 deletions test/messagesTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 now = Date.now() + (i * 60 * 1000)
var job = { headerBlob: randomHeaderBlob(now) }
var prevHeaderBlob = Buffer.from(job.headerBlob)

updateJobTimestamp(job, now)
expect(job.headerBlob).to.deep.equal(prevHeaderBlob)

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

prevHeaderBlob.writeBigUInt64BE(BigInt(newTs), prevHeaderBlob.length - 12)
expect(job.headerBlob).to.deep.equal(prevHeaderBlob)
}
})

0 comments on commit 9e01df7

Please sign in to comment.