From 64115606a0560b9078d81f9de0520e18578aa890 Mon Sep 17 00:00:00 2001 From: nex Date: Fri, 7 Feb 2020 19:53:59 +0800 Subject: [PATCH] Remove Accounting Chain --- .../bdledger/api/AccountingChainClient.java | 384 ------------------ .../api/AccountingChainClientTests.java | 187 --------- 2 files changed, 571 deletions(-) delete mode 100644 src/main/java/bdledger/api/AccountingChainClient.java delete mode 100644 src/test/java/bdledger/api/AccountingChainClientTests.java diff --git a/src/main/java/bdledger/api/AccountingChainClient.java b/src/main/java/bdledger/api/AccountingChainClient.java deleted file mode 100644 index 5f62c59..0000000 --- a/src/main/java/bdledger/api/AccountingChainClient.java +++ /dev/null @@ -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; - -/** - * 记账链客户端 - * - *

如有更灵活的需求可直接使用{@link bdchain.api.grpc.acchain.AccountingChainGrpc}类。 - * - * @see 记账链API - * @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); - } - - /** - * 查询节点客户端版本 - * (非阻塞) - */ - public ListenableFuture clientVersion() { - - info("*** clientVersion"); - - try { - return futureStub.clientVersion(Empty.getDefaultInstance()); - } catch (StatusRuntimeException e) { - warning("RPC failed: {0}", e.getStatus()); - return null; - } - } - - /** - * 查询节点客户端版本 - * (阻塞) - */ - public ClientVersionResponse clientVersionSync() { - - info("*** clientVersionSync"); - - try { - return blockingStub.clientVersion(Empty.getDefaultInstance()); - } catch (StatusRuntimeException e) { - warning("RPC failed: {0}", e.getStatus()); - return null; - } - } - - /** - * 返回最新区块的区块号 - * (非阻塞) - */ - public ListenableFuture 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; - } - } - - /** - * 返回最新区块的区块号 - * (阻塞) - */ - 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(); - } - - /** - * 返回区块号所指定区块的信息 - * (非阻塞) - */ - public ListenableFuture 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; - } - } - - /** - * 返回区块号所指定区块的信息 - * (阻塞) - */ - 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(); - } - - /** - * 返回哈希所指定区块的信息 - * (非阻塞) - */ - public ListenableFuture 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; - } - } - - /** - * 返回哈希所指定区块的信息 - * (阻塞) - */ - 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(); - } - - /** - * 返回哈希所指定事务的信息 - * (非阻塞) - */ - public ListenableFuture 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; - } - } - - /** - * 返回哈希所指定事务的信息 - * (阻塞) - */ - 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(); - } - - /** - * 返回区块号与事务index所指定事务的信息 - * (非阻塞) - */ - public ListenableFuture 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; - } - } - - /** - * 返回区块号与事务index所指定事务的信息 - * (阻塞) - */ - 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(); - } - - /** - * 返回区块的哈希与事务的index所指定事务的信息 - * (非阻塞) - */ - public ListenableFuture 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; - } - } - - /** - * 返回区块的哈希与事务的index所指定事务的信息 - * (阻塞) - */ - 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); - } -} diff --git a/src/test/java/bdledger/api/AccountingChainClientTests.java b/src/test/java/bdledger/api/AccountingChainClientTests.java deleted file mode 100644 index 802eb44..0000000 --- a/src/test/java/bdledger/api/AccountingChainClientTests.java +++ /dev/null @@ -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 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(); - } -}