All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.hyperchain.sdk.service.impl.TxServiceImpl Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package cn.hyperchain.sdk.service.impl;

import cn.hyperchain.sdk.common.utils.MethodType;
import cn.hyperchain.sdk.provider.ProviderManager;
import cn.hyperchain.sdk.request.ReceiptRequest;
import cn.hyperchain.sdk.request.Request;
import cn.hyperchain.sdk.request.SendBatchTxsRequest;
import cn.hyperchain.sdk.request.SendTxRequest;
import cn.hyperchain.sdk.request.TxRequest;
import cn.hyperchain.sdk.response.ReceiptResponse;
import cn.hyperchain.sdk.response.TxHashResponse;
import cn.hyperchain.sdk.response.TxHashesResponse;
import cn.hyperchain.sdk.response.tx.TxAvgTimeResponse;
import cn.hyperchain.sdk.response.tx.TxCountResponse;
import cn.hyperchain.sdk.response.tx.TxCountWithTSResponse;
import cn.hyperchain.sdk.response.tx.TxResponse;
import cn.hyperchain.sdk.service.ContractService;
import cn.hyperchain.sdk.service.ServiceManager;
import cn.hyperchain.sdk.service.TxService;
import cn.hyperchain.sdk.transaction.Transaction;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Tx Service Implementation.
 *
 * @author dong
 */
public class TxServiceImpl implements TxService {
    private ProviderManager providerManager;
    private static final String TX_PREFIX = "tx_";

    public TxServiceImpl(ProviderManager providerManager) {
        this.providerManager = providerManager;
    }


    @Override
    public Request getTx(BigInteger from, BigInteger to, int... nodeIds) {
        return getTx(from.toString(), to.toString(), nodeIds);
    }


    @Override
    public Request getTx(String from, String to, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getTransactions", providerManager, TxResponse.class, nodeIds);

        HashMap params = new HashMap<>();
        params.put("from", from);
        params.put("to", to);
        txRequest.addParams(params);

        return txRequest;
    }

    @Override
    public Request getDiscardTx(int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getDiscardTransactions", providerManager, TxResponse.class, nodeIds);
        return txRequest;
    }

    @Override
    public Request getTxByHash(String txHash, int... nodeIds) {
        return getTxByHash(txHash, false);
    }


    @Override
    public Request getTxByHash(String txHash, boolean isPrivateTx, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + (isPrivateTx ? "getPrivateTransactionByHash" : "getTransactionByHash"), providerManager, TxResponse.class, nodeIds);
        txRequest.addParams(txHash);

