|
| 1 | +/* |
| 2 | + * Copyright 2025 IEXEC BLOCKCHAIN TECH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.iexec.blockchain.chain; |
| 18 | + |
| 19 | +import com.iexec.commons.poco.chain.SignerService; |
| 20 | +import io.micrometer.core.instrument.Metrics; |
| 21 | +import io.micrometer.core.instrument.Tag; |
| 22 | +import lombok.extern.slf4j.Slf4j; |
| 23 | +import org.springframework.scheduling.annotation.Scheduled; |
| 24 | +import org.springframework.stereotype.Service; |
| 25 | +import org.web3j.protocol.Web3j; |
| 26 | +import org.web3j.protocol.core.DefaultBlockParameterName; |
| 27 | +import org.web3j.protocol.core.methods.response.EthBlock; |
| 28 | +import org.web3j.protocol.http.HttpService; |
| 29 | +import org.web3j.utils.Async; |
| 30 | +import org.web3j.utils.Numeric; |
| 31 | + |
| 32 | +import java.math.BigInteger; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.atomic.AtomicLong; |
| 35 | + |
| 36 | +@Slf4j |
| 37 | +@Service |
| 38 | +public class BlockchainListener { |
| 39 | + |
| 40 | + static final String LATEST_BLOCK_METRIC_NAME = "iexec.chain.block.latest"; |
| 41 | + static final String TX_COUNT_METRIC_NAME = "iexec.chain.wallet.tx-count"; |
| 42 | + |
| 43 | + private final String walletAddress; |
| 44 | + private final Web3j web3Client; |
| 45 | + private final AtomicLong lastSeenBlock; |
| 46 | + private final AtomicLong latestTxGauge; |
| 47 | + private final AtomicLong pendingTxGauge; |
| 48 | + |
| 49 | + public BlockchainListener(final ChainConfig chainConfig, final SignerService signerService) { |
| 50 | + this.walletAddress = signerService.getAddress(); |
| 51 | + this.web3Client = Web3j.build(new HttpService(chainConfig.getNodeAddress()), |
| 52 | + chainConfig.getBlockTime().toMillis(), Async.defaultExecutorService()); |
| 53 | + lastSeenBlock = Metrics.gauge(LATEST_BLOCK_METRIC_NAME, new AtomicLong(0)); |
| 54 | + latestTxGauge = Metrics.gauge(TX_COUNT_METRIC_NAME, List.of(Tag.of("block", "latest")), new AtomicLong(0)); |
| 55 | + pendingTxGauge = Metrics.gauge(TX_COUNT_METRIC_NAME, List.of(Tag.of("block", "pending")), new AtomicLong(0)); |
| 56 | + } |
| 57 | + |
| 58 | + @Scheduled(fixedRate = 5000) |
| 59 | + private void run() { |
| 60 | + try { |
| 61 | + final EthBlock.Block latestBlock = |
| 62 | + web3Client.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, true).send().getBlock(); |
| 63 | + final BigInteger blockNumber = Numeric.toBigInt(latestBlock.getNumberRaw()); |
| 64 | + lastSeenBlock.set(blockNumber.longValue()); |
| 65 | + final BigInteger pendingTxCount = web3Client.ethGetTransactionCount(walletAddress, |
| 66 | + DefaultBlockParameterName.PENDING).send().getTransactionCount(); |
| 67 | + if (pendingTxCount.longValue() > pendingTxGauge.get() || pendingTxGauge.get() != latestTxGauge.get()) { |
| 68 | + final BigInteger latestTxCount = web3Client.ethGetTransactionCount(walletAddress, |
| 69 | + DefaultBlockParameterName.LATEST).send().getTransactionCount(); |
| 70 | + pendingTxGauge.set(pendingTxCount.longValue()); |
| 71 | + latestTxGauge.set(latestTxCount.longValue()); |
| 72 | + } |
| 73 | + log.info("Transaction count [block:{}, pending:{}, latest:{}]", |
| 74 | + lastSeenBlock, pendingTxGauge.get(), latestTxGauge.get()); |
| 75 | + } catch (Exception e) { |
| 76 | + log.error("An error happened while fetching data on-chain", e); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments