use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class UtxoAccountsServiceImpl method synBlock.
/**
* @param blockHeight
* @return
*/
@Override
public boolean synBlock(long blockHeight) {
Log.debug("synBlock begin===blockHeight:" + blockHeight);
Block nodeBlock = utxoAccountsStorageService.getBlock(blockHeight).getData();
if (nodeBlock == null) {
Log.error("utxoAccounts getBlock faile,blockHeight:" + blockHeight);
return false;
}
boolean hadRoll = false;
try {
// get local pre block info /从本地取上一个已同步的区块
LocalCacheBlockBalance localLatestCacheBlock = utxoAccountsStorageService.getLocalCacheBlock(blockHeight - 1).getData();
// rollback judge /判断回滚
while (localLatestCacheBlock != null && !nodeBlock.getHeader().getPreHash().equals(localLatestCacheBlock.getHash())) {
// roll back info /进行数据回滚
rollbackBlock(localLatestCacheBlock);
blockHeight--;
// get pre block
localLatestCacheBlock = utxoAccountsStorageService.getLocalCacheBlock(blockHeight - 1).getData();
nodeBlock = utxoAccountsStorageService.getBlock(blockHeight).getData();
hadRoll = true;
}
if (hadRoll) {
return false;
}
} catch (NulsException e) {
Log.error(e);
Log.error("block syn error======blockHeight:" + blockHeight);
return false;
}
// begin syn block/开始同步区块
// analysis block/解析区块
Map<String, UtxoAccountsBalancePo> utxoAccountsMap = new HashMap<>();
if (!buildUtxoAccountsMap(utxoAccountsMap, nodeBlock)) {
return false;
}
List<UtxoAccountsBalancePo> list = new ArrayList<>();
LocalCacheBlockBalance localCacheBlockBalance = new LocalCacheBlockBalance();
// LocalCacheBlockBalance preSnapshot=new LocalCacheBlockBalance();
try {
list = utxoAccountsMapToList(utxoAccountsMap, localCacheBlockBalance);
} catch (NulsException e) {
Log.info("utxoAccountsMapToList error======blockHeight:" + blockHeight);
return false;
}
localCacheBlockBalance.setHash(nodeBlock.getHeader().getHash());
localCacheBlockBalance.setPreHash(nodeBlock.getHeader().getPreHash());
// localCacheBlockBalance.setBalanceList(list);
localCacheBlockBalance.setBlockHeight(blockHeight);
// save cache block info/缓存最近解析信息
utxoAccountsStorageService.saveLocalCacheBlock(blockHeight, localCacheBlockBalance);
// utxoAccountsStorageService.saveLocalCacheChangeSnapshot(blockHeight,localCacheBlockBalance);
utxoAccountsStorageService.batchSaveByteUtxoAcountsInfo(list);
// update latest block height/更新最近高度
utxoAccountsStorageService.saveHadSynBlockHeight(blockHeight);
// delete overdue cache data/删除过期缓存数据
if (blockHeight > UtxoAccountsStorageConstant.MAX_CACHE_BLOCK_NUM) {
utxoAccountsStorageService.deleteLocalCacheBlock(blockHeight - UtxoAccountsStorageConstant.MAX_CACHE_BLOCK_NUM);
}
Log.debug("utxoAccounts synBlock success==blockHeight:" + blockHeight);
return true;
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class UtxoAccountsResource method get.
@GET
@Path("/{address}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("[查询] 查询账户信息")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = RpcClientResult.class) })
public RpcClientResult get(@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.setLockedTimeList(new ArrayList<>());
accountBalance.setLockedHeightList(new ArrayList<>());
accountBalance.setAddress(address);
long totalNa = dbAccountsBalance.getOutputBalance() - (dbAccountsBalance.getInputBalance());
totalNa += dbAccountsBalance.getContractToBalance();
totalNa -= dbAccountsBalance.getContractFromBalance();
accountBalance.setNuls(new BigDecimal(totalNa).toPlainString());
long timeLockedNa = 0;
long heightLockedNa = 0;
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();
timeLockedNa += balance.getLockedBalance();
accountBalance.getLockedTimeList().add(balance);
} else {
break;
}
}
List<LockedBalance> heightLockedBalance = dbAccountsBalance.getLockedHeightList();
for (LockedBalance balance : heightLockedBalance) {
if (balance.getLockedTime() > synBlockHeight) {
lockedNa += balance.getLockedBalance();
heightLockedNa += balance.getLockedBalance();
accountBalance.getLockedHeightList().add(balance);
} else {
break;
}
}
accountBalance.setPermanentLocked(new BigDecimal(permanentLockedNa).toPlainString());
accountBalance.setLocked(new BigDecimal(lockedNa).toPlainString());
accountBalance.setTimeLocked(new BigDecimal(timeLockedNa).toPlainString());
accountBalance.setHeightLocked(new BigDecimal(heightLockedNa).toPlainString());
accountBalance.setSynBlockHeight(String.valueOf(synBlockHeight));
long netHeight = NulsContext.getInstance().getNetBestBlockHeight();
accountBalance.setNetBlockHeight(String.valueOf(netHeight));
accountBalance.setContractIn(String.valueOf(dbAccountsBalance.getContractToBalance()));
accountBalance.setContractOut(String.valueOf(dbAccountsBalance.getContractFromBalance()));
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 AgentStorageServiceImpl method get.
@Override
public AgentPo get(NulsDigestData hash) {
if (hash == null) {
return null;
}
byte[] body = null;
try {
body = dbService.get(ConsensusStorageConstant.DB_NAME_CONSENSUS_AGENT, hash.serialize());
} catch (IOException e) {
Log.error(e);
}
if (body == null) {
return null;
}
AgentPo agentPo = new AgentPo();
try {
agentPo.parse(body, 0);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
agentPo.setHash(hash);
return agentPo;
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class PunishLogStorageServiceImpl method getPunishList.
@Override
public List<PunishLogPo> getPunishList() {
List<Entry<byte[], byte[]>> list = dbService.entryList(ConsensusStorageConstant.DB_NAME_CONSENSUS_PUNISH_LOG);
List<PunishLogPo> polist = new ArrayList<>();
for (Entry<byte[], byte[]> entry : list) {
PunishLogPo po = new PunishLogPo();
try {
po.parse(entry.getValue(), 0);
} catch (NulsException e) {
throw new NulsRuntimeException(e);
}
polist.add(po);
}
return polist;
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class CallContractTransaction method getInfo.
/**
* 用于钱包显示资产变动
*
* 资产变动: 1. 仅有手续费 2.从钱包地址向合约转账的金额、手续费
* 此方法`getInfo`用于钱包账户,而合约地址不属于钱包账户,所以这里的入参不会是合约地址
* 若toList只有一个Coin,要么是调用者自身扣了手续费后的找零,要么是from全部转移到另一个地址
* 若toList有两个Coin,则必然有一个是从钱包地址向合约转账的金额 - 对应的是合约地址,另一个是调用者自身扣了手续费后的找零 - 对应的是调用者的地址
* 这里的地址有三种情况,一是合约调用者的地址,二是合约转账(从合约转出)的`to`地址,三是合约Token转账的`from`,`to`
* 综上,由于方法入参不会是合约地址,因此除合约调用者地址外,其他地址传入都返回 `0`
* @param address
* @return
*/
@Override
public String getInfo(byte[] address) {
List<Coin> toList = coinData.getTo();
int size = toList.size();
if (size == 1) {
Coin to1 = toList.get(0);
if (Arrays.equals(address, to1.getAddress())) {
return "-" + getFee().toCoinString();
} else {
try {
Set<String> addressSet = SignatureUtil.getAddressFromTX(this);
if (addressSet.contains(AddressTool.getStringAddressByBytes(address))) {
return "-" + to1.getNa().add(getFee()).toCoinString();
} else {
return "0";
}
} catch (NulsException e) {
Log.error(e);
return "0";
}
}
} else if (size == 2) {
Coin to1 = toList.get(0);
Coin to2 = toList.get(1);
boolean equals1 = Arrays.equals(address, to1.getAddress());
boolean equals2 = Arrays.equals(address, to2.getAddress());
if (!equals1 && !equals2) {
return "0";
} else if (!equals1) {
return "-" + to1.getNa().add(getFee()).toCoinString();
} else if (!equals2) {
return "-" + to2.getNa().add(getFee()).toCoinString();
} else {
return "--";
}
} else {
return "--";
}
}
Aggregations