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

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

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

import cn.hyperchain.sdk.provider.ProviderManager;
import cn.hyperchain.sdk.request.BlockRequest;
import cn.hyperchain.sdk.request.Request;
import cn.hyperchain.sdk.response.block.BlockAvgTimeResponse;
import cn.hyperchain.sdk.response.block.BlockCountResponse;
import cn.hyperchain.sdk.response.block.BlockNumberResponse;
import cn.hyperchain.sdk.response.block.BlockResponse;
import cn.hyperchain.sdk.service.BlockService;

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

/**
 * block service implementation.
 *
 * @author dong
 * @date 07/04/2019
 */
public class BlockServiceImpl implements BlockService {
    private ProviderManager providerManager;
    private static final String BLOCK_PREFIX = "block_";

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

    @Override
    public Request getLatestBlock(int... nodeIds) {
        BlockRequest blockResponseBlockRequest = new BlockRequest(BLOCK_PREFIX + "latestBlock", providerManager, BlockResponse.class, nodeIds);
        return blockResponseBlockRequest;
    }


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


    @Override
    public Request getBlocks(String from, String to, int... nodeIds) {
        return getBlocks(from, to, false, nodeIds);
    }


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


    @Override
    public Request getBlocks(String from, String to, boolean isPlain, int... nodeIds) {
        BlockRequest blockRequest = new BlockRequest(BLOCK_PREFIX + "getBlocks", providerManager, BlockResponse.class, nodeIds);
        HashMap params = new HashMap<>();
        params.put("from", from);
        params.put("to", to);
        params.put("isPlain", isPlain);
        blockRequest.addParams(params);

        return blockRequest;
    }


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


    @Override
    public Request getBlockByHash(String blockHash, boolean isPlain, int... nodeIds) {
        BlockRequest blockResponseBlockRequest = new BlockRequest(BLOCK_PREFIX + "getBlockByHash", providerManager, BlockResponse.class, nodeIds);

        blockResponseBlockRequest.addParams(blockHash);
        blockResponseBlockRequest.addParams(isPlain);

        return blockResponseBlockRequest;
    }


    @Override
    public Request getBatchBlocksByHash(ArrayList blockHashList, int... nodeIds) {
        return getBatchBlocksByHash(blockHashList, false);
    }


    @Override
    public Request getBatchBlocksByHash(ArrayList blockHashList, boolean isPlain, int... nodeIds) {
        BlockRequest blockRequest = new BlockRequest(BLOCK_PREFIX + "getBatchBlocksByHash", providerManager, BlockResponse.class, nodeIds);
        HashMap params = new HashMap<>();
        params.put("hashes", blockHashList);
        params.put("isPlain", isPlain);
        blockRequest.addParams(params);

        return blockRequest;
    }


    @Override
    public Request getBlockByNum(BigInteger blockNumber, int... nodeIds) {
        return getBlockByNum(blockNumber.toString());
    }


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


    @Override
    public Request getBlockByNum(BigInteger blockNumber, boolean isPlain, int... nodeIds) {
        return getBlockByNum(blockNumber.toString(), isPlain, nodeIds);
    }

    /**
     * get block based on block number.
     *
     * @param blockNumber block number
     * @param isPlain     default false, indicating that the returned block includes transaction information within the block.
     *                    If specified, the returned block does not include transactions within the block
     * @param nodeIds     specific ids
     * @return {@link Request} of {@link BlockResponse}
     */
    @Override
    public Request getBlockByNum(String blockNumber, boolean isPlain, int... nodeIds) {
        BlockRequest blockRequest = new BlockRequest(BLOCK_PREFIX + "getBlockByNumber", providerManager, BlockResponse.class, nodeIds);
        blockRequest.addParams(blockNumber);
        blockRequest.addParams(isPlain);
        return blockRequest;
    }


    @Override
    public Request getBatchBlocksByNum(ArrayList blockNumberList, int... nodeIds) {
        return getBatchBlocksByNum(blockNumberList, false, nodeIds);
    }


    @Override
    public Request getBatchBlocksByNum(ArrayList blockNumberList, boolean isPlain, int... nodeIds) {
        BlockRequest blockResponseBlockRequest = new BlockRequest(BLOCK_PREFIX + "getBatchBlocksByNumber", providerManager, BlockResponse.class, nodeIds);
        HashMap params = new HashMap<>();
        params.put("numbers", blockNumberList);
        params.put("isPlain", isPlain);
        blockResponseBlockRequest.addParams(params);
        return blockResponseBlockRequest;
    }


    @Override
    public Request getBatchBlocksByStrNum(ArrayList blockNumberList, int... nodeIds) {
        return getBatchBlocksByStrNum(blockNumberList, false, nodeIds);
    }


    @Override
    public Request getBatchBlocksByStrNum(ArrayList blockNumberList, boolean isPlain, int... nodeIds) {
        BlockRequest blockResponseBlockRequest = new BlockRequest(BLOCK_PREFIX + "getBatchBlocksByNumber", providerManager, BlockResponse.class, nodeIds);
        HashMap params = new HashMap<>();
        params.put("numbers", blockNumberList);
        params.put("isPlain", isPlain);
        blockResponseBlockRequest.addParams(params);
        return blockResponseBlockRequest;
    }


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


    @Override
    public Request getAvgGenerateTimeByBlockNumber(String from, String to, int... nodeIds) {
        Request blockRequest = new BlockRequest(BLOCK_PREFIX + "getAvgGenerateTimeByBlockNumber", providerManager, BlockAvgTimeResponse.class, nodeIds);
        HashMap params = new HashMap<>();
        params.put("from", from);
        params.put("to", to);
        blockRequest.addParams(params);
        return blockRequest;
    }


    @Override
    public Request getBlocksByTime(BigInteger startTime, BigInteger endTime, int... nodeIds) {
        BlockRequest blockRequest = new BlockRequest(BLOCK_PREFIX + "getBlocksByTime", providerManager, BlockCountResponse.class, nodeIds);
        HashMap params = new HashMap<>();
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        blockRequest.addParams(params);
        return blockRequest;
    }


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


    @Override
    public Request getChainHeight(int... nodeIds) {
        BlockRequest blockRequest = new BlockRequest(BLOCK_PREFIX + "getChainHeight", providerManager, BlockNumberResponse.class, nodeIds);
        return blockRequest;
    }


    @Override
    public Request getGenesisBlock(int... nodeIds) {
        BlockRequest blockRequest = new BlockRequest(BLOCK_PREFIX + "getGenesisBlock", providerManager, BlockNumberResponse.class, nodeIds);
        return blockRequest;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy