Skip to content

Commit 2fc977b

Browse files
authored
fix: use less RPC calls to listen on the blockchain (#179)
1 parent 61ff9e6 commit 2fc977b

File tree

3 files changed

+83
-118
lines changed

3 files changed

+83
-118
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

src/main/java/com/iexec/blockchain/chain/WebSocketBlockchainListener.java

Lines changed: 0 additions & 115 deletions
This file was deleted.

src/test/java/com/iexec/blockchain/chain/WebSocketBlockchainListenerTests.java renamed to src/test/java/com/iexec/blockchain/chain/BlockchainListenerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
import java.util.Objects;
3434
import java.util.concurrent.TimeUnit;
3535

36-
import static com.iexec.blockchain.chain.WebSocketBlockchainListener.LATEST_BLOCK_METRIC_NAME;
37-
import static com.iexec.blockchain.chain.WebSocketBlockchainListener.TX_COUNT_METRIC_NAME;
36+
import static com.iexec.blockchain.chain.BlockchainListener.LATEST_BLOCK_METRIC_NAME;
37+
import static com.iexec.blockchain.chain.BlockchainListener.TX_COUNT_METRIC_NAME;
3838
import static org.assertj.core.api.Assertions.assertThat;
3939
import static org.awaitility.Awaitility.await;
4040

4141
@Slf4j
4242
@Testcontainers
4343
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
44-
class WebSocketBlockchainListenerTests {
44+
class BlockchainListenerTests {
4545

4646
private static final String CHAIN_SVC_NAME = "ibaa-chain";
4747
private static final int CHAIN_SVC_PORT = 8545;

0 commit comments

Comments
 (0)