Search in sources :

Example 1 with Deposit

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

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

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

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

the class DepositAmountValidator method validate.

@Override
public ValidateResult validate(PocJoinConsensusTransaction data) {
    Na limit = PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT;
    Na max = PocConsensusConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT;
    List<Consensus<Deposit>> list = consensusCacheManager.getCachedDepositListByAgentHash(data.getTxData().getExtend().getAgentHash());
    if (list == null) {
        return ValidateResult.getSuccessResult();
    }
    Na total = Na.ZERO;
    for (Consensus<Deposit> cd : list) {
        total = total.add(cd.getExtend().getDeposit());
    }
    if (limit.isGreaterThan(data.getTxData().getExtend().getDeposit())) {
        return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_NOT_ENOUGH);
    }
    if (max.isLessThan(total)) {
        return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_TOO_MUCH);
    }
    if (!data.getTxData().getExtend().getDeposit().equals(data.getCoinData().getTotalNa())) {
        return ValidateResult.getFailedResult(SeverityLevelEnum.FLAGRANT_FOUL, ErrorCode.DEPOSIT_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) Na(io.nuls.core.chain.entity.Na) Consensus(io.nuls.consensus.entity.Consensus)

Example 5 with Deposit

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

the class ConsensusMeetingRunner method calcReward.

private List<ConsensusReward> calcReward(List<Transaction> txList, PocMeetingMember self) {
    List<ConsensusReward> rewardList = new ArrayList<>();
    Consensus<Agent> ca = self.getAgentConsensus();
    if (ca.getExtend().getSeed()) {
        long totalFee = 0;
        for (Transaction tx : txList) {
            totalFee += tx.getFee().getValue();
        }
        if (totalFee == 0L) {
            return rewardList;
        }
        double caReward = totalFee;
        ConsensusReward agentReword = new ConsensusReward();
        agentReword.setAddress(ca.getAddress());
        agentReword.setReward(Na.valueOf((long) caReward));
        rewardList.add(agentReword);
        return rewardList;
    }
    long totalFee = 0;
    for (Transaction tx : txList) {
        totalFee += tx.getFee().getValue();
    }
    double total = totalFee + DoubleUtils.mul(this.consensusManager.getCurrentRound().getMemberCount(), PocConsensusConstant.BLOCK_REWARD.getValue());
    ConsensusReward agentReword = new ConsensusReward();
    agentReword.setAddress(ca.getAddress());
    double caReward = DoubleUtils.mul(total, DoubleUtils.div(ca.getExtend().getDeposit().getValue(), this.consensusManager.getCurrentRound().getTotalDeposit().getValue()));
    caReward = caReward + DoubleUtils.mul(total, DoubleUtils.mul(DoubleUtils.div((this.consensusManager.getCurrentRound().getTotalDeposit().getValue() - ca.getExtend().getDeposit().getValue()), this.consensusManager.getCurrentRound().getTotalDeposit().getValue()), DoubleUtils.round(ca.getExtend().getCommissionRate() / 100, 2)));
    agentReword.setReward(Na.valueOf((long) caReward));
    Map<String, ConsensusReward> rewardMap = new HashMap<>();
    rewardMap.put(ca.getAddress(), agentReword);
    double delegateCommissionRate = DoubleUtils.div((100 - ca.getExtend().getCommissionRate()), 100, 2);
    for (Consensus<Deposit> cd : self.getDelegateList()) {
        double reward = DoubleUtils.mul(DoubleUtils.mul(total, delegateCommissionRate), DoubleUtils.div(cd.getExtend().getDeposit().getValue(), this.consensusManager.getCurrentRound().getTotalDeposit().getValue()));
        ConsensusReward delegateReword = rewardMap.get(cd.getAddress());
        if (null == delegateReword) {
            delegateReword = new ConsensusReward();
            delegateReword.setReward(Na.ZERO);
        }
        delegateReword.setAddress(cd.getAddress());
        delegateReword.setReward(delegateReword.getReward().add(Na.valueOf((long) reward)));
        rewardMap.put(cd.getAddress(), delegateReword);
    }
    rewardList.addAll(rewardMap.values());
    return rewardList;
}
Also used : Agent(io.nuls.consensus.entity.member.Agent) Deposit(io.nuls.consensus.entity.member.Deposit) ConsensusReward(io.nuls.consensus.entity.meeting.ConsensusReward) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RedPunishTransaction(io.nuls.consensus.entity.tx.RedPunishTransaction) YellowPunishTransaction(io.nuls.consensus.entity.tx.YellowPunishTransaction)

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