package bdchain.api; import bdchain.api.grpc.Block; import bdchain.api.grpc.Transaction; import bdchain.api.grpc.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.concurrent.ExecutionException; 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 = "deadc0dedeadc0dedeadc0dedeadc0dedeadc0dedeadc0dedeadc0dedeadc0de"; private static final ByteString blockHash = ByteString.copyFrom(Utils.hexStringToByteArray(blockHashStr)); private static final ByteString parentHash = ByteString.copyFrom( Utils.hexStringToByteArray( "babefacebabefacebabefacebabefacebabefacebabefacebabefacebabeface")); private static final ByteString witness = ByteString.copyFrom(Utils.hexStringToByteArray("1fee1bad1fee1bad1fee1bad1fee1bad1fee1bad")); private static final long timestamp = 2018050400000L; private static final long size = 20180504L; private static final ByteString transactionsRoot = ByteString.copyFrom( Utils.hexStringToByteArray( "50bada5550bada5550bada5550bada5550bada5550bada5550bada5550bada55")); 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 = "0404040404040404040404040404040404040404040404040404040404040404"; private static final ByteString txHash1 = ByteString.copyFrom(Utils.hexStringToByteArray(txHashStr)); private static final ByteString txHash2 = ByteString.copyFrom( Utils.hexStringToByteArray( "1313131313131313131313131313131313131313131313131313131313131313")); private static final Block block = Block.newBuilder() .setNumber(blockNumber) .setHash(blockHash) .setParentHash(parentHash) .setWitness(witness) .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 { 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 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(); } }