Skip to content

Commit

Permalink
feat: Add simulator as dependency in Suites (#242)
Browse files Browse the repository at this point in the history
Signed-off-by: georgi-l95 <[email protected]>
  • Loading branch information
georgi-l95 authored Oct 10, 2024
1 parent dca9566 commit 3a565f3
Show file tree
Hide file tree
Showing 21 changed files with 238 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ extraJavaModuleInfo {
"com.github.docker-java:docker-java-transport-zerodep",
"com.github.dockerjava.transport.zerodep"
)
module("org.slf4j:slf4j-api", "org.slf4j") { patchRealModule() }
module("io.github.cdimascio:java-dotenv", "io.github.cdimascio")
module("com.google.protobuf:protobuf-java-util", "com.google.protobuf.util")
module("com.squareup:javapoet", "com.squareup.javapoet") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static java.lang.System.Logger.Level.INFO;

import com.hedera.pbj.runtime.ParseException;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.swirlds.config.api.Configuration;
import com.swirlds.config.api.ConfigurationBuilder;
import com.swirlds.config.extensions.sources.ClasspathFileConfigSource;
Expand All @@ -41,10 +41,10 @@ private BlockStreamSimulator() {}
* @param args the arguments to be passed to the block stream simulator
* @throws IOException if an I/O error occurs
* @throws InterruptedException if the thread is interrupted
* @throws ParseException if a parse error occurs
* @throws BlockSimulatorParsingException if a parse error occurs
*/
public static void main(String[] args)
throws IOException, InterruptedException, ParseException {
throws IOException, InterruptedException, BlockSimulatorParsingException {

LOGGER.log(INFO, "Starting Block Stream Simulator");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
package com.hedera.block.simulator;

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.block.simulator.generator.BlockStreamManager;
import com.hedera.block.simulator.grpc.PublishStreamGrpcClient;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import com.swirlds.config.api.Configuration;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.inject.Inject;

/** BlockStream Simulator App */
Expand All @@ -39,7 +40,7 @@ public class BlockStreamSimulatorApp {

private final int delayBetweenBlockItems;

boolean isRunning = false;
private final AtomicBoolean isRunning = new AtomicBoolean(false);

/**
* Creates a new BlockStreamSimulatorApp instance.
Expand All @@ -66,14 +67,14 @@ public BlockStreamSimulatorApp(
* Starts the block stream simulator.
*
* @throws InterruptedException if the thread is interrupted
* @throws ParseException if a parse error occurs
* @throws BlockSimulatorParsingException if a parse error occurs
* @throws IOException if an I/O error occurs
*/
public void start() throws InterruptedException, ParseException, IOException {
public void start() throws InterruptedException, BlockSimulatorParsingException, IOException {
int delayMSBetweenBlockItems = delayBetweenBlockItems / 1_000_000;
int delayNSBetweenBlockItems = delayBetweenBlockItems % 1_000_000;

isRunning = true;
isRunning.set(true);
LOGGER.log(System.Logger.Level.INFO, "Block Stream Simulator has started");

boolean streamBlockItem = true;
Expand Down Expand Up @@ -113,12 +114,12 @@ public void start() throws InterruptedException, ParseException, IOException {
* @return true if the block stream simulator is running, false otherwise
*/
public boolean isRunning() {
return isRunning;
return isRunning.get();
}

/** Stops the block stream simulator. */
public void stop() {
isRunning = false;
isRunning.set(false);
LOGGER.log(System.Logger.Level.INFO, "Block Stream Simulator has stopped");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.block.simulator.exception;

/** Use this checked exception to represent a Block Simulator parsing exception. */
public class BlockSimulatorParsingException extends Exception {
/**
* Constructs a new parsing exception with the specified detail message.
*
* @param message the detail message
*/
public BlockSimulatorParsingException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
Expand Down Expand Up @@ -59,7 +60,7 @@ public GenerationMode getGenerationMode() {
}

@Override
public BlockItem getNextBlockItem() throws IOException, ParseException {
public BlockItem getNextBlockItem() throws IOException, BlockSimulatorParsingException {
if (currentBlock != null && currentBlock.items().size() > currentBlockItemIndex) {
return currentBlock.items().get(currentBlockItemIndex++);
} else {
Expand All @@ -74,22 +75,26 @@ public BlockItem getNextBlockItem() throws IOException, ParseException {
}

@Override
public Block getNextBlock() throws IOException, ParseException {
public Block getNextBlock() throws IOException, BlockSimulatorParsingException {
currentBlockIndex++;

String nextBlockFileName = String.format(formatString, currentBlockIndex);
File blockFile = new File(blockstreamPath, nextBlockFileName);

if (blockFile.exists()) {
if (!blockFile.exists()) {
return null;
}

try {
byte[] blockBytes = readFileBytes(blockFile.toPath());

LOGGER.log(INFO, "Loading block: " + blockFile.getName());

Block block = Block.PROTOBUF.parse(Bytes.wrap(blockBytes));
LOGGER.log(INFO, "block loaded with items size= " + block.items().size());
return block;
} catch (ParseException e) {
throw new BlockSimulatorParsingException(e.getMessage());
}

return null; // No more blocks found
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package com.hedera.block.simulator.generator;

import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import java.io.IOException;

/** The block stream manager interface. */
Expand All @@ -36,17 +36,17 @@ public interface BlockStreamManager {
* Get the next block item.
*
* @return the next block item
* @throws IOException if an I/O error occurs
* @throws ParseException if a parse error occurs
* @throws IOException if a I/O error occurs
* @throws BlockSimulatorParsingException if a parse error occurs
*/
BlockItem getNextBlockItem() throws IOException, ParseException;
BlockItem getNextBlockItem() throws IOException, BlockSimulatorParsingException;

/**
* Get the next block.
*
* @return the next block
* @throws IOException if an I/O error occurs
* @throws ParseException if a parse error occurs
* @throws IOException if a I/O error occurs
* @throws BlockSimulatorParsingException if a parse error occurs
*/
Block getNextBlock() throws IOException, ParseException;
Block getNextBlock() throws IOException, BlockSimulatorParsingException;
}
2 changes: 2 additions & 0 deletions simulator/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/** Runtime module of the simulator. */
module com.hedera.block.simulator {
exports com.hedera.block.simulator.config.data;
exports com.hedera.block.simulator.exception;
exports com.hedera.block.simulator;

requires static com.github.spotbugs.annotations;
requires static com.google.auto.service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.block.simulator.generator.BlockStreamManager;
import com.hedera.block.simulator.grpc.PublishStreamGrpcClient;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import com.swirlds.config.api.Configuration;
import java.io.IOException;
import java.nio.file.Paths;
Expand Down Expand Up @@ -63,13 +63,15 @@ void tearDown() {
}

@Test
void start_logsStartedMessage() throws InterruptedException, ParseException, IOException {
void start_logsStartedMessage()
throws InterruptedException, BlockSimulatorParsingException, IOException {
blockStreamSimulator.start();
assertTrue(blockStreamSimulator.isRunning());
}

@Test
void start_exitByBlockNull() throws InterruptedException, ParseException, IOException {
void start_exitByBlockNull()
throws InterruptedException, BlockSimulatorParsingException, IOException {

BlockStreamManager blockStreamManager = Mockito.mock(BlockStreamManager.class);
when(blockStreamManager.getNextBlockItem()).thenReturn(BlockItem.newBuilder().build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.pbj.runtime.ParseException;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import java.io.IOException;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
Expand All @@ -45,7 +45,7 @@ void getGenerationMode() {
}

@Test
void getNextBlockItem() throws IOException, ParseException {
void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsDirBlockStreamManager(getAbsoluteFolder(rootFolder));

Expand All @@ -55,7 +55,7 @@ void getNextBlockItem() throws IOException, ParseException {
}

@Test
void getNextBlock() throws IOException, ParseException {
void getNextBlock() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsDirBlockStreamManager(getAbsoluteFolder(rootFolder));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.pbj.runtime.ParseException;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import java.io.IOException;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
Expand All @@ -41,7 +41,7 @@ void getGenerationMode() {
}

@Test
void getNextBlock() throws IOException, ParseException {
void getNextBlock() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder));
for (int i = 0; i < 3000; i++) {
Expand All @@ -50,7 +50,7 @@ void getNextBlock() throws IOException, ParseException {
}

@Test
void getNextBlockItem() throws IOException, ParseException {
void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(gzRootFolder));
for (int i = 0; i < 35000; i++) {
Expand All @@ -59,7 +59,7 @@ void getNextBlockItem() throws IOException, ParseException {
}

@Test
void loadBlockBlk() throws IOException, ParseException {
void loadBlockBlk() throws IOException, BlockSimulatorParsingException {
String blkRootFolder = "src/test/resources/block-0.0.3-blk/";
BlockStreamManager blockStreamManager =
getBlockAsFileBlockStreamManager(getAbsoluteFolder(blkRootFolder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@

import com.hedera.block.simulator.config.data.BlockStreamConfig;
import com.hedera.block.simulator.config.types.GenerationMode;
import com.hedera.block.simulator.exception.BlockSimulatorParsingException;
import com.hedera.hapi.block.stream.BlockItem;
import com.hedera.pbj.runtime.ParseException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

class BlockAsFileLargeDataSetsTest {

Expand All @@ -51,7 +54,7 @@ void getGenerationMode() {
}

@Test
void getNextBlock() throws IOException, ParseException {
void getNextBlock() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileLargeDatasetsBlockStreamManager(getAbsoluteFolder(rootFolder));
for (int i = 0; i < filesInFolder; i++) {
Expand All @@ -62,7 +65,7 @@ void getNextBlock() throws IOException, ParseException {
}

@Test
void getNextBlockItem() throws IOException, ParseException {
void getNextBlockItem() throws IOException, BlockSimulatorParsingException {
BlockStreamManager blockStreamManager =
getBlockAsFileLargeDatasetsBlockStreamManager(getAbsoluteFolder(rootFolder));

Expand All @@ -75,6 +78,41 @@ void getNextBlockItem() throws IOException, ParseException {
}
}

@Test
void gettingNextBlockItemThrowsParsingException(@TempDir Path tempDir) throws IOException {
String blockFolderName = "block-0.0.3-blk";
Path blockDirPath = tempDir.resolve(blockFolderName);
Files.createDirectories(blockDirPath);

int nextBlockIndex = 1;
int paddedLength = 36;
String fileExtension = ".blk";
String formatString = "%0" + paddedLength + "d" + fileExtension;

String currentBlockFileName = String.format(formatString, nextBlockIndex);
Path currentBlockFilePath = blockDirPath.resolve(currentBlockFileName);

byte[] invalidData = "invalid block data".getBytes();
Files.write(currentBlockFilePath, invalidData);

BlockStreamConfig blockStreamConfig =
new BlockStreamConfig(
GenerationMode.DIR,
blockDirPath.toString(),
1_500_000,
"BlockAsFileBlockStreamManager",
10_000,
36,
".blk");
BlockAsFileLargeDataSets blockStreamManager =
new BlockAsFileLargeDataSets(blockStreamConfig);

assertThrows(
BlockSimulatorParsingException.class,
blockStreamManager::getNextBlock,
"Expected getNextBlock() to throw BlockSimulatorParsingException");
}

private BlockAsFileLargeDataSets getBlockAsFileLargeDatasetsBlockStreamManager(
String rootFolder) {
BlockStreamConfig blockStreamConfig =
Expand Down
Loading

0 comments on commit 3a565f3

Please sign in to comment.