Skip to content

Commit e0b6956

Browse files
committed
Update tests
Signed-off-by: Nana Essilfie-Conduah <[email protected]>
1 parent 3625c1d commit e0b6956

File tree

6 files changed

+59
-169
lines changed

6 files changed

+59
-169
lines changed

block-node/app/src/testFixtures/java/org/hiero/block/node/app/fixtures/blocks/BlockUtils.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ public record SampleBlockInfo(Bytes blockRootHash, Long blockNumber, BlockUnpars
8080
* These blocks are used for testing purposes only.
8181
*/
8282
public enum SAMPLE_BLOCKS {
83-
// to:do blockHash values excludes block proof in pre068 blocks or assume null values, should be updated in PR
84-
// 1865
83+
// to:do blockHash values excludes block proof in pre068 blocks or assumes null values, will fix in PR 1865
8584
HAPI_0_64_0_BLOCK_14(
8685
"HAPI-0-64-0/000000000000000000000000000000000014.blk.gz",
8786
"b95a5e77d3efb9f43eae6c94972b1a589bff99c98b2da6cc8e88cb29cf4f68a07effd14af54c220db548688dceca16e3",
@@ -90,10 +89,7 @@ public enum SAMPLE_BLOCKS {
9089
"HAPI-0-66-0/000000000000000000000000000000000010.blk.gz",
9190
"34bb96d57eb096d8804cf997658e3386587956e5f7d69e94ea3d97c8c7a11cb5d3c936fb47572ad8882667960d00aa87",
9291
10),
93-
HAPI_0_68_0_BLOCK_14(
94-
"HAPI-0-68-0/000000000000000000000000000000000014.blk.gz",
95-
"d9663f3dfd1a763de4a150fae6b560b479b0f585c24a7ef545de43b34401de23f9dac63e496cb28f2a3a5304f9050bff",
96-
14);
92+
HAPI_0_68_0_BLOCK_14("HAPI-0-68-0/000000000000000000000000000000000014.blk.gz", "30783030", 14);
9793

9894
// VNymlmV0HxPYyVbf+zMn7b5dVuMtK8ZtbOFFoeG2L+Yc3bdLGfF6Xam7AZM0KdXg
9995
private final String blockName;

block-node/verification/src/main/java/org/hiero/block/node/verification/session/impl/ExtendedMerkleTreeSession.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33

44
import static java.lang.System.Logger.Level.INFO;
55
import static java.lang.System.Logger.Level.TRACE;
6+
import static org.hiero.block.common.hasher.HashingUtilities.getBlockItemHash;
67

8+
import com.hedera.hapi.block.stream.BlockProof;
9+
import com.hedera.hapi.block.stream.output.BlockFooter;
710
import com.hedera.pbj.runtime.ParseException;
811
import com.hedera.pbj.runtime.io.buffer.Bytes;
12+
import java.nio.ByteBuffer;
913
import java.util.ArrayList;
1014
import java.util.List;
1115
import org.hiero.block.internal.BlockItemUnparsed;
@@ -30,6 +34,10 @@ public class ExtendedMerkleTreeSession implements VerificationSession {
3034
*/
3135
protected final List<BlockItemUnparsed> blockItems = new ArrayList<>();
3236

37+
protected final List<BlockProof> blockProofs = new ArrayList<>();
38+
private BlockFooter blockFooter = null;
39+
private BlockProof blockSignedProof = null;
40+
3341
public ExtendedMerkleTreeSession(final long blockNumber, final BlockSource blockSource, final String extraBytes) {
3442
this.blockNumber = blockNumber;
3543
this.blockSource = blockSource;
@@ -41,7 +49,23 @@ public ExtendedMerkleTreeSession(final long blockNumber, final BlockSource block
4149
public VerificationNotification processBlockItems(List<BlockItemUnparsed> blockItems) throws ParseException {
4250
this.blockItems.addAll(blockItems);
4351
LOGGER.log(TRACE, "Processed {0} block items for block {1}", blockItems.size(), blockNumber);
44-
if (blockItems.getLast().hasBlockProof()) {
52+
53+
for (BlockItemUnparsed item : blockItems) {
54+
final BlockItemUnparsed.ItemOneOfType kind = item.item().kind();
55+
final ByteBuffer hash = getBlockItemHash(item);
56+
switch (kind) {
57+
case BLOCK_FOOTER -> this.blockFooter = BlockFooter.PROTOBUF.parse(item.blockFooter());
58+
case BLOCK_PROOF -> {
59+
BlockProof blockProof = BlockProof.PROTOBUF.parse(item.blockProof());
60+
this.blockSignedProof = blockProof;
61+
blockProofs.add(blockProof);
62+
}
63+
}
64+
}
65+
66+
// scoped to ensure we observe a block footer and single proof which is the signed block proof
67+
// this is left intentionally narrow to expose any changes and will be replaced shortly by PR #1865
68+
if (blockFooter != null && this.blockSignedProof != null && blockProofs.size() == 1) {
4569
BlockUnparsed block =
4670
BlockUnparsed.newBuilder().blockItems(this.blockItems).build();
4771
Bytes blockHash = Bytes.wrap("0x00");

block-node/verification/src/test/java/org/hiero/block/node/verification/VerificationServicePluginTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.hiero.block.internal.BlockItemUnparsed.ItemOneOfType;
2020
import org.hiero.block.node.app.fixtures.async.BlockingExecutor;
2121
import org.hiero.block.node.app.fixtures.blocks.BlockUtils;
22+
import org.hiero.block.node.app.fixtures.blocks.BlockUtils.SAMPLE_BLOCKS;
2223
import org.hiero.block.node.app.fixtures.plugintest.NoBlocksHistoricalBlockFacility;
2324
import org.hiero.block.node.app.fixtures.plugintest.PluginTestBase;
2425
import org.hiero.block.node.app.fixtures.plugintest.TestHealthFacility;
@@ -38,6 +39,35 @@ public VerificationServicePluginTest() {
3839
start(new VerificationServicePlugin(), new NoBlocksHistoricalBlockFacility());
3940
}
4041

42+
@Test
43+
void testVerificationPlugin068() throws IOException, ParseException {
44+
45+
BlockUtils.SampleBlockInfo sampleBlockInfo = BlockUtils.getSampleBlockInfo(SAMPLE_BLOCKS.HAPI_0_68_0_BLOCK_14);
46+
47+
List<BlockItemUnparsed> blockItems = sampleBlockInfo.blockUnparsed().blockItems();
48+
long blockNumber = sampleBlockInfo.blockNumber();
49+
50+
blockMessaging.sendBlockItems(new BlockItems(blockItems, blockNumber));
51+
52+
// check we received a block verification
53+
VerificationNotification blockNotification =
54+
blockMessaging.getSentVerificationNotifications().getFirst();
55+
assertNotNull(blockNotification);
56+
assertEquals(
57+
blockNumber,
58+
blockNotification.blockNumber(),
59+
"The block number should be the same as the one in the block header");
60+
assertTrue(blockNotification.success(), "The verification should be successful");
61+
assertEquals(
62+
sampleBlockInfo.blockRootHash(),
63+
blockNotification.blockHash(),
64+
"The block hash should be the same as the one in the block header");
65+
assertEquals(
66+
sampleBlockInfo.blockUnparsed(),
67+
blockNotification.block(),
68+
"The block should be the same as the one sent");
69+
}
70+
4171
@Test
4272
void testVerificationPlugin() throws IOException, ParseException {
4373

block-node/verification/src/test/java/org/hiero/block/node/verification/session/impl/ExtendedMerkleTreeSessionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ExtendedMerkleTreeSessionTest {
2222

2323
@BeforeEach
2424
void setUp() throws IOException, ParseException {
25-
sampleBlockInfo = BlockUtils.getSampleBlockInfo(BlockUtils.SAMPLE_BLOCKS.HAPI_0_64_0_BLOCK_14);
25+
sampleBlockInfo = BlockUtils.getSampleBlockInfo(BlockUtils.SAMPLE_BLOCKS.HAPI_0_68_0_BLOCK_14);
2626
blockItems = sampleBlockInfo.blockUnparsed().blockItems();
2727
}
2828

block-node/verification/src/test/java/org/hiero/block/node/verification/session/impl/PreviewSimpleHashSessionTest.java

Lines changed: 1 addition & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class PreviewSimpleHashSessionTest {
2323

2424
@BeforeEach
2525
void setUp() throws IOException, ParseException {
26-
sampleBlockInfo = BlockUtils.getSampleBlockInfo(BlockUtils.SAMPLE_BLOCKS.HAPI_0_68_0_BLOCK_14);
26+
sampleBlockInfo = BlockUtils.getSampleBlockInfo(SAMPLE_BLOCKS.HAPI_0_64_0_BLOCK_14);
2727
blockItems = sampleBlockInfo.blockUnparsed().blockItems();
2828
}
2929

@@ -169,164 +169,4 @@ void invalidHash_chunked() throws ParseException {
169169
// to:do (PR #1865) revert when 0.68 verification is supported
170170
// Assertions.assertFalse(blockNotification.success(), "The block notification should be unsuccessful");
171171
}
172-
173-
/**
174-
* Happy path test for the VerificationSession class.
175-
* */
176-
@Test
177-
void happyPathPre068() throws ParseException, IOException {
178-
BlockUtils.SampleBlockInfo sampleBlockInfo064 =
179-
BlockUtils.getSampleBlockInfo(SAMPLE_BLOCKS.HAPI_0_64_0_BLOCK_14);
180-
List<BlockItemUnparsed> blockItems064 =
181-
sampleBlockInfo064.blockUnparsed().blockItems();
182-
183-
BlockHeader blockHeader =
184-
BlockHeader.PROTOBUF.parse(blockItems064.getFirst().blockHeaderOrThrow());
185-
186-
long blockNumber = blockHeader.number();
187-
188-
PreviewSimpleHashSession session = new PreviewSimpleHashSession(blockNumber, BlockSource.PUBLISHER);
189-
190-
VerificationNotification blockNotification = session.processBlockItems(blockItems064);
191-
192-
Assertions.assertArrayEquals(
193-
blockItems064.toArray(),
194-
session.blockItems.toArray(),
195-
"The internal block items should be the same as ones sent in");
196-
197-
Assertions.assertArrayEquals(
198-
blockItems064.toArray(),
199-
blockNotification.block().blockItems().toArray(),
200-
"The notification's block items should be the same as ones sent in");
201-
202-
Assertions.assertEquals(
203-
blockNumber,
204-
blockNotification.blockNumber(),
205-
"The block number should be the same as the one in the block header");
206-
207-
Assertions.assertEquals(
208-
sampleBlockInfo064.blockRootHash(),
209-
blockNotification.blockHash(),
210-
"The block hash should be the same as the one in the block header");
211-
212-
Assertions.assertTrue(blockNotification.success(), "The block notification should be successful");
213-
214-
Assertions.assertEquals(
215-
sampleBlockInfo064.blockUnparsed(),
216-
blockNotification.block(),
217-
"The block should be the same as the one sent");
218-
}
219-
220-
/**
221-
* Happy path test for the VerificationSession class with chunked list of items, to simulate actual usage.
222-
* */
223-
@Test
224-
void happyPathChunkedPre068() throws ParseException, IOException {
225-
BlockUtils.SampleBlockInfo sampleBlockInfo064 =
226-
BlockUtils.getSampleBlockInfo(SAMPLE_BLOCKS.HAPI_0_64_0_BLOCK_14);
227-
List<BlockItemUnparsed> blockItems064 =
228-
sampleBlockInfo064.blockUnparsed().blockItems();
229-
List<List<BlockItemUnparsed>> chunkifiedItems = ChunkUtils.chunkify(blockItems064, 2);
230-
int currentChunk = 0;
231-
long blockNumber = sampleBlockInfo064.blockNumber();
232-
233-
PreviewSimpleHashSession session = new PreviewSimpleHashSession(blockNumber, BlockSource.PUBLISHER);
234-
235-
VerificationNotification blockNotification = session.processBlockItems(chunkifiedItems.get(currentChunk));
236-
237-
while (blockNotification == null) {
238-
currentChunk++;
239-
blockNotification = session.processBlockItems(chunkifiedItems.get(currentChunk));
240-
}
241-
242-
Assertions.assertEquals(
243-
blockNumber,
244-
blockNotification.blockNumber(),
245-
"The block number should be the same as the one in the block header");
246-
247-
Assertions.assertEquals(
248-
sampleBlockInfo064.blockRootHash(),
249-
blockNotification.blockHash(),
250-
"The block hash should be the same as the one in the block header");
251-
252-
Assertions.assertTrue(blockNotification.success(), "The block notification should be successful");
253-
254-
Assertions.assertArrayEquals(
255-
blockItems064.toArray(),
256-
session.blockItems.toArray(),
257-
"The internal block items should be the same as ones sent in");
258-
259-
Assertions.assertArrayEquals(
260-
blockItems064.toArray(),
261-
blockNotification.block().blockItems().toArray(),
262-
"The notification's block items should be the same as ones sent in");
263-
}
264-
265-
/**
266-
* Non-Happy path test for the VerificationSession class with invalid hash
267-
* */
268-
@Test
269-
void invalidHashPre068() throws ParseException, IOException {
270-
BlockUtils.SampleBlockInfo sampleBlockInfo064 =
271-
BlockUtils.getSampleBlockInfo(SAMPLE_BLOCKS.HAPI_0_64_0_BLOCK_14);
272-
List<BlockItemUnparsed> blockItems064 =
273-
sampleBlockInfo064.blockUnparsed().blockItems();
274-
// if we remove a block item, the hash will be different
275-
blockItems064.remove(5);
276-
277-
long blockNumber = sampleBlockInfo064.blockNumber();
278-
VerificationSession session = new PreviewSimpleHashSession(blockNumber, BlockSource.PUBLISHER);
279-
VerificationNotification blockNotification = session.processBlockItems(blockItems064);
280-
281-
Assertions.assertEquals(
282-
blockNumber,
283-
blockNotification.blockNumber(),
284-
"The block number should be the same as the one in the block header");
285-
286-
Assertions.assertNotEquals(
287-
sampleBlockInfo064.blockRootHash(),
288-
blockNotification.blockHash(),
289-
"The block hash should not be the same as the one in the block header");
290-
291-
// to:do (PR #1865) revert when 0.68 verification is supported
292-
// Assertions.assertFalse(blockNotification.success(), "The block notification should be unsuccessful");
293-
}
294-
295-
/**
296-
* Non-Happy path test for the VerificationSession class with invalid hash with chunked list of items, to simulate actual usage.
297-
* */
298-
@Test
299-
void invalidHashChunkedPre068() throws ParseException, IOException {
300-
BlockUtils.SampleBlockInfo sampleBlockInfo064 =
301-
BlockUtils.getSampleBlockInfo(SAMPLE_BLOCKS.HAPI_0_64_0_BLOCK_14);
302-
List<BlockItemUnparsed> blockItems064 =
303-
sampleBlockInfo064.blockUnparsed().blockItems();
304-
// remove a block item, the hash will be different
305-
blockItems064.remove(5);
306-
// chunkify the list of items
307-
List<List<BlockItemUnparsed>> chunkifiedItems = ChunkUtils.chunkify(blockItems064, 2);
308-
int currentChunk = 0;
309-
long blockNumber = sampleBlockInfo064.blockNumber();
310-
311-
VerificationSession session = new PreviewSimpleHashSession(blockNumber, BlockSource.PUBLISHER);
312-
VerificationNotification blockNotification = session.processBlockItems(chunkifiedItems.get(currentChunk));
313-
314-
while (blockNotification == null) {
315-
currentChunk++;
316-
blockNotification = session.processBlockItems(chunkifiedItems.get(currentChunk));
317-
}
318-
319-
Assertions.assertEquals(
320-
blockNumber,
321-
blockNotification.blockNumber(),
322-
"The block number should be the same as the one in the block header");
323-
324-
Assertions.assertNotEquals(
325-
sampleBlockInfo064.blockRootHash(),
326-
blockNotification.blockHash(),
327-
"The block hash should not be the same as the one in the block header");
328-
329-
// to:do (PR #1865) revert when 0.68 verification is supported
330-
// Assertions.assertFalse(blockNotification.success(), "The block notification should be unsuccessful");
331-
}
332172
}

0 commit comments

Comments
 (0)