Search in sources :

Example 1 with PocJoinConsensusTransaction

use of io.nuls.consensus.entity.tx.PocJoinConsensusTransaction in project nuls by nuls-io.

the class PocConsensusServiceImpl method joinTheConsensus.

private Transaction joinTheConsensus(Account account, String password, long amount, String agentHash) throws IOException, NulsException {
    AssertUtil.canNotEmpty(account);
    AssertUtil.canNotEmpty(password);
    if (amount < PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT.getValue()) {
        throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
    }
    AssertUtil.canNotEmpty(agentHash);
    TransactionEvent event = new TransactionEvent();
    Consensus<Deposit> ca = new ConsensusDepositImpl();
    ca.setAddress(account.getAddress().toString());
    Deposit deposit = new Deposit();
    deposit.setAgentHash(agentHash);
    deposit.setDeposit(Na.valueOf(amount));
    deposit.setStartTime(TimeService.currentTimeMillis());
    ca.setExtend(deposit);
    CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_JOIN_CONSENSUS));
    data.setTotalNa(deposit.getDeposit());
    data.addFrom(account.getAddress().toString());
    Coin coin = new Coin();
    coin.setUnlockHeight(0);
    coin.setUnlockTime(0);
    coin.setNa(deposit.getDeposit());
    data.addTo(account.getAddress().toString(), coin);
    PocJoinConsensusTransaction tx = null;
    try {
        tx = new PocJoinConsensusTransaction(data, password);
    } catch (NulsException e) {
        throw new NulsRuntimeException(e);
    }
    tx.setTime(TimeService.currentTimeMillis());
    tx.setTxData(ca);
    tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
    tx.verifyWithException();
    event.setEventBody(tx);
    List<String> nodeList = eventBroadcaster.broadcastAndCache(event, true);
    if (null == nodeList || nodeList.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
    }
    return tx;
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) Coin(io.nuls.ledger.entity.params.Coin) TransactionEvent(io.nuls.ledger.event.TransactionEvent) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) ConsensusDepositImpl(io.nuls.consensus.entity.ConsensusDepositImpl)

Example 2 with PocJoinConsensusTransaction

use of io.nuls.consensus.entity.tx.PocJoinConsensusTransaction in project nuls by nuls-io.

the class ExitConsensusTxService method onApproval.

@Override
public void onApproval(PocExitConsensusTransaction tx) throws NulsException {
    Transaction joinTx = ledgerService.getTx(tx.getTxData());
    if (joinTx == null) {
        joinTx = ConfirmingTxCacheManager.getInstance().getTx(tx.getTxData());
    }
    if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
        RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
        manager.changeAgentStatusByHash(raTx.getTxData().getHexHash(), ConsensusStatusEnum.NOT_IN);
        manager.changeDepositStatusByAgentHash(raTx.getTxData().getHexHash(), ConsensusStatusEnum.NOT_IN);
        this.ledgerService.unlockTxApprove(tx.getTxData().getDigestHex());
        return;
    }
    PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
    Consensus<Deposit> cd = pjcTx.getTxData();
    manager.changeDepositStatus(cd.getHexHash(), ConsensusStatusEnum.NOT_IN);
    this.ledgerService.unlockTxApprove(tx.getTxData().getDigestHex());
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) 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)

Example 3 with PocJoinConsensusTransaction

use of io.nuls.consensus.entity.tx.PocJoinConsensusTransaction 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 4 with PocJoinConsensusTransaction

use of io.nuls.consensus.entity.tx.PocJoinConsensusTransaction in project nuls by nuls-io.

the class PocConsensusServiceImpl method stopConsensus.

@Override
public Transaction stopConsensus(String address, String password, Map<String, Object> paramsMap) throws NulsException, IOException {
    AbstractCoinTransaction joinTx = null;
    if (null != paramsMap && StringUtils.isNotBlank((String) paramsMap.get("txHash"))) {
        PocJoinConsensusTransaction tx = (PocJoinConsensusTransaction) ledgerService.getTx(NulsDigestData.fromDigestHex((String) paramsMap.get("txHash")));
        joinTx = tx;
    } else {
        try {
            List<Transaction> txlist = this.ledgerService.getTxList(address, TransactionConstant.TX_TYPE_REGISTER_AGENT);
            if (null != txlist || !txlist.isEmpty()) {
                joinTx = (AbstractCoinTransaction) txlist.get(0);
            }
        } catch (Exception e) {
            Log.error(e);
        }
    }
    if (null == joinTx) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "The related transaction is not exist!");
    }
    Account account = this.accountService.getAccount(address);
    if (null == account) {
        throw new NulsRuntimeException(ErrorCode.ACCOUNT_NOT_EXIST, "address:" + address.toString());
    }
    if (!account.validatePassword(password)) {
        throw new NulsRuntimeException(ErrorCode.PASSWORD_IS_WRONG);
    }
    TransactionEvent event = new TransactionEvent();
    CoinTransferData coinTransferData = new CoinTransferData(OperationType.UNLOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_EXIT_CONSENSUS));
    coinTransferData.setTotalNa(Na.ZERO);
    PocExitConsensusTransaction tx = new PocExitConsensusTransaction(coinTransferData, password);
    tx.setTxData(joinTx.getHash());
    try {
        tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(ErrorCode.HASH_ERROR, e);
    }
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
    event.setEventBody(tx);
    eventBroadcaster.broadcastHashAndCache(event, true);
    return tx;
}
Also used : Account(io.nuls.account.entity.Account) TransactionEvent(io.nuls.ledger.event.TransactionEvent) PocExitConsensusTransaction(io.nuls.consensus.entity.tx.PocExitConsensusTransaction) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) 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) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) IOException(java.io.IOException) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 5 with PocJoinConsensusTransaction

use of io.nuls.consensus.entity.tx.PocJoinConsensusTransaction 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

PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)5 Deposit (io.nuls.consensus.entity.member.Deposit)4 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)4 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)4 Transaction (io.nuls.core.chain.entity.Transaction)4 NulsException (io.nuls.core.exception.NulsException)2 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 DepositPo (io.nuls.db.entity.DepositPo)2 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)2 TransactionEvent (io.nuls.ledger.event.TransactionEvent)2 HashMap (java.util.HashMap)2 Account (io.nuls.account.entity.Account)1 ConsensusDepositImpl (io.nuls.consensus.entity.ConsensusDepositImpl)1 Agent (io.nuls.consensus.entity.member.Agent)1 CancelConsensusNotice (io.nuls.consensus.event.notice.CancelConsensusNotice)1 StopConsensusNotice (io.nuls.consensus.event.notice.StopConsensusNotice)1 AgentPo (io.nuls.db.entity.AgentPo)1 EventBroadcaster (io.nuls.event.bus.service.intf.EventBroadcaster)1 Coin (io.nuls.ledger.entity.params.Coin)1 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)1