        return txRequest;
    }


    @Override
    public Request getTxByBlockHashAndIndex(String blockHash, int idx, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getTransactionByBlockHashAndIndex", providerManager, TxResponse.class, nodeIds);
        txRequest.addParams(blockHash);
        txRequest.addParams(idx);

        return txRequest;
    }


    @Override
    public Request getTxByBlockNumAndIndex(int blockNumber, int idx, int... nodeIds) {
        TxRequest txResponseTxRequest = new TxRequest(TX_PREFIX + "getTransactionByBlockNumberAndIndex", providerManager, TxResponse.class, nodeIds);
        txResponseTxRequest.addParams(blockNumber);
        txResponseTxRequest.addParams(idx);

        return txResponseTxRequest;
    }


    @Override
    public Request getTxByBlockNumAndIndex(String blockNumber, String idx, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getTransactionByBlockNumberAndIndex", providerManager, TxResponse.class, nodeIds);
        txRequest.addParams(blockNumber);
        txRequest.addParams(idx);

        return txRequest;
    }


    @Override
    public Request getTxAvgTimeByBlockNumber(BigInteger from, BigInteger to, int... nodeIds) {
        return getTxAvgTimeByBlockNumber(from.toString(), to.toString(), nodeIds);
    }


    @Override
    public Request getTxAvgTimeByBlockNumber(String from, String to, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getTxAvgTimeByBlockNumber", providerManager, TxAvgTimeResponse.class, nodeIds);

        HashMap params = new HashMap<>();
        params.put("from", from);
        params.put("to", to);
        txRequest.addParams(params);

        return txRequest;
    }


    @Override
    public Request getTransactionsCount(int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getTransactionsCount", providerManager, TxCountWithTSResponse.class, nodeIds);
        return txRequest;
    }


    @Override
    public Request getTransactionReceipt(String txHash, int... nodeIds) {
        ReceiptRequest pollingRequest = new ReceiptRequest(TX_PREFIX + "getTransactionReceipt", providerManager, ReceiptResponse.class, nodeIds);

        pollingRequest.addParams(txHash);

        return pollingRequest;
    }


    @Override
    public Request getBlockTxCountByHash(String blockHash, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getBlockTransactionCountByHash", providerManager, TxCountResponse.class, nodeIds);

        txRequest.addParams(blockHash);

        return txRequest;
    }


    @Override
    public Request getBlockTxCountByNumber(String blockNumber, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getBlockTransactionCountByNumber", providerManager, TxCountResponse.class, nodeIds);

        txRequest.addParams(blockNumber);

        return txRequest;
    }


    @Override
    public Request getSignHash(String from, BigInteger nonce, String payload, BigInteger timestamp, int... nodeIds) {
        return getSignHash(from, nonce, null, payload, timestamp, nodeIds);
    }


    @Override
    public Request getSignHash(String from, BigInteger nonce, String extra, String payload, BigInteger timestamp, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getSignHash", providerManager, TxResponse.class, nodeIds);

        HashMap params = new HashMap<>();
        params.put("from", from);
        params.put("nonce", nonce);
        params.put("payload", payload);
        if (extra != null) {
            params.put("extra", extra);
        }
        params.put("timestamp", timestamp);
        txRequest.addParams(params);

        return txRequest;
    }


    @Override
    public Request getSignHash(String from, String to, BigInteger nonce, String value, BigInteger timestamp, int... nodeIds) {
        return getSignHash(from, to, nonce, null, value, timestamp, nodeIds);
    }


    @Override
    public Request getSignHash(String from, String to, BigInteger nonce, String extra, String value, BigInteger timestamp, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getSignHash", providerManager, TxResponse.class, nodeIds);

        HashMap params = new HashMap<>();
        params.put("from", from);
        params.put("to", to);
        params.put("nonce", nonce);
        params.put("value", value);
        if (extra != null) {
            params.put("extra", extra);
        }
        params.put("timestamp", timestamp);
        txRequest.addParams(params);

        return txRequest;
    }


    @Override
    public Request getTransactionsByTime(BigInteger startTime, BigInteger endTime, int... nodeIds) {
        return getTransactionsByTime(startTime, endTime, -1, nodeIds);
    }

    @Override
    public Request getTransactionsByTime(BigInteger startTime, BigInteger endTime, int limit, int... nodeIds) {
        Request txResponseRequest = new TxRequest(TX_PREFIX + "getTransactionsByTime", providerManager, TxResponse.class, nodeIds);

        HashMap params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        if (limit != -1) {
            params.put("limit", limit);
        }
        txResponseRequest.addParams(params);

        return txResponseRequest;
    }


    @Override
    public Request getTransactionsByTime(String startTime, String endTime, int... nodeIds) {
        return getTransactionsByTime(startTime, endTime, -1, nodeIds);
    }

    @Override
    public Request getTransactionsByTime(String startTime, String endTime, int limit, int... nodeIds) {
        return getTransactionsByTime(new BigInteger(startTime), new BigInteger(endTime), limit, nodeIds);
    }

    @Override
    public Request getDiscardTransactionsByTime(BigInteger startTime, BigInteger endTime, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getDiscardTransactionsByTime", providerManager, TxResponse.class, nodeIds);

        HashMap params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        txRequest.addParams(params);

        return txRequest;
    }


    @Override
    public Request getDiscardTransactionsByTime(String startTime, String endTime, int... nodeIds) {
        return getDiscardTransactionsByTime(new BigInteger(startTime), new BigInteger(endTime));
    }

    @Override
    public Request getTransactionsCountByContractAddr(String from, String to, String contractAddress, boolean txExtra, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getTransactionsCountByContractAddr", providerManager, TxResponse.class, nodeIds);

        HashMap params = new HashMap<>();
        params.put("from", from);
        params.put("to", to);
        params.put("address", contractAddress);
        params.put("txExtra", txExtra);
        txRequest.addParams(params);

        return txRequest;
    }


    @Override
    public Request getTransactionsCountByContractAddr(BigInteger from, BigInteger to, String contractAddress, boolean txExtra, int... nodeIds) {
        return getTransactionsCountByContractAddr(from.toString(), to.toString(), contractAddress, txExtra, nodeIds);
    }

    @Override
    public Request getNextPageTransactions(BigInteger blkNumber, BigInteger txIndex, BigInteger minBlkNumber, BigInteger maxBlkNumber, BigInteger separated, BigInteger pageSize, boolean containCurrent, String address, int... nodeIds) {
        return getNextPageTransactions(blkNumber, txIndex, minBlkNumber, maxBlkNumber, separated, pageSize, containCurrent, address, nodeIds);
    }


    @Override
    public Request getNextPageTransactions(String blkNumber, String txIndex, String minBlkNumber, String maxBlkNumber, String separated, String pageSize, boolean containCurrent, String address, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getNextPageTransactions", providerManager, TxResponse.class, nodeIds);

        HashMap params = new HashMap<>();
        params.put("blkNumber", blkNumber);
        params.put("txIndex", txIndex);
        params.put("minBlkNumber", minBlkNumber);
        params.put("maxBlkNumber", maxBlkNumber);
        params.put("separated", separated);
        params.put("pageSize", pageSize);
        params.put("containCurrent", containCurrent);
        params.put("address", address);
        txRequest.addParams(params);

        return txRequest;
    }

    @Override
    public Request getPrevPageTransactions(BigInteger blkNumber, BigInteger txIndex, BigInteger minBlkNumber, BigInteger maxBlkNumber, BigInteger separated, BigInteger pageSize, boolean containCurrent, String address, int... nodeIds) {
        return getPrevPageTransactions(blkNumber, txIndex, minBlkNumber, maxBlkNumber, separated, pageSize, containCurrent, address, nodeIds);
    }


    @Override
    public Request getPrevPageTransactions(String blkNumber, String txIndex, String minBlkNumber, String maxBlkNumber, String separated, String pageSize, boolean containCurrent, String address, int... nodeIds) {
        TxRequest txRequest = new TxRequest(TX_PREFIX + "getPrevPageTransactions", providerManager, TxResponse.class, nodeIds);

        HashMap params = new HashMap<>();
        params.put("blkNumber", blkNumber);
        params.put("txIndex", txIndex);
        params.put("minBlkNumber", minBlkNumber);
        params.put("maxBlkNumber", maxBlkNumber);
        params.put("separated", separated);
        params.put("pageSize", pageSize);
        params.put("containCurrent", containCurrent);
        params.put("address", address);
        txRequest.addParams(params);

        return txRequest;
    }

    @Override
    public Request getBatchTxByHash(ArrayList txHashList, int... nodeIds) {
        HashMap params = new HashMap<>();
        params.put("hashes", txHashList);

        TxRequest txRequest = new TxRequest(TX_PREFIX + "getBatchTransactions", providerManager, TxResponse.class, nodeIds);
        txRequest.addParams(params);

        return txRequest;
    }

    @Override
    // TODO not support array type now.
    public Request getBatchReceipt(ArrayList txHashList, int... nodeIds) {
        HashMap params = new HashMap<>();
        params.put("hashes", txHashList);

        ReceiptRequest receiptRequest = new ReceiptRequest(TX_PREFIX + "getBatchReceipt", providerManager, ReceiptResponse.class, nodeIds);
        receiptRequest.addParams(params);

        return receiptRequest;
    }


    @Override
    public Request getTxsCountByTime(BigInteger startTime, BigInteger endTime, int... nodeIds) {
        HashMap params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);

        TxRequest txRequest = new TxRequest(TX_PREFIX + "getTransactionsCountByTime", providerManager, TxCountResponse.class, nodeIds);
        txRequest.addParams(params);

        return txRequest;
    }

    @Override
    public Request sendTx(Transaction transaction, int... nodeIds) {
        SendTxRequest sendTxRequest = new SendTxRequest(TX_PREFIX + "sendTransaction", providerManager, TxHashResponse.class, nodeIds);
        sendTxRequest.addParams(transaction.commonParamMap());
        return sendTxRequest;
    }

    @Override
    public Request sendBatchTxs(ArrayList transactions, ArrayList methods, int... nodeIds) {
        ArrayList requests = new ArrayList<>();
        TxService txService = ServiceManager.getTxService(providerManager);
        ContractService contractService = ServiceManager.getContractService(providerManager);
        Request request = null;

        for (int i = 0; i < transactions.size(); i++) {
            switch (MethodType.methodType(methods.get(i))) {
                case SEND_TRANSACTION:
                    request = txService.sendTx(transactions.get(i), nodeIds);
                    break;
                case DEPLOY_CONTRACT:
                    request = contractService.deploy(transactions.get(i), nodeIds);
                    break;
                case INVOKE_CONTRACT:
                    request = contractService.invoke(transactions.get(i), nodeIds);
                    break;
                case MAINTAIN_CONTRACT:
                    request = contractService.maintain(transactions.get(i), nodeIds);
                    break;
                default:
                    throw new RuntimeException("method " + methods.get(i) + " is not supported!");
            }
            requests.add(request);
        }
        SendBatchTxsRequest batchTxsRequest = new SendBatchTxsRequest(null, providerManager, TxHashesResponse.class, requests, nodeIds);
        return batchTxsRequest;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy