Search in sources :

Example 16 with Na

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

the class CreateAgentProcessor method execute.

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

Example 17 with Na

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

the class TxFeeValidator method validate.

@Override
public ValidateResult validate(Transaction tx) {
    int txType = tx.getType();
    if (tx.isSystemTx()) {
        return ValidateResult.getSuccessResult();
    }
    CoinData coinData = tx.getCoinData();
    if (null == coinData) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.COINDATA_NOT_FOUND);
    }
    Na realFee = tx.getFee();
    Na fee = null;
    if (txType == ProtocolConstant.TX_TYPE_TRANSFER || txType == ProtocolConstant.TX_TYPE_DATA || txType == ContractConstant.TX_TYPE_CREATE_CONTRACT || txType == ContractConstant.TX_TYPE_CALL_CONTRACT || txType == ContractConstant.TX_TYPE_DELETE_CONTRACT) {
        fee = TransactionFeeCalculator.getTransferFee(tx.size());
    } else {
        fee = TransactionFeeCalculator.getMaxFee(tx.size());
    }
    if (realFee.isGreaterOrEquals(fee)) {
        return ValidateResult.getSuccessResult();
    }
    return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.FEE_NOT_RIGHT);
}
Also used : Na(io.nuls.kernel.model.Na) CoinData(io.nuls.kernel.model.CoinData)

Example 18 with Na

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

the class BalanceManager method calBalanceByAddress.

/**
 * 计算账户的余额,这个方法应该和获取余额方法互斥,避免并发导致数据不准确
 */
public Balance calBalanceByAddress(byte[] address) throws NulsException {
    lock.lock();
    try {
        if (accountService.getAccount(address).isFailed()) {
            return null;
        }
        List<Coin> coinList = getCoinListByAddress(address);
        Collections.sort(coinList, CoinComparator.getInstance());
        BalanceCacheEntity balanceCacheEntity = new BalanceCacheEntity();
        Na usable = Na.ZERO;
        Na locked = Na.ZERO;
        for (Coin coin : coinList) {
            if (coin.usable()) {
                usable = usable.add(coin.getNa());
            } else {
                locked = locked.add(coin.getNa());
                long lockTime = coin.getLockTime();
                // the consensus lock type
                if (lockTime <= 0L) {
                    continue;
                }
                // the height lock type
                if (balanceCacheEntity.getLowestLockHeigh() == 0L || (lockTime < NulsConstant.BlOCKHEIGHT_TIME_DIVIDE && lockTime < balanceCacheEntity.getLowestLockHeigh())) {
                    balanceCacheEntity.setLowestLockHeigh(lockTime);
                    continue;
                }
                // the time lock type
                if (balanceCacheEntity.getEarlistLockTime() == 0L || (lockTime > NulsConstant.BlOCKHEIGHT_TIME_DIVIDE && lockTime < balanceCacheEntity.getEarlistLockTime())) {
                    balanceCacheEntity.setEarlistLockTime(lockTime);
                    continue;
                }
            }
        }
        Balance balance = new Balance();
        balance.setUsable(usable);
        balance.setLocked(locked);
        balance.setBalance(usable.add(locked));
        balanceCacheEntity.setBalance(balance);
        balanceMap.put(AddressTool.getStringAddressByBytes(address), balanceCacheEntity);
        return balance;
    } finally {
        lock.unlock();
    }
}
Also used : Coin(io.nuls.kernel.model.Coin) Na(io.nuls.kernel.model.Na) Balance(io.nuls.account.model.Balance)

Example 19 with Na

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

the class DeleteContractTxValidator method validate.

@Override
public ValidateResult validate(DeleteContractTransaction tx) throws NulsException {
    DeleteContractData txData = tx.getTxData();
    byte[] sender = txData.getSender();
    byte[] contractAddressBytes = txData.getContractAddress();
    Set<String> addressSet = SignatureUtil.getAddressFromTX(tx);
    if (!addressSet.contains(AddressTool.getStringAddressByBytes(sender))) {
        Log.error("contract delete error: The contract deleter is not the transaction creator.");
        return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Result<ContractAddressInfoPo> contractAddressInfoPoResult = contractAddressStorageService.getContractAddressInfo(contractAddressBytes);
    if (contractAddressInfoPoResult.isFailed()) {
        return ValidateResult.getFailedResult(this.getClass().getSimpleName(), contractAddressInfoPoResult.getErrorCode());
    }
    ContractAddressInfoPo contractAddressInfoPo = contractAddressInfoPoResult.getData();
    if (contractAddressInfoPo == null) {
        Log.error("contract delete error: The contract does not exist.");
        return ValidateResult.getFailedResult(this.getClass().getSimpleName(), ContractErrorCode.CONTRACT_ADDRESS_NOT_EXIST);
    }
    if (!ArraysTool.arrayEquals(sender, contractAddressInfoPo.getSender())) {
        Log.error("contract delete error: The contract deleter is not the contract creator.");
        return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Result<ContractBalance> result = contractUtxoService.getBalance(contractAddressBytes);
    ContractBalance balance = (ContractBalance) result.getData();
    if (balance == null) {
        Log.error("contract delete error: That balance of the contract is abnormal.");
        return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Na totalBalance = balance.getBalance();
    if (totalBalance.compareTo(Na.ZERO) != 0) {
        Log.error("contract delete error: The balance of the contract is not 0.");
        return ValidateResult.getFailedResult(this.getClass().getSimpleName(), ContractErrorCode.CONTRACT_DELETE_BALANCE);
    }
    return ValidateResult.getSuccessResult();
}
Also used : ContractAddressInfoPo(io.nuls.contract.storage.po.ContractAddressInfoPo) Na(io.nuls.kernel.model.Na) ContractBalance(io.nuls.contract.ledger.module.ContractBalance) DeleteContractData(io.nuls.contract.entity.txdata.DeleteContractData)

Example 20 with Na

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

the class PocRewardCacheService method calcRewardHistory.

private void calcRewardHistory(String address, List<TransactionInfo> list, long startHeight) {
    byte[] addressByte = AddressTool.getAddress(address);
    for (TransactionInfo info : list) {
        if (info.getTxType() != ProtocolConstant.TX_TYPE_COINBASE) {
            continue;
        }
        CoinBaseTransaction tx = (CoinBaseTransaction) ledgerService.getTx(info.getTxHash());
        if (null == tx) {
            continue;
        }
        if (info.getBlockHeight() >= startHeight) {
            continue;
        }
        if (null == tx.getCoinData().getTo() || tx.getCoinData().getTo().isEmpty()) {
            continue;
        }
        for (Coin coin : tx.getCoinData().getTo()) {
            // if (!Arrays.equals(addressByte, coin.()))
            if (!Arrays.equals(addressByte, coin.getAddress())) {
                continue;
            }
            Na na = totalMap.get(address);
            if (na == null) {
                na = Na.ZERO;
            }
            na = na.add(coin.getNa());
            totalMap.put(address, na);
        }
    }
}
Also used : Coin(io.nuls.kernel.model.Coin) Na(io.nuls.kernel.model.Na) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) TransactionInfo(io.nuls.account.ledger.model.TransactionInfo)

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