Search in sources :

Example 1 with BalanceInsufficientException

use of org.tron.core.exception.BalanceInsufficientException in project java-tron by tronprotocol.

the class AssetIssueActuator method execute.

@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
    long fee = calcFee();
    try {
        if (!this.contract.is(AssetIssueContract.class)) {
            throw new ContractExeException();
        }
        if (dbManager == null) {
            throw new ContractExeException();
        }
        AssetIssueContract assetIssueContract = contract.unpack(AssetIssueContract.class);
        AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule(assetIssueContract);
        dbManager.getAssetIssueStore().put(assetIssueCapsule.getName().toByteArray(), assetIssueCapsule);
        dbManager.adjustBalance(assetIssueContract.getOwnerAddress().toByteArray(), -calcFee());
        ret.setStatus(fee, code.SUCESS);
        AccountCapsule accountCapsule = dbManager.getAccountStore().get(assetIssueContract.getOwnerAddress().toByteArray());
        accountCapsule.addAsset(ByteArray.toStr(assetIssueContract.getName().toByteArray()), assetIssueContract.getTotalSupply());
        dbManager.getAccountStore().put(assetIssueContract.getOwnerAddress().toByteArray(), accountCapsule);
    } catch (InvalidProtocolBufferException e) {
        ret.setStatus(fee, code.FAILED);
        throw new ContractExeException();
    } catch (BalanceInsufficientException e) {
        ret.setStatus(fee, code.FAILED);
        throw new ContractExeException();
    }
    return true;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) AssetIssueCapsule(org.tron.core.capsule.AssetIssueCapsule) AssetIssueContract(org.tron.protos.Contract.AssetIssueContract) ContractExeException(org.tron.core.exception.ContractExeException)

Example 2 with BalanceInsufficientException

use of org.tron.core.exception.BalanceInsufficientException in project java-tron by tronprotocol.

the class Manager method updateSignedWitness.

/**
 * @param block the block update signed witness.  set witness who signed block the 1. the latest
 * block num 2. pay the trx to witness. 3. (TODO)the latest slot num.
 */
public void updateSignedWitness(BlockCapsule block) {
    // TODO: add verification
    WitnessCapsule witnessCapsule = witnessStore.get(block.getInstance().getBlockHeader().getRawData().getWitnessAddress().toByteArray());
    long latestSlotNum = 0;
    witnessCapsule.getInstance().toBuilder().setLatestBlockNum(block.getNum()).setLatestSlotNum(latestSlotNum).build();
    AccountCapsule sun = accountStore.getSun();
    try {
        adjustBalance(sun.getAddress().toByteArray(), -WITNESS_PAY_PER_BLOCK);
    } catch (BalanceInsufficientException e) {
    }
    try {
        adjustBalance(witnessCapsule.getAddress().toByteArray(), WITNESS_PAY_PER_BLOCK);
    } catch (BalanceInsufficientException e) {
        logger.debug(e.getMessage(), e);
    }
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) WitnessCapsule(org.tron.core.capsule.WitnessCapsule) BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException)

Example 3 with BalanceInsufficientException

use of org.tron.core.exception.BalanceInsufficientException in project java-tron by tronprotocol.

the class Manager method adjustBalance.

/**
 * judge balance.
 */
public void adjustBalance(byte[] accountAddress, long amount) throws BalanceInsufficientException {
    AccountCapsule account = getAccountStore().get(accountAddress);
    long balance = account.getBalance();
    if (amount == 0) {
        return;
    }
    if (amount < 0 && balance < -amount) {
        throw new BalanceInsufficientException(accountAddress + " Insufficient");
    }
    account.setBalance(balance + amount);
    this.getAccountStore().put(account.getAddress().toByteArray(), account);
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException)

Example 4 with BalanceInsufficientException

use of org.tron.core.exception.BalanceInsufficientException in project java-tron by tronprotocol.

the class TransferActuator method execute.

@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
    long fee = calcFee();
    try {
        TransferContract transferContract = null;
        transferContract = contract.unpack(TransferContract.class);
        dbManager.adjustBalance(transferContract.getOwnerAddress().toByteArray(), -calcFee());
        ret.setStatus(fee, code.SUCESS);
        dbManager.adjustBalance(transferContract.getOwnerAddress().toByteArray(), -transferContract.getAmount());
        dbManager.adjustBalance(transferContract.getToAddress().toByteArray(), transferContract.getAmount());
    } catch (InvalidProtocolBufferException e) {
        logger.debug(e.getMessage(), e);
        ret.setStatus(fee, code.FAILED);
        throw new ContractExeException(e.getMessage());
    } catch (BalanceInsufficientException e) {
        logger.debug(e.getMessage(), e);
        ret.setStatus(fee, code.FAILED);
        throw new ContractExeException(e.getMessage());
    }
    return true;
}
Also used : BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TransferContract(org.tron.protos.Contract.TransferContract) ContractExeException(org.tron.core.exception.ContractExeException)

Aggregations

BalanceInsufficientException (org.tron.core.exception.BalanceInsufficientException)4 AccountCapsule (org.tron.core.capsule.AccountCapsule)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ContractExeException (org.tron.core.exception.ContractExeException)2 AssetIssueCapsule (org.tron.core.capsule.AssetIssueCapsule)1 WitnessCapsule (org.tron.core.capsule.WitnessCapsule)1 AssetIssueContract (org.tron.protos.Contract.AssetIssueContract)1 TransferContract (org.tron.protos.Contract.TransferContract)1