Search in sources :

Example 6 with Deposit

use of io.nuls.consensus.entity.member.Deposit 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 Deposit

use of io.nuls.consensus.entity.member.Deposit in project nuls by nuls-io.

the class ConsensusTool method fromPojo.

public static Consensus<Deposit> fromPojo(DepositPo po) {
    if (null == po) {
        return null;
    }
    Consensus<Deposit> ca = new ConsensusDepositImpl();
    ca.setAddress(po.getAddress());
    Deposit deposit = new Deposit();
    deposit.setAgentHash(po.getAgentHash());
    deposit.setDeposit(Na.valueOf(po.getDeposit()));
    deposit.setStartTime(po.getTime());
    deposit.setTxHash(po.getTxHash());
    ca.setHash(NulsDigestData.fromDigestHex(po.getId()));
    ca.setExtend(deposit);
    return ca;
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) ConsensusDepositImpl(io.nuls.consensus.entity.ConsensusDepositImpl)

Example 8 with Deposit

use of io.nuls.consensus.entity.member.Deposit 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 Deposit

use of io.nuls.consensus.entity.member.Deposit in project nuls by nuls-io.

the class PocConsensusServiceImpl method getConsensusInfoByAddress.

private Map<String, Object> getConsensusInfoByAddress(String address) {
    if (!Address.validAddress(address)) {
        return null;
    }
    Consensus<Agent> agent = this.consensusCacheManager.getCachedAgentByAddress(address);
    List<Consensus<Deposit>> depositList = this.consensusCacheManager.getCachedDepositListByAddress(address);
    long totalDeposit = 0;
    Set<String> joinedAgent = new HashSet<>();
    for (Consensus<Deposit> cd : depositList) {
        totalDeposit += cd.getExtend().getDeposit().getValue();
        joinedAgent.add(cd.getExtend().getAgentHash());
    }
    Map<String, Object> map = new HashMap<>();
    if (null != agent) {
        map.put("agentCount", 1);
    // totalDeposit += agent.getExtend().getDeposit().getValue();
    } else {
        map.put("agentCount", 0);
    }
    map.put("totalDeposit", totalDeposit);
    long lastDayTime = TimeService.currentTimeMillis() - DateUtil.DATE_TIME;
    long reward = ledgerService.getAccountReward(address, 0);
    long rewardOfDay = ledgerService.getAccountReward(address, lastDayTime);
    Balance balance = ledgerService.getBalance(address);
    map.put("reward", reward);
    map.put("joinAccountCount", joinedAgent.size());
    if (null == balance || balance.getUsable() == null) {
        map.put("usableBalance", 0);
    } else {
        map.put("usableBalance", balance.getUsable().getValue());
    }
    map.put("rewardOfDay", rewardOfDay);
    return map;
}
Also used : 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 10 with Deposit

use of io.nuls.consensus.entity.member.Deposit in project nuls by nuls-io.

the class ExitConsensusTxService method onRollback.

@Override
public void onRollback(PocExitConsensusTransaction tx) throws NulsException {
    Transaction joinTx = ledgerService.getTx(tx.getTxData());
    if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
        RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
        Consensus<Agent> ca = raTx.getTxData();
        ca.getExtend().setStatus(ConsensusStatusEnum.IN.getCode());
        manager.cacheAgent(ca);
        AgentPo agentPo = new AgentPo();
        agentPo.setId(raTx.getTxData().getHexHash());
        agentPo.setStatus(ConsensusStatusEnum.IN.getCode());
        this.agentDataService.updateSelective(agentPo);
        DepositPo dpo = new DepositPo();
        dpo.setId(raTx.getTxData().getHexHash());
        dpo.setStatus(ConsensusStatusEnum.IN.getCode());
        this.depositDataService.updateSelectiveByAgentHash(dpo);
        CancelConsensusNotice notice = new CancelConsensusNotice();
        notice.setEventBody(tx);
        NulsContext.getServiceBean(EventBroadcaster.class).publishToLocal(notice);
        // cache delegates
        Map<String, Object> params = new HashMap<>();
        params.put("agentHash", raTx.getTxData().getHexHash());
        List<DepositPo> polist = this.depositDataService.getList(params);
        if (null == polist || polist.isEmpty()) {
            return;
        }
        for (DepositPo po : polist) {
            Consensus<Deposit> cd = ConsensusTool.fromPojo(po);
            this.manager.cacheDeposit(cd);
        }
        this.ledgerService.unlockTxRollback(tx.getTxData().getDigestHex());
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("agentHash", ca.getHexHash());
        List<DepositPo> poList = depositDataService.getList(paramsMap);
        for (DepositPo po : poList) {
            this.ledgerService.unlockTxRollback(po.getTxHash());
        }
        return;
    }
    PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
    Consensus<Deposit> cd = pjcTx.getTxData();
    cd.getExtend().setStatus(ConsensusStatusEnum.IN.getCode());
    manager.cacheDeposit(cd);
    DepositPo dPo = this.depositDataService.get(cd.getHexHash());
    if (dPo == null) {
        dPo = ConsensusTool.depositToPojo(cd, tx.getHash().getDigestHex());
        this.depositDataService.save(dPo);
    }
    StopConsensusNotice notice = new StopConsensusNotice();
    notice.setEventBody(tx);
    NulsContext.getServiceBean(EventBroadcaster.class).publishToLocal(notice);
    this.ledgerService.unlockTxRollback(tx.getTxData().getDigestHex());
}
Also used : EventBroadcaster(io.nuls.event.bus.service.intf.EventBroadcaster) Agent(io.nuls.consensus.entity.member.Agent) Deposit(io.nuls.consensus.entity.member.Deposit) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) HashMap(java.util.HashMap) StopConsensusNotice(io.nuls.consensus.event.notice.StopConsensusNotice) CancelConsensusNotice(io.nuls.consensus.event.notice.CancelConsensusNotice) DepositPo(io.nuls.db.entity.DepositPo) PocExitConsensusTransaction(io.nuls.consensus.entity.tx.PocExitConsensusTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) AgentPo(io.nuls.db.entity.AgentPo)

Aggregations

Deposit (io.nuls.consensus.entity.member.Deposit)13 Agent (io.nuls.consensus.entity.member.Agent)6 Consensus (io.nuls.consensus.entity.Consensus)5 PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)4 Transaction (io.nuls.core.chain.entity.Transaction)4 DepositPo (io.nuls.db.entity.DepositPo)4 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)3 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)3 Na (io.nuls.core.chain.entity.Na)3 AgentPo (io.nuls.db.entity.AgentPo)3 HashMap (java.util.HashMap)3 Account (io.nuls.account.entity.Account)2 ConsensusDepositImpl (io.nuls.consensus.entity.ConsensusDepositImpl)2 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 EventBroadcaster (io.nuls.event.bus.service.intf.EventBroadcaster)2 Balance (io.nuls.ledger.entity.Balance)2 ConsensusStatusInfo (io.nuls.consensus.entity.ConsensusStatusInfo)1 ConsensusReward (io.nuls.consensus.entity.meeting.ConsensusReward)1 PocMeetingMember (io.nuls.consensus.entity.meeting.PocMeetingMember)1 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)1