Search in sources :

Example 1 with NulsDigestData

use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.

the class UtxoInput method parse.

@Override
public void parse(NulsByteBuffer byteBuffer) throws NulsException {
    index = (int) byteBuffer.readVarInt();
    fromHash = byteBuffer.readNulsData(new NulsDigestData());
    fromIndex = (int) byteBuffer.readVarInt();
    LedgerCacheService ledgerCacheService = LedgerCacheService.getInstance();
    UtxoOutput output = ledgerCacheService.getUtxo(this.getKey());
    if (output == null) {
        UtxoOutputDataService utxoOutputDataService = NulsContext.getServiceBean(UtxoOutputDataService.class);
        Map<String, Object> map = new HashMap<>();
        map.put("txHash", this.fromHash.getDigestHex());
        map.put("outIndex", this.fromIndex);
        UtxoOutputPo outputPo = utxoOutputDataService.get(map);
        if (outputPo != null) {
            output = UtxoTransferTool.toOutput(outputPo);
        }
    }
    from = output;
}
Also used : LedgerCacheService(io.nuls.ledger.service.impl.LedgerCacheService) HashMap(java.util.HashMap) UtxoOutputDataService(io.nuls.db.dao.UtxoOutputDataService) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo)

Example 2 with NulsDigestData

use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.

the class UtxoCoinDataProvider method createByTransferData.

@Override
public CoinData createByTransferData(Transaction tx, CoinTransferData coinParam, String password) throws NulsException {
    UtxoData utxoData = new UtxoData();
    List<UtxoInput> inputs = new ArrayList<>();
    List<UtxoOutput> outputs = new ArrayList<>();
    if (coinParam.getTotalNa().equals(Na.ZERO)) {
        utxoData.setInputs(inputs);
        utxoData.setOutputs(outputs);
        return utxoData;
    }
    long inputValue = 0;
    if (!coinParam.getFrom().isEmpty()) {
        // find unSpends to create inputs for this tx
        Na totalFee = Na.ZERO;
        if (tx instanceof UnlockNulsTransaction) {
            totalFee = coinParam.getFee();
        } else {
            totalFee = coinParam.getTotalNa().add(coinParam.getFee());
        }
        List<UtxoOutput> unSpends = coinManager.getAccountsUnSpend(coinParam.getFrom(), totalFee);
        if (unSpends.isEmpty()) {
            throw new NulsException(ErrorCode.BALANCE_NOT_ENOUGH);
        }
        for (int i = 0; i < unSpends.size(); i++) {
            UtxoOutput output = unSpends.get(i);
            UtxoInput input = new UtxoInput();
            input.setFrom(output);
            input.setFromHash(output.getTxHash());
            input.setFromIndex(output.getIndex());
            input.setTxHash(tx.getHash());
            input.setIndex(i);
            inputValue += output.getValue();
            inputs.add(input);
        }
    }
    // get EcKey for output's script
    Account account = null;
    byte[] priKey = null;
    if (coinParam.getPriKey() != null) {
        priKey = coinParam.getPriKey();
    } else if (!coinParam.getFrom().isEmpty()) {
        account = accountService.getAccount(coinParam.getFrom().get(0));
        if (account == null) {
            throw new NulsException(ErrorCode.ACCOUNT_NOT_EXIST);
        }
        if (account.isEncrypted() && account.isLocked()) {
            if (!account.unlock(password)) {
                throw new NulsException(ErrorCode.PASSWORD_IS_WRONG);
            }
            priKey = account.getPriKey();
            account.lock();
        } else {
            priKey = account.getPriKey();
        }
    }
    // create outputs
    int i = 0;
    long outputValue = 0;
    for (Map.Entry<String, List<Coin>> entry : coinParam.getToMap().entrySet()) {
        String address = entry.getKey();
        List<Coin> coinList = entry.getValue();
        for (Coin coin : coinList) {
            UtxoOutput output = new UtxoOutput();
            output.setAddress(address);
            output.setValue(coin.getNa().getValue());
            if (output.getLockTime() > 0) {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_TIME_LOCK);
            } else if (tx instanceof LockNulsTransaction) {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_CONSENSUS_LOCK);
            } else {
                output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND);
            }
            output.setIndex(i);
            P2PKHScript p2PKHScript = new P2PKHScript(new NulsDigestData(NulsDigestData.DIGEST_ALG_SHA160, new Address(address).getHash160()));
            output.setP2PKHScript(p2PKHScript);
            if (coin.getUnlockHeight() > 0) {
                output.setLockTime(coin.getUnlockHeight());
            } else if (coin.getUnlockTime() > 0) {
                output.setLockTime(coin.getUnlockTime());
            } else {
                output.setLockTime(0L);
            }
            output.setTxHash(tx.getHash());
            outputValue += output.getValue();
            outputs.add(output);
            i++;
        }
    }
    // the balance leave to myself
    long balance = inputValue - outputValue - coinParam.getFee().getValue();
    if (balance > 0) {
        UtxoOutput output = new UtxoOutput();
        // todo script
        output.setAddress(inputs.get(0).getFrom().getAddress());
        output.setValue(balance);
        output.setIndex(i);
        output.setTxHash(tx.getHash());
        output.setStatus(OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND);
        P2PKHScript p2PKHScript = new P2PKHScript(new NulsDigestData(NulsDigestData.DIGEST_ALG_SHA160, account.getHash160()));
        output.setP2PKHScript(p2PKHScript);
        outputs.add(output);
    }
    utxoData.setInputs(inputs);
    utxoData.setOutputs(outputs);
    return utxoData;
}
Also used : P2PKHScript(io.nuls.core.script.P2PKHScript) Account(io.nuls.account.entity.Account) Address(io.nuls.account.entity.Address) Coin(io.nuls.ledger.entity.params.Coin) LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) Na(io.nuls.core.chain.entity.Na) NulsException(io.nuls.core.exception.NulsException) UnlockNulsTransaction(io.nuls.ledger.entity.tx.UnlockNulsTransaction) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 3 with NulsDigestData

