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