Search in sources :

Example 1 with Deposit

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

the class CacheLoader method loadDepositList.

public List<Deposit> loadDepositList() {
    List<Deposit> depositList = new ArrayList<>();
    List<DepositPo> poList = depositStorageService.getList();
    for (DepositPo po : poList) {
        depositList.add(PoConvertUtil.poToDeposit(po));
    }
    Collections.sort(depositList, new DepositComparator());
    return depositList;
}
Also used : Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) DepositComparator(io.nuls.consensus.poc.protocol.util.DepositComparator) ArrayList(java.util.ArrayList)

Example 2 with Deposit

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

the class ChainContainer method addBlock.

public boolean addBlock(Block block) {
    if (!chain.getEndBlockHeader().getHash().equals(block.getHeader().getPreHash()) || chain.getEndBlockHeader().getHeight() + 1 != block.getHeader().getHeight()) {
        return false;
    }
    List<Agent> agentList = chain.getAgentList();
    List<Deposit> depositList = chain.getDepositList();
    List<PunishLogPo> yellowList = chain.getYellowPunishList();
    List<PunishLogPo> redList = chain.getRedPunishList();
    long height = block.getHeader().getHeight();
    BlockExtendsData extendsData = new BlockExtendsData(block.getHeader().getExtend());
    List<Transaction> txs = block.getTxs();
    for (Transaction tx : txs) {
        int txType = tx.getType();
        if (txType == ConsensusConstant.TX_TYPE_REGISTER_AGENT) {
            // Registered agent transaction
            // 注册代理交易
            CreateAgentTransaction registerAgentTx = (CreateAgentTransaction) tx;
            CreateAgentTransaction agentTx = registerAgentTx.clone();
            Agent agent = agentTx.getTxData();
            agent.setDelHeight(-1L);
            agent.setBlockHeight(height);
            agent.setTxHash(agentTx.getHash());
            agent.setTime(agentTx.getTime());
            agentList.add(agent);
        } else if (txType == ConsensusConstant.TX_TYPE_JOIN_CONSENSUS) {
            // 加入共识交易,设置该交易的高度和删除高度,然后加入列表
            DepositTransaction joinConsensusTx = (DepositTransaction) tx;
            DepositTransaction depositTx = joinConsensusTx.clone();
            Deposit deposit = depositTx.getTxData();
            deposit.setDelHeight(-1L);
            deposit.setBlockHeight(height);
            deposit.setTxHash(depositTx.getHash());
            deposit.setTime(depositTx.getTime());
            depositList.add(deposit);
        } else if (txType == ConsensusConstant.TX_TYPE_CANCEL_DEPOSIT) {
            CancelDepositTransaction cancelDepositTx = (CancelDepositTransaction) tx;
            NulsDigestData joinHash = cancelDepositTx.getTxData().getJoinTxHash();
            Iterator<Deposit> it = depositList.iterator();
            while (it.hasNext()) {
                Deposit deposit = it.next();
                cancelDepositTx.getTxData().setAddress(deposit.getAddress());
                if (deposit.getTxHash().equals(joinHash)) {
                    if (deposit.getDelHeight() == -1L) {
                        deposit.setDelHeight(height);
                    }
                    break;
                }
            }
        } else if (txType == ConsensusConstant.TX_TYPE_STOP_AGENT) {
            StopAgentTransaction stopAgentTx = (StopAgentTransaction) tx;
            NulsDigestData agentHash = stopAgentTx.getTxData().getCreateTxHash();
            Iterator<Deposit> it = depositList.iterator();
            while (it.hasNext()) {
                Deposit deposit = it.next();
                if (deposit.getAgentHash().equals(agentHash) && deposit.getDelHeight() == -1L) {
                    deposit.setDelHeight(height);
                }
            }
            Iterator<Agent> ita = agentList.iterator();
            while (ita.hasNext()) {
                Agent agent = ita.next();
                stopAgentTx.getTxData().setAddress(agent.getAgentAddress());
                if (agent.getTxHash().equals(agentHash)) {
                    if (agent.getDelHeight() == -1L) {
                        agent.setDelHeight(height);
                    }
                    break;
                }
            }
        } else if (txType == ConsensusConstant.TX_TYPE_RED_PUNISH) {
            RedPunishTransaction transaction = (RedPunishTransaction) tx;
            RedPunishData redPunishData = transaction.getTxData();
            PunishLogPo po = new PunishLogPo();
            po.setAddress(redPunishData.getAddress());
            po.setHeight(height);
            po.setRoundIndex(extendsData.getRoundIndex());
            po.setTime(tx.getTime());
            po.setType(PunishType.RED.getCode());
            redList.add(po);
            for (Agent agent : agentList) {
                if (!Arrays.equals(agent.getAgentAddress(), po.getAddress())) {
                    continue;
                }
                if (agent.getDelHeight() > 0) {
                    continue;
                }
                agent.setDelHeight(height);
                for (Deposit deposit : depositList) {
                    if (!deposit.getAgentHash().equals(agent.getTxHash())) {
                        continue;
                    }
                    if (deposit.getDelHeight() > 0) {
                        continue;
                    }
                    deposit.setDelHeight(height);
                }
            }
        } else if (txType == ConsensusConstant.TX_TYPE_YELLOW_PUNISH) {
            YellowPunishTransaction transaction = (YellowPunishTransaction) tx;
            for (byte[] bytes : transaction.getTxData().getAddressList()) {
                PunishLogPo po = new PunishLogPo();
                po.setAddress(bytes);
                po.setHeight(height);
                po.setRoundIndex(extendsData.getRoundIndex());
                po.setTime(tx.getTime());
                po.setType(PunishType.YELLOW.getCode());
                yellowList.add(po);
            }
        }
    }
    chain.addBlock(block);
    return true;
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 3 with Deposit

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

the class ChainContainer method rollback.

public boolean rollback(Block block) {
    int length = chain.getAllBlockList().size();
    if (block == null || length == 0) {
        return false;
    }
    Block bestBlock = chain.getBestBlock();
    if (!block.getHeader().getHash().equals(bestBlock.getHeader().getHash())) {
        Log.warn("rollbackTransaction block is not best block");
        return false;
    }
    if (length <= 2) {
        addBlockInBlockList(chain);
    }
    BlockHeader rollbackBlockHeader = chain.rollbackBlock();
    // update txs
    List<Agent> agentList = chain.getAgentList();
    List<Deposit> depositList = chain.getDepositList();
    List<PunishLogPo> yellowList = chain.getYellowPunishList();
    List<PunishLogPo> redPunishList = chain.getRedPunishList();
    long height = rollbackBlockHeader.getHeight();
    for (int i = agentList.size() - 1; i >= 0; i--) {
        Agent agent = agentList.get(i);
        if (agent.getDelHeight() == height) {
            agent.setDelHeight(-1L);
        }
        if (agent.getBlockHeight() == height) {
            agentList.remove(i);
        }
    }
    for (int i = depositList.size() - 1; i >= 0; i--) {
        Deposit deposit = depositList.get(i);
        if (deposit.getDelHeight() == height) {
            deposit.setDelHeight(-1L);
        }
        if (deposit.getBlockHeight() == height) {
            depositList.remove(i);
        }
    }
    for (int i = yellowList.size() - 1; i >= 0; i--) {
        PunishLogPo tempYellow = yellowList.get(i);
        if (tempYellow.getHeight() < height) {
            break;
        }
        if (tempYellow.getHeight() == height) {
            yellowList.remove(i);
        }
    }
    for (int i = redPunishList.size() - 1; i >= 0; i--) {
        PunishLogPo redPunish = redPunishList.get(i);
        if (redPunish.getHeight() < height) {
            break;
        }
        if (redPunish.getHeight() == height) {
            redPunishList.remove(i);
        }
    }
    // 判断是否需要重新计算轮次
    roundManager.checkIsNeedReset();
    return true;
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) SmallBlock(io.nuls.protocol.model.SmallBlock) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 4 with Deposit

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

the class RoundManager method setMemberList.

private void setMemberList(MeetingRound round, BlockHeader startBlockHeader) {
    List<MeetingMember> memberList = new ArrayList<>();
    double totalWeight = 0;
    for (byte[] address : ConsensusConfig.getSeedNodeList()) {
        MeetingMember member = new MeetingMember();
        member.setAgentAddress(address);
        member.setPackingAddress(address);
        member.setRewardAddress(address);
        member.setCreditVal(0);
        member.setRoundStartTime(round.getStartTime());
        memberList.add(member);
    }
    if (NulsContext.isNetFinished(NulsConfig.MODULES_CONFIG.getCfgValue(PocConsensusProtocolConstant.CFG_CONSENSUS_SECTION, PocConsensusProtocolConstant.STOP_DELAY, Integer.MAX_VALUE))) {
        round.init(memberList);
        return;
    }
    List<Deposit> depositTempList = new ArrayList<>();
    BlockExtendsData roundData = new BlockExtendsData(startBlockHeader.getExtend());
    long roundStart = roundData.getRoundIndex() - PocConsensusProtocolConstant.RANGE_OF_CAPACITY_COEFFICIENT;
    if (roundStart < 0) {
        roundStart = 0;
    }
    long roundEnd = roundData.getRoundIndex() - 1;
    Map<String, Integer> blockCountMap = getBlockCountMap(roundStart, roundEnd);
    Map<String, Integer> punishCountMap = getPunishCountMap(roundStart, roundEnd, PunishType.YELLOW.getCode());
    Map<NulsDigestData, List<Deposit>> agentDepositMap = getDepositListMap(startBlockHeader.getHeight());
    List<Agent> agentList = getAliveAgentList(startBlockHeader.getHeight());
    for (Agent agent : agentList) {
        List<Deposit> cdlist = agentDepositMap.get(agent.getTxHash());
        if (null == cdlist) {
            continue;
        }
        MeetingMember member = new MeetingMember();
        member.setAgent(agent);
        member.setAgentHash(agent.getTxHash());
        member.setAgentAddress(agent.getAgentAddress());
        member.setRewardAddress(agent.getRewardAddress());
        member.setPackingAddress(agent.getPackingAddress());
        member.setOwnDeposit(agent.getDeposit());
        member.setCommissionRate(agent.getCommissionRate());
        member.setRoundStartTime(round.getStartTime());
        member.setPackingAddressStr(agent.getPackingAddressStr());
        member.setAgentAddressStr(agent.getAgentAddressStr());
        for (Deposit dtx : cdlist) {
            member.setTotalDeposit(member.getTotalDeposit().add(dtx.getDeposit()));
            depositTempList.add(dtx);
        }
        member.setDepositList(cdlist);
        agent.setTotalDeposit(member.getTotalDeposit().getValue());
        boolean isItIn = member.getTotalDeposit().isGreaterOrEquals(PocConsensusProtocolConstant.SUM_OF_DEPOSIT_OF_AGENT_LOWER_LIMIT);
        if (isItIn) {
            member.setCreditVal(calcCreditVal(blockCountMap.get(member.getPackingAddressStr()), punishCountMap.get(member.getAgentAddressStr())));
            agent.setCreditVal(member.getRealCreditVal());
            totalWeight = DoubleUtils.sum(totalWeight, DoubleUtils.mul(agent.getDeposit().getValue(), member.getCalcCreditVal()));
            totalWeight = DoubleUtils.sum(totalWeight, DoubleUtils.mul(member.getTotalDeposit().getValue(), member.getCalcCreditVal()));
            memberList.add(member);
        }
    }
    Collections.sort(memberList);
    for (int i = 0; i < memberList.size(); i++) {
        MeetingMember member = memberList.get(i);
        member.setRoundIndex(round.getIndex());
        member.setPackingIndexOfRound(i + 1);
    }
    round.init(memberList);
    Collections.sort(depositTempList, new Comparator<Deposit>() {

        @Override
        public int compare(Deposit o1, Deposit o2) {
            return o1.getTxHash().getDigestHex().compareTo(o2.getTxHash().getDigestHex());
        }
    });
}
Also used : Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) Agent(io.nuls.consensus.poc.protocol.entity.Agent) NulsDigestData(io.nuls.kernel.model.NulsDigestData)

