Commit 3630a582 authored by jianhua.zhang's avatar jianhua.zhang

账户余额功能调整

parent 370e0efd
......@@ -62,7 +62,7 @@ public class Bootstrapper {
public synchronized void start() {
this.init();
this.startEngine();
this.executorService.scheduleWithFixedDelay(() -> this.lastBlockService.sync(), 0, 30, TimeUnit.MINUTES);
this.executorService.scheduleWithFixedDelay(() -> this.lastBlockService.sync(), 0, 3, TimeUnit.MINUTES);
this.executorService.scheduleWithFixedDelay(() -> this.lastBlockService.refresh(), 1, 1, TimeUnit.MINUTES);
this.executorService.scheduleWithFixedDelay(() -> this.transactionService.censusBlockByLastDay(), getInitialDelay(), 24 * 60 * 60, TimeUnit.SECONDS);
}
......
......@@ -99,7 +99,15 @@ public class TransactionControllerV1 {
Address address = this.addressService.selectOneByExample(addressExample);
if (address != null) {
model.setAddress(address.getAddress());
model.setBalance(address.getBalance()+"");
// 账户余额 = 可用余额+冻结余额
BigDecimal accountBalance = new BigDecimal(0);
if (address.getBalance() != null) {
accountBalance = BigDecimalUtil.getAdd(accountBalance, new BigDecimal(address.getBalance()));
}
if (address.getFrozenBalance() != null) {
accountBalance = BigDecimalUtil.getAdd(accountBalance, new BigDecimal(address.getFrozenBalance()));
}
model.setBalance(accountBalance.toPlainString());
}
resDataModel.setData(model);
resDataModel.setT(HomeSearchTypeEnum.ADDRESS_INFO.getCode());
......@@ -148,8 +156,8 @@ public class TransactionControllerV1 {
@RequestMapping(value="indexGetBlockInfo", method = RequestMethod.GET)
public ApiResponse lastBlockList() {
BlockHeaderExample headerExample = new BlockHeaderExample();
PageInfo<BlockHeader> pageInfo = this.blockHeaderService.getByPagerEx(null, null, headerExample);
List<BlockInfoModel> list = transferBlockInfoModel(pageInfo);
PageInfo<BlockHeader> pageInfo = this.blockHeaderService.getByPagerEx(1, 11, headerExample);
List<BlockInfoModel> list = transferBlockInfoModelV2(pageInfo);
return ResponseKit.success(list);
}
......@@ -171,6 +179,33 @@ public class TransactionControllerV1 {
return ResponseKit.success(model);
}
private List<BlockInfoModel> transferBlockInfoModelV2(PageInfo<BlockHeader> pageInfo) {
List<BlockInfoModel> list = new ArrayList<>();
if (!CollectionUtils.isEmpty(pageInfo.getList())) {
for (int i = 0; i < pageInfo.getList().size() - 1; i++) {
BlockHeader o = pageInfo.getList().get(i);
BlockInfoModel model = BlockInfoModel.getInstance();
model.setNumber(o.getNumber());
BlockHeader pre = pageInfo.getList().get(i+1);
Long preTime = pre.getTimestamp()/1000;
model.setMined_time(preTime.toString());
if (o.getTimestamp() != null) {
Long tt = o.getTimestamp()/1000;
model.setTimestamp(tt.toString());
}
model.setMiner(o.getWitnessAddress());
model.setParentHash(o.getParentHash());
model.setHash(o.getBlockId());
model.setTrans_number(o.getTransactionVolume());
model.setSize(o.getBlockBytes());
list.add(model);
}
}
return list;
}
private List<BlockInfoModel> transferBlockInfoModel(PageInfo<BlockHeader> pageInfo) {
List<BlockInfoModel> list = new ArrayList<>();
if (!CollectionUtils.isEmpty(pageInfo.getList())) {
......@@ -382,15 +417,27 @@ public class TransactionControllerV1 {
PageInfo<Address> pageInfo = this.addressService.selectByPager(pageNo, pageSize, example);
resDataModel.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
List<AccountInfoModel> modelList = new ArrayList<>();
if (!CollectionUtils.isEmpty(pageInfo.getList())) {
pageInfo.getList().forEach(o -> {
AccountInfoModel infoModel = AccountInfoModel.getInstance();
infoModel.setAddress(o.getAddress());
infoModel.setBalance(o.getBalance().toString());
//BigDecimal balance = BigDecimalUtil.getDevide(new BigDecimal(o.getBalance()), BigDecimalUtil.NUM);
// 账户余额 = 可用余额+冻结余额
BigDecimal accountBalance = new BigDecimal(0);
if (o.getBalance() != null) {
accountBalance = BigDecimalUtil.getAdd(accountBalance, new BigDecimal(o.getBalance()));
}
if (o.getFrozenBalance() != null) {
accountBalance = BigDecimalUtil.getAdd(accountBalance, new BigDecimal(o.getFrozenBalance()));
}
infoModel.setBalance(accountBalance.toPlainString());
modelList.add(infoModel);
});
}
BigDecimal bg = this.totalBalance();
resDataModel.setSum(bg.toPlainString());
resDataModel.setData(modelList);
......@@ -411,6 +458,7 @@ public class TransactionControllerV1 {
}
return totalBalance;
//return BigDecimalUtil.getDevide(totalBalance, BigDecimalUtil.NUM);
}
/**
......@@ -427,11 +475,11 @@ public class TransactionControllerV1 {
List<BlockDayCensus> list = pageInfo.getList();
HomeChartModel model;
int id = 1;
for (int i = 0; i < list.size(); i++) {
for (int i = list.size() - 1; i >=0; i--) {
model = new HomeChartModel();
model.setId(id);
long time = DateUtil.getDateFromDateStr(list.get(i).getCensusDate(), DateUtil.PATTERN_YMD);
model.setTime(time);
model.setTime(time/1000);
model.setCount(list.get(i).getTotalVolume());
modelList.add(model);
id++;
......
......@@ -2,6 +2,8 @@ package com.wuban.tron.explore.domain;
import lombok.Data;
import java.util.List;
/**
* <core>波场区块账号信息</core>
*
......@@ -13,6 +15,7 @@ public class TronAccount {
private String address;
private Long balance;
private List<TronFreeze> frozen;
/* private Long create_time;
private Long latest_opration_time;
private Long latest_consume_free_time;
......
package com.wuban.tron.explore.domain;
import lombok.Data;
@Data
public class TronFreeze {
private long frozen_balance;
private long expire_time;
}
package com.wuban.tron.explore.entity;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
import lombok.*;
/**
* 账户地址表
......@@ -23,15 +16,22 @@ public class Address {
* 账户地址
*/
private String address;
/**
* 余额
*/
private Long balance;
/**
* 冻结余额
*/
private Long frozenBalance;
public static Address getInstance() {
return Address.builder()
.address("")
.balance(0L)
.frozenBalance(0L)
.build();
}
}
\ No newline at end of file
......@@ -45,7 +45,7 @@ public class Engine {
*/
private AddressBalanceHandler addressHandler;
public static final int BLOCK_FETCHER_NUM = 10;
public static final int BLOCK_FETCHER_NUM = 5;
public static final int BALANCE_FETCHER_NUM = 3;
......@@ -78,7 +78,7 @@ public class Engine {
this.handler = new BlockDataHandler();
this.fetcher = new BlockDataFetcher(this.handler);
this.executor.execute(this.handler);
for (int i = 0; i <= BLOCK_FETCHER_NUM; i++) {
for (int i = 0; i < BLOCK_FETCHER_NUM; i++) {
this.executor.execute(this.fetcher);
}
......
......@@ -20,16 +20,16 @@ import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
public class Executor {
private static final int SIZE = 20;
private static final int SIZE = 15;
private final List<AbstractJob> jobList = new ArrayList<>();
private final String name;
private ThreadPoolExecutor pool;
public Executor(final String name) {
this.name = name;
this.pool = new ThreadPoolExecutor(SIZE, SIZE * 3, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
this.pool = new ThreadPoolExecutor(SIZE, SIZE * 2, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
new ExecutorThreadFactory());
log.info("初始化线程池 name:{} coreSize:{} maxSize:{}", name, SIZE, SIZE * 3);
log.info("初始化线程池 name:{} coreSize:{} maxSize:{}", name, SIZE, SIZE * 2);
}
public void execute(final AbstractJob r) {
......
package com.wuban.tron.explore.handler;
import com.wuban.tron.explore.domain.TronAccount;
import com.wuban.tron.explore.domain.TronFreeze;
import com.wuban.tron.explore.entity.Address;
import com.wuban.tron.explore.fetch.AbstractJob;
import com.wuban.tron.explore.fetch.PersistThreadPool;
import com.wuban.tron.explore.service.AddressService;
import com.wuban.tron.explore.util.BigDecimalUtil;
import com.wuban.tron.explore.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.tron.common.utils.ByteArray;
import org.tron.walletserver.WalletApi;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
......@@ -54,11 +57,23 @@ public class AddressBalanceHandler extends AbstractJob implements IAddressBalanc
@Override
public void flush(List<TronAccount> e) {
if (!CollectionUtils.isEmpty(e)) {
TronAccount account = e.get(0);
Address obj = new Address();
String address = WalletApi.encode58Check(ByteArray.fromHexString(e.get(0).getAddress()));
obj.setAddress(address);
obj.setBalance(e.get(0).getBalance());
obj.setBalance(account.getBalance());
// 冻结余额
BigDecimal frozenBalance = new BigDecimal(0);
List<TronFreeze> tronFreezes = account.getFrozen();
if (!CollectionUtils.isEmpty(tronFreezes)) {
for (int i = 0; i < tronFreezes.size(); i++) {
frozenBalance = BigDecimalUtil.getAdd(frozenBalance, new BigDecimal(tronFreezes.get(i).getFrozen_balance()));
}
}
obj.setFrozenBalance(frozenBalance.longValue());
this.addressService.updateById(obj);
log.info("更新账户余额 account:{}", obj.toString());
}
......
......@@ -37,6 +37,8 @@ public abstract class BaseCommonService {
*/
protected static final String GET_ACCOUNT = PREFIX + "/getaccount";
protected static final String FREEZE_BALANCE = "/wallet/freezebalance";
@Value("${tron.site}")
private String tronSite;
......
......@@ -56,7 +56,7 @@ public class BlockHeaderServiceImpl implements BlockHeaderService {
pageSize = PageConstant.MAX_PAGE_SIZE;
}
example.setOrderByClause("`timestamp` DESC");
example.setOrderByClause("`number` DESC");
PageHelper.startPage(startIndex, pageSize);
List<BlockHeader> list = this.blockHeaderRepository.selectByPagerEx(example);
PageInfo<BlockHeader> pageInfo = new PageInfo<>(list);
......
......@@ -70,4 +70,5 @@ public class TronServiceImpl extends BaseCommonService implements TronService {
return JSONObject.parseObject(str, TronAccount.class);
}
}
......@@ -13,6 +13,7 @@ import java.math.RoundingMode;
public class BigDecimalUtil {
public static final int SCALE = 32;
public static final BigDecimal NUM = new BigDecimal(1000000);
public static BigDecimal getDevide(BigDecimal bg1, BigDecimal bg2){
MathContext mc = new MathContext(SCALE, RoundingMode.HALF_DOWN);
......
......@@ -169,7 +169,7 @@ public class DateUtil {
if (date != null) {
cal.setTime(date);
}
cal.add(0, days);
cal.add(Calendar.DATE, days);
return cal.getTime();
}
......
......@@ -5,6 +5,7 @@
<resultMap id="AddressMap" type="com.wuban.tron.explore.entity.Address">
<result column="address" property="address" jdbcType="VARCHAR" />
<result column="balance" property="balance" jdbcType="BIGINT" />
<result column="frozen_balance" property="frozenBalance" jdbcType="BIGINT" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
......@@ -59,6 +60,7 @@
<sql id="Update_Set_From_Bean">
<if test="record.address != null">address = #{record.address,jdbcType=VARCHAR} ,</if>
<if test="record.balance != null">balance = #{record.balance,jdbcType=BIGINT} ,</if>
<if test="record.frozenBalance != null">frozen_balance = #{record.frozenBalance,jdbcType=BIGINT} ,</if>
</sql>
<!-- insert -->
<insert id="insert" parameterType="java.util.Map">
......@@ -164,7 +166,7 @@
</select>
<select id="selectOneByExample" resultMap="AddressMap" parameterType="java.util.Map">
select
address,balance
address,balance,frozen_balance
from <include refid="Table_Name"/>
<if test="example != null">
<include refid="Example_Where_Clause"/>
......@@ -197,7 +199,7 @@
<select id="selectByPager" resultMap="AddressMap" parameterType="java.util.Map">
select
address,balance
address,balance,frozen_balance
from <include refid="Table_Name"/>
<if test="example != null">
<include refid="Example_Where_Clause"/>
......
package com.wuban.tron.explore.service.impl;
import com.wuban.tron.explore.domain.TronAccount;
import com.wuban.tron.explore.domain.TronResponseArrayData;
import com.wuban.tron.explore.domain.TronResponseData;
import com.wuban.tron.explore.service.TronService;
......@@ -9,6 +10,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.tron.common.utils.ByteArray;
import org.tron.walletserver.WalletApi;
@RunWith(SpringRunner.class)
@SpringBootTest
......@@ -32,7 +35,16 @@ class TronServiceImplTest {
@Test
void getAccount() {
this.tronService.getAccount("41705b35c6bb37387cd4cfb57bfc83b74d4374a78a");
TronAccount account = this.tronService.getAccount("4151ac26e9d4b3810ce891d660e13202f7cda09a41");
System.out.println(account.toString());
}
@Test
void getFreezeBalance() {
String base58check = "THR3xvo49HY6UzFQugRTaeUdgWx7zbkM1k";
String hexString = ByteArray.toHexString(WalletApi.decodeFromBase58Check(base58check));
log.info(hexString);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment