Search in sources :

Example 51 with NulsException

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

the class GenesisBlock method initGengsisTxs.

private void initGengsisTxs(Map<String, Object> jsonMap) {
    List<Map<String, Object>> list = (List<Map<String, Object>>) jsonMap.get(CONFIG_FILED_TXS);
    if (null == list || list.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.CONFIG_ERROR);
    }
    CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, Na.ZERO);
    data.setPriKey(Hex.decode(priKey));
    Na total = Na.ZERO;
    for (Map<String, Object> map : list) {
        String address = (String) map.get(CONFIG_FILED_ADDRESS);
        AssertUtil.canNotEmpty(address, ErrorCode.NULL_PARAMETER);
        Integer nuls = (Integer) map.get(CONFIG_FILED_NULS);
        AssertUtil.canNotEmpty(nuls, ErrorCode.NULL_PARAMETER);
        Integer height = (Integer) map.get(CONFIG_FILED_UNLOCK_HEIGHT);
        Coin coin = new Coin();
        coin.setNa(Na.parseNuls(nuls));
        coin.setUnlockTime(0);
        if (height == null) {
            coin.setUnlockTime(0);
        } else {
            coin.setUnlockHeight(height.longValue());
        }
        data.addTo(address, coin);
        total = total.add(coin.getNa());
    }
    data.setTotalNa(total);
    CoinBaseTransaction tx = null;
    try {
        tx = new CoinBaseTransaction(data, null);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    tx.setTime(this.blockTime);
    tx.setFee(Na.ZERO);
    try {
        tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    P2PKHScriptSig p2PKHScriptSig = new P2PKHScriptSig();
    Account account = null;
    try {
        account = AccountTool.createAccount(priKey);
    } catch (NulsException e) {
        e.printStackTrace();
    }
    AccountService accountService = NulsContext.getServiceBean(AccountService.class);
    P2PKHScriptSig scriptSig = null;
    try {
        scriptSig = accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, "");
    } catch (NulsException e) {
        e.printStackTrace();
    }
    try {
        tx.setScriptSig(scriptSig.serialize());
    } catch (IOException e) {
        e.printStackTrace();
    }
    List<Transaction> txlist = new ArrayList<>();
    // tx.setStatus(TxStatusEnum.AGREED);
    txlist.add(tx);
    setTxs(txlist);
}
Also used : Account(io.nuls.account.entity.Account) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException) BigInteger(java.math.BigInteger) Coin(io.nuls.ledger.entity.params.Coin) P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) NulsException(io.nuls.core.exception.NulsException) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) AccountService(io.nuls.account.service.intf.AccountService)

Example 52 with NulsException

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

the class UtxoTxInputsValidator method validate.

@Override
public ValidateResult validate(AbstractCoinTransaction tx) {
    UtxoData data = (UtxoData) tx.getCoinData();
    for (int i = 0; i < data.getInputs().size(); i++) {
        UtxoInput input = data.getInputs().get(i);
        UtxoOutput output = input.getFrom();
        if (output == null && tx.getStatus() == TxStatusEnum.CACHED) {
            return ValidateResult.getFailedResult(ErrorCode.ORPHAN_TX);
        }
        if (tx.getStatus() == TxStatusEnum.CACHED) {
            if (!output.isUsable()) {
                return ValidateResult.getFailedResult(ErrorCode.UTXO_STATUS_CHANGE);
            }
        } else if (tx.getStatus() == TxStatusEnum.AGREED) {
            if (!output.isSpend()) {
                return ValidateResult.getFailedResult(ErrorCode.UTXO_STATUS_CHANGE);
            }
        }
        byte[] owner = output.getOwner();
        P2PKHScriptSig p2PKHScriptSig = null;
        try {
            p2PKHScriptSig = P2PKHScriptSig.createFromBytes(tx.getScriptSig());
        } catch (NulsException e) {
            return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
        }
        byte[] user = p2PKHScriptSig.getSignerHash160();
        if (!Arrays.equals(owner, user)) {
            return ValidateResult.getFailedResult(ErrorCode.INVALID_OUTPUT);
        }
        return ValidateResult.getSuccessResult();
    }
    return ValidateResult.getSuccessResult();
}
Also used : P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) NulsException(io.nuls.core.exception.NulsException) UtxoData(io.nuls.ledger.entity.UtxoData) UtxoInput(io.nuls.ledger.entity.UtxoInput) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Example 53 with NulsException

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

the class ConnectionManager method receiveMessage.

public void receiveMessage(ByteBuffer buffer, Node node) {
    List<NulsMessage> list;
    try {
        buffer.flip();
        if (!node.isAlive()) {
            buffer.clear();
            return;
        }
        list = new ArrayList<>();
        while (buffer.hasRemaining()) {
            NulsMessage message = new NulsMessage(buffer);
            list.add(message);
        }
        for (NulsMessage message : list) {
            if (MessageFilterChain.getInstance().doFilter(message)) {
                BaseEvent event = EventManager.getInstance(message.getData());
                MsgLog.info("get(" + node.getId() + "):\n" + Hex.encode(message.getHeader().serialize()) + "--" + Hex.encode(message.getData()));
                processMessage(event, node);
            }
        }
    } catch (NulsException e) {
        // todo
        Log.error(e);
    } catch (Exception e) {
        // todo
        Log.error(e);
        return;
    } finally {
        buffer.clear();
    }
}
Also used : NulsMessage(io.nuls.core.mesasge.NulsMessage) NulsException(io.nuls.core.exception.NulsException) BaseEvent(io.nuls.core.event.BaseEvent) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException)

Example 54 with NulsException

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

the class UtxoLedgerServiceImpl method transfer.

private Result transfer(CoinTransferData coinData, String password, String remark) {
    TransferTransaction tx = null;
    try {
        tx = UtxoTransactionTool.getInstance().createTransferTx(coinData, password, remark);
        ValidateResult result = tx.verify();
        if (result.isFailed()) {
            throw new NulsException(result.getErrorCode());
        }
        byte[] txbytes = tx.serialize();
        TransferTransaction new_tx = new NulsByteBuffer(txbytes).readNulsData(new TransferTransaction());
        result = new_tx.verify();
        if (result.isFailed()) {
            throw new NulsException(result.getErrorCode());
        }
        TransactionEvent event = new TransactionEvent();
        event.setEventBody(tx);
        eventBroadcaster.broadcastAndCacheAysn(event, true);
    } catch (Exception e) {
        Log.error(e);
        return new Result(false, e.getMessage());
    }
    return new Result(true, "OK", tx.getHash().getDigestHex());
}
Also used : TransactionEvent(io.nuls.ledger.event.TransactionEvent) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) TransferTransaction(io.nuls.ledger.entity.tx.TransferTransaction) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsByteBuffer(io.nuls.core.utils.io.NulsByteBuffer) ValidateResult(io.nuls.core.validate.ValidateResult)

Example 55 with NulsException

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

the class NulsByteBuffer method readUint32LE.

public long readUint32LE() throws NulsException {
    try {
        long u = Utils.readUint32LE(payload, cursor);
        cursor += 4;
        return u;
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new NulsException(ErrorCode.DATA_PARSE_ERROR, e);
    }
}
Also used : NulsException(io.nuls.core.exception.NulsException)

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