Search in sources :

Example 16 with Coin

use of io.nuls.kernel.model.Coin in project nuls by nuls-io.

the class TxCoinValidator method validate.

@Override
public ValidateResult validate(Transaction tx) throws NulsException {
    try {
        if (tx.getCoinData() == null) {
            return ValidateResult.getSuccessResult();
        }
        List<Coin> toList = tx.getCoinData().getTo();
        if (toList == null || toList.size() == 0) {
            return ValidateResult.getSuccessResult();
        }
        Set<String> fromAddressSet = null;
        byte[] owner;
        String address;
        Map<String, Integer> addressSet = MapUtil.createHashMap(toList.size());
        for (Coin coin : toList) {
            owner = coin.getOwner();
            if (owner.length == Address.ADDRESS_LENGTH && owner[2] == NulsContext.P2SH_ADDRESS_TYPE) {
                return ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.COIN_OWNER_ERROR);
            }
            if (tx.isSystemTx() && tx.getType() != ContractConstant.TX_TYPE_CONTRACT_TRANSFER) {
                continue;
            }
            address = AddressTool.getStringAddressByBytes(coin.getAddress());
            if (fromAddressSet == null) {
                fromAddressSet = SignatureUtil.getAddressFromTX(tx);
            }
            if (fromAddressSet != null && fromAddressSet.contains(address)) {
                continue;
            }
            Integer count = addressSet.get(address);
            if (count == null) {
                addressSet.put(address, count = 1);
            } else {
                addressSet.put(address, ++count);
            }
            if (count > 2) {
                return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.INVALID_AMOUNT);
            }
        }
    } catch (Exception e) {
        Log.error(e);
        return ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.DATA_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : Coin(io.nuls.kernel.model.Coin) NulsException(io.nuls.kernel.exception.NulsException)

Example 17 with Coin

use of io.nuls.kernel.model.Coin in project nuls by nuls-io.

the class UtxoAccountsServiceImpl method buildUtxoAccountsMap.

private boolean buildUtxoAccountsMap(Map<String, UtxoAccountsBalancePo> utxoAccountsMap, Block block) {
    List<Transaction> txs = block.getTxs();
    int txIndex = 0;
    for (Transaction tx : txs) {
        try {
            if (tx.getCoinData() == null) {
                continue;
            }
            List<Coin> from = tx.getCoinData().getFrom();
            List<Coin> to = tx.getCoinData().getTo();
            for (Coin inputCoin : from) {
                byte[] inputOwner = getInputAddress(inputCoin);
                inputCoin.setOwner(inputOwner);
                buildUtxoAccountsBalance(utxoAccountsMap, inputCoin, tx, txIndex, true);
            }
            for (Coin outputCoin : to) {
                buildUtxoAccountsBalance(utxoAccountsMap, outputCoin, tx, txIndex, false);
            }
            // 若区块中得到合约转账(从合约转出)交易,这段代码应该去掉
            // 增加智能合约内部交易逻辑
            // if (tx.getType() == UtxoAccountsConstant.TX_TYPE_CALL_CONTRACT) {
            // ContractResult contractExecuteResult = contractService.getContractExecuteResult(tx.getHash());
            // List<ContractTransfer> transferList = contractExecuteResult.getTransfers();
            // buildContractTranfersBalance(utxoAccountsMap, transferList, block.getHeader().getHeight(), txIndex);
            // }
            txIndex++;
        } catch (Exception e) {
            Log.error(e);
        }
    }
    return true;
}
Also used : Coin(io.nuls.kernel.model.Coin) Transaction(io.nuls.kernel.model.Transaction) NulsException(io.nuls.kernel.exception.NulsException)

Example 18 with Coin

use of io.nuls.kernel.model.Coin 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 "--";
    }
}
Also used : Coin(io.nuls.kernel.model.Coin) NulsException(io.nuls.kernel.exception.NulsException)

Example 19 with Coin

use of io.nuls.kernel.model.Coin in project nuls by nuls-io.

the class TxVersionForScriptValidator method validate.

@Override
public ValidateResult validate(Transaction tx) {
    if (NulsContext.MAIN_NET_VERSION > 1) {
        return ValidateResult.getSuccessResult();
    }
    if (null == tx.getCoinData() || tx.getCoinData().getTo() == null || tx.getCoinData().getTo().isEmpty()) {
        return ValidateResult.getSuccessResult();
    }
    List<Coin> toList = tx.getCoinData().getTo();
    ValidateResult failed = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.VERSION_NOT_NEWEST);
    for (Coin coin : toList) {
        if (coin.getOwner().length != Address.ADDRESS_LENGTH) {
            return failed;
        }
        if (coin.getOwner()[2] != NulsContext.DEFAULT_ADDRESS_TYPE) {
            return failed;
        }
    }
    return ValidateResult.getSuccessResult();
}
Also used : Coin(io.nuls.kernel.model.Coin) ValidateResult(io.nuls.kernel.validate.ValidateResult)

Example 20 with Coin

use of io.nuls.kernel.model.Coin in project nuls by nuls-io.

the class LocalUtxoStorageServiceImpl method getUtxo.

@Override
public Coin getUtxo(byte[] owner) {
    byte[] utxoBytes = getUtxoBytes(owner);
    Coin coin = null;
    try {
        if (utxoBytes != null) {
            coin = new Coin();
            coin.parse(utxoBytes, 0);
        }
    } catch (NulsException e) {
        Log.error(e);
        return null;
    }
    return coin;
}
Also used : Coin(io.nuls.kernel.model.Coin) NulsException(io.nuls.kernel.exception.NulsException)

Aggregations

Coin (io.nuls.kernel.model.Coin)31 NulsException (io.nuls.kernel.exception.NulsException)16 Na (io.nuls.kernel.model.Na)8 Entry (io.nuls.db.model.Entry)6 ValidateResult (io.nuls.kernel.validate.ValidateResult)6 CoinData (io.nuls.kernel.model.CoinData)5 TransactionSignature (io.nuls.kernel.script.TransactionSignature)4 ContractBalance (io.nuls.contract.ledger.module.ContractBalance)3 Result (io.nuls.kernel.model.Result)3 LedgerUtil.asString (io.nuls.ledger.util.LedgerUtil.asString)3 ArrayList (java.util.ArrayList)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)2 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)2 ECKey (io.nuls.core.tools.crypto.ECKey)2 RpcClientResult (io.nuls.kernel.model.RpcClientResult)2 Transaction (io.nuls.kernel.model.Transaction)2 Script (io.nuls.kernel.script.Script)2