Remove Accounting Chain
This commit is contained in:
@@ -1,384 +0,0 @@
|
||||
package bdledger.api;
|
||||
|
||||
import bdchain.api.grpc.acchain.*;
|
||||
import bdchain.api.grpc.common.ClientVersionResponse;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.protobuf.ByteString;
|
||||
import com.google.protobuf.Empty;
|
||||
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.acchain.AccountingChainGrpc.AccountingChainStub;
|
||||
|
||||
/**
|
||||
* 记账链客户端
|
||||
*
|
||||
* <p>如有更灵活的需求可直接使用{@link bdchain.api.grpc.acchain.AccountingChainGrpc}类。
|
||||
*
|
||||
* @see <a href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#api-1">记账链API</a>
|
||||
* @author nex
|
||||
*/
|
||||
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;
|
||||
|
||||
/** 构造客户端来访问{@code host:port}的记账链服务。 */
|
||||
public AccountingChainClient(String host, int port) {
|
||||
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
|
||||
}
|
||||
|
||||
/** 用已有的{@link io.grpc.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);
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#clientversion-1">查询节点客户端版本</a>
|
||||
* (非阻塞)
|
||||
*/
|
||||
public ListenableFuture<ClientVersionResponse> clientVersion() {
|
||||
|
||||
info("*** clientVersion");
|
||||
|
||||
try {
|
||||
return futureStub.clientVersion(Empty.getDefaultInstance());
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#clientversion-1">查询节点客户端版本</a>
|
||||
* (阻塞)
|
||||
*/
|
||||
public ClientVersionResponse clientVersionSync() {
|
||||
|
||||
info("*** clientVersionSync");
|
||||
|
||||
try {
|
||||
return blockingStub.clientVersion(Empty.getDefaultInstance());
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#blocknumber">返回最新区块的区块号</a>
|
||||
* (非阻塞)
|
||||
*/
|
||||
public ListenableFuture<BlockNumberResponse> blockNumber(String ledger) {
|
||||
|
||||
info("*** blockNumber: ledger={0}", ledger);
|
||||
|
||||
try {
|
||||
return futureStub.blockNumber(blockNumberRequest(ledger));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#blocknumber">返回最新区块的区块号</a>
|
||||
* (阻塞)
|
||||
*/
|
||||
public BlockNumberResponse blockNumberSync(String ledger) {
|
||||
|
||||
info("*** blockNumberSync: ledger={0}", ledger);
|
||||
|
||||
try {
|
||||
return blockingStub.blockNumber(blockNumberRequest(ledger));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private BlockNumberRequest blockNumberRequest(String ledger) {
|
||||
return BlockNumberRequest.newBuilder().setLedger(ledger).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getblockbynumber">返回区块号所指定区块的信息</a>
|
||||
* (非阻塞)
|
||||
*/
|
||||
public ListenableFuture<Block> getBlockByNumber(
|
||||
String ledger, long number, boolean fullTransaction) {
|
||||
|
||||
info(
|
||||
"*** getBlockByNumber: ledger={0} number={1} fullTransaction={2}",
|
||||
ledger, number, fullTransaction);
|
||||
|
||||
try {
|
||||
return futureStub.getBlockByNumber(getBlockByNumberRequest(ledger, number, fullTransaction));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getblockbynumber">返回区块号所指定区块的信息</a>
|
||||
* (阻塞)
|
||||
*/
|
||||
public Block getBlockByNumberSync(String ledger, long number, boolean fullTransaction) {
|
||||
|
||||
info(
|
||||
"*** getBlockByNumberSync: ledger={0} number={1} fullTransaction={2}",
|
||||
ledger, number, fullTransaction);
|
||||
|
||||
try {
|
||||
return blockingStub.getBlockByNumber(
|
||||
getBlockByNumberRequest(ledger, number, fullTransaction));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private GetBlockByNumberRequest getBlockByNumberRequest(
|
||||
String ledger, long number, boolean fullTransaction) {
|
||||
return GetBlockByNumberRequest.newBuilder()
|
||||
.setLedger(ledger)
|
||||
.setNumber(number)
|
||||
.setFullTransaction(fullTransaction)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getblockbyhash">返回哈希所指定区块的信息</a>
|
||||
* (非阻塞)
|
||||
*/
|
||||
public ListenableFuture<Block> getBlockByHash(
|
||||
String ledger, String hash, boolean fullTransaction) {
|
||||
|
||||
info(
|
||||
"*** getBlockByHash: ledger={0} hash={1} fullTransaction={2}",
|
||||
ledger, hash, fullTransaction);
|
||||
|
||||
try {
|
||||
return futureStub.getBlockByHash(getBlockByHashRequest(ledger, hash, fullTransaction));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getblockbyhash">返回哈希所指定区块的信息</a>
|
||||
* (阻塞)
|
||||
*/
|
||||
public Block getBlockByHashSync(String ledger, String hash, boolean fullTransaction) {
|
||||
|
||||
info(
|
||||
"*** getBlockByHashSync: ledger={0} hash={1} fullTransaction={2}",
|
||||
ledger, hash, fullTransaction);
|
||||
|
||||
try {
|
||||
return blockingStub.getBlockByHash(getBlockByHashRequest(ledger, hash, fullTransaction));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private GetBlockByHashRequest getBlockByHashRequest(
|
||||
String ledger, String hash, boolean fullTransaction) {
|
||||
|
||||
GetBlockByHashRequest.Builder reqBuilder =
|
||||
GetBlockByHashRequest.newBuilder().setLedger(ledger).setFullTransaction(fullTransaction);
|
||||
if (hash != null) {
|
||||
reqBuilder.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)));
|
||||
}
|
||||
|
||||
return reqBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyhash">返回哈希所指定事务的信息</a>
|
||||
* (非阻塞)
|
||||
*/
|
||||
public ListenableFuture<Transaction> getTransactionByHash(String ledger, String hash) {
|
||||
|
||||
info("*** getTransactionByHash: ledger={0} hash={1}", ledger, hash);
|
||||
|
||||
try {
|
||||
return futureStub.getTransactionByHash(getTransactionByHashRequest(ledger, hash));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyhash">返回哈希所指定事务的信息</a>
|
||||
* (阻塞)
|
||||
*/
|
||||
public Transaction getTransactionByHashSync(String ledger, String hash) {
|
||||
|
||||
info("*** getTransactionByHashSync: ledger={0} hash={1}", ledger, hash);
|
||||
|
||||
try {
|
||||
return blockingStub.getTransactionByHash(getTransactionByHashRequest(ledger, hash));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private GetTransactionByHashRequest getTransactionByHashRequest(String ledger, String hash) {
|
||||
|
||||
GetTransactionByHashRequest.Builder reqBuilder =
|
||||
GetTransactionByHashRequest.newBuilder().setLedger(ledger);
|
||||
if (hash != null) {
|
||||
reqBuilder.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)));
|
||||
}
|
||||
|
||||
return reqBuilder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyblocknum">返回区块号与事务index所指定事务的信息</a>
|
||||
* (非阻塞)
|
||||
*/
|
||||
public ListenableFuture<Transaction> getTransactionByBlockNumberAndIndex(
|
||||
String ledger, long block_number, int index) {
|
||||
|
||||
info(
|
||||
"*** getTransactionByBlockNumberAndIndex: ledger={0} block_number={1} index={2}",
|
||||
ledger, block_number, index);
|
||||
|
||||
try {
|
||||
return futureStub.getTransactionByBlockNumberAndIndex(
|
||||
getTransactionByBlockNumberAndIndexRequest(ledger, block_number, index));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyblocknum">返回区块号与事务index所指定事务的信息</a>
|
||||
* (阻塞)
|
||||
*/
|
||||
public Transaction getTransactionByBlockNumberAndIndexSync(
|
||||
String ledger, long block_number, int index) {
|
||||
|
||||
info(
|
||||
"*** getTransactionByBlockNumberAndIndexSync: ledger={0} block_number={1} index={2}",
|
||||
ledger, block_number, index);
|
||||
|
||||
try {
|
||||
return blockingStub.getTransactionByBlockNumberAndIndex(
|
||||
getTransactionByBlockNumberAndIndexRequest(ledger, block_number, index));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private GetTransactionByBlockNumberAndIndexRequest getTransactionByBlockNumberAndIndexRequest(
|
||||
String ledger, long block_number, int index) {
|
||||
return GetTransactionByBlockNumberAndIndexRequest.newBuilder()
|
||||
.setLedger(ledger)
|
||||
.setBlockNumber(block_number)
|
||||
.setIndex(index)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyblockhas">返回区块的哈希与事务的index所指定事务的信息</a>
|
||||
* (非阻塞)
|
||||
*/
|
||||
public ListenableFuture<Transaction> getTransactionByBlockHashAndIndex(
|
||||
String ledger, String block_hash, int index) {
|
||||
|
||||
info(
|
||||
"*** getTransactionByBlockHashAndIndex: ledger={0} block_hash={1} index={2}",
|
||||
ledger, block_hash, index);
|
||||
|
||||
try {
|
||||
return futureStub.getTransactionByBlockHashAndIndex(
|
||||
getTransactionByBlockHashAndIndexRequest(ledger, block_hash, index));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <a
|
||||
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyblockhas">返回区块的哈希与事务的index所指定事务的信息</a>
|
||||
* (阻塞)
|
||||
*/
|
||||
public Transaction getTransactionByBlockHashAndIndexSync(
|
||||
String ledger, String block_hash, int index) {
|
||||
|
||||
info(
|
||||
"*** getTransactionByBlockHashAndIndexSync: ledger={0} block_hash={1} index={2}",
|
||||
ledger, block_hash, index);
|
||||
|
||||
try {
|
||||
return blockingStub.getTransactionByBlockHashAndIndex(
|
||||
getTransactionByBlockHashAndIndexRequest(ledger, block_hash, index));
|
||||
} catch (StatusRuntimeException e) {
|
||||
warning("RPC failed: {0}", e.getStatus());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private GetTransactionByBlockHashAndIndexRequest getTransactionByBlockHashAndIndexRequest(
|
||||
String ledger, String block_hash, int index) {
|
||||
|
||||
GetTransactionByBlockHashAndIndexRequest.Builder reqBuilder =
|
||||
GetTransactionByBlockHashAndIndexRequest.newBuilder().setLedger(ledger).setIndex(index);
|
||||
|
||||
if (block_hash != null) {
|
||||
reqBuilder.setBlockHash(ByteString.copyFrom(Utils.hexStringToByteArray(block_hash)));
|
||||
}
|
||||
|
||||
return reqBuilder.build();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user