Search in sources :

Example 1 with LedgerService

use of io.nuls.ledger.service.LedgerService in project nuls by nuls-io.

the class CancelDepositTransaction method getInfo.

@Override
public String getInfo(byte[] address) {
    if (null != this.txData) {
        LedgerService ledgerService = NulsContext.getServiceBean(LedgerService.class);
        DepositTransaction tx = (DepositTransaction) ledgerService.getTx(this.txData.getJoinTxHash());
        if (null != tx) {
            return "unlock " + tx.getTxData().getDeposit().toCoinString();
        }
    }
    return "--";
}
Also used : LedgerService(io.nuls.ledger.service.LedgerService)

Example 2 with LedgerService

use of io.nuls.ledger.service.LedgerService in project nuls by nuls-io.

the class StopAgentTransaction method getInfo.

@Override
public String getInfo(byte[] address) {
    if (null != this.txData) {
        LedgerService ledgerService = NulsContext.getServiceBean(LedgerService.class);
        CreateAgentTransaction tx = (CreateAgentTransaction) ledgerService.getTx(this.txData.getCreateTxHash());
        if (null != tx) {
            return "unlock " + tx.getTxData().getDeposit().toCoinString();
        }
    }
    return "--";
}
Also used : LedgerService(io.nuls.ledger.service.LedgerService)

Example 3 with LedgerService

use of io.nuls.ledger.service.LedgerService in project nuls by nuls-io.

the class ChainContainer method verifyRedPunish.

private boolean verifyRedPunish(RedPunishTransaction data) {
    RedPunishData punishData = data.getTxData();
    if (ConsensusConfig.getSeedNodeStringList().contains(AddressTool.getStringAddressByBytes(punishData.getAddress()))) {
        Log.warn("The seed node can not be punished!");
        return false;
    }
    HeaderSignValidator validator = NulsContext.getServiceBean(HeaderSignValidator.class);
    LedgerService ledgerService = NulsContext.getServiceBean(LedgerService.class);
    if (punishData.getReasonCode() == PunishReasonEnum.DOUBLE_SPEND.getCode()) {
        if (NulsContext.MAIN_NET_VERSION <= 1) {
            Log.warn("The red punish tx need higher version!");
            return false;
        }
        SmallBlock smallBlock = new SmallBlock();
        try {
            smallBlock.parse(punishData.getEvidence(), 0);
        } catch (NulsException e) {
            Log.error(e);
            return false;
        }
        BlockHeader header = smallBlock.getHeader();
        if (header.getTime() != data.getTime()) {
            return false;
        }
        ValidateResult result = validator.validate(header);
        if (result.isFailed()) {
            Log.warn(result.getMsg());
            return false;
        }
        List<NulsDigestData> txHashList = smallBlock.getTxHashList();
        if (!header.getMerkleHash().equals(NulsDigestData.calcMerkleDigestData(txHashList))) {
            Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
            return false;
        }
        List<Transaction> txList = smallBlock.getSubTxList();
        if (null == txList || txList.size() < 2) {
            Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
            return false;
        }
        result = ledgerService.verifyDoubleSpend(txList);
        if (result.isSuccess()) {
            Log.warn(PocConsensusErrorCode.TRANSACTIONS_NEVER_DOUBLE_SPEND.getMsg());
            return false;
        }
    } else if (punishData.getReasonCode() == PunishReasonEnum.BIFURCATION.getCode()) {
        if (NulsContext.MAIN_NET_VERSION <= 1) {
            Log.warn("The red punish tx need higher version!");
            return false;
        }
        NulsByteBuffer byteBuffer = new NulsByteBuffer(punishData.getEvidence());
        // 轮次
        long[] roundIndex = new long[NulsContext.REDPUNISH_BIFURCATION];
        for (int i = 0; i < NulsContext.REDPUNISH_BIFURCATION && !byteBuffer.isFinished(); i++) {
            BlockHeader header1 = null;
            BlockHeader header2 = null;
            try {
                header1 = byteBuffer.readNulsData(new BlockHeader());
                header2 = byteBuffer.readNulsData(new BlockHeader());
            } catch (NulsException e) {
                Log.error(e);
            }
            if (null == header1 || null == header2) {
                Log.warn(KernelErrorCode.DATA_NOT_FOUND.getMsg());
                return false;
            }
            if (header1.getHeight() != header2.getHeight()) {
                Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
                return false;
            }
            if (i == NulsContext.REDPUNISH_BIFURCATION - 1 && (header1.getTime() + header2.getTime()) / 2 != data.getTime()) {
                return false;
            }
            ValidateResult result = validator.validate(header1);
            if (result.isFailed()) {
                Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
                return false;
            }
            result = validator.validate(header2);
            if (result.isFailed()) {
                Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
                return false;
            }
            if (!Arrays.equals(header1.getBlockSignature().getPublicKey(), header2.getBlockSignature().getPublicKey())) {
                Log.warn(TransactionErrorCode.SIGNATURE_ERROR.getMsg());
                return false;
            }
            BlockExtendsData blockExtendsData = new BlockExtendsData(header1.getExtend());
            roundIndex[i] = blockExtendsData.getRoundIndex();
        }
        // 验证轮次是否连续
        boolean rs = true;
        for (int i = 0; i < roundIndex.length; i++) {
            if (i < roundIndex.length - 2 && roundIndex[i + 1] - roundIndex[i] != 1) {
                rs = false;
                break;
            }
        }
        if (!rs) {
            Log.warn(PocConsensusErrorCode.RED_CARD_VERIFICATION_FAILED.getMsg());
            return false;
        }
    } else if (punishData.getReasonCode() != PunishReasonEnum.TOO_MUCH_YELLOW_PUNISH.getCode()) {
        Log.warn(PocConsensusErrorCode.RED_CARD_VERIFICATION_FAILED.getMsg());
        return false;
    }
    try {
        return verifyCoinData(data);
    } catch (IOException e) {
        Log.error(e);
        return false;
    }
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock) ValidateResult(io.nuls.kernel.validate.ValidateResult) IOException(java.io.IOException) LedgerService(io.nuls.ledger.service.LedgerService) HeaderSignValidator(io.nuls.protocol.model.validator.HeaderSignValidator) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) NulsException(io.nuls.kernel.exception.NulsException) NulsByteBuffer(io.nuls.kernel.utils.NulsByteBuffer)

Aggregations

LedgerService (io.nuls.ledger.service.LedgerService)3 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)1 NulsException (io.nuls.kernel.exception.NulsException)1 NulsByteBuffer (io.nuls.kernel.utils.NulsByteBuffer)1 ValidateResult (io.nuls.kernel.validate.ValidateResult)1 SmallBlock (io.nuls.protocol.model.SmallBlock)1 CoinBaseTransaction (io.nuls.protocol.model.tx.CoinBaseTransaction)1 HeaderSignValidator (io.nuls.protocol.model.validator.HeaderSignValidator)1 IOException (java.io.IOException)1