Search in sources :

Example 76 with NulsException

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

the class NulsByteBuffer method readUint32LE.

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

Example 77 with NulsException

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

the class Block method parse.

@Override
public void parse(NulsByteBuffer byteBuffer) throws NulsException {
    header = new BlockHeader();
    header.parse(byteBuffer);
    try {
        txs = TransactionManager.getInstances(byteBuffer, header.getTxCount());
    } catch (Exception e) {
        throw new NulsRuntimeException(KernelErrorCode.DESERIALIZE_ERROR);
    }
    for (Transaction tx : txs) {
        tx.setBlockHeight(header.getHeight());
    }
}
Also used : NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsException(io.nuls.kernel.exception.NulsException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException)

Example 78 with NulsException

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

the class UtxoAccountsServiceImpl method validateIntegrityBootstrap.

@Override
public boolean validateIntegrityBootstrap(long hadSynBlockHeight) throws NulsException {
    Log.info("utxoAccountsModule validateIntegrityBootstrap hadSynBlockHeight:" + hadSynBlockHeight);
    LocalCacheBlockBalance localCacheNextBlock = null;
    try {
        localCacheNextBlock = utxoAccountsStorageService.getLocalCacheBlock(hadSynBlockHeight + 1).getData();
    } catch (NulsException e) {
        Log.error(e);
        return false;
    }
    if (localCacheNextBlock == null) {
        // 无不一致数据
        return true;
    }
    List<UtxoAccountsBalancePo> utxoAccountsBalances = localCacheNextBlock.getBalanceList();
    if (utxoAccountsBalances == null) {
        // 无交易数据
        return true;
    }
    return rollbackBlock(localCacheNextBlock);
}
Also used : UtxoAccountsBalancePo(io.nuls.utxo.accounts.storage.po.UtxoAccountsBalancePo) NulsException(io.nuls.kernel.exception.NulsException) LocalCacheBlockBalance(io.nuls.utxo.accounts.storage.po.LocalCacheBlockBalance)

Example 79 with NulsException

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

the class UtxoAccountsResource method getBalance.

