Search in sources :

Example 11 with Agent

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

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

Example 13 with Agent

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

the class AgentCountValidator method validate.

@Override
public ValidateResult validate(RegisterAgentTransaction tx) {
    ValidateResult result = ValidateResult.getSuccessResult();
    Agent agent = tx.getTxData().getExtend();
    String agentName = agent.getAgentName();
    List<Consensus<Agent>> caList = consensusCacheManager.getCachedAgentList();
    if (caList != null) {
        for (Consensus<Agent> ca : caList) {
            if (ca.getHexHash().equals(tx.getTxData().getHexHash())) {
                continue;
            }
            if (ca.getAddress().equals(tx.getTxData().getAddress())) {
                return ValidateResult.getFailedResult("An address can only create one agent");
            }
            if (ca.getAddress().equals(agent.getPackingAddress())) {
                return ValidateResult.getFailedResult("The address can only create one agent");
            }
            if (agent.getPackingAddress().equals(ca.getAddress())) {
                return ValidateResult.getFailedResult("The packingAddress is an agentAddress");
            }
            if (agent.getPackingAddress().equals(ca.getExtend().getPackingAddress())) {
                return ValidateResult.getFailedResult("The packingAddress is busy!");
            }
            if (agentName.equals(ca.getExtend().getAgentName())) {
                return ValidateResult.getFailedResult("AgentName repetition!");
            }
        }
    }
    return result;
}
Also used : Agent(io.nuls.consensus.entity.member.Agent) ValidateResult(io.nuls.core.validate.ValidateResult) Consensus(io.nuls.consensus.entity.Consensus)

Example 14 with Agent

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

the class ConsensusCacheManager method init.

public void init() {
    Account self = accountService.getDefaultAccount();
    List<DepositPo> depositPoList = this.depositDao.getList();
    List<AgentPo> agentPoList = this.agentDao.getList();
    Consensus mine = null;
    for (AgentPo po : agentPoList) {
        Consensus<Agent> ca = ConsensusTool.fromPojo(po);
        this.cacheAgent(ca);
        if (null != self && ca.getAddress().equals(self.getAddress().toString())) {
            mine = ca;
        }
    }
    for (DepositPo dpo : depositPoList) {
        Consensus<Deposit> cd = ConsensusTool.fromPojo(dpo);
        this.cacheDeposit(cd);
        if (null != self && null == mine && cd.getAddress().equals(self.getAddress().toString())) {
            mine = cd;
        }
    }
    if (null == self) {
        return;
    }
    ConsensusStatusInfo info = new ConsensusStatusInfo();
    info.setAccount(self);
    if (null == mine) {
        info.setStatus(ConsensusStatusEnum.NOT_IN.getCode());
    } else if (mine.getExtend() instanceof Agent) {
        info.setSeed(((Agent) mine.getExtend()).getSeed());
        info.setStatus(((Agent) mine.getExtend()).getStatus());
    }
    this.updateConsensusStatusInfo(info);
}
Also used : Account(io.nuls.account.entity.Account) Agent(io.nuls.consensus.entity.member.Agent) Deposit(io.nuls.consensus.entity.member.Deposit) DepositPo(io.nuls.db.entity.DepositPo) Consensus(io.nuls.consensus.entity.Consensus) AgentPo(io.nuls.db.entity.AgentPo) ConsensusStatusInfo(io.nuls.consensus.entity.ConsensusStatusInfo)

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