Example 5 with Deposit

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

the class ConsensusTool method getStopAgentCoinData.

public static CoinData getStopAgentCoinData(Agent agent, long lockTime, Long hight) throws IOException {
    if (null == agent) {
        return null;
    }
    NulsDigestData createTxHash = agent.getTxHash();
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    if (agent.getAgentAddress()[2] == NulsContext.P2SH_ADDRESS_TYPE) {
        Script scriptPubkey = SignatureUtil.createOutputScript(agent.getAgentAddress());
        toList.add(new Coin(scriptPubkey.getProgram(), agent.getDeposit(), lockTime));
    } else {
        toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), lockTime));
    }
    coinData.setTo(toList);
    CreateAgentTransaction transaction = (CreateAgentTransaction) ledgerService.getTx(createTxHash);
    if (null == transaction) {
        throw new NulsRuntimeException(TransactionErrorCode.TX_NOT_EXIST);
    }
    List<Coin> fromList = new ArrayList<>();
    for (int index = 0; index < transaction.getCoinData().getTo().size(); index++) {
        Coin coin = transaction.getCoinData().getTo().get(index);
        if (coin.getNa().equals(agent.getDeposit()) && coin.getLockTime() == -1L) {
            coin.setOwner(ArraysTool.concatenate(transaction.getHash().serialize(), new VarInt(index).encode()));
            fromList.add(coin);
            break;
        }
    }
    if (fromList.isEmpty()) {
        throw new NulsRuntimeException(KernelErrorCode.DATA_ERROR);
    }
    coinData.setFrom(fromList);
    List<Deposit> deposits = PocConsensusContext.getChainManager().getMasterChain().getChain().getDepositList();
    List<String> addressList = new ArrayList<>();
    Map<String, Coin> toMap = new HashMap<>();
    long blockHeight = null == hight ? -1 : hight;
    for (Deposit deposit : deposits) {
        if (deposit.getDelHeight() > 0 && (blockHeight <= 0 || deposit.getDelHeight() < blockHeight)) {
            continue;
        }
        if (!deposit.getAgentHash().equals(agent.getTxHash())) {
            continue;
        }
        DepositTransaction dtx = (DepositTransaction) ledgerService.getTx(deposit.getTxHash());
        Coin fromCoin = null;
        for (Coin coin : dtx.getCoinData().getTo()) {
            if (!coin.getNa().equals(deposit.getDeposit()) || coin.getLockTime() != -1L) {
                continue;
            }
            fromCoin = new Coin(ArraysTool.concatenate(dtx.getHash().serialize(), new VarInt(0).encode()), coin.getNa(), coin.getLockTime());
            fromCoin.setLockTime(-1L);
            fromList.add(fromCoin);
            break;
        }
        String address = AddressTool.getStringAddressByBytes(deposit.getAddress());
        Coin coin = toMap.get(address);
        if (null == coin) {
            if (deposit.getAddress()[2] == NulsContext.P2SH_ADDRESS_TYPE) {
                Script scriptPubkey = SignatureUtil.createOutputScript(deposit.getAddress());
                coin = new Coin(scriptPubkey.getProgram(), deposit.getDeposit(), 0);
            } else {
                coin = new Coin(deposit.getAddress(), deposit.getDeposit(), 0);
            }
            addressList.add(address);
            toMap.put(address, coin);
        } else {
            coin.setNa(coin.getNa().add(fromCoin.getNa()));
        }
    }
    for (String address : addressList) {
        coinData.getTo().add(toMap.get(address));
    }
    return coinData;
}
Also used : Script(io.nuls.kernel.script.Script) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) VarInt(io.nuls.kernel.utils.VarInt) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)

Aggregations

Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)27 Agent (io.nuls.consensus.poc.protocol.entity.Agent)12 CancelDeposit (io.nuls.consensus.poc.protocol.entity.CancelDeposit)10 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)9 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)8 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)6 MultiSigAccount (io.nuls.account.model.MultiSigAccount)4 MeetingMember (io.nuls.consensus.poc.model.MeetingMember)4 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)4 CancelDepositTransaction (io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)4 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)4 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)4 Account (io.nuls.account.model.Account)3 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)3 Page (io.nuls.core.tools.page.Page)3 NulsException (io.nuls.kernel.exception.NulsException)3 ValidateResult (io.nuls.kernel.validate.ValidateResult)3 ChainContainer (io.nuls.consensus.poc.container.ChainContainer)2 Chain (io.nuls.consensus.poc.model.Chain)2 NulsDigestData (io.nuls.kernel.model.NulsDigestData)2