rpc: refactor getblocktemplate.

This commit is contained in:
Christopher Jeffrey 2016-12-15 19:06:22 -08:00
parent 6b69e2bfad
commit 29579fc8d0
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1560,20 +1560,21 @@ RPC.prototype._template = co(function* _template(version, coinbase, rules) {
}); });
RPC.prototype.__template = co(function* _template(version, coinbase, rules) { RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
var attempt = yield this._getAttempt(false);
var scale = attempt.witness ? 1 : constants.WITNESS_SCALE_FACTOR;
var block = attempt.block;
var mutable = ['time', 'transactions', 'prevblock']; var mutable = ['time', 'transactions', 'prevblock'];
var txs = []; var txs = [];
var txIndex = {}; var index = {};
var vbavailable = {}; var vbavailable = {};
var vbrules = []; var vbrules = [];
var attempt = yield this._getAttempt(false); var i, j, entry, tx, input, output;
var block = attempt.block; var dep, deps, json, name, deploy;
var scale = attempt.witness ? 1 : constants.WITNESS_SCALE_FACTOR; var state;
var i, j, entry, tx, deps, input, dep, output, raw, rwhash;
var template, name, deployment, state;
for (i = 0; i < attempt.items.length; i++) { for (i = 0; i < attempt.items.length; i++) {
entry = attempt.items[i]; entry = attempt.items[i];
txIndex[entry.hash] = i; index[entry.hash] = i;
} }
for (i = 0; i < attempt.items.length; i++) { for (i = 0; i < attempt.items.length; i++) {
@ -1583,7 +1584,7 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
for (j = 0; j < tx.inputs.length; j++) { for (j = 0; j < tx.inputs.length; j++) {
input = tx.inputs[j]; input = tx.inputs[j];
dep = txIndex[input.prevout.hash]; dep = index[input.prevout.hash];
if (dep != null && deps.indexOf(dep) === -1) { if (dep != null && deps.indexOf(dep) === -1) {
assert(dep < i); assert(dep < i);
deps.push(dep); deps.push(dep);
@ -1593,7 +1594,7 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
txs.push({ txs.push({
data: tx.toRaw().toString('hex'), data: tx.toRaw().toString('hex'),
txid: tx.txid(), txid: tx.txid(),
hash: tx.rwhash(), hash: tx.wtxid(),
depends: deps, depends: deps,
fee: entry.fee, fee: entry.fee,
sigops: entry.sigops / scale | 0, sigops: entry.sigops / scale | 0,
@ -1605,29 +1606,29 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
mutable.push('version/force'); mutable.push('version/force');
for (i = 0; i < this.network.deploys.length; i++) { for (i = 0; i < this.network.deploys.length; i++) {
deployment = this.network.deploys[i]; deploy = this.network.deploys[i];
state = yield this.chain.getState(this.chain.tip, deployment); state = yield this.chain.getState(this.chain.tip, deploy);
name = deployment.name; name = deploy.name;
switch (state) { switch (state) {
case constants.thresholdStates.DEFINED: case constants.thresholdStates.DEFINED:
case constants.thresholdStates.FAILED: case constants.thresholdStates.FAILED:
break; break;
case constants.thresholdStates.LOCKED_IN: case constants.thresholdStates.LOCKED_IN:
block.version |= 1 << deployment.bit; block.version |= 1 << deploy.bit;
case constants.thresholdStates.STARTED: case constants.thresholdStates.STARTED:
vbavailable[name] = deployment.bit; vbavailable[name] = deploy.bit;
if (rules) { if (rules) {
if (rules.indexOf(name) === -1 && !deployment.force) if (rules.indexOf(name) === -1 && !deploy.force)
block.version &= ~(1 << deployment.bit); block.version &= ~(1 << deploy.bit);
} }
break; break;
case constants.thresholdStates.ACTIVE: case constants.thresholdStates.ACTIVE:
if (rules) { if (rules) {
if (rules.indexOf(name) === -1 && !deployment.force) if (rules.indexOf(name) === -1 && !deploy.force)
throw new RPCError('Client must support ' + name + '.'); throw new RPCError('Client must support ' + name + '.');
} }
if (!deployment.force) if (!deploy.force)
name = '!' + name; name = '!' + name;
vbrules.push(name); vbrules.push(name);
break; break;
@ -1636,28 +1637,33 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
block.version >>>= 0; block.version >>>= 0;
template = { json = {
capabilities: ['proposal'], capabilities: ['proposal'],
mutable: mutable,
version: block.version, version: block.version,
rules: vbrules, rules: vbrules,
vbavailable: vbavailable, vbavailable: vbavailable,
vbrequired: 0, vbrequired: 0,
height: attempt.height,
previousblockhash: util.revHex(block.prevBlock), previousblockhash: util.revHex(block.prevBlock),
transactions: txs,
longpollid: this.chain.tip.rhash() + util.pad32(this._totalTX()),
target: util.revHex(attempt.target.toString('hex')), target: util.revHex(attempt.target.toString('hex')),
submitold: false, bits: util.hex32(block.bits),
mintime: block.ts,
maxtime: this.network.now() + 2 * 60 * 60,
mutable: mutable,
expires: 0xffffffff,
noncerange: '00000000ffffffff', noncerange: '00000000ffffffff',
curtime: block.ts,
mintime: block.ts,
maxtime: block.ts + 7200,
expires: block.ts + 7200,
sigoplimit: constants.block.MAX_SIGOPS_COST / scale | 0, sigoplimit: constants.block.MAX_SIGOPS_COST / scale | 0,
sizelimit: constants.block.MAX_RAW_SIZE, sizelimit: constants.block.MAX_RAW_SIZE,
weightlimit: constants.block.MAX_WEIGHT, weightlimit: constants.block.MAX_WEIGHT,
curtime: block.ts, longpollid: this.chain.tip.rhash() + util.pad32(this._totalTX()),
bits: util.hex32(block.bits), submitold: false,
height: attempt.height coinbaseaux: {
flags: attempt.coinbaseFlags.toString('hex')
},
coinbasevalue: attempt.coinbase.getOutputValue(),
coinbasetxn: undefined,
transactions: txs
}; };
if (coinbase) { if (coinbase) {
@ -1668,39 +1674,30 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
if (attempt.witness) { if (attempt.witness) {
output = tx.outputs.pop(); output = tx.outputs.pop();
assert(output.script.isCommitment()); assert(output.script.isCommitment());
raw = tx.toRaw();
rwhash = tx.rwhash();
tx.outputs.push(output);
} else {
raw = tx.toRaw();
rwhash = tx.rwhash();
} }
template.coinbasetxn = { json.coinbasetxn = {
data: raw.toString('hex'), data: tx.toRaw().toString('hex'),
txid: tx.txid(), txid: tx.txid(),
hash: rwhash, hash: tx.wtxid(),
depends: [], depends: [],
fee: 0, fee: 0,
sigops: tx.getSigopsCost() / scale | 0, sigops: tx.getSigopsCost() / scale | 0,
weight: tx.getWeight() weight: tx.getWeight()
}; };
if (attempt.witness)
tx.outputs.push(output);
} }
template.coinbaseaux = {
flags: attempt.coinbaseFlags.toString('hex')
};
template.coinbasevalue = attempt.coinbase.getOutputValue();
if (attempt.witness) { if (attempt.witness) {
tx = attempt.coinbase; tx = attempt.coinbase;
output = tx.outputs[tx.outputs.length - 1]; output = tx.outputs[tx.outputs.length - 1];
assert(output.script.isCommitment()); assert(output.script.isCommitment());
template.default_witness_commitment = output.script.toJSON(); json.default_witness_commitment = output.script.toJSON();
} }
return template; return json;
}); });
RPC.prototype._poll = co(function* _poll(lpid) { RPC.prototype._poll = co(function* _poll(lpid) {