Search in sources :

Example 21 with TransferTransaction

use of io.nuls.protocol.model.tx.TransferTransaction in project nuls by nuls-io.

the class AccountLedgerServiceImpl method transfer.

@Override
public Result transfer(byte[] from, byte[] to, Na values, String password, byte[] remark, Na price) {
    try {
        if (NulsContext.WALLET_STATUS == NulsConstant.SYNCHING) {
            return Result.getFailed(KernelErrorCode.WALLET_STATUS_SYNCHING);
        } else if (NulsContext.WALLET_STATUS == NulsConstant.ROLLBACK) {
            return Result.getFailed(KernelErrorCode.WALLET_STATUS_ROLLBACK);
        }
        Result<Account> accountResult = accountService.getAccount(from);
        if (accountResult.isFailed()) {
            return accountResult;
        }
        Account account = accountResult.getData();
        if (account.isEncrypted() && account.isLocked()) {
            AssertUtil.canNotEmpty(password, "the password can not be empty");
            if (!account.validatePassword(password)) {
                return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
            }
        }
        if (!account.isOk()) {
            return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT);
        }
        // 检查to是否为合约地址,如果是合约地址,则返回错误
        if (contractService.isContractAddress(to)) {
            return Result.getFailed(ContractErrorCode.NON_CONTRACTUAL_TRANSACTION_NO_TRANSFER);
        }
        TransferTransaction tx = new TransferTransaction();
        tx.setRemark(remark);
        tx.setTime(TimeService.currentTimeMillis());
        CoinData coinData = new CoinData();
        // 如果为多签地址则以脚本方式存储
        Coin toCoin;
        if (to[2] == NulsContext.P2SH_ADDRESS_TYPE) {
            Script scriptPubkey = SignatureUtil.createOutputScript(to);
            toCoin = new Coin(scriptPubkey.getProgram(), values);
        } else {
            toCoin = new Coin(to, values);
        }
        coinData.getTo().add(toCoin);
        if (price == null) {
            price = TransactionFeeCalculator.MIN_PRICE_PRE_1024_BYTES;
        }
        CoinDataResult coinDataResult = getCoinData(from, values, tx.size() + coinData.size(), price);
        if (!coinDataResult.isEnough()) {
            return Result.getFailed(AccountLedgerErrorCode.INSUFFICIENT_BALANCE);
        }
        coinData.setFrom(coinDataResult.getCoinList());
        if (coinDataResult.getChange() != null) {
            coinData.getTo().add(coinDataResult.getChange());
        }
        tx.setCoinData(coinData);
        tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
        // 生成签名
        List<ECKey> signEckeys = new ArrayList<>();
        List<ECKey> scriptEckeys = new ArrayList<>();
        ECKey eckey = account.getEcKey(password);
        // 如果最后一位为1则表示该交易包含普通签名
        if ((coinDataResult.getSignType() & 0x01) == 0x01) {
            signEckeys.add(eckey);
        }
        // 如果倒数第二位位为1则表示该交易包含脚本签名
        if ((coinDataResult.getSignType() & 0x02) == 0x02) {
            scriptEckeys.add(eckey);
        }
        SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys);
        // 保存未确认交易到本地账户
        Result saveResult = verifyAndSaveUnconfirmedTransaction(tx);
        if (saveResult.isFailed()) {
            if (KernelErrorCode.DATA_SIZE_ERROR.getCode().equals(saveResult.getErrorCode().getCode())) {
                // 重新算一次交易(不超出最大交易数据大小下)的最大金额
                Result rs = getMaxAmountOfOnce(from, tx, price);
                if (rs.isSuccess()) {
                    Na maxAmount = (Na) rs.getData();
                    rs = Result.getFailed(KernelErrorCode.DATA_SIZE_ERROR_EXTEND);
                    rs.setMsg(rs.getMsg() + maxAmount.toDouble());
                }
                return rs;
            }
            return saveResult;
        }
        // transactionService.newTx(tx);
        Result sendResult = transactionService.broadcastTx(tx);
        if (sendResult.isFailed()) {
            this.deleteTransaction(tx);
            return sendResult;
        }
        return Result.getSuccess().setData(tx.getHash().getDigestHex());
    } catch (IOException e) {
        Log.error(e);
        return Result.getFailed(KernelErrorCode.IO_ERROR);
    } catch (NulsException e) {
        Log.error(e);
        return Result.getFailed(e.getErrorCode());
    }
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) ECKey(io.nuls.core.tools.crypto.ECKey) IOException(java.io.IOException) TransactionDataResult(io.nuls.account.ledger.model.TransactionDataResult) ValidateResult(io.nuls.kernel.validate.ValidateResult) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult) NulsException(io.nuls.kernel.exception.NulsException) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 22 with TransferTransaction

use of io.nuls.protocol.model.tx.TransferTransaction in project nuls by nuls-io.

the class InventoryFilterTest method test1.

@Test
public void test1() {
    BloomFilter<byte[]> filter = BloomFilter.create(Funnels.byteArrayFunnel(), 1000000, 0.00001);
    ArrayList<Transaction> txList = new ArrayList<>();
    for (int i = 1000000; i < 2000000; i++) {
        Transaction tx = new TransferTransaction();
        tx.setTime(i);
        tx.setRemark("sdfsdfsdfsdfsdfsdfaaadsfasdfsadfsdfasdfasdfasdfasdfasdfsadfaaaaaaaaaaaaaaaaaaaaaabsdsadfsadfsdfsdfsdfsdfsdfsdfsdfaaadsfasdfsadfsdfasdfasdfasdfasdfasdfsadfaaaaaaaaaaaaaaaaaaaaaabsdsadfsadfsdfsdfsdfsdfsdfsdfsdfaa".getBytes());
        txList.add(tx);
    }
    System.out.println("start....");
    long start = System.currentTimeMillis();
    for (Transaction tx : txList) {
        NulsDigestData hash = tx.getHash();
        if (!filter.mightContain(hash.getDigestBytes())) {
            filter.put(hash.getDigestBytes());
            int num = count.incrementAndGet();
            if (num % 100 == 0) {
                System.out.println("count::::::" + num);
            }
        }
    }
    System.out.println("use time::" + (System.currentTimeMillis() - start));
    System.out.println(count.get());
}
Also used : Transaction(io.nuls.kernel.model.Transaction) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.kernel.model.NulsDigestData) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction) Test(org.junit.Test)

Aggregations

TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)22 NulsException (io.nuls.kernel.exception.NulsException)11 IOException (java.io.IOException)11 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)9 ECKey (io.nuls.core.tools.crypto.ECKey)8 ArrayList (java.util.ArrayList)8 Account (io.nuls.account.model.Account)7 MultiSigAccount (io.nuls.account.model.MultiSigAccount)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)7 ValidateResult (io.nuls.kernel.validate.ValidateResult)7 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)6 MultipleAddressTransferModel (io.nuls.account.ledger.model.MultipleAddressTransferModel)4 NulsDigestData (io.nuls.kernel.model.NulsDigestData)4 Transaction (io.nuls.kernel.model.Transaction)4 Script (io.nuls.kernel.script.Script)4 Test (org.junit.Test)4 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)2 Coin (io.nuls.kernel.model.Coin)2 CoinData (io.nuls.kernel.model.CoinData)2