block-syncer-js

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

sync.js (1044B)


      1 if (typeof module != 'undefined') {
      2 	module.exports = {
      3 		by_filter: sync_by_filter,
      4 		by_filter_block: sync_by_filter_block,
      5 	}
      6 }
      7 
      8 function sync_by_filter_block(block, count, buf, bloom_blocktx, result_callback) {
      9 	for (let j = 0; j < count; j++) {
     10 		let w = new DataView(buf);
     11 		w.setInt32(4, j);
     12 		const r = new Uint8Array(buf);
     13 		bloom_blocktx.check(r).then(function(ok) {
     14 			if (ok) {
     15 				console.debug('match in block ' + block + ' tx ' + j);
     16 				result_callback(block, j);
     17 			}
     18 		});
     19 	}
     20 }
     21 
     22 function sync_by_filter(lo, hi, bloom_block, bloom_blocktx, tx_count_getter, result_callback) {
     23 
     24 	for (let i = lo; i <= hi; i++) {
     25 		let a = new ArrayBuffer(8);
     26 		let w = new DataView(a);
     27 		w.setInt32(0, i);
     28 		const r = new Uint8Array(a.slice(0, 4));
     29 		bloom_block.check(r).then(function(ok) {
     30 			if (ok) {
     31 				console.debug('match in block ' + i);
     32 				tx_count_getter(i).then(function(n) {
     33 					sync_by_filter_block(i, n, a, bloom_blocktx, result_callback);
     34 				}).catch((e) => {
     35 					console.error('get count fail', e);
     36 				});;
     37 			}
     38 		});
     39 	}
     40 }