291 lines
9.1 KiB
Java
291 lines
9.1 KiB
Java
package bdchain.api;
|
|
|
|
import bdchain.api.grpc.*;
|
|
import bdchain.api.grpc.AccountingChainGrpc.AccountingChainBlockingStub;
|
|
import bdchain.api.grpc.AccountingChainGrpc.AccountingChainFutureStub;
|
|
import com.google.common.util.concurrent.ListenableFuture;
|
|
import com.google.protobuf.ByteString;
|
|
import io.grpc.ManagedChannel;
|
|
import io.grpc.ManagedChannelBuilder;
|
|
import io.grpc.StatusRuntimeException;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
// import bdchain.api.grpc.AccountingChainGrpc.AccountingChainStub;
|
|
|
|
public class AccountingChainClient {
|
|
|
|
private static final Logger logger = Logger.getLogger(AccountingChainClient.class.getName());
|
|
|
|
private final ManagedChannel channel;
|
|
private final AccountingChainFutureStub futureStub;
|
|
private final AccountingChainBlockingStub blockingStub;
|
|
// private final AccountingChainStub asyncStub;
|
|
|
|
/** Construct client for accessing AccountingChain server at {@code host:port}. */
|
|
public AccountingChainClient(String host, int port) {
|
|
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
|
|
}
|
|
|
|
/** Construct client for accessing AccountingChain server using the existing channel. */
|
|
public AccountingChainClient(ManagedChannelBuilder<?> channelBuilder) {
|
|
channel = channelBuilder.build();
|
|
AccountingChainGrpc.newFutureStub(channel);
|
|
futureStub = AccountingChainGrpc.newFutureStub(channel);
|
|
blockingStub = AccountingChainGrpc.newBlockingStub(channel);
|
|
// asyncStub = AccountingChainGrpc.newStub(channel);
|
|
}
|
|
|
|
public void shutdown() throws InterruptedException {
|
|
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
|
|
}
|
|
|
|
public ListenableFuture<BlockNumberResponse> blockNumber(String ledger) {
|
|
|
|
info("*** blockNumber: ledger={0}", ledger);
|
|
|
|
BlockNumberRequest request = BlockNumberRequest.newBuilder().setLedger(ledger).build();
|
|
|
|
try {
|
|
return futureStub.blockNumber(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public BlockNumberResponse blockNumberSync(String ledger) {
|
|
|
|
info("*** blockNumberSync: ledger={0}", ledger);
|
|
|
|
BlockNumberRequest request = BlockNumberRequest.newBuilder().setLedger(ledger).build();
|
|
|
|
try {
|
|
return blockingStub.blockNumber(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public ListenableFuture<Block> getBlockByNumber(
|
|
String ledger, long number, boolean fullTransaction) {
|
|
|
|
info(
|
|
"*** getBlockByNumber: ledger={0} number={1} fullTransaction={2}",
|
|
ledger, number, fullTransaction);
|
|
|
|
GetBlockByNumberRequest request =
|
|
GetBlockByNumberRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setNumber(number)
|
|
.setFullTransaction(fullTransaction)
|
|
.build();
|
|
|
|
try {
|
|
return futureStub.getBlockByNumber(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Block getBlockByNumberSync(String ledger, long number, boolean fullTransaction) {
|
|
|
|
info(
|
|
"*** getBlockByNumberSync: ledger={0} number={1} fullTransaction={2}",
|
|
ledger, number, fullTransaction);
|
|
|
|
GetBlockByNumberRequest request =
|
|
GetBlockByNumberRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setNumber(number)
|
|
.setFullTransaction(fullTransaction)
|
|
.build();
|
|
|
|
try {
|
|
return blockingStub.getBlockByNumber(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public ListenableFuture<Block> getBlockByHash(
|
|
String ledger, String hash, boolean fullTransaction) {
|
|
|
|
info(
|
|
"*** getBlockByHash: ledger={0} hash={1} fullTransaction={2}",
|
|
ledger, hash, fullTransaction);
|
|
|
|
GetBlockByHashRequest request =
|
|
GetBlockByHashRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
|
|
.setFullTransaction(fullTransaction)
|
|
.build();
|
|
|
|
try {
|
|
return futureStub.getBlockByHash(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Block getBlockByHashSync(String ledger, String hash, boolean fullTransaction) {
|
|
|
|
info(
|
|
"*** getBlockByHashSync: ledger={0} hash={1} fullTransaction={2}",
|
|
ledger, hash, fullTransaction);
|
|
|
|
GetBlockByHashRequest request =
|
|
GetBlockByHashRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
|
|
.setFullTransaction(fullTransaction)
|
|
.build();
|
|
|
|
try {
|
|
return blockingStub.getBlockByHash(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public ListenableFuture<Block> getTransactionByHash(String ledger, String hash) {
|
|
|
|
info("*** getTransactionByHash: ledger={0} hash={1}", ledger, hash);
|
|
|
|
GetTransactionByHashRequest request =
|
|
GetTransactionByHashRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
|
|
.build();
|
|
|
|
try {
|
|
return futureStub.getTransactionByHash(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Block getTransactionByHashSync(String ledger, String hash) {
|
|
|
|
info("*** getTransactionByHashSync: ledger={0} hash={1}", ledger, hash);
|
|
|
|
GetTransactionByHashRequest request =
|
|
GetTransactionByHashRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
|
|
.build();
|
|
|
|
try {
|
|
return blockingStub.getTransactionByHash(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public ListenableFuture<Transaction> getTransactionByBlockNumberAndIndex(
|
|
String ledger, long block_number, int index) {
|
|
|
|
info(
|
|
"*** getTransactionByBlockNumberAndIndex: ledger={0} block_number={1} index={2}",
|
|
ledger, block_number, index);
|
|
|
|
GetTransactionByBlockNumberAndIndexRequest request =
|
|
GetTransactionByBlockNumberAndIndexRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setBlockNumber(block_number)
|
|
.setIndex(index)
|
|
.build();
|
|
|
|
try {
|
|
return futureStub.getTransactionByBlockNumberAndIndex(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Transaction getTransactionByBlockNumberAndIndexSync(
|
|
String ledger, long block_number, int index) {
|
|
|
|
info(
|
|
"*** getTransactionByBlockNumberAndIndexSync: ledger={0} block_number={1} index={2}",
|
|
ledger, block_number, index);
|
|
|
|
GetTransactionByBlockNumberAndIndexRequest request =
|
|
GetTransactionByBlockNumberAndIndexRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setBlockNumber(block_number)
|
|
.setIndex(index)
|
|
.build();
|
|
|
|
try {
|
|
return blockingStub.getTransactionByBlockNumberAndIndex(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public ListenableFuture<Transaction> getTransactionByBlockHashAndIndex(
|
|
String ledger, String block_hash, int index) {
|
|
|
|
info(
|
|
"*** getTransactionByBlockHashAndIndex: ledger={0} block_hash={1} index={2}",
|
|
ledger, block_hash, index);
|
|
|
|
GetTransactionByBlockHashAndIndexRequest request =
|
|
GetTransactionByBlockHashAndIndexRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setBlockHash(ByteString.copyFrom(Utils.hexStringToByteArray(block_hash)))
|
|
.setIndex(index)
|
|
.build();
|
|
|
|
try {
|
|
return futureStub.getTransactionByBlockHashAndIndex(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Transaction getTransactionByBlockHashAndIndexSync(
|
|
String ledger, String block_hash, int index) {
|
|
|
|
info(
|
|
"*** getTransactionByBlockHashAndIndexSync: ledger={0} block_hash={1} index={2}",
|
|
ledger, block_hash, index);
|
|
|
|
GetTransactionByBlockHashAndIndexRequest request =
|
|
GetTransactionByBlockHashAndIndexRequest.newBuilder()
|
|
.setLedger(ledger)
|
|
.setBlockHash(ByteString.copyFrom(Utils.hexStringToByteArray(block_hash)))
|
|
.setIndex(index)
|
|
.build();
|
|
|
|
try {
|
|
return blockingStub.getTransactionByBlockHashAndIndex(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private void info(String msg, Object... params) {
|
|
logger.log(Level.INFO, msg, params);
|
|
}
|
|
|
|
private void warning(String msg, Object... params) {
|
|
logger.log(Level.WARNING, msg, params);
|
|
}
|
|
}
|