rpc: fix wit commitment for getblocktemplate.
This commit is contained in:
parent
a266391f1b
commit
023591978a
@ -1307,6 +1307,8 @@ RPC.prototype._createTemplate = co(function* _createTemplate(version, coinbase,
|
|||||||
output = tx.outputs[tx.outputs.length - 1];
|
output = tx.outputs[tx.outputs.length - 1];
|
||||||
assert(output.script.isCommitment());
|
assert(output.script.isCommitment());
|
||||||
json.default_witness_commitment = output.script.toJSON();
|
json.default_witness_commitment = output.script.toJSON();
|
||||||
|
} else if (rules && rules.indexOf('segwit') !== -1) {
|
||||||
|
json.default_witness_commitment = attempt.getWitnessScript().toJSON();
|
||||||
}
|
}
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
|
|||||||
@ -52,6 +52,7 @@ function BlockTemplate(options) {
|
|||||||
this.interval = 210000;
|
this.interval = 210000;
|
||||||
this.fees = 0;
|
this.fees = 0;
|
||||||
this.tree = new MerkleTree();
|
this.tree = new MerkleTree();
|
||||||
|
this.commitment = DUMMY;
|
||||||
this.left = DUMMY;
|
this.left = DUMMY;
|
||||||
this.right = DUMMY;
|
this.right = DUMMY;
|
||||||
this.items = [];
|
this.items = [];
|
||||||
@ -188,6 +189,15 @@ BlockTemplate.prototype.getWitnessHash = function getWitnessHash() {
|
|||||||
return crypto.hash256(data);
|
return crypto.hash256(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create witness commitment script.
|
||||||
|
* @returns {Script}
|
||||||
|
*/
|
||||||
|
|
||||||
|
BlockTemplate.prototype.getWitnessScript = function getWitnessScript() {
|
||||||
|
return Script.fromCommitment(this.commitment);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the target (bits).
|
* Set the target (bits).
|
||||||
* @param {Number} bits
|
* @param {Number} bits
|
||||||
@ -222,14 +232,15 @@ BlockTemplate.prototype.getReward = function getReward() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the default coinbase.
|
* Initialize the default coinbase.
|
||||||
|
* @param {Buffer} hash - Witness commitment hash.
|
||||||
* @returns {TX}
|
* @returns {TX}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
BlockTemplate.prototype.createCoinbase = function createCoinbase() {
|
BlockTemplate.prototype.createCoinbase = function createCoinbase(hash) {
|
||||||
var scale = consensus.WITNESS_SCALE_FACTOR;
|
var scale = consensus.WITNESS_SCALE_FACTOR;
|
||||||
var cb = new TX();
|
var cb = new TX();
|
||||||
var padding = 0;
|
var padding = 0;
|
||||||
var input, output, commit, hash;
|
var input, output, commit;
|
||||||
|
|
||||||
// Coinbase input.
|
// Coinbase input.
|
||||||
input = new Input();
|
input = new Input();
|
||||||
@ -241,11 +252,11 @@ BlockTemplate.prototype.createCoinbase = function createCoinbase() {
|
|||||||
input.script.push(encoding.ZERO_HASH160);
|
input.script.push(encoding.ZERO_HASH160);
|
||||||
|
|
||||||
// Smaller nonce for good measure.
|
// Smaller nonce for good measure.
|
||||||
input.script.push(util.nonce().slice(0, 4));
|
input.script.push(util.nonce(4));
|
||||||
|
|
||||||
// Extra nonce: incremented when
|
// Extra nonce: incremented when
|
||||||
// the nonce overflows.
|
// the nonce overflows.
|
||||||
input.script.push(extraNonce(0, 0));
|
input.script.push(encoding.ZERO_U64);
|
||||||
|
|
||||||
input.script.compile();
|
input.script.compile();
|
||||||
|
|
||||||
@ -269,7 +280,6 @@ BlockTemplate.prototype.createCoinbase = function createCoinbase() {
|
|||||||
if (this.witness) {
|
if (this.witness) {
|
||||||
// Commitment output.
|
// Commitment output.
|
||||||
commit = new Output();
|
commit = new Output();
|
||||||
hash = this.getWitnessHash();
|
|
||||||
commit.script.fromCommitment(hash);
|
commit.script.fromCommitment(hash);
|
||||||
cb.outputs.push(commit);
|
cb.outputs.push(commit);
|
||||||
}
|
}
|
||||||
@ -320,7 +330,8 @@ BlockTemplate.prototype.createCoinbase = function createCoinbase() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
BlockTemplate.prototype.refresh = function refresh() {
|
BlockTemplate.prototype.refresh = function refresh() {
|
||||||
var cb = this.createCoinbase();
|
var hash = this.getWitnessHash();
|
||||||
|
var cb = this.createCoinbase(hash);
|
||||||
var raw = cb.toNormal();
|
var raw = cb.toNormal();
|
||||||
var size = 0;
|
var size = 0;
|
||||||
var left, right;
|
var left, right;
|
||||||
@ -338,6 +349,7 @@ BlockTemplate.prototype.refresh = function refresh() {
|
|||||||
size += 4 + 4; // nonce1 + nonce2
|
size += 4 + 4; // nonce1 + nonce2
|
||||||
right = raw.slice(size);
|
right = raw.slice(size);
|
||||||
|
|
||||||
|
this.commitment = hash;
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
this.tree = MerkleTree.fromItems(this.items);
|
this.tree = MerkleTree.fromItems(this.items);
|
||||||
@ -644,6 +656,38 @@ BlockEntry.fromEntry = function fromEntry(entry, attempt) {
|
|||||||
return item;
|
return item;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* BlockProof
|
||||||
|
* @constructor
|
||||||
|
* @param {Hash} hash
|
||||||
|
* @param {Hash} root
|
||||||
|
* @param {Number} nonce1
|
||||||
|
* @param {Number} nonce2
|
||||||
|
* @param {Number} ts
|
||||||
|
* @param {Number} nonce
|
||||||
|
*/
|
||||||
|
|
||||||
|
function BlockProof(hash, root, nonce1, nonce2, ts, nonce) {
|
||||||
|
this.hash = hash;
|
||||||
|
this.root = root;
|
||||||
|
this.nonce1 = nonce1;
|
||||||
|
this.nonce2 = nonce2;
|
||||||
|
this.ts = ts;
|
||||||
|
this.nonce = nonce;
|
||||||
|
}
|
||||||
|
|
||||||
|
BlockProof.prototype.rhash = function rhash() {
|
||||||
|
return util.revHex(this.hash.toString('hex'));
|
||||||
|
};
|
||||||
|
|
||||||
|
BlockProof.prototype.verify = function verify(target) {
|
||||||
|
return common.rcmp(this.hash, target) <= 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
BlockProof.prototype.getDifficulty = function getDifficulty() {
|
||||||
|
return common.getDifficulty(this.hash);
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* MerkleTree
|
* MerkleTree
|
||||||
* @constructor
|
* @constructor
|
||||||
@ -743,49 +787,6 @@ MerkleTree.fromLeaves = function fromLeaves(leaves) {
|
|||||||
return new MerkleTree().fromLeaves(leaves);
|
return new MerkleTree().fromLeaves(leaves);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
* BlockProof
|
|
||||||
* @constructor
|
|
||||||
* @param {Hash} hash
|
|
||||||
* @param {Hash} root
|
|
||||||
* @param {Number} nonce1
|
|
||||||
* @param {Number} nonce2
|
|
||||||
* @param {Number} ts
|
|
||||||
* @param {Number} nonce
|
|
||||||
*/
|
|
||||||
|
|
||||||
function BlockProof(hash, root, nonce1, nonce2, ts, nonce) {
|
|
||||||
this.hash = hash;
|
|
||||||
this.root = root;
|
|
||||||
this.nonce1 = nonce1;
|
|
||||||
this.nonce2 = nonce2;
|
|
||||||
this.ts = ts;
|
|
||||||
this.nonce = nonce;
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockProof.prototype.rhash = function rhash() {
|
|
||||||
return util.revHex(this.hash.toString('hex'));
|
|
||||||
};
|
|
||||||
|
|
||||||
BlockProof.prototype.verify = function verify(target) {
|
|
||||||
return common.rcmp(this.hash, target) <= 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
BlockProof.prototype.getDifficulty = function getDifficulty() {
|
|
||||||
return common.getDifficulty(this.hash);
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Helpers
|
|
||||||
*/
|
|
||||||
|
|
||||||
function extraNonce(nonce1, nonce2) {
|
|
||||||
var bw = new StaticWriter(8);
|
|
||||||
bw.writeU32BE(nonce1);
|
|
||||||
bw.writeU32BE(nonce2);
|
|
||||||
return bw.render();
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -568,17 +568,34 @@ util.random = function random(min, max) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a 64 bit nonce.
|
* Create a 32 or 64 bit nonce.
|
||||||
|
* @param {Number} size
|
||||||
* @returns {Buffer}
|
* @returns {Buffer}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
util.nonce = function _nonce() {
|
util.nonce = function _nonce(size) {
|
||||||
var nonce = new Buffer(8);
|
var n, nonce;
|
||||||
var a = util.random(0, 0x100000000);
|
|
||||||
var b = util.random(0, 0x100000000);
|
|
||||||
|
|
||||||
nonce.writeUInt32LE(a, 0, true);
|
if (!size)
|
||||||
nonce.writeUInt32LE(b, 4, true);
|
size = 8;
|
||||||
|
|
||||||
|
switch (size) {
|
||||||
|
case 8:
|
||||||
|
nonce = new Buffer(8);
|
||||||
|
n = util.random(0, 0x100000000);
|
||||||
|
nonce.writeUInt32LE(n, 0, true);
|
||||||
|
n = util.random(0, 0x100000000);
|
||||||
|
nonce.writeUInt32LE(n, 4, true);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
nonce = new Buffer(4);
|
||||||
|
n = util.random(0, 0x100000000);
|
||||||
|
nonce.writeUInt32LE(n, 0, true);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(false, 'Bad nonce size.');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return nonce;
|
return nonce;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user