use of io.nuls.consensus.entity.member.Agent 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.member.Agent 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.member.Agent 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;
}
use of io.nuls.consensus.entity.member.Agent in project nuls by nuls-io.
the class PocConsensusServiceImpl method registerAgent.
private Transaction registerAgent(Agent agent, Account account, String password) throws IOException, NulsException {
TransactionEvent event = new TransactionEvent();
CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_REGISTER_AGENT));
data.setTotalNa(agent.getDeposit());
data.addFrom(account.getAddress().toString());
Coin coin = new Coin();
coin.setUnlockHeight(0);
coin.setUnlockTime(0);
coin.setNa(agent.getDeposit());
data.addTo(account.getAddress().toString(), coin);
RegisterAgentTransaction tx = null;
try {
tx = new RegisterAgentTransaction(data, password);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
Consensus<Agent> con = new ConsensusAgentImpl();
con.setAddress(account.getAddress().toString());
agent.setStartTime(TimeService.currentTimeMillis());
con.setExtend(agent);
tx.setTxData(con);
tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
tx.verifyWithException();
event.setEventBody(tx);
List<String> nodeList = eventBroadcaster.broadcastHashAndCache(event, true);
if (null == nodeList || nodeList.isEmpty()) {
throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
}
return tx;
}
use of io.nuls.consensus.entity.member.Agent in project nuls by nuls-io.
the class PocConsensusServiceImpl method startConsensus.
@Override
public Transaction startConsensus(String address, String password, Map<String, Object> paramsMap) throws NulsException {
Account account = this.accountService.getAccount(address);
if (null == account) {
throw new NulsRuntimeException(ErrorCode.FAILED, "The account is not exist,address:" + address);
}
if (paramsMap == null || paramsMap.size() < 2) {
throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
}
if (!account.validatePassword(password)) {
throw new NulsRuntimeException(ErrorCode.PASSWORD_IS_WRONG);
}
JoinConsensusParam params = new JoinConsensusParam(paramsMap);
if (StringUtils.isNotBlank(params.getIntroduction())) {
Agent agent = new Agent();
agent.setPackingAddress(params.getPackingAddress());
agent.setDeposit(Na.valueOf(params.getDeposit()));
agent.setIntroduction(params.getIntroduction());
agent.setSeed(params.isSeed());
agent.setCommissionRate(params.getCommissionRate());
agent.setAgentName(params.getAgentName());
try {
return this.registerAgent(agent, account, password);
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
}
try {
return this.joinTheConsensus(account, password, params.getDeposit(), params.getAgentHash());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
}
Aggregations