Search in sources :

Example 11 with Na

use of io.nuls.kernel.model.Na in project nuls by nuls-io.

the class CreateP2shTransactionForm method getTodata.

public static List<MultipleTxToDto> getTodata(String todata) {
    List<MultipleTxToDto> toDatas = new ArrayList<>();
    String[] dataList = todata.split(";");
    long toAmount;
    for (String data : dataList) {
        // 将每个to数据拆分为数据
        MultipleTxToDto toData = new MultipleTxToDto();
        String[] separateData = data.split(",");
        Na toNa = Na.parseNuls(separateData[1]);
        toAmount = toNa.getValue();
        if (toAmount <= 0) {
            return null;
        }
        toData.setAmount(toAmount);
        toData.setToAddress(separateData[0]);
        toDatas.add(toData);
    }
    return toDatas;
}
Also used : Na(io.nuls.kernel.model.Na) ArrayList(java.util.ArrayList) MultipleTxToDto(io.nuls.accout.ledger.rpc.dto.MultipleTxToDto)

Example 12 with Na

use of io.nuls.kernel.model.Na in project nuls by nuls-io.

the class TransferProcessor method getTransferForm.

private TransferForm getTransferForm(String[] args) {
    TransferForm form = null;
    Long amount = null;
    try {
        Na na = Na.parseNuls(args[3]);
        if (na != null) {
            amount = na.getValue();
            form = new TransferForm();
        } else {
            return null;
        }
    } catch (Exception e) {
        return null;
    }
    switch(args.length) {
        case 4:
            form.setAddress(args[1]);
            form.setToAddress(args[2]);
            form.setAmount(amount);
            break;
        case 5:
            form.setAddress(args[1]);
            form.setToAddress(args[2]);
            form.setAmount(amount);
            form.setRemark(args[4]);
            break;
    }
    return form;
}
Also used : Na(io.nuls.kernel.model.Na) TransferForm(io.nuls.accout.ledger.rpc.form.TransferForm)

Example 13 with Na

use of io.nuls.kernel.model.Na in project nuls by nuls-io.

the class StopAgentTxValidator method validate.

@Override
public ValidateResult validate(StopAgentTransaction data) throws NulsException {
    AgentPo agentPo = agentStorageService.get(data.getTxData().getCreateTxHash());
    if (null == agentPo || agentPo.getDelHeight() > 0) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    TransactionSignature sig = new TransactionSignature();
    try {
        sig.parse(data.getTransactionSignature(), 0);
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
    }
    if (!SignatureUtil.containsAddress(data, agentPo.getAgentAddress())) {
        ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
        result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
        return result;
    }
    if (data.getCoinData().getTo() == null || data.getCoinData().getTo().isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    List<DepositPo> allDepositList = depositStorageService.getList();
    Map<NulsDigestData, DepositPo> depositMap = new HashMap<>();
    Na totalNa = agentPo.getDeposit();
    DepositPo ownDeposit = new DepositPo();
    ownDeposit.setDeposit(agentPo.getDeposit());
    ownDeposit.setAddress(agentPo.getAgentAddress());
    depositMap.put(data.getTxData().getCreateTxHash(), ownDeposit);
    for (DepositPo deposit : allDepositList) {
        if (deposit.getDelHeight() > -1L && (data.getBlockHeight() == -1L || deposit.getDelHeight() < data.getBlockHeight())) {
            continue;
        }
        if (!deposit.getAgentHash().equals(agentPo.getHash())) {
            continue;
        }
        depositMap.put(deposit.getTxHash(), deposit);
        totalNa = totalNa.add(deposit.getDeposit());
    }
    Na fromTotal = Na.ZERO;
    Map<String, Na> verifyToMap = new HashMap<>();
    for (Coin coin : data.getCoinData().getFrom()) {
        if (coin.getLockTime() != -1L) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        NulsDigestData txHash = new NulsDigestData();
        txHash.parse(coin.getOwner(), 0);
        DepositPo deposit = depositMap.remove(txHash);
        if (deposit == null) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        if (deposit.getAgentHash() == null && !coin.getNa().equals(agentPo.getDeposit())) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        } else if (!deposit.getDeposit().equals(coin.getNa())) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        fromTotal = fromTotal.add(coin.getNa());
        if (deposit.getAgentHash() == null) {
            continue;
        }
        String address = AddressTool.getStringAddressByBytes(deposit.getAddress());
        Na na = verifyToMap.get(address);
        if (null == na) {
            na = deposit.getDeposit();
        } else {
            na = na.add(deposit.getDeposit());
        }
        verifyToMap.put(address, na);
    }
    if (!depositMap.isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    if (!totalNa.equals(fromTotal)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Na ownToCoin = ownDeposit.getDeposit().subtract(data.getFee());
    long ownLockTime = 0L;
    for (Coin coin : data.getCoinData().getTo()) {
        // String address = AddressTool.getStringAddressByBytes(coin.());
        String address = AddressTool.getStringAddressByBytes(coin.getAddress());
        Na na = verifyToMap.get(address);
        if (null != na && na.equals(coin.getNa())) {
            verifyToMap.remove(address);
            continue;
        }
        if (ownToCoin != null && Arrays.equals(coin.getAddress(), ownDeposit.getAddress()) && coin.getNa().equals(ownToCoin)) {
            ownToCoin = null;
            ownLockTime = coin.getLockTime();
            continue;
        } else {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
    }
    if (ownLockTime < (data.getTime() + PocConsensusConstant.STOP_AGENT_LOCK_TIME)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
    } else if (data.getBlockHeight() <= 0 && ownLockTime < (TimeService.currentTimeMillis() + PocConsensusConstant.STOP_AGENT_LOCK_TIME - 300000L)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
    }
    if (!verifyToMap.isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : ValidateResult(io.nuls.kernel.validate.ValidateResult) TransactionSignature(io.nuls.kernel.script.TransactionSignature) Coin(io.nuls.kernel.model.Coin) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) Na(io.nuls.kernel.model.Na) NulsException(io.nuls.kernel.exception.NulsException) NulsDigestData(io.nuls.kernel.model.NulsDigestData) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 14 with Na

use of io.nuls.kernel.model.Na in project nuls by nuls-io.

the class DepositTxValidator method validate.

@Override
public ValidateResult validate(DepositTransaction tx) throws NulsException {
    if (null == tx || null == tx.getTxData() || null == tx.getTxData().getAgentHash() || null == tx.getTxData().getDeposit() || null == tx.getTxData().getAddress()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Deposit deposit = tx.getTxData();
    AgentPo agentPo = agentStorageService.get(deposit.getAgentHash());
    if (null == agentPo || agentPo.getDelHeight() > 0) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    List<DepositPo> poList = this.getDepositListByAgent(deposit.getAgentHash());
    if (null != poList && poList.size() >= PocConsensusProtocolConstant.MAX_ACCEPT_NUM_OF_DEPOSIT) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_OVER_COUNT);
    }
    Na limit = PocConsensusProtocolConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT;
    Na max = PocConsensusProtocolConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT;
    Na total = Na.ZERO;
    for (DepositPo cd : poList) {
        total = total.add(cd.getDeposit());
    }
    if (limit.isGreaterThan(deposit.getDeposit())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_NOT_ENOUGH);
    }
    if (max.isLessThan(total.add(deposit.getDeposit()))) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
    }
    if (!isDepositOk(deposit.getDeposit(), tx.getCoinData())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), SeverityLevelEnum.FLAGRANT_FOUL, PocConsensusErrorCode.DEPOSIT_ERROR);
    }
    TransactionSignature sig = new TransactionSignature();
    try {
        sig.parse(tx.getTransactionSignature(), 0);
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
    }
    if (!SignatureUtil.containsAddress(tx, deposit.getAddress())) {
        ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
        result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
        return result;
    }
    CoinData coinData = tx.getCoinData();
    Set<String> addressSet = new HashSet<>();
    int lockCount = 0;
    for (Coin coin : coinData.getTo()) {
        if (coin.getLockTime() == PocConsensusConstant.CONSENSUS_LOCK_TIME) {
            lockCount++;
        }
        // addressSet.add(AddressTool.getStringAddressByBytes(coin.()));
        addressSet.add(AddressTool.getStringAddressByBytes(coin.getAddress()));
    }
    if (lockCount > 1) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    if (addressSet.size() > 1) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) CoinData(io.nuls.kernel.model.CoinData) ValidateResult(io.nuls.kernel.validate.ValidateResult) TransactionSignature(io.nuls.kernel.script.TransactionSignature) Coin(io.nuls.kernel.model.Coin) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) Na(io.nuls.kernel.model.Na) NulsException(io.nuls.kernel.exception.NulsException) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 15 with Na