@GET
@Path("/balance/{address}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("[查询] 查询账户余额信息")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = RpcClientResult.class) })
public RpcClientResult getBalance(@ApiParam(name = "address", value = "账户地址", required = true) @PathParam("address") String address) {
    if (!AddressTool.validAddress(address)) {
        return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
    }
    try {
        Result<UtxoAccountsBalancePo> utxoAccountsBalance = utxoAccountsStorageService.getUtxoAccountsBalanceByAddress(AddressTool.getAddress(address));
        long synBlockHeight = utxoAccountsStorageService.getHadSynBlockHeight();
        if (null == utxoAccountsBalance || null == utxoAccountsBalance.getData()) {
            return Result.getFailed(UtxoAccountsErrorCode.DATA_NOT_FOUND).toRpcClientResult();
        }
        UtxoAccountsBalancePo dbAccountsBalance = utxoAccountsBalance.getData();
        AccountBalanceDto accountBalance = new AccountBalanceDto();
        accountBalance.setAddress(address);
        long totalNa = dbAccountsBalance.getOutputBalance() - (dbAccountsBalance.getInputBalance());
        totalNa += dbAccountsBalance.getContractToBalance();
        totalNa -= dbAccountsBalance.getContractFromBalance();
        accountBalance.setNuls(new BigDecimal(totalNa).toPlainString());
        long permanentLockedNa = dbAccountsBalance.getLockedPermanentBalance() - (dbAccountsBalance.getUnLockedPermanentBalance());
        long lockedNa = permanentLockedNa;
        List<LockedBalance> timeLockedBalance = dbAccountsBalance.getLockedTimeList();
        long currentTime = TimeService.currentTimeMillis();
        for (LockedBalance balance : timeLockedBalance) {
            if (balance.getLockedTime() > currentTime) {
                lockedNa += balance.getLockedBalance();
            } else {
                break;
            }
        }
        List<LockedBalance> heightLockedBalance = dbAccountsBalance.getLockedHeightList();
        for (LockedBalance balance : heightLockedBalance) {
            if (balance.getLockedTime() > synBlockHeight) {
                lockedNa += balance.getLockedBalance();
            } else {
                break;
            }
        }
        accountBalance.setLocked(new BigDecimal(lockedNa).toPlainString());
        accountBalance.setSynBlockHeight(String.valueOf(synBlockHeight));
        long netHeight = NulsContext.getInstance().getNetBestBlockHeight();
        accountBalance.setNetBlockHeight(String.valueOf(netHeight));
        return Result.getSuccess().setData(accountBalance).toRpcClientResult();
    } catch (NulsException e) {
        Log.error(e);
    }
    return Result.getFailed(UtxoAccountsErrorCode.SYS_UNKOWN_EXCEPTION).toRpcClientResult();
}
Also used : UtxoAccountsBalancePo(io.nuls.utxo.accounts.storage.po.UtxoAccountsBalancePo) LockedBalance(io.nuls.utxo.accounts.storage.po.LockedBalance) AccountBalanceDto(io.nuls.utxo.accounts.rpc.dto.AccountBalanceDto) NulsException(io.nuls.kernel.exception.NulsException) BigDecimal(java.math.BigDecimal) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 80 with NulsException

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

the class UtxoAccountsBalanceServiceImpl method getUtxoAccountsBalance.

@Override
public Result<UtxoAccountsBalance> getUtxoAccountsBalance(byte[] owner) {
    if (!AddressTool.validAddress(AddressTool.getStringAddressByBytes(owner))) {
        return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
    }
    try {
        Result<UtxoAccountsBalancePo> utxoAccountsBalance = utxoAccountsStorageService.getUtxoAccountsBalanceByAddress(owner);
        long synBlockHeight = utxoAccountsStorageService.getHadSynBlockHeight();
        if (null == utxoAccountsBalance || null == utxoAccountsBalance.getData()) {
            return Result.getFailed(UtxoAccountsErrorCode.DATA_NOT_FOUND);
        }
        UtxoAccountsBalancePo dbAccountsBalance = utxoAccountsBalance.getData();
        UtxoAccountsBalance accountBalance = new UtxoAccountsBalance();
        accountBalance.setOwner(dbAccountsBalance.getOwner());
        long totalNa = dbAccountsBalance.getOutputBalance() - (dbAccountsBalance.getInputBalance());
        totalNa += dbAccountsBalance.getContractToBalance();
        totalNa -= dbAccountsBalance.getContractFromBalance();
        accountBalance.setBalance(Na.valueOf(totalNa));
        long permanentLockedNa = dbAccountsBalance.getLockedPermanentBalance() - (dbAccountsBalance.getUnLockedPermanentBalance());
        long lockedNa = permanentLockedNa;
        List<LockedBalance> timeLockedBalance = dbAccountsBalance.getLockedTimeList();
        long currentTime = TimeService.currentTimeMillis();
        for (LockedBalance balance : timeLockedBalance) {
            if (balance.getLockedTime() > currentTime) {
                lockedNa += balance.getLockedBalance();
            } else {
                break;
            }
        }
        List<LockedBalance> heightLockedBalance = dbAccountsBalance.getLockedHeightList();
        for (LockedBalance balance : heightLockedBalance) {
            if (balance.getLockedTime() > synBlockHeight) {
                lockedNa += balance.getLockedBalance();
            } else {
                break;
            }
        }
        accountBalance.setHadLocked(Na.valueOf(lockedNa));
        return Result.getSuccess().setData(accountBalance);
    } catch (NulsException e) {
        Log.error(e);
    }
    return Result.getFailed(UtxoAccountsErrorCode.SYS_UNKOWN_EXCEPTION);
}
Also used : UtxoAccountsBalancePo(io.nuls.utxo.accounts.storage.po.UtxoAccountsBalancePo) LockedBalance(io.nuls.utxo.accounts.storage.po.LockedBalance) NulsException(io.nuls.kernel.exception.NulsException) UtxoAccountsBalance(io.nuls.utxo.accounts.model.UtxoAccountsBalance)

Aggregations

NulsException (io.nuls.kernel.exception.NulsException)109 IOException (java.io.IOException)35 Account (io.nuls.account.model.Account)25 ValidateResult (io.nuls.kernel.validate.ValidateResult)23 ECKey (io.nuls.core.tools.crypto.ECKey)20 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)18 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)13 Coin (io.nuls.kernel.model.Coin)12 MultiSigAccount (io.nuls.account.model.MultiSigAccount)11 ContractResult (io.nuls.contract.dto.ContractResult)9 NulsByteBuffer (io.nuls.kernel.utils.NulsByteBuffer)9 ArrayList (java.util.ArrayList)9 TransactionSignature (io.nuls.kernel.script.TransactionSignature)8 BigInteger (java.math.BigInteger)8 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)7 AccountPo (io.nuls.account.storage.po.AccountPo)7 AliasPo (io.nuls.account.storage.po.AliasPo)7 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)7