use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.

the class ConsensusMeetingRunner method addOrphanTx.

private void addOrphanTx(List<Transaction> txList, long totalSize, PocMeetingMember self) {
    if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
        return;
    }
    List<Transaction> orphanTxList = orphanTxCacheManager.getTxList();
    if (null == orphanTxList || orphanTxList.isEmpty()) {
        return;
    }
    txList.sort(TxTimeComparator.getInstance());
    List<NulsDigestData> outHashList = new ArrayList<>();
    for (Transaction tx : orphanTxList) {
        if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
            break;
        }
        totalSize += tx.size();
        if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
            break;
        }
        ValidateResult result = tx.verify();
        if (result.isFailed()) {
            Log.error(result.getMessage());
            continue;
        }
        try {
            ledgerService.approvalTx(tx);
        } catch (Exception e) {
            Log.error(result.getMessage());
            Log.error(e);
            continue;
        }
        confirmingTxCacheManager.putTx(tx);
        txList.add(tx);
        outHashList.add(tx.getHash());
    }
    orphanTxCacheManager.removeTx(outHashList);
}
Also used : CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RedPunishTransaction(io.nuls.consensus.entity.tx.RedPunishTransaction) YellowPunishTransaction(io.nuls.consensus.entity.tx.YellowPunishTransaction) ValidateResult(io.nuls.core.validate.ValidateResult) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 4 with NulsDigestData

use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.

the class DistributedBlockInfoRequestUtils method calc.

private void calc() {
    if (null == nodeIdList || nodeIdList.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "success list of nodes is empty!");
    }
    int size = nodeIdList.size();
    int halfSize = (size + 1) / 2;
    // 
    if (hashesMap.size() < halfSize) {
        return;
    }
    BlockInfo result = null;
    for (String key : calcMap.keySet()) {
        List<String> nodes = calcMap.get(key);
        if (nodes == null) {
            continue;
        }
        // todo =
        if (nodes.size() >= halfSize) {
            result = new BlockInfo();
            BlockHashResponse response = hashesMap.get(nodes.get(0));
            if (response == null || response.getHeightList() == null) {
                // todo check it
                continue;
            }
            Long bestHeight = 0L;
            NulsDigestData bestHash = null;
            for (int i = 0; i < response.getHeightList().size(); i++) {
                Long height = response.getHeightList().get(i);
                NulsDigestData hash = response.getHashList().get(i);
                if (height >= bestHeight) {
                    bestHash = hash;
                    bestHeight = height;
                }
                result.putHash(height, hash);
            }
            result.setBestHash(bestHash);
            result.setBestHeight(bestHeight);
            result.setNodeIdList(nodes);
            result.setFinished(true);
            break;
        }
    }
    if (null != result) {
        bestBlockInfo = result;
    }
// else if (size == calcMap.size()) {
// try {
// Thread.sleep(2000L);
// } catch (InterruptedException e) {
// Log.error(e);
// }
// try {
// this.request(start, end, split);
// } catch (Exception e) {
// Log.error(e.getMessage());
// }
// }
}
Also used : BlockHashResponse(io.nuls.consensus.entity.BlockHashResponse) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 5 with NulsDigestData

use of io.nuls.core.chain.entity.NulsDigestData in project nuls by nuls-io.

the class GetTxGroupParam method serializeToStream.

@Override
protected void serializeToStream(NulsOutputStreamBuffer stream) throws IOException {
    stream.writeInt48(time);
    stream.writeNulsData(blockHash);
    stream.writeVarInt(txHashList.size());
    for (NulsDigestData data : txHashList) {
        stream.writeNulsData(data);
    }
}
Also used : NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Aggregations

NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)22 Transaction (io.nuls.core.chain.entity.Transaction)6 NulsException (io.nuls.core.exception.NulsException)5 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)5 ValidateResult (io.nuls.core.validate.ValidateResult)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 BlockHashResponse (io.nuls.consensus.entity.BlockHashResponse)3 Block (io.nuls.core.chain.entity.Block)3 NulsByteBuffer (io.nuls.core.utils.io.NulsByteBuffer)3 LockNulsTransaction (io.nuls.ledger.entity.tx.LockNulsTransaction)3 RedPunishTransaction (io.nuls.consensus.entity.tx.RedPunishTransaction)2 YellowPunishTransaction (io.nuls.consensus.entity.tx.YellowPunishTransaction)2 BlockHeader (io.nuls.core.chain.entity.BlockHeader)2 P2PKHScript (io.nuls.core.script.P2PKHScript)2 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)2 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)2 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)2 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)2 HashMap (java.util.HashMap)2