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