Search in sources :

Example 1 with DefaultBlockParameterNumber

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());
        }
    });
}
Also used : StorableBlock(com.codingchili.ethereumingest.model.StorableBlock) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) EthBlock(org.web3j.protocol.core.methods.response.EthBlock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TX_ADDR(com.codingchili.ethereumingest.importer.ApplicationContext.TX_ADDR) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Observable(rx.Observable) Serializer(com.codingchili.core.protocol.Serializer) CompositeFuture(io.vertx.core.CompositeFuture) Importer(com.codingchili.ethereumingest.model.Importer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JsonObject(io.vertx.core.json.JsonObject) ID_TIME(com.codingchili.core.configuration.CoreStrings.ID_TIME) CoreContext(com.codingchili.core.context.CoreContext) AsyncStorage(com.codingchili.core.storage.AsyncStorage) Subscriber(rx.Subscriber) ApplicationContext.getIpcClient(com.codingchili.ethereumingest.importer.ApplicationContext.getIpcClient) IOException(java.io.IOException) Future(io.vertx.core.Future) BlockLogListener(com.codingchili.ethereumingest.model.BlockLogListener) DefaultBlockParameterNumber(org.web3j.protocol.core.DefaultBlockParameterNumber) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Web3j(org.web3j.protocol.Web3j) ImportListener(com.codingchili.ethereumingest.model.ImportListener) ApplicationConfig(com.codingchili.ethereumingest.model.ApplicationConfig) EthBlock(org.web3j.protocol.core.methods.response.EthBlock) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Web3j(org.web3j.protocol.Web3j) Consumer(java.util.function.Consumer) DefaultBlockParameterNumber(org.web3j.protocol.core.DefaultBlockParameterNumber) Supplier(java.util.function.Supplier)

Aggregations

ID_TIME (com.codingchili.core.configuration.CoreStrings.ID_TIME)1 CoreContext (com.codingchili.core.context.CoreContext)1 Serializer (com.codingchili.core.protocol.Serializer)1 AsyncStorage (com.codingchili.core.storage.AsyncStorage)1 TX_ADDR (com.codingchili.ethereumingest.importer.ApplicationContext.TX_ADDR)1 ApplicationContext.getIpcClient (com.codingchili.ethereumingest.importer.ApplicationContext.getIpcClient)1 ApplicationConfig (com.codingchili.ethereumingest.model.ApplicationConfig)1 BlockLogListener (com.codingchili.ethereumingest.model.BlockLogListener)1 ImportListener (com.codingchili.ethereumingest.model.ImportListener)1 Importer (com.codingchili.ethereumingest.model.Importer)1 StorableBlock (com.codingchili.ethereumingest.model.StorableBlock)1 CompositeFuture (io.vertx.core.CompositeFuture)1 Future (io.vertx.core.Future)1 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)1 JsonObject (io.vertx.core.json.JsonObject)1 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1