use of io.nuls.consensus.entity.Consensus in project nuls by nuls-io.
the class ConsensusMeetingRunner method getDefaultSeedList.
private List<Consensus<Agent>> getDefaultSeedList() throws IOException {
List<Consensus<Agent>> seedList = new ArrayList<>();
if (consensusManager.getSeedNodeList() == null) {
return seedList;
}
for (String address : consensusManager.getSeedNodeList()) {
Consensus<Agent> member = new ConsensusAgentImpl();
member.setAddress(address);
Agent agent = new Agent();
agent.setPackingAddress(address);
agent.setStartTime(0);
agent.setIntroduction("seed");
agent.setCommissionRate(0);
agent.setStatus(ConsensusStatusEnum.IN.getCode());
agent.setSeed(true);
agent.setDeposit(Na.ZERO);
member.setExtend(agent);
seedList.add(member);
}
return seedList;
}
use of io.nuls.consensus.entity.Consensus in project nuls by nuls-io.
the class DepositAmountValidator method validate.
@Override
public ValidateResult validate(PocJoinConsensusTransaction data) {
Na limit = PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT;
Na max = PocConsensusConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT;
List<Consensus<Deposit>> list = consensusCacheManager.getCachedDepositListByAgentHash(data.getTxData().getExtend().getAgentHash());
if (list == null) {
return ValidateResult.getSuccessResult();
}
Na total = Na.ZERO;
for (Consensus<Deposit> cd : list) {
total = total.add(cd.getExtend().getDeposit());
}
if (limit.isGreaterThan(data.getTxData().getExtend().getDeposit())) {
return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_NOT_ENOUGH);
}
if (max.isLessThan(total)) {
return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_TOO_MUCH);
}
if (!data.getTxData().getExtend().getDeposit().equals(data.getCoinData().getTotalNa())) {
return ValidateResult.getFailedResult(SeverityLevelEnum.FLAGRANT_FOUL, ErrorCode.DEPOSIT_ERROR);
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.consensus.entity.Consensus in project nuls by nuls-io.
the class ConsensusMeetingRunner method nextRound.
private void nextRound() throws NulsException, IOException {
consensusManager.initConsensusStatusInfo();
PocMeetingRound currentRound = calcRound();
consensusManager.setCurrentRound(currentRound);
while (TimeService.currentTimeMillis() < (currentRound.getStartTime())) {
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
Log.error(e);
}
}
boolean imIn = consensusManager.isPartakePacking();
List<Consensus<Agent>> list = calcConsensusAgentList();
currentRound.setMemberCount(list.size());
while (currentRound.getEndTime() < TimeService.currentTimeMillis()) {
long time = TimeService.currentTimeMillis() - currentRound.getStartTime();
long roundTime = currentRound.getEndTime() - currentRound.getStartTime();
long index = time / roundTime;
long startTime = currentRound.getStartTime() + index * roundTime;
currentRound.setStartTime(startTime);
currentRound.setIndex(currentRound.getPreviousRound().getIndex() + index);
}
Map<String, List<Consensus<Deposit>>> depositMap = new HashMap<>();
List<Consensus<Deposit>> delegateList = consensusCacheManager.getCachedDepositList();
Na totalDeposit = Na.ZERO;
for (Consensus<Deposit> cd : delegateList) {
List<Consensus<Deposit>> sonList = depositMap.get(cd.getExtend().getAgentHash());
if (null == sonList) {
sonList = new ArrayList<>();
}
sonList.add(cd);
depositMap.put(cd.getExtend().getAgentHash(), sonList);
totalDeposit = totalDeposit.add(cd.getExtend().getDeposit());
}
List<PocMeetingMember> memberList = new ArrayList<>();
for (Consensus<Agent> ca : list) {
boolean isSeed = ca.getExtend().getSeed();
if (!isSeed && ca.getExtend().getDeposit().isLessThan(PocConsensusConstant.AGENT_DEPOSIT_LOWER_LIMIT)) {
continue;
}
PocMeetingMember mm = new PocMeetingMember();
mm.setAgentConsensus(ca);
mm.setDelegateList(depositMap.get(ca.getHexHash()));
if (!isSeed && (mm.getDelegateList() == null || mm.getDelegateList().size() > PocConsensusConstant.MAX_ACCEPT_NUM_OF_DEPOSIT)) {
continue;
}
mm.calcDeposit();
if (!isSeed && mm.getTotolEntrustDeposit().isLessThan(PocConsensusConstant.SUM_OF_DEPOSIT_OF_AGENT_LOWER_LIMIT)) {
continue;
}
mm.setRoundIndex(currentRound.getIndex());
mm.setAgentHash(ca.getHexHash());
mm.setAgentAddress(ca.getAddress());
mm.setPackerAddress(ca.getExtend().getPackingAddress());
mm.setRoundStartTime(currentRound.getStartTime());
memberList.add(mm);
totalDeposit = totalDeposit.add(ca.getExtend().getDeposit());
}
Collections.sort(memberList);
currentRound.setMemberList(memberList);
currentRound.setTotalDeposit(totalDeposit);
if (imIn) {
startMeeting();
}
}
use of io.nuls.consensus.entity.Consensus in project nuls by nuls-io.
the class ConsensusManager method initConsensusStatusInfo.
public void initConsensusStatusInfo() {
List<Consensus<Agent>> agentList = consensusCacheManager.getCachedAgentList();
ConsensusStatusInfo info = new ConsensusStatusInfo();
for (String address : NulsContext.LOCAL_ADDRESS_LIST) {
if (this.seedNodeList.contains(address)) {
info.setAccount(accountService.getAccount(address));
info.setStatus(ConsensusStatusEnum.IN.getCode());
info.setSeed(true);
break;
}
for (Consensus<Agent> agent : agentList) {
if (agent.getExtend().getPackingAddress().equals(address)) {
info.setAccount(accountService.getAccount(address));
info.setStatus(agent.getExtend().getStatus());
info.setSeed(agent.getExtend().getSeed());
if (ConsensusStatusEnum.NOT_IN.getCode() != info.getStatus()) {
break;
}
}
}
}
if (info.getAccount() == null) {
info.setStatus(ConsensusStatusEnum.NOT_IN.getCode());
}
this.consensusStatusInfo = info;
}
use of io.nuls.consensus.entity.Consensus in project nuls by nuls-io.
the class PocConsensusServiceImpl method getConsensusInfo.
@Override
public Map<String, Object> getConsensusInfo(String address) {
if (StringUtils.isNotBlank(address)) {
return getConsensusInfoByAddress(address);
}
List<Account> accountList = this.accountService.getAccountList();
if (accountList == null || accountList.isEmpty()) {
return null;
}
long lastDayTime = TimeService.currentTimeMillis() - DateUtil.DATE_TIME;
int agentCount = 0;
long totalDeposit = 0;
long reward = 0;
long rewardOfDay = 0;
long usableBalance = 0;
Set<String> joinedAgent = new HashSet<>();
for (Account account : accountList) {
Consensus<Agent> agent = this.consensusCacheManager.getCachedAgentByAddress(account.getAddress().toString());
List<Consensus<Deposit>> depositList = this.consensusCacheManager.getCachedDepositListByAddress(account.getAddress().toString());
for (Consensus<Deposit> cd : depositList) {
totalDeposit += cd.getExtend().getDeposit().getValue();
joinedAgent.add(cd.getExtend().getAgentHash());
}
if (null != agent) {
agentCount++;
totalDeposit += agent.getExtend().getDeposit().getValue();
}
reward += ledgerService.getAccountReward(account.getAddress().getBase58(), 0);
rewardOfDay += ledgerService.getAccountReward(account.getAddress().getBase58(), lastDayTime);
Balance balance = ledgerService.getBalance(account.getAddress().getBase58());
if (balance != null) {
usableBalance += balance.getUsable().getValue();
}
}
Map<String, Object> map = new HashMap<>();
map.put("agentCount", agentCount);
map.put("totalDeposit", totalDeposit);
map.put("joinAccountCount", joinedAgent.size());
map.put("usableBalance", usableBalance);
map.put("reward", reward);
map.put("rewardOfDay", rewardOfDay);
return map;
}
Aggregations