Skip to content

Commit

Permalink
Add getAddressAuthorizeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
leej1012 committed Aug 29, 2023
1 parent 5dbdc52 commit 17dcc62
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,18 @@ public void exportAddressTransferTxs(
HttpServletResponse resp) throws IOException {
addressService.exportAddressTransferTxs(token, language, address, start, end, resp);
}

@RequestLimit(count = 30)
@ApiOperation(value = "get address stake authorize info")
@GetMapping(value = "/{address}/authorize-info")
public ResponseBean getAddressAuthorizeInfo(@PathVariable @Length(min = 34, max = 42, message = "Incorrect address format") String address) {
return addressService.getAddressAuthorizeInfo(address);
}

@RequestLimit(count = 30)
@ApiOperation(value = "get address stake authorize info when round start")
@GetMapping(value = "/{address}/authorize-info/round-start")
public ResponseBean getAddressAuthorizeInfoWhenRoundStart(@PathVariable @Length(min = 34, max = 42, message = "Incorrect address format") String address) {
return addressService.getAddressAuthorizeInfoWhenRoundStart(address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
@Repository
public interface CommonMapper {

List<GovernanceInfoDto> findGovernanceInfo(@Param("pubKey") String pubKey, @Param("start") int start, @Param("size") int size);
List<GovernanceInfoDto> findGovernanceInfo(@Param("pubKey") String pubKey, @Param("start") int start, @Param("size") int size);

int countGovernanceInfo(@Param("pubKey") String pubKey);
int countGovernanceInfo(@Param("pubKey") String pubKey);

List<GovernanceInfoDto> getAuthorizeInfoByAddress(@Param("address") String address);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.ontio.model.common;

/**
* @author zhouq
* @version 1.0
* @date 2020/2/26
*/
public enum StakeStatusEnum {

PENDING(1),//待生效
IN_STAKE(2),//质押中
WITHDRAWABLE(3),//可提取
CANCELLING(4); //取消中

private int state;

StakeStatusEnum(int state) {
this.state = state;
}

public int state() {
return state;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@
@Setter
public class GovernanceInfoDto implements Serializable {

private String address;
private String address;

private Long consensusPos;
private Long consensusPos;

private Long candidatePos;
private Long candidatePos;

private Long newPos;
private Long newPos;

private Long withdrawConsensusPos;
private Long withdrawConsensusPos;

private Long withdrawCandidatePos;
private Long withdrawCandidatePos;

private Long withdrawUnfreezePos;

private String publicKey;

private String name;

private Long withdrawUnfreezePos;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.ontio.model.dto;

import lombok.Data;

/**
* @author lijie
* @version 1.0
* @date 2019/8/6
*/
@Data
public class NodeStakeDto {

private String nodeName;

private String nodePubKey;

private String amount;

private int state;

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ ResponseBean queryTransferTxsByTimeAndPage4Onto(String address, String assetName
ResponseBean queryTransferTxsOfTokenTypeByPage(String address, String tokenType, Integer pageNumber, Integer pageSize);

void exportAddressTransferTxs(String token, String language, String address, Integer start, Integer end, HttpServletResponse resp) throws IOException;

ResponseBean getAddressAuthorizeInfo(String address);

ResponseBean getAddressAuthorizeInfoWhenRoundStart(String address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
import com.github.ontio.mapper.*;
import com.github.ontio.model.common.PageResponseBean;
import com.github.ontio.model.common.ResponseBean;
import com.github.ontio.model.common.StakeStatusEnum;
import com.github.ontio.model.dao.*;
import com.github.ontio.model.dto.BalanceDto;
import com.github.ontio.model.dto.QueryBatchBalanceDto;
import com.github.ontio.model.dto.TransferTxDetailDto;
import com.github.ontio.model.dto.TransferTxDto;
import com.github.ontio.model.dto.*;
import com.github.ontio.model.dto.aggregation.AddressAggregationDto;
import com.github.ontio.model.dto.aggregation.AddressBalanceAggregationsDto;
import com.github.ontio.model.dto.aggregation.ExtremeBalanceDto;
import com.github.ontio.model.dto.ranking.AddressRankingDto;
import com.github.ontio.service.IAddressService;
import com.github.ontio.service.INodesService;
import com.github.ontio.util.*;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -62,12 +61,14 @@ public class AddressServiceImpl implements IAddressService {
private final CommonService commonService;
private final AddressDailyAggregationMapper addressDailyAggregationMapper;
private final RankingMapper rankingMapper;
private final INodesService nodesService;
private final CommonMapper commonMapper;


@Autowired
public AddressServiceImpl(Oep4Mapper oep4Mapper, Oep8Mapper oep8Mapper, Oep5Mapper oep5Mapper, Orc20Mapper orc20Mapper, Orc721Mapper orc721Mapper, Orc1155Mapper orc1155Mapper,
TxDetailMapper txDetailMapper, TxDetailIndexMapper txDetailIndexMapper, ParamsConfig paramsConfig, CommonService commonService,
AddressDailyAggregationMapper addressDailyAggregationMapper, RankingMapper rankingMapper) {
AddressDailyAggregationMapper addressDailyAggregationMapper, RankingMapper rankingMapper, INodesService nodesService, CommonMapper commonMapper) {
this.oep4Mapper = oep4Mapper;
this.oep8Mapper = oep8Mapper;
this.oep5Mapper = oep5Mapper;
Expand All @@ -80,6 +81,8 @@ public AddressServiceImpl(Oep4Mapper oep4Mapper, Oep8Mapper oep8Mapper, Oep5Mapp
this.commonService = commonService;
this.addressDailyAggregationMapper = addressDailyAggregationMapper;
this.rankingMapper = rankingMapper;
this.nodesService = nodesService;
this.commonMapper = commonMapper;
}

private OntologySDKService sdk;
Expand Down Expand Up @@ -1577,6 +1580,129 @@ private boolean verifyReCAPTCHAToken(String token) {
}
}

@Override
public ResponseBean getAddressAuthorizeInfo(String address) {
List<NodeInfoOffChain> currentOffChainInfo = nodesService.getCurrentOffChainInfo();
List<NodeStakeDto> nodeStakeDtos = new ArrayList<>();
initSDK();
for (NodeInfoOffChain nodeInfoOffChain : currentOffChainInfo) {
String publicKey = nodeInfoOffChain.getPublicKey();
try {
if (!publicKey.startsWith(ConstantParam.FAKE_NODE_PUBKEY_PREFIX)) {
String name = nodeInfoOffChain.getName();
String authorizeInfo = sdk.getAuthorizeInfo(publicKey, address);
putAuthorizeInfoList(authorizeInfo, name, publicKey, nodeStakeDtos);
}
} catch (Exception e) {
log.error("getAddressAuthorizeInfo error:{},{},{}", address, publicKey, e.getMessage());
}
}
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), nodeStakeDtos);
}

private void putAuthorizeInfoList(String authorizeInfo, String nodeName, String publicKey, List<NodeStakeDto> nodeStakeDtos) {
if (authorizeInfo != null) {
JSONObject authorizeInfoObj = JSONObject.parseObject(authorizeInfo);
Long consensusPos = authorizeInfoObj.getLong("consensusPos");
Long freezePos = authorizeInfoObj.getLong("freezePos");
Long newPos = authorizeInfoObj.getLong("newPos");
Long withdrawPos = authorizeInfoObj.getLong("withdrawPos");
Long withdrawFreezePos = authorizeInfoObj.getLong("withdrawFreezePos");
Long withdrawUnfreezePos = authorizeInfoObj.getLong("withdrawUnfreezePos");

if (newPos > 0) {
NodeStakeDto dto = new NodeStakeDto();
dto.setNodeName(nodeName);
dto.setNodePubKey(publicKey);
dto.setAmount(newPos.toString());
dto.setState(StakeStatusEnum.PENDING.state());
nodeStakeDtos.add(dto);
}
if (consensusPos + freezePos > 0) {
NodeStakeDto dto = new NodeStakeDto();
dto.setNodeName(nodeName);
dto.setNodePubKey(publicKey);
Long amount = consensusPos + freezePos;
dto.setAmount(amount.toString());
dto.setState(StakeStatusEnum.IN_STAKE.state());
nodeStakeDtos.add(dto);
}
if (withdrawUnfreezePos > 0) {
NodeStakeDto dto = new NodeStakeDto();
dto.setNodeName(nodeName);
dto.setNodePubKey(publicKey);
dto.setAmount(withdrawUnfreezePos.toString());
dto.setState(StakeStatusEnum.WITHDRAWABLE.state());
nodeStakeDtos.add(dto);
}
if (withdrawPos + withdrawFreezePos > 0) {
NodeStakeDto dto = new NodeStakeDto();
dto.setNodeName(nodeName);
dto.setNodePubKey(publicKey);
Long amount = withdrawPos + withdrawFreezePos;
dto.setAmount(amount.toString());
dto.setState(StakeStatusEnum.CANCELLING.state());
nodeStakeDtos.add(dto);
}
}
}

@Override
public ResponseBean getAddressAuthorizeInfoWhenRoundStart(String address) {
List<GovernanceInfoDto> authorizeInfoList = commonMapper.getAuthorizeInfoByAddress(address);
List<NodeStakeDto> nodeStakeDtos = new ArrayList<>();
for (GovernanceInfoDto governanceInfoDto : authorizeInfoList) {
putAuthorizeInfoList(governanceInfoDto, nodeStakeDtos);
}
return new ResponseBean(ErrorInfo.SUCCESS.code(), ErrorInfo.SUCCESS.desc(), nodeStakeDtos);
}

private void putAuthorizeInfoList(GovernanceInfoDto governanceInfoDto, List<NodeStakeDto> nodeStakeDtos) {
Long consensusPos = governanceInfoDto.getConsensusPos();
Long freezePos = governanceInfoDto.getCandidatePos();
Long newPos = governanceInfoDto.getNewPos();
Long withdrawPos = governanceInfoDto.getWithdrawConsensusPos();
Long withdrawFreezePos = governanceInfoDto.getWithdrawCandidatePos();
Long withdrawUnfreezePos = governanceInfoDto.getWithdrawUnfreezePos();
String publicKey = governanceInfoDto.getPublicKey();
String nodeName = governanceInfoDto.getName();

if (newPos > 0) {
NodeStakeDto dto = new NodeStakeDto();
dto.setNodeName(nodeName);
dto.setNodePubKey(publicKey);
dto.setAmount(newPos.toString());
dto.setState(StakeStatusEnum.PENDING.state());
nodeStakeDtos.add(dto);
}
if (consensusPos + freezePos > 0) {
NodeStakeDto dto = new NodeStakeDto();
dto.setNodeName(nodeName);
dto.setNodePubKey(publicKey);
Long amount = consensusPos + freezePos;
dto.setAmount(amount.toString());
dto.setState(StakeStatusEnum.IN_STAKE.state());
nodeStakeDtos.add(dto);
}
if (withdrawUnfreezePos > 0) {
NodeStakeDto dto = new NodeStakeDto();
dto.setNodeName(nodeName);
dto.setNodePubKey(publicKey);
dto.setAmount(withdrawUnfreezePos.toString());
dto.setState(StakeStatusEnum.WITHDRAWABLE.state());
nodeStakeDtos.add(dto);
}
if (withdrawPos + withdrawFreezePos > 0) {
NodeStakeDto dto = new NodeStakeDto();
dto.setNodeName(nodeName);
dto.setNodePubKey(publicKey);
Long amount = withdrawPos + withdrawFreezePos;
dto.setAmount(amount.toString());
dto.setState(StakeStatusEnum.CANCELLING.state());
nodeStakeDtos.add(dto);
}
}

private String getDescriptionByEventType(int eventType) {
String description;
switch (eventType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ public class ConstantParam {

public static final String FUN_TOTAL_SUPPLY = "totalSupply";

public static final String TOTAL_STAKE = "totalStake";

public static final String FAKE_NODE_PUBKEY_PREFIX = "00aaaaaaaaa";

/**
* pattern
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import com.github.ontio.crypto.Curve;
import com.github.ontio.crypto.KeyType;
import com.github.ontio.io.BinaryReader;
import com.github.ontio.io.BinaryWriter;
import com.github.ontio.io.Serializable;
import com.github.ontio.sdk.exception.SDKException;
import com.github.ontio.smartcontract.nativevm.abi.NativeBuildParams;
import com.github.ontio.smartcontract.neovm.abi.BuildParams;
Expand All @@ -47,6 +49,7 @@
import org.springframework.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
Expand Down Expand Up @@ -850,4 +853,52 @@ public BigDecimal getWasmvmOep4TotalSupply(String contractHash) {
}
}

public long getAddressTotalStake(String address) {
try {
OntSdk ontSdk = getOntSdk();
byte[] totalStakeBytes = ConstantParam.TOTAL_STAKE.getBytes();
byte[] addressBytes = Address.decodeBase58(address).toArray();
byte[] key = new byte[totalStakeBytes.length + addressBytes.length];
System.arraycopy(totalStakeBytes, 0, key, 0, totalStakeBytes.length);
System.arraycopy(addressBytes, 0, key, totalStakeBytes.length, addressBytes.length);
String res = ontSdk.getConnect().getStorage(Helper.reverse(contractAddress), Helper.toHexString(key));
if (res == null || "".equals(res)) {
return 0;
}
TotalStake totalStake = new TotalStake();
ByteArrayInputStream bais = new ByteArrayInputStream(Helper.hexToBytes(res));
BinaryReader reader = new BinaryReader(bais);
totalStake.deserialize(reader);
return totalStake.stake;
} catch (Exception e) {
return 0;
}
}

class TotalStake implements Serializable {
public Address address;
public long stake;
public int timeOffset;

public TotalStake() {
}

@Override
public void deserialize(BinaryReader reader) throws IOException {
try {
this.address = reader.readSerializable(Address.class);
this.stake = reader.readLong();
this.timeOffset = reader.readInt();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

@Override
public void serialize(BinaryWriter writer) throws IOException {

}
}
}
Loading

0 comments on commit 17dcc62

Please sign in to comment.