cic-staff-client/src/assets/js/worker.js

64 lines
1.5 KiB
JavaScript

let window = self;
importScripts('moolb.js');
importScripts('sync.js');
importScripts('web3.min.js');
let s = undefined;
// TODO: as worker has its own scope we don't need the object
function Driver(messager, provider, lo, filters, syncer) {
this.w3 = new Web3(provider);
this.lo = lo;
this.hi = lo;
this.filters = filters;
this.syncer = syncer;
this.messager = messager;
}
Driver.prototype.start = function(hi) {
let self = this;
if (hi !== undefined) {
self.sync(hi);
return;
}
s.w3.eth.getBlockNumber().then(function(n) {
self.sync(n);
});
};
Driver.prototype.sync = function(n) {
this.hi = n;
this.syncer(this, this.lo, this.hi, this.filters[0], this.filters[1], this.getCount, this.process);
};
Driver.prototype.process = function(b, t) {
s.w3.eth.getTransactionFromBlock(b, t).then((t) => {
s.w3.eth.getTransactionReceipt(t.hash).then((r) => {
this.postMessage(r);
});
}).catch(function(e) {
//this.postMessage(['failed getTransactionFromBlock(' + b + ', ' + t + ')']);
console.error('failed getTransactionFromBlock(' + b + ', ' + t + ')');
});
}
Driver.prototype.getCount = async function (b) {
const n = s.w3.eth.getBlockTransactionCount(b);
return n;
};
onmessage = function(o) {
const filters = [
bloomFromBytes(o.data.filters[0], o.data.filter_rounds),
bloomFromBytes(o.data.filters[1], o.data.filter_rounds),
];
s = new Driver(postMessage, o.data.w3_provider, o.data.lo, filters, sync_by_filter);
let hi = undefined;
if (o.data.hi > 0) {
hi = o.data.hi;
}
s.start(hi);
};