Search in sources :

Example 1 with Na

use of io.nuls.core.chain.entity.Na 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 2 with Na

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

the class DepositAmountValidator method validate.

@Override
public ValidateResult validate(PocJoinConsensusTransaction data) {
    Na limit = PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT;
    Na max = PocConsensusConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT;
    List<Consensus<Deposit>> list = consensusCacheManager.getCachedDepositListByAgentHash(data.getTxData().getExtend().getAgentHash());
    if (list == null) {
        return ValidateResult.getSuccessResult();
    }
    Na total = Na.ZERO;
    for (Consensus<Deposit> cd : list) {
        total = total.add(cd.getExtend().getDeposit());
    }
    if (limit.isGreaterThan(data.getTxData().getExtend().getDeposit())) {
        return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_NOT_ENOUGH);
    }
    if (max.isLessThan(total)) {
        return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_TOO_MUCH);
    }
    if (!data.getTxData().getExtend().getDeposit().equals(data.getCoinData().getTotalNa())) {
        return ValidateResult.getFailedResult(SeverityLevelEnum.FLAGRANT_FOUL, ErrorCode.DEPOSIT_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) Na(io.nuls.core.chain.entity.Na) Consensus(io.nuls.consensus.entity.Consensus)

Example 3 with Na

use of io.nuls.core.chain.entity.Na 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 4 with Na

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

the class ConsensusMeetingRunner method nextRound.

private void nextRound() throws NulsException, IOException {
    consensusManager.initConsensusStatusInfo();
    PocMeetingRound currentRound = calcRound();
    consensusManager.setCurrentRound(currentRound);
    while (TimeService.currentTimeMillis() < (currentRound.getStartTime())) {
        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
            Log.error(e);
        }
    }
    boolean imIn = consensusManager.isPartakePacking();
    List<Consensus<Agent>> list = calcConsensusAgentList();
    currentRound.setMemberCount(list.size());
    while (currentRound.getEndTime() < TimeService.currentTimeMillis()) {
        long time = TimeService.currentTimeMillis() - currentRound.getStartTime();
        long roundTime = currentRound.getEndTime() - currentRound.getStartTime();
        long index = time / roundTime;
        long startTime = currentRound.getStartTime() + index * roundTime;
        currentRound.setStartTime(startTime);
        currentRound.setIndex(currentRound.getPreviousRound().getIndex() + index);
    }
    Map<String, List<Consensus<Deposit>>> depositMap = new HashMap<>();
    List<Consensus<Deposit>> delegateList = consensusCacheManager.getCachedDepositList();
    Na totalDeposit = Na.ZERO;
    for (Consensus<Deposit> cd : delegateList) {
        List<Consensus<Deposit>> sonList = depositMap.get(cd.getExtend().getAgentHash());
        if (null == sonList) {
            sonList = new ArrayList<>();
        }
        sonList.add(cd);
        depositMap.put(cd.getExtend().getAgentHash(), sonList);
        totalDeposit = totalDeposit.add(cd.getExtend().getDeposit());
    }
    List<PocMeetingMember> memberList = new ArrayList<>();
    for (Consensus<Agent> ca : list) {
        boolean isSeed = ca.getExtend().getSeed();
        if (!isSeed && ca.getExtend().getDeposit().isLessThan(PocConsensusConstant.AGENT_DEPOSIT_LOWER_LIMIT)) {
            continue;
        }
        PocMeetingMember mm = new PocMeetingMember();
        mm.setAgentConsensus(ca);
        mm.setDelegateList(depositMap.get(ca.getHexHash()));
        if (!isSeed && (mm.getDelegateList() == null || mm.getDelegateList().size() > PocConsensusConstant.MAX_ACCEPT_NUM_OF_DEPOSIT)) {
            continue;
        }
        mm.calcDeposit();
        if (!isSeed && mm.getTotolEntrustDeposit().isLessThan(PocConsensusConstant.SUM_OF_DEPOSIT_OF_AGENT_LOWER_LIMIT)) {
            continue;
        }
        mm.setRoundIndex(currentRound.getIndex());
        mm.setAgentHash(ca.getHexHash());
        mm.setAgentAddress(ca.getAddress());
        mm.setPackerAddress(ca.getExtend().getPackingAddress());
        mm.setRoundStartTime(currentRound.getStartTime());
        memberList.add(mm);
        totalDeposit = totalDeposit.add(ca.getExtend().getDeposit());
    }
    Collections.sort(memberList);
    currentRound.setMemberList(memberList);
    currentRound.setTotalDeposit(totalDeposit);
    if (imIn) {
        startMeeting();
    }
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) Agent(io.nuls.consensus.entity.member.Agent) PocMeetingRound(io.nuls.consensus.entity.meeting.PocMeetingRound) PocMeetingMember(io.nuls.consensus.entity.meeting.PocMeetingMember) Consensus(io.nuls.consensus.entity.Consensus) Na(io.nuls.core.chain.entity.Na)

Example 5 with Na

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

the class PocMeetingMember method calcDeposit.

public void calcDeposit() {
    Na totolEntrustDeposit = Na.ZERO;
    if (delegateList == null) {
        return;
    }
    for (Consensus<Deposit> dc : delegateList) {
        totolEntrustDeposit = totolEntrustDeposit.add(dc.getExtend().getDeposit());
    }
    this.totolEntrustDeposit = totolEntrustDeposit;
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) Na(io.nuls.core.chain.entity.Na)

Aggregations

Na (io.nuls.core.chain.entity.Na)7 Deposit (io.nuls.consensus.entity.member.Deposit)3 Consensus (io.nuls.consensus.entity.Consensus)2 NulsException (io.nuls.core.exception.NulsException)2 Coin (io.nuls.ledger.entity.params.Coin)2 Account (io.nuls.account.entity.Account)1 Address (io.nuls.account.entity.Address)1 ConsensusReward (io.nuls.consensus.entity.meeting.ConsensusReward)1 PocMeetingMember (io.nuls.consensus.entity.meeting.PocMeetingMember)1 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)1 Agent (io.nuls.consensus.entity.member.Agent)1 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1 P2PKHScript (io.nuls.core.script.P2PKHScript)1 ValidateResult (io.nuls.core.validate.ValidateResult)1 UtxoBalance (io.nuls.ledger.entity.UtxoBalance)1 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)1 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)1 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)1 LockNulsTransaction (io.nuls.ledger.entity.tx.LockNulsTransaction)1