wip
This commit is contained in:
parent
6bb383f0d9
commit
d426169f33
@ -24,6 +24,7 @@ Encoding.prototype.decodeHeaderKey = function(buffer) {
|
||||
};
|
||||
|
||||
Encoding.prototype.encodeHeaderValue = function(header) {
|
||||
var hashBuf = new Buffer(header.hash, 'hex');
|
||||
var versionBuf = new Buffer(4);
|
||||
versionBuf.writeInt32BE(header.version);
|
||||
var prevHash = new Buffer(header.prevHash, 'hex');
|
||||
@ -37,19 +38,21 @@ Encoding.prototype.encodeHeaderValue = function(header) {
|
||||
var heightBuf = new Buffer(4);
|
||||
heightBuf.writeUInt32BE(header.height);
|
||||
var chainworkBuf = new Buffer(header.chainwork, 'hex');
|
||||
return Buffer.concat([ versionBuf, prevHash.reverse(), merkleRoot.reverse(), tsBuf, bitsBuf, nonceBuf, heightBuf, chainworkBuf ]);
|
||||
return Buffer.concat([hashBuf, versionBuf, prevHash.reverse(), merkleRoot.reverse(), tsBuf, bitsBuf, nonceBuf, heightBuf, chainworkBuf ]);
|
||||
};
|
||||
|
||||
Encoding.prototype.decodeHeaderValue = function(buffer) {
|
||||
var version = buffer.readInt32BE();
|
||||
var prevHash = buffer.slice(4, 36).toString('hex');
|
||||
var merkleRoot = buffer.slice(36, 68).toString('hex');
|
||||
var ts = buffer.readUInt32BE(68);
|
||||
var bits = buffer.readUInt32BE(72);
|
||||
var nonce = buffer.readUInt32BE(76);
|
||||
var height = buffer.readUInt32BE(80);
|
||||
var chainwork = buffer.slice(84).toString('hex');
|
||||
var hash = buffer.slice(0, 32).toString('hex');
|
||||
var version = buffer.readInt32BE(32);
|
||||
var prevHash = buffer.slice(36, 68).toString('hex');
|
||||
var merkleRoot = buffer.slice(68, 100).toString('hex');
|
||||
var ts = buffer.readUInt32BE(100);
|
||||
var bits = buffer.readUInt32BE(104);
|
||||
var nonce = buffer.readUInt32BE(108);
|
||||
var height = buffer.readUInt32BE(112);
|
||||
var chainwork = buffer.slice(116).toString('hex');
|
||||
return {
|
||||
hash: hash,
|
||||
version: version,
|
||||
prevHash: prevHash,
|
||||
merkleRoot: merkleRoot,
|
||||
|
||||
@ -17,6 +17,7 @@ var HeaderService = function(options) {
|
||||
this._tip = null;
|
||||
this._p2p = this.node.services.p2p;
|
||||
this._db = this.node.services.db;
|
||||
this._checkpoint = options.checkpoint || 0; // the # of header to look back on boot
|
||||
this._headers = new utils.SimpleMap();
|
||||
};
|
||||
|
||||
@ -69,6 +70,9 @@ HeaderService.prototype.start = function(callback) {
|
||||
function(tip, next) {
|
||||
self._tip = tip;
|
||||
next();
|
||||
},
|
||||
function(next) {
|
||||
self._getPersistedHeaders(next);
|
||||
}
|
||||
], function(err) {
|
||||
|
||||
@ -135,17 +139,24 @@ HeaderService.prototype._onHeaders = function(headers, convert) {
|
||||
var runningHeight = this._tip.height;
|
||||
var prevHeader = this._headers.getLastIndex();
|
||||
|
||||
var dbOps = [];
|
||||
for(var i = 0; i < headers.length; i++) {
|
||||
var header = headers[i];
|
||||
header.height = ++runningHeight;
|
||||
header.chainwork = this._getChainwork(header, prevHeader).toString(16, 32);
|
||||
dbOps.push({
|
||||
type: 'put',
|
||||
key: this._encoding.encodeHeaderKey(header.hash),
|
||||
value: this._encoding.encodeHeaderValue(header)
|
||||
});
|
||||
prevHeader = header;
|
||||
this._headers.set(header.hash, header);
|
||||
}
|
||||
|
||||
this._tip.hash = newHeaders[newHeaders.length - 1].hash;
|
||||
this._startingHash = this._tip.hash = newHeaders[newHeaders.length - 1].hash;
|
||||
this._tip.height = this._tip.height + newHeaders.length;
|
||||
|
||||
this._db.batch(dbOps);
|
||||
this._sync();
|
||||
|
||||
};
|
||||
@ -180,7 +191,6 @@ HeaderService.prototype._sync = function() {
|
||||
log.debug('Header Service: download progress: ' + this._tip.height + '/' +
|
||||
this._numNeeded + ' (' + (this._tip.height / this._numNeeded*100).toFixed(2) + '%)');
|
||||
|
||||
|
||||
this._p2p.getHeaders({ startHash: this._tip.hash });
|
||||
|
||||
return;
|
||||
@ -199,7 +209,6 @@ HeaderService.prototype.getAllHeaders = function() {
|
||||
HeaderService.prototype._getPersistedHeaders = function(callback) {
|
||||
|
||||
var self = this;
|
||||
var results = [];
|
||||
var start = self._encoding.encodeHeaderKey(0);
|
||||
var end = self._encoding.encodeHeaderKey(0xffffffff);
|
||||
var criteria = {
|
||||
@ -215,16 +224,14 @@ HeaderService.prototype._getPersistedHeaders = function(callback) {
|
||||
});
|
||||
|
||||
stream.on('data', function(data) {
|
||||
var res = {};
|
||||
res[self._encoding.decodeHeaderKey(data.key).hash] = self._encoding.decodeHeaderValue(data.value);
|
||||
results.push(res);
|
||||
self._headers.set(self._encoding.decodeHeaderKey(data.key).hash, self._encoding.decodeHeaderValue(data.value));
|
||||
});
|
||||
|
||||
stream.on('end', function() {
|
||||
if (streamErr) {
|
||||
return streamErr;
|
||||
}
|
||||
callback(null, results);
|
||||
callback();
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user