Search in sources :

Example 6 with Agent

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();
    }
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) Agent(io.nuls.consensus.entity.member.Agent) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) PocMeetingMember(io.nuls.consensus.entity.meeting.PocMeetingMember) Consensus(io.nuls.consensus.entity.Consensus) Na(io.nuls.core.chain.entity.Na)

Example 7 with Agent

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;
}
Also used : Agent(io.nuls.consensus.entity.member.Agent) Consensus(io.nuls.consensus.entity.Consensus) ConsensusStatusInfo(io.nuls.consensus.entity.ConsensusStatusInfo)

Example 8 with Agent

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;
}
Also used : Account(io.nuls.account.entity.Account) Agent(io.nuls.consensus.entity.member.Agent) Deposit(io.nuls.consensus.entity.member.Deposit) Consensus(io.nuls.consensus.entity.Consensus) Balance(io.nuls.ledger.entity.Balance)

Example 9 with Agent

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;
}
Also used : Coin(io.nuls.ledger.entity.params.Coin) Agent(io.nuls.consensus.entity.member.Agent) TransactionEvent(io.nuls.ledger.event.TransactionEvent) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) ConsensusAgentImpl(io.nuls.consensus.entity.ConsensusAgentImpl) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 10 with Agent

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);
    }
}
Also used : Account(io.nuls.account.entity.Account) Agent(io.nuls.consensus.entity.member.Agent) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException) JoinConsensusParam(io.nuls.consensus.entity.params.JoinConsensusParam)

Aggregations

Agent (io.nuls.consensus.entity.member.Agent)14 Consensus (io.nuls.consensus.entity.Consensus)7 Deposit (io.nuls.consensus.entity.member.Deposit)6 Account (io.nuls.account.entity.Account)3 ConsensusAgentImpl (io.nuls.consensus.entity.ConsensusAgentImpl)3 AgentPo (io.nuls.db.entity.AgentPo)3 ConsensusStatusInfo (io.nuls.consensus.entity.ConsensusStatusInfo)2 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)2 Transaction (io.nuls.core.chain.entity.Transaction)2 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 ValidateResult (io.nuls.core.validate.ValidateResult)2 DepositPo (io.nuls.db.entity.DepositPo)2 EventBroadcaster (io.nuls.event.bus.service.intf.EventBroadcaster)2 Balance (io.nuls.ledger.entity.Balance)2 ConsensusReward (io.nuls.consensus.entity.meeting.ConsensusReward)1 PocMeetingMember (io.nuls.consensus.entity.meeting.PocMeetingMember)1 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)1 JoinConsensusParam (io.nuls.consensus.entity.params.JoinConsensusParam)1 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)1 PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)1