Search in sources :

Example 1 with DepositPo

use of io.nuls.db.entity.DepositPo in project nuls by nuls-io.

the class ExitConsensusTxService method onCommit.

@Override
public void onCommit(PocExitConsensusTransaction tx) throws NulsException {
    Transaction joinTx = ledgerService.getTx(tx.getTxData());
    if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
        RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
        manager.delAgent(raTx.getTxData().getHexHash());
        manager.delDepositByAgentHash(raTx.getTxData().getHexHash());
        this.ledgerService.unlockTxSave(tx.getTxData().getDigestHex());
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("agentHash", raTx.getTxData().getHexHash());
        List<DepositPo> poList = depositDataService.getList(paramsMap);
        for (DepositPo po : poList) {
            this.ledgerService.unlockTxSave(po.getTxHash());
        }
        this.agentDataService.delete(raTx.getTxData().getHexHash());
        this.depositDataService.deleteByAgentHash(raTx.getTxData().getHexHash());
        return;
    }
    PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
    Consensus<Deposit> cd = pjcTx.getTxData();
    manager.delDeposit(cd.getHexHash());
    this.depositDataService.delete(cd.getHexHash());
    this.ledgerService.unlockTxSave(tx.getTxData().getDigestHex());
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) 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) HashMap(java.util.HashMap) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)

Example 2 with DepositPo

use of io.nuls.db.entity.DepositPo in project nuls by nuls-io.

the class ConsensusTool method depositToPojo.

public static DepositPo depositToPojo(Consensus<Deposit> bean, String txHash) {
    if (null == bean) {
        return null;
    }
    DepositPo po = new DepositPo();
    po.setAddress(bean.getAddress());
    po.setDeposit(bean.getExtend().getDeposit().getValue());
    po.setTime(bean.getExtend().getStartTime());
    po.setAgentHash(bean.getExtend().getAgentHash());
    po.setId(bean.getHexHash());
    po.setTxHash(txHash);
    return po;
}
Also used : DepositPo(io.nuls.db.entity.DepositPo)

Example 3 with DepositPo

use of io.nuls.db.entity.DepositPo 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 4 with DepositPo

use of io.nuls.db.entity.DepositPo in project nuls by nuls-io.

the class JoinConsensusTxService method onCommit.

@Override
public void onCommit(PocJoinConsensusTransaction tx) throws NulsException {
    manager.changeDepositStatus(tx.getTxData().getHexHash(), ConsensusStatusEnum.WAITING);
    Consensus<Deposit> cd = tx.getTxData();
    cd.getExtend().setTxHash(tx.getHash().getDigestHex());
    cd.getExtend().setStatus(ConsensusStatusEnum.WAITING.getCode());
    DepositPo po = ConsensusTool.depositToPojo(cd, tx.getHash().getDigestHex());
    po.setBlockHeight(tx.getBlockHeight());
    po.setTime(tx.getTime());
    depositDataService.save(po);
    Map<String, Object> paramsMap = new HashMap<>();
    paramsMap.put("agentHash", cd.getExtend().getAgentHash());
    List<DepositPo> poList = depositDataService.getList(paramsMap);
    long sum = 0L;
    for (DepositPo depositPo : poList) {
        sum += depositPo.getDeposit();
    }
    if (sum >= PocConsensusConstant.SUM_OF_DEPOSIT_OF_AGENT_LOWER_LIMIT.getValue()) {
        manager.changeAgentStatusByHash(tx.getTxData().getExtend().getAgentHash(), ConsensusStatusEnum.IN);
        manager.changeDepositStatusByAgentHash(tx.getTxData().getExtend().getAgentHash(), ConsensusStatusEnum.IN);
        AgentPo daPo = this.accountDataService.get(cd.getExtend().getAgentHash());
        if (null == daPo) {
            throw new NulsRuntimeException(ErrorCode.DATA_ERROR, "the agent cannot find,agent hash:" + cd.getExtend().getAgentHash());
        }
        daPo.setStatus(ConsensusStatusEnum.IN.getCode());
        this.accountDataService.updateSelective(daPo);
    }
    EntrustConsensusNotice notice = new EntrustConsensusNotice();
    notice.setEventBody(tx);
    NulsContext.getServiceBean(EventBroadcaster.class).publishToLocal(notice);
}
Also used : EventBroadcaster(io.nuls.event.bus.service.intf.EventBroadcaster) Deposit(io.nuls.consensus.entity.member.Deposit) HashMap(java.util.HashMap) EntrustConsensusNotice(io.nuls.consensus.event.notice.EntrustConsensusNotice) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) DepositPo(io.nuls.db.entity.DepositPo) AgentPo(io.nuls.db.entity.AgentPo)

Example 5 with DepositPo

use of io.nuls.db.entity.DepositPo 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

DepositPo (io.nuls.db.entity.DepositPo)5 Deposit (io.nuls.consensus.entity.member.Deposit)4 AgentPo (io.nuls.db.entity.AgentPo)3 HashMap (java.util.HashMap)3 Agent (io.nuls.consensus.entity.member.Agent)2 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)2 PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)2 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)2 Transaction (io.nuls.core.chain.entity.Transaction)2 EventBroadcaster (io.nuls.event.bus.service.intf.EventBroadcaster)2 Account (io.nuls.account.entity.Account)1 Consensus (io.nuls.consensus.entity.Consensus)1 ConsensusStatusInfo (io.nuls.consensus.entity.ConsensusStatusInfo)1 CancelConsensusNotice (io.nuls.consensus.event.notice.CancelConsensusNotice)1 EntrustConsensusNotice (io.nuls.consensus.event.notice.EntrustConsensusNotice)1 StopConsensusNotice (io.nuls.consensus.event.notice.StopConsensusNotice)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1