block-syncer-js

Fast block sync and retrieval using bloom filters
git clone git://git.defalsify.org/block-sync-js.git
Log | Files | Refs

driver.js (1419B)


      1 if (typeof module != 'undefined') {
      2 	module.exports = {
      3 		Driver: Driver,
      4 	}
      5 }
      6 
      7 
      8 function Driver(w3, lo, filters, syncer, callback) {
      9 	this.w3 = w3
     10 	this.lo = lo;
     11 	this.hi = undefined;
     12 	this.filters = filters;
     13 	this.syncer = syncer;
     14 	this.callback = callback
     15 }
     16 
     17 Driver.prototype.start = function(hi) {
     18 	const self = this;
     19 
     20 	if (hi !== undefined) {
     21 		self.sync(hi);
     22 		return;
     23 	}
     24 	self.w3.eth.getBlockNumber().then(function(n) {
     25 		self.sync(n);
     26 	}).catch((e) => {
     27 		console.error('blocknumber fail', e);
     28 	});
     29 };
     30 
     31 Driver.prototype.sync = function(n) {
     32 	const self = this;
     33 
     34 	self.hi = n;
     35 
     36 	const countGetter = async (b) => {
     37 		return await self.getCount(b);
     38 	};
     39 
     40 	const processor = async (b, t) => {
     41 		return await self.process(b, t);
     42 	}
     43 	self.syncer(self.lo, self.hi, self.filters[0], self.filters[1], countGetter, processor);
     44 };
     45 
     46 
     47 Driver.prototype.process = function(b, t) {
     48 	const self = this;
     49 
     50 	self.w3.eth.getTransactionFromBlock(b, t).then((t) => {
     51 		self.w3.eth.getTransactionReceipt(t.hash).then((r) => {
     52 			self.callback(r);
     53 		}).catch((e) => {
     54 			console.error('fail get recept', e);
     55 		});
     56 	}).catch(function(e) { 
     57 		self.callback(['failed getTransactionFromBlock(' + b + ', ' + t + ')']);
     58 		console.error('failed getTransactionFromBlock(' + b + ', ' + t + ')');
     59 	});
     60 }
     61 
     62 Driver.prototype.getCount = async function (b) {
     63 	const self = this;
     64 
     65 	const n = await self.w3.eth.getBlockTransactionCount(b);
     66 	return n;
     67 };