Update AccountingChainClient
This commit is contained in:
parent
86bf122733
commit
bc6332c13e
165
src/main/java/bdchain/api/AccountingChainClient.java
Normal file
165
src/main/java/bdchain/api/AccountingChainClient.java
Normal file
@ -0,0 +1,165 @@
|
||||
package bdchain.api;
|
||||
|
||||
import bdchain.api.grpc.*;
|
||||
import bdchain.api.grpc.AccountingChainGrpc.AccountingChainBlockingStub;
|
||||
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 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();
|
||||
blockingStub = AccountingChainGrpc.newBlockingStub(channel);
|
||||
// asyncStub = AccountingChainGrpc.newStub(channel);
|
||||
}
|
||||
|
||||
public void shutdown() throws InterruptedException {
|
||||
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public BlockNumberResponse blockNumber(String ledger) {
|
||||
|
||||
info("*** blockNumber: 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 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 blockingStub.getBlockByNumber(request);
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public 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 blockingStub.getBlockByHash(request);
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public 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 blockingStub.getTransactionByHash(request);
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public 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 blockingStub.getTransactionByBlockNumberAndIndex(request);
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public 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 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);
|
||||
}
|
||||
}
|
16
src/main/java/bdchain/api/Utils.java
Normal file
16
src/main/java/bdchain/api/Utils.java
Normal file
@ -0,0 +1,16 @@
|
||||
package bdchain.api;
|
||||
|
||||
public class Utils {
|
||||
public static byte[] hexStringToByteArray(String s) {
|
||||
if (s.startsWith("0x")) {
|
||||
s = s.substring(2);
|
||||
}
|
||||
int len = s.length();
|
||||
byte[] data = new byte[len / 2];
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
data[i / 2] =
|
||||
(byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user