use of org.web3j.protocol.core.DefaultBlockParameterNumber in project ethereum-ingest by codingchili.
the class BlockService method start.
@Override
public void start(Future<Void> future) {
context.blockStorage().setHandler(done -> {
if (done.succeeded()) {
future.complete();
storage = done.result();
Web3j client = getIpcClient();
Integer start = Integer.parseInt(config.getStartBlock());
Integer end = Integer.parseInt(config.getBlockEnd());
Observable.range(start, end - start).subscribe(new Subscriber<Integer>() {
final AtomicReference<String> hash = new AtomicReference<>();
@Override
public void onStart() {
request(config.getBackpressureBlocks());
}
@Override
public void onCompleted() {
listener.onFinished();
}
@Override
public void onError(Throwable e) {
if (!stopping.get()) {
listener.onError(e, hash.get());
unsubscribe();
stopping.set(true);
}
}
@Override
public void onNext(Integer blockNum) {
Consumer<EthBlock> importer = eth -> {
hash.set(eth.getBlock().getHash());
startImport(eth).setHandler(done1 -> {
if (done1.succeeded()) {
request(1);
} else {
onError(done1.cause());
}
});
};
Supplier<EthBlock> chain = () -> {
DefaultBlockParameterNumber param = new DefaultBlockParameterNumber(Long.valueOf(blockNum + ""));
// prevent json corruption over the ipc connection.
synchronized (BlockService.class) {
try {
return client.ethGetBlockByNumber(param, config.isTxImport()).send();
} catch (IOException e) {
onError(e);
return null;
}
}
};
// avoid blocking the event loop while waiting for the ipc.
context.blocking(exec -> {
if (!stopping.get()) {
EthBlock event = chain.get();
if (event.getBlock() == null) {
listener.onSourceDepleted();
// block does not exist yet, wait a bit.
context.periodic(() -> ONE_SECOND, BLOCK_RETRY_TIMER, done -> {
timerId.set(done);
EthBlock retry = chain.get();
if (retry.getBlock() != null) {
context.cancel(done);
importer.accept(retry);
}
});
} else {
importer.accept(event);
}
exec.complete();
}
}, (done) -> {
});
}
});
} else {
future.fail(done.cause());
}
});
}
Aggregations