Search in sources :

Example 36 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class HeaderPackerValidator method validate.

@Override
public ValidateResult validate(BlockHeader header) {
    BlockHeader preHeader = null;
    try {
        preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getPreHash());
    } catch (NulsException e) {
    // todo
    }
    PocMeetingRound currentRound = consensusManager.getCurrentRound();
    if (header.getHeight() == 0) {
        return ValidateResult.getSuccessResult();
    }
    if (preHeader == null) {
        return ValidateResult.getSuccessResult();
    }
    BlockRoundData roundData = null;
    try {
        roundData = new BlockRoundData(preHeader.getExtend());
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(e.getMessage());
    }
    if (null == currentRound) {
        return ValidateResult.getSuccessResult();
    }
    if (currentRound.getIndex() == roundData.getRoundIndex() && currentRound.getMemberCount() > (roundData.getPackingIndexOfRound() + 1)) {
        if (currentRound.indexOf(header.getPackingAddress()) <= roundData.getPackingIndexOfRound()) {
            return ValidateResult.getFailedResult(ERROR_MESSAGE);
        }
    }
    byte[] packingHash160 = AccountTool.getHash160ByAddress(header.getPackingAddress());
    byte[] hash160InScriptSig = header.getScriptSig().getSignerHash160();
    if (!Arrays.equals(packingHash160, hash160InScriptSig)) {
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    BlockRoundData nowRoundData = null;
    try {
        nowRoundData = new BlockRoundData(header.getExtend());
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    if (!isAdjacent(roundData, nowRoundData)) {
        return ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return ValidateResult.getSuccessResult();
}
Also used : NulsException(io.nuls.core.exception.NulsException) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) BlockHeader(io.nuls.core.chain.entity.BlockHeader)

Example 37 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class TxSignValidator method validate.

@Override
public ValidateResult validate(Transaction tx) {
    byte[] scriptSig = tx.getScriptSig();
    tx.setScriptSig(null);
    NulsDigestData nulsDigestData;
    try {
        nulsDigestData = NulsDigestData.calcDigestData(tx.serialize());
    } catch (Exception e) {
        return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
    } finally {
        tx.setScriptSig(scriptSig);
    }
    if (!Arrays.equals(nulsDigestData.getDigestBytes(), tx.getHash().getDigestBytes())) {
        return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
    }
    P2PKHScriptSig p2PKHScriptSig = null;
    try {
        p2PKHScriptSig = new NulsByteBuffer(scriptSig).readNulsData(new P2PKHScriptSig());
    } catch (NulsException e) {
        return ValidateResult.getFailedResult(ErrorCode.SIGNATURE_ERROR);
    }
    return p2PKHScriptSig.verifySign(tx.getHash());
}
Also used : P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) NulsException(io.nuls.core.exception.NulsException) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsException(io.nuls.core.exception.NulsException) NulsByteBuffer(io.nuls.core.utils.io.NulsByteBuffer)

Example 38 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class PocConsensusResource method exitConsensus.

@POST
@Path("/withdraw")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult exitConsensus(WithdrawForm form) {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getTxHash());
    AssertUtil.canNotEmpty(form.getPassword());
    AssertUtil.canNotEmpty(form.getAddress());
    Map<String, Object> params = new HashMap<>();
    params.put("txHash", form.getTxHash());
    Transaction tx = null;
    try {
        tx = consensusService.stopConsensus(form.getAddress(), form.getPassword(), params);
    } catch (NulsException e) {
        Log.error(e);
        return RpcResult.getFailed(e.getMessage());
    } catch (IOException e) {
        Log.error(e);
        return RpcResult.getFailed(e.getMessage());
    }
    return RpcResult.getSuccess().setData(tx.getHash().getDigestHex());
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) HashMap(java.util.HashMap) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException)

Example 39 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class ConsensusMeetingRunner method coinBaseTx.

private void coinBaseTx(List<Transaction> txList, PocMeetingMember self) throws NulsException, IOException {
    CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_COIN_BASE));
    data.setFee(Na.ZERO);
    List<ConsensusReward> rewardList = calcReward(txList, self);
    Na total = Na.ZERO;
    for (ConsensusReward reward : rewardList) {
        Coin coin = new Coin();
        coin.setNa(reward.getReward());
        data.addTo(reward.getAddress(), coin);
        total = total.add(reward.getReward());
    }
    data.setTotalNa(total);
    CoinBaseTransaction tx;
    try {
        tx = new CoinBaseTransaction(data, null);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    tx.setFee(Na.ZERO);
    tx.setHash(NulsDigestData.calcDigestData(tx));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), consensusManager.getConsensusStatusInfo().getAccount(), NulsContext.CACHED_PASSWORD_OF_WALLET).serialize());
    ValidateResult validateResult = tx.verify();
    confirmingTxCacheManager.putTx(tx);
    if (null == validateResult || validateResult.isFailed()) {
        throw new NulsRuntimeException(ErrorCode.CONSENSUS_EXCEPTION);
    }
    try {
        ledgerService.approvalTx(tx);
    } catch (NulsException e) {
        throw new NulsRuntimeException(e);
    }
    txList.add(0, tx);
}
Also used : Coin(io.nuls.ledger.entity.params.Coin) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) Na(io.nuls.core.chain.entity.Na) ConsensusReward(io.nuls.consensus.entity.meeting.ConsensusReward) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 40 with NulsException

