Update clients according to API updates

This commit is contained in:
Nex 2018-11-28 16:44:08 +08:00
parent cd1471d1ed
commit 4d5f392539
3 changed files with 117 additions and 117 deletions

View File

@ -95,10 +95,8 @@ public class AccountingChainClient {
info("*** blockNumber: ledger={0}", ledger); info("*** blockNumber: ledger={0}", ledger);
BlockNumberRequest request = BlockNumberRequest.newBuilder().setLedger(ledger).build();
try { try {
return futureStub.blockNumber(request); return futureStub.blockNumber(blockNumberRequest(ledger));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -114,16 +112,18 @@ public class AccountingChainClient {
info("*** blockNumberSync: ledger={0}", ledger); info("*** blockNumberSync: ledger={0}", ledger);
BlockNumberRequest request = BlockNumberRequest.newBuilder().setLedger(ledger).build();
try { try {
return blockingStub.blockNumber(request); return blockingStub.blockNumber(blockNumberRequest(ledger));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
} }
} }
private BlockNumberRequest blockNumberRequest(String ledger) {
return BlockNumberRequest.newBuilder().setLedger(ledger).build();
}
/** /**
* <a * <a
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getblockbynumber">返回区块号所指定区块的信息</a> * href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getblockbynumber">返回区块号所指定区块的信息</a>
@ -136,15 +136,8 @@ public class AccountingChainClient {
"*** getBlockByNumber: ledger={0} number={1} fullTransaction={2}", "*** getBlockByNumber: ledger={0} number={1} fullTransaction={2}",
ledger, number, fullTransaction); ledger, number, fullTransaction);
GetBlockByNumberRequest request =
GetBlockByNumberRequest.newBuilder()
.setLedger(ledger)
.setNumber(number)
.setFullTransaction(fullTransaction)
.build();
try { try {
return futureStub.getBlockByNumber(request); return futureStub.getBlockByNumber(getBlockByNumberRequest(ledger, number, fullTransaction));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -162,21 +155,24 @@ public class AccountingChainClient {
"*** getBlockByNumberSync: ledger={0} number={1} fullTransaction={2}", "*** getBlockByNumberSync: ledger={0} number={1} fullTransaction={2}",
ledger, number, fullTransaction); ledger, number, fullTransaction);
GetBlockByNumberRequest request =
GetBlockByNumberRequest.newBuilder()
.setLedger(ledger)
.setNumber(number)
.setFullTransaction(fullTransaction)
.build();
try { try {
return blockingStub.getBlockByNumber(request); return blockingStub.getBlockByNumber(
getBlockByNumberRequest(ledger, number, fullTransaction));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
} }
} }
private GetBlockByNumberRequest getBlockByNumberRequest(
String ledger, long number, boolean fullTransaction) {
return GetBlockByNumberRequest.newBuilder()
.setLedger(ledger)
.setNumber(number)
.setFullTransaction(fullTransaction)
.build();
}
/** /**
* <a * <a
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getblockbyhash">返回哈希所指定区块的信息</a> * href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getblockbyhash">返回哈希所指定区块的信息</a>
@ -189,15 +185,8 @@ public class AccountingChainClient {
"*** getBlockByHash: ledger={0} hash={1} fullTransaction={2}", "*** getBlockByHash: ledger={0} hash={1} fullTransaction={2}",
ledger, hash, fullTransaction); ledger, hash, fullTransaction);
GetBlockByHashRequest request =
GetBlockByHashRequest.newBuilder()
.setLedger(ledger)
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
.setFullTransaction(fullTransaction)
.build();
try { try {
return futureStub.getBlockByHash(request); return futureStub.getBlockByHash(getBlockByHashRequest(ledger, hash, fullTransaction));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -215,21 +204,26 @@ public class AccountingChainClient {
"*** getBlockByHashSync: ledger={0} hash={1} fullTransaction={2}", "*** getBlockByHashSync: ledger={0} hash={1} fullTransaction={2}",
ledger, hash, fullTransaction); ledger, hash, fullTransaction);
GetBlockByHashRequest request =
GetBlockByHashRequest.newBuilder()
.setLedger(ledger)
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
.setFullTransaction(fullTransaction)
.build();
try { try {
return blockingStub.getBlockByHash(request); return blockingStub.getBlockByHash(getBlockByHashRequest(ledger, hash, fullTransaction));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; 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 * <a
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyhash">返回哈希所指定事务的信息</a> * href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyhash">返回哈希所指定事务的信息</a>
@ -239,14 +233,8 @@ public class AccountingChainClient {
info("*** getTransactionByHash: ledger={0} hash={1}", ledger, hash); info("*** getTransactionByHash: ledger={0} hash={1}", ledger, hash);
GetTransactionByHashRequest request =
GetTransactionByHashRequest.newBuilder()
.setLedger(ledger)
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
.build();
try { try {
return futureStub.getTransactionByHash(request); return futureStub.getTransactionByHash(getTransactionByHashRequest(ledger, hash));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -262,20 +250,25 @@ public class AccountingChainClient {
info("*** getTransactionByHashSync: ledger={0} hash={1}", ledger, hash); info("*** getTransactionByHashSync: ledger={0} hash={1}", ledger, hash);
GetTransactionByHashRequest request =
GetTransactionByHashRequest.newBuilder()
.setLedger(ledger)
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
.build();
try { try {
return blockingStub.getTransactionByHash(request); return blockingStub.getTransactionByHash(getTransactionByHashRequest(ledger, hash));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; 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 * <a
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyblocknum">返回区块号与事务index所指定事务的信息</a> * href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyblocknum">返回区块号与事务index所指定事务的信息</a>
@ -288,15 +281,9 @@ public class AccountingChainClient {
"*** getTransactionByBlockNumberAndIndex: ledger={0} block_number={1} index={2}", "*** getTransactionByBlockNumberAndIndex: ledger={0} block_number={1} index={2}",
ledger, block_number, index); ledger, block_number, index);
GetTransactionByBlockNumberAndIndexRequest request =
GetTransactionByBlockNumberAndIndexRequest.newBuilder()
.setLedger(ledger)
.setBlockNumber(block_number)
.setIndex(index)
.build();
try { try {
return futureStub.getTransactionByBlockNumberAndIndex(request); return futureStub.getTransactionByBlockNumberAndIndex(
getTransactionByBlockNumberAndIndexRequest(ledger, block_number, index));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -315,21 +302,24 @@ public class AccountingChainClient {
"*** getTransactionByBlockNumberAndIndexSync: ledger={0} block_number={1} index={2}", "*** getTransactionByBlockNumberAndIndexSync: ledger={0} block_number={1} index={2}",
ledger, block_number, index); ledger, block_number, index);
GetTransactionByBlockNumberAndIndexRequest request =
GetTransactionByBlockNumberAndIndexRequest.newBuilder()
.setLedger(ledger)
.setBlockNumber(block_number)
.setIndex(index)
.build();
try { try {
return blockingStub.getTransactionByBlockNumberAndIndex(request); return blockingStub.getTransactionByBlockNumberAndIndex(
getTransactionByBlockNumberAndIndexRequest(ledger, block_number, index));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
} }
} }
private GetTransactionByBlockNumberAndIndexRequest getTransactionByBlockNumberAndIndexRequest(
String ledger, long block_number, int index) {
return GetTransactionByBlockNumberAndIndexRequest.newBuilder()
.setLedger(ledger)
.setBlockNumber(block_number)
.setIndex(index)
.build();
}
/** /**
* <a * <a
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyblockhas">返回区块的哈希与事务的index所指定事务的信息</a> * href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#gettransactionbyblockhas">返回区块的哈希与事务的index所指定事务的信息</a>
@ -342,15 +332,9 @@ public class AccountingChainClient {
"*** getTransactionByBlockHashAndIndex: ledger={0} block_hash={1} index={2}", "*** getTransactionByBlockHashAndIndex: ledger={0} block_hash={1} index={2}",
ledger, block_hash, index); ledger, block_hash, index);
GetTransactionByBlockHashAndIndexRequest request =
GetTransactionByBlockHashAndIndexRequest.newBuilder()
.setLedger(ledger)
.setBlockHash(ByteString.copyFrom(Utils.hexStringToByteArray(block_hash)))
.setIndex(index)
.build();
try { try {
return futureStub.getTransactionByBlockHashAndIndex(request); return futureStub.getTransactionByBlockHashAndIndex(
getTransactionByBlockHashAndIndexRequest(ledger, block_hash, index));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -369,21 +353,28 @@ public class AccountingChainClient {
"*** getTransactionByBlockHashAndIndexSync: ledger={0} block_hash={1} index={2}", "*** getTransactionByBlockHashAndIndexSync: ledger={0} block_hash={1} index={2}",
ledger, block_hash, index); ledger, block_hash, index);
GetTransactionByBlockHashAndIndexRequest request =
GetTransactionByBlockHashAndIndexRequest.newBuilder()
.setLedger(ledger)
.setBlockHash(ByteString.copyFrom(Utils.hexStringToByteArray(block_hash)))
.setIndex(index)
.build();
try { try {
return blockingStub.getTransactionByBlockHashAndIndex(request); return blockingStub.getTransactionByBlockHashAndIndex(
getTransactionByBlockHashAndIndexRequest(ledger, block_hash, index));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; 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) { private void info(String msg, Object... params) {
logger.log(Level.INFO, msg, params); logger.log(Level.INFO, msg, params);
} }

View File

@ -93,10 +93,8 @@ public class TransactionLedgerClient {
info("*** createLedger: name={0}", name); info("*** createLedger: name={0}", name);
CreateLedgerRequest request = CreateLedgerRequest.newBuilder().setName(name).build();
try { try {
return futureStub.createLedger(request); return futureStub.createLedger(createLedgerRequest(name));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -111,16 +109,18 @@ public class TransactionLedgerClient {
info("*** createLedgerSync: name={0}", name); info("*** createLedgerSync: name={0}", name);
CreateLedgerRequest request = CreateLedgerRequest.newBuilder().setName(name).build();
try { try {
return blockingStub.createLedger(request); return blockingStub.createLedger(createLedgerRequest(name));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
} }
} }
private CreateLedgerRequest createLedgerRequest(String name) {
return CreateLedgerRequest.newBuilder().setName(name).build();
}
/** /**
* <a href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getledgers">返回账本列表</a> * <a href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getledgers">返回账本列表</a>
* 非阻塞 * 非阻塞
@ -165,19 +165,8 @@ public class TransactionLedgerClient {
"*** sendTransaction: ledger={0} type={1} from={2} to={3} data={4}", "*** sendTransaction: ledger={0} type={1} from={2} to={3} data={4}",
ledger, type, from, to, data); ledger, type, from, to, data);
SendTransactionRequest request =
SendTransactionRequest.newBuilder()
.setLedger(ledger)
.setTransaction(
SendTransactionRequest.Transaction.newBuilder()
.setType(type)
.setFrom(ByteString.copyFrom(Utils.hexStringToByteArray(from)))
.setTo(ByteString.copyFrom(Utils.hexStringToByteArray(from)))
.setData(ByteString.copyFrom(data)))
.build();
try { try {
return futureStub.sendTransaction(request); return futureStub.sendTransaction(SendTransactionRequest(ledger, type, from, to, data));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
@ -196,25 +185,35 @@ public class TransactionLedgerClient {
"*** sendTransactionSync: ledger={0} type={1} from={2} to={3} data={4}", "*** sendTransactionSync: ledger={0} type={1} from={2} to={3} data={4}",
ledger, type, from, to, data); ledger, type, from, to, data);
SendTransactionRequest request =
SendTransactionRequest.newBuilder()
.setLedger(ledger)
.setTransaction(
SendTransactionRequest.Transaction.newBuilder()
.setType(type)
.setFrom(ByteString.copyFrom(Utils.hexStringToByteArray(from)))
.setTo(ByteString.copyFrom(Utils.hexStringToByteArray(from)))
.setData(ByteString.copyFrom(data)))
.build();
try { try {
return blockingStub.sendTransaction(request); return blockingStub.sendTransaction(SendTransactionRequest(ledger, type, from, to, data));
} catch (StatusRuntimeException e) { } catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus()); warning("RPC failed: {0}", e.getStatus());
return null; return null;
} }
} }
private SendTransactionRequest SendTransactionRequest(
String ledger, TransactionType type, String from, String to, byte[] data) {
SendTransactionRequest.Transaction.Builder txBuilder =
SendTransactionRequest.Transaction.newBuilder().setType(type);
if (from != null) {
txBuilder.setFrom(ByteString.copyFrom(Utils.hexStringToByteArray(from)));
}
if (to != null) {
txBuilder.setTo(ByteString.copyFrom(Utils.hexStringToByteArray(to)));
}
if (data != null) {
txBuilder.setData(ByteString.copyFrom(data));
}
SendTransactionRequest request =
SendTransactionRequest.newBuilder().setLedger(ledger).setTransaction(txBuilder).build();
return request;
}
private void info(String msg, Object... params) { private void info(String msg, Object... params) {
logger.log(Level.INFO, msg, params); logger.log(Level.INFO, msg, params);
} }

View File

@ -1,16 +1,26 @@
package bdchain.api; package bdchain.api;
public class Utils { public class Utils {
public static byte[] hexStringToByteArray(String s) { public static byte[] hexStringToByteArray(String s) {
if (s.startsWith("0x")) { if (s.startsWith("0x")) {
s = s.substring(2); s = s.substring(2);
} }
int len = s.length(); int l = s.length();
byte[] data = new byte[len / 2]; byte[] data = new byte[l / 2];
for (int i = 0; i < len; i += 2) { for (int i = 0; i < l; i += 2) {
data[i / 2] = data[i / 2] =
(byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
} }
return data; return data;
} }
public static String byteArrayToHexString(byte[] bs) {
StringBuilder data = new StringBuilder();
for (byte b : bs) {
data.append(Integer.toHexString((b >> 4) & 0xf));
data.append(Integer.toHexString(b & 0xf));
}
return data.toString();
}
} }