Search in sources :

Example 16 with Transaction

use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.

the class BlockMaintenanceThread method checkGenesisBlock.

public void checkGenesisBlock() throws Exception {
    Block genesisBlock = NulsContext.getInstance().getGenesisBlock();
    ValidateResult result = genesisBlock.verify();
    if (result.isFailed()) {
        throw new NulsRuntimeException(ErrorCode.DATA_ERROR, result.getMessage());
    }
    Block localGenesisBlock = this.blockService.getGengsisBlock();
    if (null == localGenesisBlock) {
        for (Transaction tx : genesisBlock.getTxs()) {
            ledgerService.approvalTx(tx);
        }
        this.blockService.saveBlock(genesisBlock);
        return;
    }
    localGenesisBlock.verify();
    String logicHash = genesisBlock.getHeader().getHash().getDigestHex();
    String localHash = localGenesisBlock.getHeader().getHash().getDigestHex();
    if (!logicHash.equals(localHash)) {
        throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
    }
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) ValidateResult(io.nuls.core.validate.ValidateResult) Block(io.nuls.core.chain.entity.Block) BestCorrectBlock(io.nuls.consensus.entity.block.BestCorrectBlock) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 17 with Transaction

use of io.nuls.core.chain.entity.Transaction 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)

Example 18 with Transaction

use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.

the class ConsensusMeetingRunner method packing.

private void packing(PocMeetingMember self) throws NulsException, IOException {
    Block bestBlock = context.getBestBlock();
    List<Transaction> txList = txCacheManager.getTxList();
    txList.sort(TxTimeComparator.getInstance());
    BlockData bd = new BlockData();
    bd.setHeight(bestBlock.getHeader().getHeight() + 1);
    bd.setPreHash(bestBlock.getHeader().getHash());
    BlockRoundData roundData = new BlockRoundData();
    roundData.setRoundIndex(consensusManager.getCurrentRound().getIndex());
    roundData.setConsensusMemberCount(consensusManager.getCurrentRound().getMemberCount());
    roundData.setPackingIndexOfRound(self.getIndexOfRound());
    roundData.setRoundStartTime(consensusManager.getCurrentRound().getStartTime());
    bd.setRoundData(roundData);
    List<Integer> outTxList = new ArrayList<>();
    List<NulsDigestData> outHashList = new ArrayList<>();
    List<NulsDigestData> hashList = new ArrayList<>();
    long totalSize = 0L;
    for (int i = 0; i < txList.size(); i++) {
        if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
            break;
        }
        Transaction tx = txList.get(i);
        totalSize += tx.size();
        if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
            break;
        }
        outHashList.add(tx.getHash());
        ValidateResult result = tx.verify();
        if (result.isFailed()) {
            Log.error(result.getMessage());
            outTxList.add(i);
            continue;
        }
        try {
            ledgerService.approvalTx(tx);
        } catch (Exception e) {
            Log.error(e);
            outTxList.add(i);
            continue;
        }
        confirmingTxCacheManager.putTx(tx);
    }
    txCacheManager.removeTx(hashList);
    for (int i = outTxList.size() - 1; i >= 0; i--) {
        txList.remove(i);
    }
    txCacheManager.removeTx(outHashList);
    if (totalSize < PocConsensusConstant.MAX_BLOCK_SIZE) {
        addOrphanTx(txList, totalSize, self);
    }
    addConsensusTx(bestBlock, txList, self);
    bd.setTxList(txList);
    Log.info("txCount:" + txList.size());
    Block newBlock = ConsensusTool.createBlock(bd, consensusManager.getConsensusStatusInfo().getAccount());
    ValidateResult result = newBlock.verify();
    if (result.isFailed()) {
        Log.warn("packing block error:" + result.getMessage());
        for (Transaction tx : newBlock.getTxs()) {
            if (tx.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
                continue;
            }
            ledgerService.rollbackTx(tx);
        }
        return;
    }
    confirmingTxCacheManager.putTx(newBlock.getTxs().get(0));
    blockManager.addBlock(newBlock, false, null);
    BlockHeaderEvent event = new BlockHeaderEvent();
    event.setEventBody(newBlock.getHeader());
    eventBroadcaster.broadcastAndCache(event, false);
    PackedBlockNotice notice = new PackedBlockNotice();
    notice.setEventBody(newBlock.getHeader());
    eventBroadcaster.publishToLocal(notice);
}
Also used : ValidateResult(io.nuls.core.validate.ValidateResult) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) BlockHeaderEvent(io.nuls.consensus.event.BlockHeaderEvent) 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) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) Block(io.nuls.core.chain.entity.Block) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) PackedBlockNotice(io.nuls.consensus.event.notice.PackedBlockNotice) BlockData(io.nuls.consensus.entity.block.BlockData)

Example 19 with Transaction

use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.

the class BlockStorageService method txListGrouping.

private Map<Long, List<Transaction>> txListGrouping(List<Transaction> txList) {
    Map<Long, List<Transaction>> map = new HashMap<>();
    for (Transaction tx : txList) {
        List<Transaction> list = map.get(tx.getBlockHeight());
        if (null == list) {
            list = new ArrayList<>();
        }
        list.add(tx);
        map.put(tx.getBlockHeight(), list);
    }
    return map;
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 20 with Transaction

use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.

the class BlockStorageService method getBlock.

public Block getBlock(long height) throws Exception {
    Block block = blockCacheManager.getBlock(height);
    if (null != block && block.getTxs().size() == block.getHeader().getTxCount()) {
        return block;
    }
    BlockHeader header = getBlockHeader(height);
    if (null == header) {
        return null;
    }
    List<Transaction> txList = null;
    try {
        txList = ledgerService.getTxList(height);
    } catch (Exception e) {
        Log.error(e);
    }
    if (header.getTxCount() != txList.size()) {
        Log.warn("block has wrong tx size!");
    }
    return fillBlock(header, txList);
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) Block(io.nuls.core.chain.entity.Block) BlockHeader(io.nuls.core.chain.entity.BlockHeader) NulsException(io.nuls.core.exception.NulsException)

Aggregations

Transaction (io.nuls.core.chain.entity.Transaction)34 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)13 NulsException (io.nuls.core.exception.NulsException)12 Block (io.nuls.core.chain.entity.Block)8 HashMap (java.util.HashMap)7 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)6 ValidateResult (io.nuls.core.validate.ValidateResult)6 BlockHeader (io.nuls.core.chain.entity.BlockHeader)5 IOException (java.io.IOException)5 Deposit (io.nuls.consensus.entity.member.Deposit)4 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)4 PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)4 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)4 ArrayList (java.util.ArrayList)4 RedPunishTransaction (io.nuls.consensus.entity.tx.RedPunishTransaction)3 YellowPunishTransaction (io.nuls.consensus.entity.tx.YellowPunishTransaction)3 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)3 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)3 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)3 Account (io.nuls.account.entity.Account)2