Remove Accounting Chain

This commit is contained in:
Xiaomin Zhu 2020-02-07 19:53:59 +08:00
parent 4fca4bcdfb
commit 64115606a0
2 changed files with 0 additions and 571 deletions

View File

@ -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);
}
}

View File

@ -1,187 +0,0 @@
package bdledger.api;
import bdchain.api.grpc.acchain.Block;
import bdchain.api.grpc.acchain.Transaction;
import bdchain.api.grpc.common.TransactionType;
import com.google.protobuf.ByteString;
import io.grpc.Status;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DisplayName("Accounting Chain tests")
class AccountingChainClientTests {
private static final String ledger = "test";
private static final int blockNumber = 2018;
private static final String blockHashStr = "deadc0dedeadc0dedeadc0dedeadc0dedeadc0de";
private static final ByteString blockHash =
ByteString.copyFrom(Utils.hexStringToByteArray(blockHashStr));
private static final ByteString parentHash =
ByteString.copyFrom(Utils.hexStringToByteArray("babefacebabefacebabefacebabefacebabeface"));
private static final String[] witnessStrs = {
"0404040404040404040404040404040404040404",
"1313131313131313131313131313131313131313",
"5252525252525252525252525252525252525252"
};
private static final List<ByteString> witnesses =
Arrays.stream(witnessStrs)
.map(s -> ByteString.copyFrom(Utils.hexStringToByteArray(s)))
.collect(Collectors.toList());
private static final long timestamp = 2018050400000L;
private static final long size = 20180504L;
private static final ByteString transactionsRoot =
ByteString.copyFrom(Utils.hexStringToByteArray("50bada5550bada5550bada5550bada5550bada55"));
private static final int index = 0;
private static final ByteString from =
ByteString.copyFrom(Utils.hexStringToByteArray("f00dcafef00dcafef00dcafef00dcafef00dcafe"));
private static final long nonce = 2018L;
private static final ByteString to =
ByteString.copyFrom(Utils.hexStringToByteArray("feedbabefeedbabefeedbabefeedbabefeedbabe"));
private static final ByteString data =
ByteString.copyFrom(Utils.hexStringToByteArray("deadbeef"));
private static final ByteString v = ByteString.copyFrom(Utils.hexStringToByteArray("25"));
private static final ByteString r =
ByteString.copyFrom(
Utils.hexStringToByteArray(
"1b5e176d927f8e9ab405058b2d2457392da3e20f328b16ddabcebc33eaac5fea"));
private static final ByteString s =
ByteString.copyFrom(
Utils.hexStringToByteArray(
"4ba69724e8f69de52f0125ad8b3c5c2cef33019bac3249e2c0a2192766d1721c"));
private static final String txHashStr = "0404040404040404040404040404040404040404";
private static final ByteString txHash1 =
ByteString.copyFrom(Utils.hexStringToByteArray(txHashStr));
private static final ByteString txHash2 =
ByteString.copyFrom(Utils.hexStringToByteArray("1313131313131313131313131313131313131313"));
private static final Block block =
Block.newBuilder()
.setNumber(blockNumber)
.setHash(blockHash)
.setParentHash(parentHash)
.addAllWitnesses(witnesses)
.setTimestamp(timestamp)
.setSize(size)
.setTransactionsRoot(transactionsRoot)
.addAllTransactionHashes(Arrays.asList(txHash1, txHash2))
.build();
private static final Transaction tx =
Transaction.newBuilder()
.setBlockNumber(blockNumber)
.setBlockHash(blockHash)
.setIndex(index)
.setHash(txHash1)
.setType(TransactionType.RECORD)
.setFrom(from)
.setNonce(nonce)
.setTo(from)
.setData(data)
.setV(v)
.setR(r)
.setS(s)
.build();
private static AccountingChainClient acClient;
@BeforeAll
static void initAll() {
acClient = new AccountingChainClient("localhost", 10001);
}
@Test
@DisplayName("ClientVersion#1")
void clientVersion1() throws InterruptedException, ExecutionException {
assertEquals(
"AcChainGo/v0.0.1alpha/darwin/go1.11", acClient.clientVersion().get().getVersion());
}
@Test
@DisplayName("BlockNumber#1")
void blockNumber1() throws InterruptedException, ExecutionException {
System.out.println(acClient.blockNumber(ledger).get().getBlockNumber());
// assertEquals(blockNumber, acClient.blockNumber(ledger).get().getBlockNumber());
}
@Test
@DisplayName("BlockNumber#2")
void blockNumber2() {
Throwable e = assertThrows(Exception.class, () -> acClient.blockNumber("").get());
Status s = Status.fromThrowable(e);
assertEquals(Status.Code.INVALID_ARGUMENT, s.getCode());
assertEquals("ledger must not be empty", s.getDescription());
}
@Test
@DisplayName("GetBlockByNumber#1")
void getBlockByNumber1() throws ExecutionException, InterruptedException {
// Block b = acClient.getBlockByNumber(ledger, blockNumber, true).get();
Block b = acClient.getBlockByNumber(ledger, blockNumber, false).get();
System.out.println(b.getNumber());
// Block blockFull =
// Block.newBuilder(block)
// .addAllTransactions(
// Arrays.asList(
// Transaction.newBuilder(tx).setIndex(0).build(),
// Transaction.newBuilder(tx)
// .setIndex(1)
// .setHash(txHash2)
// .setType(TransactionType.MESSAGE)
// .setNonce(2019L)
// .setTo(to)
// .clearData()
// .build()))
// .clearTransactionHashes()
// .build();
// assertEquals(blockFull, b);
}
@Test
@DisplayName("GetBlockByNumber#2")
void GetBlockByNumber2() throws ExecutionException, InterruptedException {
Block b = acClient.getBlockByNumber(ledger, blockNumber, false).get();
assertEquals(block, b);
}
@Test
@DisplayName("GetBlockByHash#1")
void getBlockByHash1() throws ExecutionException, InterruptedException {
Block b = acClient.getBlockByHash(ledger, blockHashStr, false).get();
assertEquals(block, b);
}
@Test
@DisplayName("GetTransactionByHash#1")
void getTransactionByHash1() throws ExecutionException, InterruptedException {
Transaction t = acClient.getTransactionByHash(ledger, txHashStr).get();
assertEquals(tx, t);
}
@Test
@DisplayName("GetTransactionByBlockNumberAndIndex#1")
void GetTransactionByBlockNumberAndIndex1() throws ExecutionException, InterruptedException {
Transaction t = acClient.getTransactionByBlockNumberAndIndex(ledger, blockNumber, index).get();
assertEquals(tx, t);
}
@Test
@DisplayName("GetTransactionByBlockHashAndIndex#1")
void GetTransactionByBlockHashAndIndex1() throws ExecutionException, InterruptedException {
Transaction t = acClient.getTransactionByBlockHashAndIndex(ledger, blockHashStr, index).get();
assertEquals(tx, t);
}
@AfterAll
static void teadDwonAll() throws InterruptedException {
acClient.shutdown();
}
}