rpc: softforks and chain tips.

This commit is contained in:
Christopher Jeffrey 2016-08-09 19:16:52 -07:00
parent 7490ea2b87
commit 20a8d23c76
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 94 additions and 16 deletions

View File

@ -1267,6 +1267,24 @@ ChainDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, call
});
};
/**
* Get all entries.
* @param {Function} callback - Returns [Error, {@link ChainEntry}[]].
*/
ChainDB.prototype.getEntries = function getEntries(callback) {
var self = this;
this.db.iterate({
gte: layout.e(constants.ZERO_HASH),
lte: layout.e(constants.MAX_HASH),
values: true,
keyAsBuffer: true,
parse: function(data) {
return bcoin.chainentry.fromRaw(self.chain, data);
}
}, callback);
};
/**
* Get all transactions pertinent to an address.
* @param {Base58Address|Base58Address[]} addresses

View File

@ -107,7 +107,7 @@ function Fullnode(options) {
proxyServer: this.options.proxyServer,
preferredSeed: this.options.preferredSeed,
ignoreDiscovery: this.options.ignoreDiscovery,
pool: this.options.port,
port: this.options.port,
listen: this.options.listen,
spv: false
});

View File

@ -418,11 +418,11 @@ function send(res, code, msg, type) {
else if (type === 'text')
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
else if (type === 'json')
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Content-Type', 'application/json');
else if (type === 'js')
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
else if (type === 'binary')
res.setHeader('Content-Type', 'application/octet-stream; charset=utf-8');
res.setHeader('Content-Type', 'application/octet-stream');
len = typeof msg === 'string'
? Buffer.byteLength(msg, 'utf8')

View File

@ -578,10 +578,11 @@ RPC.prototype._getSoftforks = function _getSoftforks() {
RPC.prototype._getBIP9Softforks = function _getBIP9Softforks(callback) {
var self = this;
var forks = [];
var forks = {};
var keys = Object.keys(this.network.deployments);
utils.forEachSerial(keys, function(id, next) {
var deployment = self.network.deployments[id];
self.chain.getState(self.chain.tip, id, function(err, state) {
if (err)
return next(err);
@ -604,10 +605,12 @@ RPC.prototype._getBIP9Softforks = function _getBIP9Softforks(callback) {
break;
}
forks.push({
id: id,
state: state
});
forks[id] = {
status: state,
bit: deployment.bit,
startTime: deployment.startTime,
timeout: deployment.timeout
};
next();
});
@ -913,15 +916,72 @@ RPC.prototype.blockToJSON = function blockToJSON(entry, block, txDetails, callba
};
RPC.prototype.getchaintips = function getchaintips(args, callback) {
var self = this;
var i, tips, orphans, prevs, result, orphan;
if (args.help || args.length !== 0)
return callback(new RPCError('getchaintips'));
callback(null, [{
height: this.chain.height,
hash: this.chain.tip.rhash,
branchlen: 0,
status: 'active'
}]);
tips = [];
orphans = [];
prevs = {};
result = [];
this.chain.db.getEntries(function(err, entries) {
if (err)
return callback(err);
utils.forEachSerial(entries, function(entry, next) {
entry.isMainChain(function(err, main) {
if (err)
return next(err);
if (!main) {
orphans.push(entry);
prevs[entry.prevBlock] = true;
}
next();
});
}, function(err) {
if (err)
return callback(err);
for (i = 0; i < orphans.length; i++) {
orphan = orphans[i];
if (!prevs[orphan.hash])
tips.push(orphan);
}
tips.push(self.chain.tip);
utils.forEachSerial(tips, function(entry, next) {
self.chain.findFork(entry, self.chain.tip, function(err, fork) {
if (err)
return next(err);
entry.isMainChain(function(err, main) {
if (err)
return next(err);
result.push({
height: entry.height,
hash: entry.rhash,
branchlen: entry.height - fork.height,
status: main ? 'active' : 'valid-headers'
});
next();
});
});
}, function(err) {
if (err)
return callback(err);
callback(null, result);
});
});
});
};
RPC.prototype.getdifficulty = function getdifficulty(args, callback) {
@ -1746,7 +1806,7 @@ RPC.prototype._createRedeem = function _createRedeem(args, callback) {
try {
script = bcoin.script.fromMultisig(m, n, keys);
} catch (e) {
return next(new RPCError('Invalid parameters.'));
return callback(new RPCError('Invalid parameters.'));
}
if (script.toRaw().length > constants.script.MAX_PUSH)

View File

@ -218,7 +218,7 @@ VersionPacket.prototype.fromRaw = function fromRaw(data) {
assert(this.version >= 0, 'Version is negative.');
assert(this.ts >= 0, 'Timestamp is negative.');
assert(this.height >= 0, 'Height is negative.');
// assert(this.height >= 0, 'Height is negative.');
return this;
};