diff --git a/lib/http/rpc.js b/lib/http/rpc.js index c903079f..a378c1ca 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -1256,7 +1256,7 @@ RPC.prototype._createTemplate = co(function* _createTemplate(version, coinbase, bits: util.hex32(attempt.bits), noncerange: '00000000ffffffff', curtime: attempt.ts, - mintime: attempt.ts, + mintime: attempt.mtp + 1, maxtime: attempt.ts + 7200, expires: attempt.ts + 7200, sigoplimit: consensus.MAX_BLOCK_SIGOPS_COST / scale | 0, @@ -2153,7 +2153,9 @@ RPC.prototype.getTemplate = co(function* getTemplate() { this.bindChain(); - if (!attempt) { + if (attempt) { + this.miner.updateTime(attempt); + } else { attempt = yield this.miner.createBlock(); this.attempt = attempt; this.lastActivity = util.now(); @@ -2169,6 +2171,7 @@ RPC.prototype.updateWork = co(function* updateWork() { this.bindChain(); if (attempt) { + this.miner.updateTime(attempt); if (++this.nonce2 === 0x100000000) { this.nonce2 = 0; this.nonce1++; diff --git a/lib/mining/miner.js b/lib/mining/miner.js index bfb3f437..1a3876b3 100644 --- a/lib/mining/miner.js +++ b/lib/mining/miner.js @@ -151,6 +151,7 @@ Miner.prototype._createBlock = co(function* createBlock(tip, address) { ts: ts, bits: target, locktime: locktime, + mtp: mtp, flags: this.chain.state.flags, address: address, coinbaseFlags: this.options.coinbaseFlags, @@ -192,6 +193,15 @@ Miner.prototype._createBlock = co(function* createBlock(tip, address) { return attempt; }); +/** + * Update block timestamp. + * @param {BlockTemplate} attempt + */ + +Miner.prototype.updateTime = function updateTime(attempt) { + attempt.ts = Math.max(this.network.now(), attempt.mtp + 1); +}; + /** * Create a cpu miner job. * @method diff --git a/lib/mining/template.js b/lib/mining/template.js index 611f2a2b..37f4c898 100644 --- a/lib/mining/template.js +++ b/lib/mining/template.js @@ -42,6 +42,7 @@ function BlockTemplate(options) { this.bits = 0; this.target = encoding.ZERO_HASH; this.locktime = 0; + this.mtp = 0; this.flags = 0; this.coinbaseFlags = DUMMY; this.witness = false; @@ -100,6 +101,11 @@ BlockTemplate.prototype.fromOptions = function fromOptions(options) { this.locktime = options.locktime; } + if (options.mtp != null) { + assert(typeof options.mtp === 'number'); + this.mtp = options.mtp; + } + if (options.flags != null) { assert(typeof options.flags === 'number'); this.flags = options.flags;