From 67385dc8932381c1406e3f5a0f03cc8048acb739 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Wed, 03 Aug 2022 15:01:48 +0800
Subject: [PATCH] fix
---
src/main/java/cc/mrbird/febs/job/ChainListenerJob.java | 82 +++++++++++++++++++++++++++++++++--------
1 files changed, 66 insertions(+), 16 deletions(-)
diff --git a/src/main/java/cc/mrbird/febs/job/ChainListenerJob.java b/src/main/java/cc/mrbird/febs/job/ChainListenerJob.java
index 04c0bbb..c1efb2c 100644
--- a/src/main/java/cc/mrbird/febs/job/ChainListenerJob.java
+++ b/src/main/java/cc/mrbird/febs/job/ChainListenerJob.java
@@ -7,6 +7,11 @@
import cc.mrbird.febs.dapp.chain.ContractEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@@ -14,7 +19,8 @@
@Slf4j
@Component
-public class ChainListenerJob {
+@ConditionalOnProperty(prefix = "system", name = "chain-listener", havingValue = "true")
+public class ChainListenerJob implements ApplicationRunner {
@Autowired
private ContractEventService bscCoinContractEvent;
@@ -25,26 +31,70 @@
@Autowired
private RedisUtils redisUtils;
- @PostConstruct
- public void chainListenerJob() {
- log.info("监听打开");
- BigInteger usdtBlock;
- BigInteger coinBlock;
- Object usdt = redisUtils.get(AppContants.REDIS_KEY_BLOCK_USDT_NUM);
- if (usdt == null) {
- usdtBlock = new BigInteger("19811973");
+ @Scheduled(cron = "0 0/5 * * * ? ")
+ public void chainBlockUpdate() {
+ log.info("最新区块更新");
+ BigInteger blockNumber = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber();
+
+ redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM, blockNumber);
+ }
+
+ @Scheduled(cron = "0/2 * * * * ? ")
+ public void chainIncrementBlock() {
+ Object newestBlockObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_NEWEST_NUM);
+ BigInteger newestBlock;
+ if (newestBlockObj == null) {
+ newestBlock = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber();
} else {
- usdtBlock = (BigInteger) usdt;
+ newestBlock = (BigInteger) newestBlockObj;
}
- Object coin = redisUtils.get(AppContants.REDIS_KEY_BLOCK_COIN_NUM);
- if (coin == null) {
- coinBlock = new BigInteger("19811973");
+ Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM);
+ BigInteger toIncrement;
+ if (incrementObj == null) {
+ toIncrement = newestBlock;
} else {
- coinBlock = (BigInteger) coin;
+ BigInteger incrementBlock = (BigInteger) incrementObj;
+
+ // 最新区块小于增加区块
+ if (newestBlock.compareTo(incrementBlock) <= 0) {
+ return;
+ }
+ toIncrement = incrementBlock.add(BigInteger.ONE);
}
- ChainService.contractEventListener(usdtBlock, bscUsdtContractEvent, ChainEnum.BSC_USDT.name());
- ChainService.contractEventListener(coinBlock, bscCoinContractEvent, ChainEnum.BSC_TFC.name());
+ redisUtils.set(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM, toIncrement);
+ }
+
+ @Override
+ public void run(ApplicationArguments args) throws Exception {
+ long start = System.currentTimeMillis();
+ log.info("区块链监听开始启动");
+ Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM);
+ BigInteger newest = ChainService.getInstance(ChainEnum.BSC_TFC.name()).blockNumber();
+ BigInteger block;
+ if (incrementObj == null) {
+ block = newest;
+ } else {
+ block = (BigInteger) incrementObj;
+ }
+
+ BigInteger section = BigInteger.valueOf(5000);
+ while (newest.subtract(block).compareTo(section) > -1) {
+ BigInteger end = block.add(section);
+ log.info("监听:[{} - {}]", block, end);
+ ChainService.contractEventListener(block, end, bscUsdtContractEvent, ChainEnum.BSC_USDT.name());
+ ChainService.contractEventListener(block, end, bscCoinContractEvent, ChainEnum.BSC_TFC.name());
+
+ block = block.add(section);
+ if (block.compareTo(newest) > 0) {
+ block = newest;
+ }
+ }
+ ChainService.contractEventListener(block, bscUsdtContractEvent, ChainEnum.BSC_USDT.name());
+ ChainService.contractEventListener(block, bscCoinContractEvent, ChainEnum.BSC_TFC.name());
+
+ long end = System.currentTimeMillis();
+ log.info("区块链监听启动完成, 消耗时间{}", end - start);
}
}
--
Gitblit v1.9.1