diff --git a/src/main/java/bdchain/api/AccountingChainClient.java b/src/main/java/bdchain/api/AccountingChainClient.java
index c8844f4..d7a5cf0 100644
--- a/src/main/java/bdchain/api/AccountingChainClient.java
+++ b/src/main/java/bdchain/api/AccountingChainClient.java
@@ -95,10 +95,8 @@ public class AccountingChainClient {
info("*** blockNumber: ledger={0}", ledger);
- BlockNumberRequest request = BlockNumberRequest.newBuilder().setLedger(ledger).build();
-
try {
- return futureStub.blockNumber(request);
+ return futureStub.blockNumber(blockNumberRequest(ledger));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
@@ -114,16 +112,18 @@ public class AccountingChainClient {
info("*** blockNumberSync: ledger={0}", ledger);
- BlockNumberRequest request = BlockNumberRequest.newBuilder().setLedger(ledger).build();
-
try {
- return blockingStub.blockNumber(request);
+ 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();
+ }
+
/**
* 返回区块号所指定区块的信息
@@ -136,15 +136,8 @@ public class AccountingChainClient {
"*** 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);
+ return futureStub.getBlockByNumber(getBlockByNumberRequest(ledger, number, fullTransaction));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
@@ -162,21 +155,24 @@ public class AccountingChainClient {
"*** 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);
+ 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();
+ }
+
/**
* 返回哈希所指定区块的信息
@@ -189,15 +185,8 @@ public class AccountingChainClient {
"*** 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);
+ return futureStub.getBlockByHash(getBlockByHashRequest(ledger, hash, fullTransaction));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
@@ -215,21 +204,26 @@ public class AccountingChainClient {
"*** 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);
+ 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();
+ }
+
/**
* 返回哈希所指定事务的信息
@@ -239,14 +233,8 @@ public class AccountingChainClient {
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);
+ return futureStub.getTransactionByHash(getTransactionByHashRequest(ledger, hash));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
@@ -262,20 +250,25 @@ public class AccountingChainClient {
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);
+ 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();
+ }
+
/**
* 返回区块号与事务index所指定事务的信息
@@ -288,15 +281,9 @@ public class AccountingChainClient {
"*** 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);
+ return futureStub.getTransactionByBlockNumberAndIndex(
+ getTransactionByBlockNumberAndIndexRequest(ledger, block_number, index));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
@@ -315,21 +302,24 @@ public class AccountingChainClient {
"*** 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);
+ 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();
+ }
+
/**
* 返回区块的哈希与事务的index所指定事务的信息
@@ -342,15 +332,9 @@ public class AccountingChainClient {
"*** 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);
+ return futureStub.getTransactionByBlockHashAndIndex(
+ getTransactionByBlockHashAndIndexRequest(ledger, block_hash, index));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
@@ -369,21 +353,28 @@ public class AccountingChainClient {
"*** 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);
+ 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);
}
diff --git a/src/main/java/bdchain/api/TransactionLedgerClient.java b/src/main/java/bdchain/api/TransactionLedgerClient.java
index 1432ae9..da14de5 100644
--- a/src/main/java/bdchain/api/TransactionLedgerClient.java
+++ b/src/main/java/bdchain/api/TransactionLedgerClient.java
@@ -93,10 +93,8 @@ public class TransactionLedgerClient {
info("*** createLedger: name={0}", name);
- CreateLedgerRequest request = CreateLedgerRequest.newBuilder().setName(name).build();
-
try {
- return futureStub.createLedger(request);
+ return futureStub.createLedger(createLedgerRequest(name));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
@@ -111,16 +109,18 @@ public class TransactionLedgerClient {
info("*** createLedgerSync: name={0}", name);
- CreateLedgerRequest request = CreateLedgerRequest.newBuilder().setName(name).build();
-
try {
- return blockingStub.createLedger(request);
+ return blockingStub.createLedger(createLedgerRequest(name));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
+ private CreateLedgerRequest createLedgerRequest(String name) {
+ return CreateLedgerRequest.newBuilder().setName(name).build();
+ }
+
/**
* 返回账本列表
* (非阻塞)
@@ -165,19 +165,8 @@ public class TransactionLedgerClient {
"*** sendTransaction: ledger={0} type={1} from={2} to={3} data={4}",
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 {
- return futureStub.sendTransaction(request);
+ return futureStub.sendTransaction(SendTransactionRequest(ledger, type, from, to, data));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
@@ -196,25 +185,35 @@ public class TransactionLedgerClient {
"*** sendTransactionSync: ledger={0} type={1} from={2} to={3} data={4}",
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 {
- return blockingStub.sendTransaction(request);
+ return blockingStub.sendTransaction(SendTransactionRequest(ledger, type, from, to, data));
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
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) {
logger.log(Level.INFO, msg, params);
}
diff --git a/src/main/java/bdchain/api/Utils.java b/src/main/java/bdchain/api/Utils.java
index a800415..11a38a3 100644
--- a/src/main/java/bdchain/api/Utils.java
+++ b/src/main/java/bdchain/api/Utils.java
@@ -1,16 +1,26 @@
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) {
+ int l = s.length();
+ byte[] data = new byte[l / 2];
+ for (int i = 0; i < l; i += 2) {
data[i / 2] =
(byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
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();
+ }
}