Search in sources :

Example 21 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData in project nuls by nuls-io.

the class CacheLoader method loadBlocks.

/**
 * 从数据存储中加载指定个数的最新块
 * Loads the latest block of the specified number from the data store.
 *
 * @param size 加载数量/load count
 * @return 区块列表/block list
 */
public List<Block> loadBlocks(int size) {
    List<Block> blockList = new ArrayList<>();
    Block block = blockService.getBestBlock().getData();
    if (null == block) {
        return blockList;
    }
    for (int i = size; i >= 0; i--) {
        if (block == null) {
            break;
        }
        blockList.add(0, block);
        if (block.getHeader().getHeight() == 0L) {
            break;
        }
        NulsDigestData preHash = block.getHeader().getPreHash();
        block = blockService.getBlock(preHash).getData();
        if (block == null || block.getHeader().getHeight() == 0L) {
            break;
        }
    }
    return blockList;
}
Also used : ArrayList(java.util.ArrayList) Block(io.nuls.kernel.model.Block) NulsDigestData(io.nuls.kernel.model.NulsDigestData)

Example 22 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData in project nuls by nuls-io.

the class CacheLoader method loadBlockHeaders.

/**
 * @param size
 * @return
 */
public List<BlockHeader> loadBlockHeaders(int size) {
    List<BlockHeader> blockHeaderList = new ArrayList<>();
    BlockHeader blockHeader = blockService.getBestBlockHeader().getData();
    if (null == blockHeader) {
        return blockHeaderList;
    }
    BlockExtendsData roundData = new BlockExtendsData(blockHeader.getExtend());
    long breakRoundIndex = roundData.getRoundIndex() - size;
    while (true) {
        if (blockHeader == null) {
            break;
        }
        blockHeaderList.add(0, blockHeader);
        if (blockHeader.getHeight() == 0L) {
            break;
        }
        NulsDigestData preHash = blockHeader.getPreHash();
        blockHeader = blockService.getBlockHeader(preHash).getData();
        BlockExtendsData blockRoundData = new BlockExtendsData(blockHeader.getExtend());
        if (blockRoundData.getRoundIndex() <= breakRoundIndex) {
            break;
        }
    }
    return blockHeaderList;
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BlockHeader(io.nuls.kernel.model.BlockHeader)

Example 23 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData 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 24 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData in project nuls by nuls-io.

the class StopAgentTxValidator method validate.

@Override
public ValidateResult validate(StopAgentTransaction data) throws NulsException {
    AgentPo agentPo = agentStorageService.get(data.getTxData().getCreateTxHash());
    if (null == agentPo || agentPo.getDelHeight() > 0) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    TransactionSignature sig = new TransactionSignature();
    try {
        sig.parse(data.getTransactionSignature(), 0);
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
    }
    if (!SignatureUtil.containsAddress(data, agentPo.getAgentAddress())) {
        ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
        result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
        return result;
    }
    if (data.getCoinData().getTo() == null || data.getCoinData().getTo().isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    List<DepositPo> allDepositList = depositStorageService.getList();
    Map<NulsDigestData, DepositPo> depositMap = new HashMap<>();
    Na totalNa = agentPo.getDeposit();
    DepositPo ownDeposit = new DepositPo();
    ownDeposit.setDeposit(agentPo.getDeposit());
    ownDeposit.setAddress(agentPo.getAgentAddress());
    depositMap.put(data.getTxData().getCreateTxHash(), ownDeposit);
    for (DepositPo deposit : allDepositList) {
        if (deposit.getDelHeight() > -1L && (data.getBlockHeight() == -1L || deposit.getDelHeight() < data.getBlockHeight())) {
            continue;
        }
        if (!deposit.getAgentHash().equals(agentPo.getHash())) {
            continue;
        }
        depositMap.put(deposit.getTxHash(), deposit);
        totalNa = totalNa.add(deposit.getDeposit());
    }
    Na fromTotal = Na.ZERO;
    Map<String, Na> verifyToMap = new HashMap<>();
    for (Coin coin : data.getCoinData().getFrom()) {
        if (coin.getLockTime() != -1L) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        NulsDigestData txHash = new NulsDigestData();
        txHash.parse(coin.getOwner(), 0);
        DepositPo deposit = depositMap.remove(txHash);
        if (deposit == null) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        if (deposit.getAgentHash() == null && !coin.getNa().equals(agentPo.getDeposit())) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        } else if (!deposit.getDeposit().equals(coin.getNa())) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        fromTotal = fromTotal.add(coin.getNa());
        if (deposit.getAgentHash() == null) {
            continue;
        }
        String address = AddressTool.getStringAddressByBytes(deposit.getAddress());
        Na na = verifyToMap.get(address);
        if (null == na) {
            na = deposit.getDeposit();
        } else {
            na = na.add(deposit.getDeposit());
        }
        verifyToMap.put(address, na);
    }
    if (!depositMap.isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    if (!totalNa.equals(fromTotal)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Na ownToCoin = ownDeposit.getDeposit().subtract(data.getFee());
    long ownLockTime = 0L;
    for (Coin coin : data.getCoinData().getTo()) {
        // String address = AddressTool.getStringAddressByBytes(coin.());
        String address = AddressTool.getStringAddressByBytes(coin.getAddress());
        Na na = verifyToMap.get(address);
        if (null != na && na.equals(coin.getNa())) {
            verifyToMap.remove(address);
            continue;
        }
        if (ownToCoin != null && Arrays.equals(coin.getAddress(), ownDeposit.getAddress()) && coin.getNa().equals(ownToCoin)) {
            ownToCoin = null;
            ownLockTime = coin.getLockTime();
            continue;
        } else {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
    }
    if (ownLockTime < (data.getTime() + PocConsensusConstant.STOP_AGENT_LOCK_TIME)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
    } else if (data.getBlockHeight() <= 0 && ownLockTime < (TimeService.currentTimeMillis() + PocConsensusConstant.STOP_AGENT_LOCK_TIME - 300000L)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
    }
    if (!verifyToMap.isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : ValidateResult(io.nuls.kernel.validate.ValidateResult) TransactionSignature(io.nuls.kernel.script.TransactionSignature) Coin(io.nuls.kernel.model.Coin) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) Na(io.nuls.kernel.model.Na) NulsException(io.nuls.kernel.exception.NulsException) NulsDigestData(io.nuls.kernel.model.NulsDigestData) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 25 with NulsDigestData

use of io.nuls.kernel.model.NulsDigestData in project nuls by nuls-io.

the class StopAgentTxProcessor method conflictDetect.

@Override
public ValidateResult conflictDetect(List<Transaction> txList) {
    if (txList == null || txList.isEmpty()) {
        return ValidateResult.getSuccessResult();
    }
    Set<NulsDigestData> hashSet = new HashSet<>();
    Set<String> addressSet = new HashSet<>();
    for (Transaction tx : txList) {
        if (tx.getType() == ConsensusConstant.TX_TYPE_RED_PUNISH) {
            RedPunishTransaction transaction = (RedPunishTransaction) tx;
            addressSet.add(AddressTool.getStringAddressByBytes(transaction.getTxData().getAddress()));
        }
    }
    for (Transaction tx : txList) {
        if (tx.getType() == ConsensusConstant.TX_TYPE_STOP_AGENT) {
            StopAgentTransaction transaction = (StopAgentTransaction) tx;
            if (!hashSet.add(transaction.getTxData().getCreateTxHash())) {
                ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
                result.setData(transaction);
                return result;
            }
            if (transaction.getTxData().getAddress() == null) {
                CreateAgentTransaction agentTransaction = (CreateAgentTransaction) ledgerService.getTx(transaction.getTxData().getCreateTxHash());
                if (null == agentTransaction) {
                    ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
                    result.setData(transaction);
                    return result;
                }
                transaction.getTxData().setAddress(agentTransaction.getTxData().getAgentAddress());
            }
            AgentPo po = agentStorageService.get(transaction.getTxData().getCreateTxHash());
            if (null == po || po.getDelHeight() > 0) {
                ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
                result.setData(transaction);
                return result;
            }
            if (addressSet.contains(AddressTool.getStringAddressByBytes(transaction.getTxData().getAddress()))) {
                ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
                result.setData(transaction);
                return result;
            }
        }
    }
    return ValidateResult.getSuccessResult();
}
Also used : StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) Transaction(io.nuls.kernel.model.Transaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) ValidateResult(io.nuls.kernel.validate.ValidateResult) NulsDigestData(io.nuls.kernel.model.NulsDigestData) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) HashSet(java.util.HashSet) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Aggregations

NulsDigestData (io.nuls.kernel.model.NulsDigestData)54 ArrayList (java.util.ArrayList)16 Transaction (io.nuls.kernel.model.Transaction)12 Test (org.junit.Test)12 Block (io.nuls.kernel.model.Block)9 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)8 NulsException (io.nuls.kernel.exception.NulsException)8 BlockHeader (io.nuls.kernel.model.BlockHeader)7 IOException (java.io.IOException)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)5 Result (io.nuls.kernel.model.Result)5 BaseTest (io.nuls.consensus.poc.storage.BaseTest)4 HashSet (java.util.HashSet)4 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)3 MicroKernelBootstrap (io.nuls.kernel.MicroKernelBootstrap)3 BlockSignature (io.nuls.kernel.script.BlockSignature)3 ValidateResult (io.nuls.kernel.validate.ValidateResult)3 Node (io.nuls.network.model.Node)3 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)3 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)2