block-syncer-js

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

example.html (3327B)


      1 <html>
      2 	<head>
      3 		<title>Worker demo</title>
      4 		<script src='example_files/web3.min.js'></script>
      5 		<script src='example_files/cic-client.web.js'></script>
      6 		<script src='example_files/plugin.js'></script>
      7 		<script>
      8 
      9 //const registry_address = "0xb708175e3f6Cd850643aAF7B32212AFad50e2549";
     10 const registry_address = "0x4f8af296202Bff3B8589DA4Af87A8cfe74ad4d3A";
     11 const trusted_declarator_address = "0x5567139c7a1C2977A391f51D8cA45B1D6700f5F6";
     12 
     13 const web3_provider = 'ws://localhost:63545';
     14 
     15 const readyStateTarget = 2;
     16 const readyStateElements = {
     17 	token: 1,
     18 	network: 2,
     19 }
     20 let readyState = 0;
     21 
     22 function httpGetter() {
     23 }
     24 
     25 httpGetter.prototype.get = function(filename) {
     26 	return new Promise((whohoo, doh) => {
     27 		const xhr = new XMLHttpRequest();
     28 		xhr.addEventListener('load', (e) => {
     29 			if (xhr.status == 200) {
     30 				whohoo(xhr.responseText);
     31 				return;
     32 			}
     33 			doh('failed with status ' + xhr.status + ": " + xhr.statusText);
     34 		});
     35 		xhr.open('GET', filename);
     36 		xhr.send();
     37 	});
     38 }
     39 
     40 function readyStateProcessor(settings, bit) {
     41 	console.debug('setting readystate bit', bit);
     42 	readyState |= bit;
     43 
     44 	if (readyStateTarget == (readyState & readyStateTarget)) {
     45 		console.log('reached readyState target', readyStateTarget);
     46 		const wHeadSync = new Worker('src/workers/head.js');
     47 		wHeadSync.onmessage = (m) => {
     48 			settings.txHelper.processReceipt(m.data);
     49 		}
     50 		wHeadSync.postMessage({
     51 			w3_provider: settings.w3.provider,
     52 		});
     53 		fetcher(settings);
     54 	}
     55 }
     56 
     57 function Settings(f) {
     58 	this.w3 = {
     59 		engine: undefined,
     60 		provider: undefined,
     61 	}
     62 	this.scanFilter = f;
     63 }
     64 
     65 function newTransferEvent(tx) {
     66 	return new CustomEvent('cic_transfer', {
     67 		detail: {
     68 			tx: tx,
     69 		},
     70 	});
     71 }
     72 
     73 window.addEventListener('cic_transfer', (e) => {
     74 	console.log('have tx ', e.detail.tx);
     75 });
     76 
     77 function newConversionEvent(tx) {
     78 	return new CustomEvent('cic_convert', {
     79 		detail: {
     80 			tx: tx,
     81 		},
     82 	});
     83 }
     84 
     85 window.addEventListener('cic_convert', (e) => {
     86 	console.log('have convert ', e.detail.tx);
     87 });
     88 
     89 window.addEventListener('load', () => {
     90 	let settings = new Settings(scan);
     91 	const provider = web3_provider;
     92 	settings.w3.provider = provider;
     93 	settings.w3.engine = new Web3(provider);
     94 	const fileGetter = new httpGetter();
     95 	settings.registry = new cic.CICRegistry(settings.w3.engine, registry_address, fileGetter, ['/example_files/data']);
     96 	settings.registry.addTrust(trusted_declarator_address);
     97 	settings.txHelper = new cic.TransactionHelper(settings.w3.engine, settings.registry);
     98 
     99 	settings.txHelper.ontransfer = async function(t) {
    100 		window.dispatchEvent(newTransferEvent(t));
    101 	};
    102 	settings.txHelper.onconversion = async function(t) {
    103 		window.dispatchEvent(newConversionEvent(t));
    104 	};
    105 	settings.registry.onload = function(n) {
    106 		console.debug('loaded network contracts', n);
    107 		readyStateProcessor(settings, readyStateElements.network);
    108 	};
    109 
    110 	settings.registry.load();
    111 });
    112 
    113 async function scan(settings, lo, hi, bloom_block_bytes, bloom_blocktx_bytes, bloom_rounds) {
    114 	const w = new Worker('src/workers/ondemand.js');
    115 	w.onmessage = (m) => {
    116 		settings.txHelper.processReceipt(m.data);
    117 	}
    118 	console.log('lo hi', lo, hi);
    119 	w.postMessage({
    120 		w3_provider: settings.w3.provider,
    121 		lo: lo,
    122 		hi: hi,
    123 	       	filters: [
    124 			bloom_block_bytes,
    125 			bloom_blocktx_bytes,
    126 		],
    127 		filter_rounds: bloom_rounds,
    128 	});
    129 }
    130 
    131 		</script>
    132 	</head>
    133 	<body>
    134 	</body>
    135 </html>