use of io.nuls.core.exception.NulsException in project nuls by nuls-io.

the class ConsensusMeetingRunner method yellowPunishTx.

private void yellowPunishTx(Block bestBlock, List<Transaction> txList, PocMeetingMember self) throws NulsException, IOException {
    BlockRoundData lastBlockRoundData = new BlockRoundData();
    try {
        lastBlockRoundData.parse(bestBlock.getHeader().getExtend());
    } catch (NulsException e) {
        Log.error(e);
    }
    // continuous blocks in the same round
    boolean ok = (self.getRoundIndex() == lastBlockRoundData.getRoundIndex()) && (self.getIndexOfRound() == (1 + lastBlockRoundData.getPackingIndexOfRound()));
    // continuous blocks between two rounds
    ok = ok || (self.getRoundIndex() == (lastBlockRoundData.getRoundIndex() + 1) && self.getIndexOfRound() == 1 && lastBlockRoundData.getPackingIndexOfRound() == lastBlockRoundData.getConsensusMemberCount());
    // two rounds
    ok = ok || (self.getRoundIndex() - 1) > lastBlockRoundData.getRoundIndex();
    if (ok) {
        return;
    }
    List<Address> addressList = new ArrayList<>();
    PocMeetingRound round = consensusManager.getCurrentRound();
    long roundIndex = lastBlockRoundData.getRoundIndex();
    int packingIndex = 0;
    if (lastBlockRoundData.getPackingIndexOfRound() == lastBlockRoundData.getConsensusMemberCount()) {
        packingIndex = 1;
    } else {
        packingIndex = lastBlockRoundData.getPackingIndexOfRound() + 1;
    }
    while (true) {
        PocMeetingRound tempRound;
        if (roundIndex == self.getRoundIndex()) {
            tempRound = round;
        } else if (roundIndex == (self.getRoundIndex() - 1)) {
            tempRound = round.getPreviousRound();
        } else {
            break;
        }
        if (tempRound.getIndex() > round.getIndex()) {
            break;
        }
        if (tempRound.getIndex() == round.getIndex() && packingIndex >= self.getIndexOfRound()) {
            break;
        }
        if (packingIndex >= tempRound.getMemberCount()) {
            roundIndex++;
            packingIndex = 1;
            continue;
        }
        PocMeetingMember member;
        try {
            member = tempRound.getMember(packingIndex);
            if (null == member) {
                break;
            }
        } catch (Exception e) {
            break;
        }
        packingIndex++;
        addressList.add(Address.fromHashs(member.getAgentAddress()));
    }
    if (addressList.isEmpty()) {
        return;
    }
    YellowPunishTransaction punishTx = new YellowPunishTransaction();
    YellowPunishData data = new YellowPunishData();
    data.setAddressList(addressList);
    data.setHeight(bestBlock.getHeader().getHeight() + 1);
    punishTx.setTxData(data);
    punishTx.setTime(TimeService.currentTimeMillis());
    punishTx.setFee(Na.ZERO);
    punishTx.setHash(NulsDigestData.calcDigestData(punishTx));
    punishTx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(punishTx.getHash(), consensusManager.getConsensusStatusInfo().getAccount(), NulsContext.CACHED_PASSWORD_OF_WALLET).serialize());
    txList.add(punishTx);
}
Also used : Address(io.nuls.account.entity.Address) YellowPunishTransaction(io.nuls.consensus.entity.tx.YellowPunishTransaction) YellowPunishData(io.nuls.consensus.entity.YellowPunishData) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) PocMeetingMember(io.nuls.consensus.entity.meeting.PocMeetingMember) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData)

Aggregations

NulsException (io.nuls.core.exception.NulsException)69 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)32 IOException (java.io.IOException)17 ValidateResult (io.nuls.core.validate.ValidateResult)12 Account (io.nuls.account.entity.Account)11 Transaction (io.nuls.core.chain.entity.Transaction)8 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)7 Block (io.nuls.core.chain.entity.Block)6 DbSession (io.nuls.db.transactional.annotation.DbSession)6 Coin (io.nuls.ledger.entity.params.Coin)6 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)6 Result (io.nuls.core.chain.entity.Result)5 TransactionEvent (io.nuls.ledger.event.TransactionEvent)5 BlockHeader (io.nuls.core.chain.entity.BlockHeader)4 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)4 Address (io.nuls.account.entity.Address)3 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)3 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)3 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)3 AccountPo (io.nuls.db.entity.AccountPo)3