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