@Override public void broadcastBlock(Proto.Block request, StreamObserver<NetProto.Empty> responseObserver) { long id = ByteUtil.byteArrayToLong( request.getHeader().getIndex().toByteArray()); BlockHusk block = new BlockHusk(request); log.debug("Received block id=[{}], hash={}", id, block.getHash()); blockChainConsumer.broadcastBlock(block); responseObserver.onNext(EMPTY); responseObserver.onCompleted(); }
@Override public void broadcastTransaction(Proto.Transaction request, StreamObserver<NetProto.Empty> responseObserver) { TransactionHusk tx = new TransactionHusk(request); log.debug("Received transaction: hash={}", tx.getHash()); blockChainConsumer.broadcastTransaction(tx); responseObserver.onNext(EMPTY); responseObserver.onCompleted(); } }
/** * Sync block response * * @param syncLimit the start branch id, block index and limit to sync * @param responseObserver the observer response to the block list */ @Override public void syncBlock(NetProto.SyncLimit syncLimit, StreamObserver<Proto.BlockList> responseObserver) { BranchId branchId = BranchId.of(syncLimit.getBranch().toByteArray()); long offset = syncLimit.getOffset(); long limit = syncLimit.getLimit(); log.debug("Received syncBlock request branch={} offset={}, limit={}", branchId, offset, limit); List<BlockHusk> blockList = blockChainConsumer.syncBlock(branchId, offset, limit); Proto.BlockList.Builder builder = Proto.BlockList.newBuilder(); for (BlockHusk block : blockList) { builder.addBlocks(block.getInstance()); } responseObserver.onNext(builder.build()); responseObserver.onCompleted(); }
/** * Sync transaction response * * @param syncLimit the branch id to sync * @param responseObserver the observer response to the transaction list */ @Override public void syncTransaction(NetProto.SyncLimit syncLimit, StreamObserver<Proto.TransactionList> responseObserver) { log.debug("Received syncTransaction request"); BranchId branchId = BranchId.of(syncLimit.getBranch().toByteArray()); List<TransactionHusk> txList = blockChainConsumer.syncTransaction(branchId); Proto.TransactionList.Builder builder = Proto.TransactionList.newBuilder(); for (TransactionHusk husk : txList) { builder.addTransactions(husk.getInstance()); } responseObserver.onNext(builder.build()); responseObserver.onCompleted(); }
@Test public void syncBlock() { Set<BlockHusk> blocks = new HashSet<>(); blocks.add(block); when(blockChainConsumerMock.syncBlock(branchId, 0L, 100L)) .thenReturn(Collections.singletonList(block)); BlockChainGrpc.BlockChainBlockingStub blockingStub = BlockChainGrpc.newBlockingStub(grpcServerRule.getChannel()); ByteString branch = ByteString.copyFrom(branchId.getBytes()); NetProto.SyncLimit syncLimit = NetProto.SyncLimit.newBuilder().setOffset(0).setLimit(100) .setBranch(branch).build(); Proto.BlockList list = blockingStub.syncBlock(syncLimit); assertEquals(1, list.getBlocksCount()); }
@Test public void syncTransaction() { when(blockChainConsumerMock.syncTransaction(branchId)) .thenReturn(Collections.singletonList(tx)); BlockChainGrpc.BlockChainBlockingStub blockingStub = BlockChainGrpc.newBlockingStub(grpcServerRule.getChannel()); ByteString branch = ByteString.copyFrom(branchId.getBytes()); NetProto.SyncLimit syncLimit = NetProto.SyncLimit.newBuilder().setBranch(branch).build(); Proto.TransactionList list = blockingStub.syncTransaction(syncLimit); assertEquals(1, list.getTransactionsCount()); } }