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);
}
}
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());
}
}
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);
}
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();
}
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);
}
Aggregations