Skip to content

Commit

Permalink
added minimal BlockProof type to add intermediate BlockItem type
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Jul 16, 2024
1 parent 04dd9c5 commit 31be928
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
11 changes: 8 additions & 3 deletions protos/src/main/protobuf/blockstream.proto
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,22 @@ message Block {
message BlockItem {

oneof items {
BlockHeader block_header = 1;
BlockProof state_proof = 2;
BlockHeader header = 1;
EventMetadata start_event = 2;
BlockProof state_proof = 3;
}

string value = 3;
string value = 4;
}

message BlockHeader {
uint64 block_number = 1;
}

message EventMetadata {
uint64 creator_id = 1;
}

message BlockProof {
uint64 block = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public static long getBlockNumber(final Block block) {
}

public static BlockHeader getBlockHeader(final Block block) {
return block.getBlockItems(0).getBlockHeader();
return block.getBlockItems(0).getHeader();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ private Optional<BlockItem> readBlockItem(final String blockItemPath) {

private String getAbsoluteFilePath(final BlockItem blockItem) throws IOException {

if (blockItem.hasBlockHeader()) {
if (blockItem.hasHeader()) {

// A "block" is a directory of blockItems. Create the "block"
// based on the block_number
currentBlockDir = Path.of(String.valueOf(blockItem.getBlockHeader().getBlockNumber()));
currentBlockDir = Path.of(String.valueOf(blockItem.getHeader().getBlockNumber()));

final Path blockPath = blockNodeRootPath.resolve(currentBlockDir);
createPath(blockPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static List<Block> generateBlocks(int numOfBlocks) {
case 0:
blockItems.add(
BlockItem.newBuilder()
.setBlockHeader(
.setHeader(
BlockStreamService.BlockHeader.newBuilder()
.setBlockNumber(i + 1)
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ public class RangeTest {
@Test
public void testReadRangeWithEvenEntries() {

int maxEntries = 100;
int numOfBlocks = 100;

BlockPersistenceHandler<Block, BlockItem> blockPersistenceHandler =
generateInMemoryTestBlockPersistenceHandler(maxEntries);
generateInMemoryTestBlockPersistenceHandler();

for (Block block : generateBlocks(numOfBlocks)) {
for (BlockItem blockItem : block.getBlockItemsList()) {
Expand All @@ -52,22 +51,20 @@ public void testReadRangeWithEvenEntries() {

@Test
public void testReadRangeWithNoBlocks() {
int maxEntries = 100;

BlockPersistenceHandler<Block, BlockItem> blockPersistenceHandler =
generateInMemoryTestBlockPersistenceHandler(maxEntries);
generateInMemoryTestBlockPersistenceHandler();
Queue<Block> results = blockPersistenceHandler.readRange(1, 100);
assertNotNull(results);
assertEquals(0, results.size());
}

@Test
public void testReadRangeWhenBlocksLessThanWindow() {
int maxEntries = 100;
int numOfBlocks = 9;

BlockPersistenceHandler<Block, BlockItem> blockPersistenceHandler =
generateInMemoryTestBlockPersistenceHandler(maxEntries);
generateInMemoryTestBlockPersistenceHandler();
List<Block> blocks = generateBlocks(numOfBlocks);
for (Block block : blocks) {
for (BlockItem blockItem : block.getBlockItemsList()) {
Expand Down Expand Up @@ -96,13 +93,13 @@ private static void verifyReadRange(
for (int i = startBlockId; i <= endBlockId && results.peek() != null; ++i) {
Block block = results.poll();
assertNotNull(block);
assertEquals(i, block.getBlockItems(0).getBlockHeader().getBlockNumber());
assertEquals(i, block.getBlockItems(0).getHeader().getBlockNumber());
}
}
}

private static BlockPersistenceHandler<Block, BlockItem>
generateInMemoryTestBlockPersistenceHandler(int maxEntries) {
generateInMemoryTestBlockPersistenceHandler() {

// Mock up a simple, in-memory persistence handler
BlockStorage<Block, BlockItem> blockStorage = new NoOpTestBlockStorage();
Expand All @@ -120,8 +117,8 @@ public NoOpTestBlockStorage() {

@Override
public void write(BlockItem blockItem) {
if (blockItem.hasBlockHeader()) {
index = blockItem.getBlockHeader().getBlockNumber();
if (blockItem.hasHeader()) {
index = blockItem.getHeader().getBlockNumber();
}

cache.compute(
Expand Down
8 changes: 5 additions & 3 deletions server/src/test/resources/producer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ trap cleanup SIGINT
for ((i=1; i<=10; i++))
do

if [[ i -eq 1 ]]; then
echo "{\"block_item\": {\"block_header\": $iter,\"value\": \"Payload[...]\"}}"
if [[ $i -eq 1 ]]; then
echo "{\"block_item\": {\"header\": {\"block_number\": $iter},\"value\": \"Payload[...]\"}}"
elif [[ $i -eq 10 ]]; then
echo "{\"block_item\": {\"state_proof\": {\"block\": $iter},\"value\": \"Payload[...]\"}}"
else
echo "{\"block_item\": {\"block_header\": 0,\"value\": \"Payload[...]\"}}"
echo "{\"block_item\": {\"start_event\": {\"creator_id\": $i},\"value\": \"Payload[...]\"}}"
fi

sleep 0.5
Expand Down

0 comments on commit 31be928

Please sign in to comment.