use of io.nuls.kernel.model.Na in project nuls by nuls-io.

the class CreateMultiAgentProcessor method execute.

@Override
public CommandResult execute(String[] args) {
    String signAddress = args[3];
    RpcClientResult res = CommandHelper.getPassword(signAddress, restFul);
    if (!res.isSuccess()) {
        return CommandResult.getFailed(res);
    }
    String password = (String) res.getData();
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("agentAddress", args[1]);
    parameters.put("packingAddress", args[2]);
    parameters.put("signAddress", args[3]);
    parameters.put("commissionRate", Double.valueOf(args[4]));
    Long deposit = null;
    try {
        Na na = Na.parseNuls(args[5]);
        deposit = na.getValue();
    } catch (Exception e) {
        return CommandResult.getFailed("Parameter deposit error");
    }
    parameters.put("deposit", deposit);
    parameters.put("password", password);
    if (args.length == 7) {
        parameters.put("rewardAddress", args[6]);
    }
    RpcClientResult result = restFul.post("/consensus/multiAccount/createMultiAgent", parameters);
    if (result.isFailed()) {
        return CommandResult.getFailed(result);
    }
    return CommandResult.getResult(CommandResult.dataMultiTransformValue(result));
}
Also used : Na(io.nuls.kernel.model.Na) HashMap(java.util.HashMap) RpcClientResult(io.nuls.kernel.model.RpcClientResult)

Aggregations

Na (io.nuls.kernel.model.Na)20 Coin (io.nuls.kernel.model.Coin)8 RewardItem (io.nuls.consensus.poc.model.RewardItem)3 NulsException (io.nuls.kernel.exception.NulsException)3 RpcClientResult (io.nuls.kernel.model.RpcClientResult)3 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)2 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)2 CoinData (io.nuls.kernel.model.CoinData)2 TransactionSignature (io.nuls.kernel.script.TransactionSignature)2 ValidateResult (io.nuls.kernel.validate.ValidateResult)2 CoinBaseTransaction (io.nuls.protocol.model.tx.CoinBaseTransaction)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)1 TransactionInfo (io.nuls.account.ledger.model.TransactionInfo)1 Balance (io.nuls.account.model.Balance)1 MultipleTxToDto (io.nuls.accout.ledger.rpc.dto.MultipleTxToDto)1 TransferForm (io.nuls.accout.ledger.rpc.form.TransferForm)1 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)1 CallContractData (io.nuls.contract.entity.txdata.CallContractData)1