Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/main/java/com/iexec/core/chain/BlockchainListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2025-2026 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.iexec.core.chain;

import com.iexec.core.chain.event.LatestBlockEvent;
import io.micrometer.core.instrument.Metrics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.exceptions.JsonRpcError;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Async;

import java.time.Instant;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
@Service
public class BlockchainListener {
static final String LATEST_BLOCK_METRIC_NAME = "iexec.chain.block.latest";

private final ApplicationEventPublisher applicationEventPublisher;
private final Web3j web3Client;
private final AtomicLong lastSeenBlock;

public BlockchainListener(final ApplicationEventPublisher applicationEventPublisher,
final ChainConfig chainConfig) {
this.applicationEventPublisher = applicationEventPublisher;
this.web3Client = Web3j.build(new HttpService(chainConfig.getNodeAddress()),
chainConfig.getBlockTime().toMillis(), Async.defaultExecutorService());
lastSeenBlock = Metrics.gauge(LATEST_BLOCK_METRIC_NAME, new AtomicLong(0));
}

@Scheduled(fixedRate = 5000)
Comment thread
jbern0rd marked this conversation as resolved.
private void run() {
try {
final EthBlock ethBlock = web3Client.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send();
if (ethBlock.hasError()) {
throw new JsonRpcError(ethBlock.getError());
}
Comment thread
jbern0rd marked this conversation as resolved.
final long blockNumber = ethBlock.getBlock().getNumber().longValue();
final long lastSeenBlockNumber = lastSeenBlock.get();
if (blockNumber > lastSeenBlockNumber) {
final String blockHash = ethBlock.getBlock().getHash();
final long blockTimestamp = ethBlock.getBlock().getTimestamp().longValue();
final Instant blockTimestampInstant = Instant.ofEpochSecond(blockTimestamp);
log.info("Last seen block [number:{}, hash:{}, timestamp:{}, instant:{}]",
blockNumber, blockHash, blockTimestamp, blockTimestampInstant);
lastSeenBlock.set(blockNumber);
applicationEventPublisher.publishEvent(new LatestBlockEvent(this, blockNumber, blockHash, blockTimestamp));
}
} catch (Exception e) {
log.error("An error happened while fetching data on-chain", e);
}
Comment thread
jbern0rd marked this conversation as resolved.
Comment thread
jbern0rd marked this conversation as resolved.
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ wallet:

chain:
node-address: ${IEXEC_BLOCKCHAIN_NODE_ADDRESS:http://localhost:8545}
out-of-service-threshold: PT5S
out-of-service-threshold: PT15S
pool-address: ${POOL_ADDRESS:0x365E7BABAa85eC61Dffe5b520763062e6C29dA27}
start-block-number: ${IEXEC_START_BLOCK_NUMBER:0}
gas-price-multiplier: ${IEXEC_GAS_PRICE_MULTIPLIER:1.0} # txs will be sent with networkGasPrice*gasPriceMultiplier, 4.0 means super fast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static com.iexec.core.chain.WebSocketBlockchainListener.LATEST_BLOCK_METRIC_NAME;
import static com.iexec.core.chain.BlockchainListener.LATEST_BLOCK_METRIC_NAME;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

@Testcontainers
@SpringBootTest(properties = "chain.out-of-service-threshold=PT30S")
class WebSocketBlockchainListenerTests {
class BlockchainListenerTests {
private static final String CHAIN_SVC_NAME = "chain";
private static final int CHAIN_SVC_PORT = 8545;
private static final String CONFIG_SVC_NAME = "config-server";
Expand Down