Helius
2021-05-14 baaab35e79a61784cde5c6a503eef3114e2169f0
Merge branch 'activity' into yunding
1 files added
2 files modified
83 ■■■■ changed files
src/main/java/com/xcong/excoin/modules/blackchain/service/TrxUsdtUpdateService.java 13 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/quartz/job/BlockCoinUpdateJob.java 22 ●●●●● patch | view | raw | blame | history
src/test/java/com/xcong/excoin/TrcTest.java 48 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/excoin/modules/blackchain/service/TrxUsdtUpdateService.java
@@ -1,6 +1,8 @@
package com.xcong.excoin.modules.blackchain.service;
import cn.hutool.core.math.MathUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@@ -167,7 +169,8 @@
        Map<String, Object> map = new HashMap<>();
        map.put("num", num);
        String param = JSON.toJSONString(map);
        return postForEntity(url, param).getBody();
//        return postForEntity(url, param).getBody();
        return postForEntityHuTool(url, param).body();
    }
@@ -208,6 +211,14 @@
        return result;
    }
    private static HttpResponse postForEntityHuTool(String url, String param) {
        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
        return HttpUtil.createPost(url).body(param)
                .timeout(20000).contentType("application/json")
                .header("TRON-PRO-API-KEY", Trc20Service.API_KEY)
                .execute();
    }
    /**
     * 比对本地地址 同步充值USDT-TRC20
     *
src/main/java/com/xcong/excoin/quartz/job/BlockCoinUpdateJob.java
@@ -1,5 +1,6 @@
package com.xcong.excoin.quartz.job;
import cn.hutool.http.HttpException;
import com.xcong.excoin.modules.blackchain.service.TrxUsdtUpdateService;
import com.xcong.excoin.modules.coin.service.BlockCoinService;
import com.xcong.excoin.utils.RedisUtils;
@@ -21,7 +22,7 @@
 **/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app", name = "block-job", havingValue = "true")
//@ConditionalOnProperty(prefix = "app", name = "block-job", havingValue = "true")
public class BlockCoinUpdateJob {
    @Resource
@@ -50,7 +51,7 @@
        redisUtils.set("USDT_TRC20_CURRENT_BLOCK_NUM", blocnNum);
        try {
            trxUsdtUpdateService.monitorCoinListener(blocnNum);
        } catch (RestClientException e) {
        } catch (RestClientException | HttpException e) {
            //  此时是连接问题 这个块需要重新扫描
            log.info("查询区块超时:" + blocnNum);
            TRC_BLOCK.add(blocnNum);
@@ -60,11 +61,11 @@
    }
    @Scheduled(cron = "0/2 * * * * ? ")
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void usdtTc20UpdateQueue() {
        // 查询最新区块号
        long getnowblock = trxUsdtUpdateService.getnowblockFromTronScan() - 25;
        // 生成块到队列
        // 拿到redis里最新区块
        Object trc20BlockNum = redisUtils.get("USDT_TRC20_BLOCK_NUM");
        if (trc20BlockNum == null) {
            // 没有则取最新的块
@@ -72,13 +73,18 @@
            redisUtils.set("USDT_TRC20_BLOCK_NUM", getnowblock);
        }
        Long blockNum = Long.valueOf(trc20BlockNum.toString());
        if (getnowblock < blockNum) {
        if (getnowblock <= blockNum) {
            // 如果当前区块比最新已确认区块还大,则不继续执行
            return;
        }
        // 将得到的区块+1 放入队列
        TRC_BLOCK.add(blockNum + 1L);
        redisUtils.incr("USDT_TRC20_BLOCK_NUM", 1);
        // 得到最新区块和当前区块的差值
        Long diff = getnowblock-blockNum;
        for(long i=1;i<=diff;i++){
            blockNum++;
            TRC_BLOCK.add(blockNum);
        }
        // 将最新的最大区块放入redis
        redisUtils.set("USDT_TRC20_BLOCK_NUM", blockNum);
    }
    /**
src/test/java/com/xcong/excoin/TrcTest.java
New file
@@ -0,0 +1,48 @@
package com.xcong.excoin;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.xcong.excoin.modules.blackchain.service.Trc20Service;
import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/**
 * @author wzy
 * @date 2021-05-04
 **/
@Slf4j
@SpringBootTest
public class TrcTest {
    @Autowired
    private RedisUtils redisUtils;
    @Test
    public void trc20Test() {
        String url = "https://api.trongrid.io/wallet/getblockbynum";
        while(true) {
            Object current = redisUtils.get("USDT_TRC20_CURRENT_BLOCK_NUM");
            Map<String, Object> map = new HashMap<>();
            map.put("num", current);
            String param = JSON.toJSONString(map);
            System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
            HttpResponse response = HttpUtil.createPost(url).body(param).contentType("application/json").header("TRON-PRO-API-KEY", Trc20Service.API_KEY).execute();
//            log.info(response.body());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}