wip
This commit is contained in:
parent
63cada9e8d
commit
1dbba6708d
@ -27,8 +27,6 @@ var BlockService = function(options) {
|
||||
this._subscriptions.reorg = [];
|
||||
|
||||
// meta is [{ chainwork: chainwork, hash: hash }]
|
||||
// properties that apply to blocks that are not already stored on the blocks,
|
||||
// i.e. chainwork, height
|
||||
this._meta = [];
|
||||
|
||||
// this is the in-memory full/raw block cache
|
||||
@ -42,7 +40,8 @@ var BlockService = function(options) {
|
||||
// keep track of out-of-order blocks, this is a list of chains (which are lists themselves)
|
||||
// e.g. [ [ block5, block4 ], [ block8, block7 ] ];
|
||||
this._incompleteChains = [];
|
||||
this._chainTips = []; // list of all chain tips, including main chain and any chains that were orphaned after a reorg
|
||||
// list of all chain tips, including main chain and any chains that were orphaned after a reorg
|
||||
this._chainTips = [];
|
||||
this._blockCount = 0;
|
||||
this.GENESIS_HASH = constants.BITCOIN_GENESIS_HASH[this.node.getNetworkName()];
|
||||
|
||||
@ -68,6 +67,111 @@ BlockService.prototype.getAPIMethods = function() {
|
||||
return methods;
|
||||
};
|
||||
|
||||
BlockService.prototype.getBestBlockHash = function(callback) {
|
||||
callback(this._meta[this._meta.length - 1].hash);
|
||||
};
|
||||
|
||||
BlockService.prototype.getBlock = function(hash, callback) {
|
||||
var self = this;
|
||||
this._db.get(this._encoding.encodeBlockKey(hash), function(err, data) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
callback(null, self._encoding.decodeBlockValue(data));
|
||||
});
|
||||
};
|
||||
|
||||
BlockService.prototype.getBlockHashesByTimestamp = function(high, low, options, callback) {
|
||||
|
||||
var self = this;
|
||||
if (_.isFunction(options)) {
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
|
||||
self.client.getBlockHashes(high, low, options, function(err, response) {
|
||||
if (err) {
|
||||
return callback(self._wrapRPCError(err));
|
||||
}
|
||||
callback(null, response.result);
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
BlockService.prototype._getHash = function(blockArg) {
|
||||
|
||||
(_.isNumber(blockArg) || (blockArg.length < 40 && /^[0-9]+$/.test(blockArg))) &&
|
||||
this._meta[blockArg] ? meta.hash : null;
|
||||
|
||||
};
|
||||
|
||||
BlockService.prototype.getBlockHeader = function(blockArg, callback) {
|
||||
|
||||
blockArg = this._getHash(blockArg);
|
||||
|
||||
// by hash
|
||||
this._getBlock(meta.hash, function(err, block) {
|
||||
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!block) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
callback(null, block.header.toJSON());
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
BlockService.prototype.getBlockOverview = function(hash, callback) {
|
||||
var self = this;
|
||||
|
||||
function queryBlock(err, blockhash) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
var cachedBlock = self.blockOverviewCache.get(blockhash);
|
||||
if (cachedBlock) {
|
||||
return setImmediate(function() {
|
||||
callback(null, cachedBlock);
|
||||
});
|
||||
} else {
|
||||
self._tryAllClients(function(client, done) {
|
||||
client.getBlock(blockhash, true, function(err, response) {
|
||||
if (err) {
|
||||
return done(self._wrapRPCError(err));
|
||||
}
|
||||
var result = response.result;
|
||||
var blockOverview = {
|
||||
hash: result.hash,
|
||||
version: result.version,
|
||||
confirmations: result.confirmations,
|
||||
height: result.height,
|
||||
chainWork: result.chainwork,
|
||||
prevHash: result.previousblockhash,
|
||||
nextHash: result.nextblockhash,
|
||||
merkleRoot: result.merkleroot,
|
||||
time: result.time,
|
||||
medianTime: result.mediantime,
|
||||
nonce: result.nonce,
|
||||
bits: result.bits,
|
||||
difficulty: result.difficulty,
|
||||
txids: result.tx
|
||||
};
|
||||
self.blockOverviewCache.set(blockhash, blockOverview);
|
||||
done(null, blockOverview);
|
||||
});
|
||||
}, callback);
|
||||
}
|
||||
}
|
||||
|
||||
self._maybeGetBlockHash(blockArg, queryBlock);
|
||||
|
||||
};
|
||||
|
||||
BlockService.prototype.getPublishEvents = function() {
|
||||
|
||||
return [
|
||||
@ -87,6 +191,15 @@ BlockService.prototype.getPublishEvents = function() {
|
||||
|
||||
};
|
||||
|
||||
BlockService.prototype.getRawBlock = function(hash, callback) {
|
||||
this.getBlock(hash, function(err, data) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
data.toString();
|
||||
});
|
||||
};
|
||||
|
||||
BlockService.prototype.start = function(callback) {
|
||||
|
||||
var self = this;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user