bitcore-node-zcash/lib/BlockExtractor.js

143 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
2014-05-07 05:45:44 -07:00
var bitcore = require('bitcore'),
Block = bitcore.Block,
networks = bitcore.networks,
2014-05-07 06:42:45 -07:00
Parser = bitcore.BinaryParser,
2014-05-07 05:45:44 -07:00
fs = require('fs'),
Buffer = bitcore.Buffer,
glob = require('glob'),
async = require('async');
2014-03-05 18:03:56 -08:00
function BlockExtractor(dataDir, network) {
var path = dataDir + '/blocks/blk*.dat';
2014-05-29 19:37:26 -07:00
this.dataDir = dataDir;
this.files = glob.sync(path);
this.nfiles = this.files.length;
2014-03-05 18:03:56 -08:00
2014-05-29 19:37:26 -07:00
if (this.nfiles === 0)
2014-03-05 18:03:56 -08:00
throw new Error('Could not find block files at: ' + path);
2014-05-29 19:37:26 -07:00
this.currentFileIndex = 0;
this.isCurrentRead = false;
this.currentBuffer = null;
this.currentParser = null;
this.network = network === 'testnet' ? networks.testnet: networks.livenet;
this.magic = this.network.magic.toString('hex');
2014-03-05 18:03:56 -08:00
}
2014-03-05 18:03:56 -08:00
BlockExtractor.prototype.currentFile = function() {
2014-05-29 19:37:26 -07:00
return this.files[this.currentFileIndex];
2014-03-05 18:03:56 -08:00
};
2014-02-01 17:57:31 -08:00
2014-03-05 18:03:56 -08:00
BlockExtractor.prototype.nextFile = function() {
2014-05-29 19:37:26 -07:00
if (this.currentFileIndex < 0) return false;
2014-03-05 18:03:56 -08:00
var ret = true;
2014-05-29 19:37:26 -07:00
this.isCurrentRead = false;
this.currentBuffer = null;
this.currentParser = null;
2014-05-29 19:37:26 -07:00
if (this.currentFileIndex < this.nfiles - 1) {
this.currentFileIndex++;
}
2014-03-05 18:03:56 -08:00
else {
2014-05-29 19:37:26 -07:00
this.currentFileIndex=-1;
2014-03-05 18:03:56 -08:00
ret = false;
}
return ret;
};
2014-03-05 18:03:56 -08:00
BlockExtractor.prototype.readCurrentFileSync = function() {
2014-05-29 19:37:26 -07:00
if (this.currentFileIndex < 0 || this.isCurrentRead) return;
2014-05-29 19:37:26 -07:00
this.isCurrentRead = true;
2014-05-29 19:37:26 -07:00
var fname = this.currentFile();
2014-03-05 18:03:56 -08:00
if (!fname) return;
2014-03-05 18:03:56 -08:00
var stats = fs.statSync(fname);
2014-03-05 18:03:56 -08:00
var size = stats.size;
2014-03-05 18:03:56 -08:00
console.log('Reading Blockfile %s [%d MB]',
fname, parseInt(size/1024/1024));
2014-03-05 18:03:56 -08:00
var fd = fs.openSync(fname, 'r');
2014-03-05 18:03:56 -08:00
var buffer = new Buffer(size);
2014-03-05 18:03:56 -08:00
fs.readSync(fd, buffer, 0, size, 0);
2014-05-29 19:37:26 -07:00
this.currentBuffer = buffer;
this.currentParser = new Parser(buffer);
2014-03-05 18:03:56 -08:00
};
2014-05-29 19:37:26 -07:00
BlockExtractor.prototype._getMagic = function() {
if (!this.currentParser)
return null;
var byte0 = this.currentParser ? this.currentParser.buffer(1).toString('hex') : null;
// Grab 3 bytes from block without removing them
var p = this.currentParser.pos;
var bytes123 = this.currentParser.subject.toString('hex',p,p+3);
var magic = byte0 + bytes123;
if (magic !=='00000000' && magic !== this.magic) {
if(this.errorCount++ > 4)
throw new Error('CRITICAL ERROR: Magic number mismatch: ' +
magic + '!=' + this.magic);
magic=null;
}
if (magic==='00000000')
magic =null;
return magic;
};
BlockExtractor.prototype.getNextBlock = function(cb) {
2014-03-05 18:03:56 -08:00
var b;
var magic;
2014-05-29 19:37:26 -07:00
var isFinished = 0;
while(!magic && !isFinished) {
this.readCurrentFileSync();
magic= this._getMagic();
if (!this.currentParser || this.currentParser.eof() ) {
if (this.nextFile()) {
console.log('Moving forward to file:' + this.currentFile() );
magic = null;
} else {
console.log('Finished all files');
isFinished = 1;
}
}
}
if (isFinished)
return cb();
// Remove 3 bytes from magic and spacer
this.currentParser.buffer(3+4);
b = new Block();
b.parse(this.currentParser);
b.getHash();
this.errorCount=0;
return cb(null,b);
2014-03-05 18:03:56 -08:00
};
module.exports = require('soop')(BlockExtractor);