|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package org.hiero.mirror.importer.downloader.block.simulator; |
| 4 | + |
| 5 | +import com.hedera.hapi.block.stream.input.protoc.EventHeader; |
| 6 | +import com.hedera.hapi.block.stream.input.protoc.RoundHeader; |
| 7 | +import com.hedera.hapi.block.stream.output.protoc.BlockHeader; |
| 8 | +import com.hedera.hapi.block.stream.output.protoc.TransactionResult; |
| 9 | +import com.hedera.hapi.block.stream.protoc.BlockItem; |
| 10 | +import com.hedera.hapi.block.stream.protoc.BlockProof; |
| 11 | +import com.hedera.hapi.platform.event.legacy.EventTransaction; |
| 12 | +import com.hederahashgraph.api.proto.java.ResponseCodeEnum; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import lombok.SneakyThrows; |
| 16 | +import org.apache.commons.codec.binary.Hex; |
| 17 | +import org.hiero.block.api.protoc.BlockItemSet; |
| 18 | +import org.hiero.mirror.common.util.DomainUtils; |
| 19 | +import org.hiero.mirror.importer.parser.domain.RecordItemBuilder; |
| 20 | +import org.hiero.mirror.importer.reader.block.BlockRootHashDigest; |
| 21 | + |
| 22 | +public final class BlockGenerator { |
| 23 | + private static final byte[] ALL_ZERO_HASH = new byte[48]; |
| 24 | + |
| 25 | + private final RecordItemBuilder recordItemBuilder = new RecordItemBuilder(); |
| 26 | + |
| 27 | + private int blockNumber; |
| 28 | + private byte[] previousBlockRootHash; |
| 29 | + |
| 30 | + public BlockGenerator(int startBlockNumber) { |
| 31 | + blockNumber = startBlockNumber; |
| 32 | + if (blockNumber == 0) { |
| 33 | + previousBlockRootHash = ALL_ZERO_HASH; |
| 34 | + } else { |
| 35 | + previousBlockRootHash = recordItemBuilder.randomBytes(48); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public List<BlockItemSet> next(int count) { |
| 40 | + var blocks = new ArrayList<BlockItemSet>(); |
| 41 | + for (int i = 0; i < count; i++) { |
| 42 | + blocks.add(next()); |
| 43 | + } |
| 44 | + return blocks; |
| 45 | + } |
| 46 | + |
| 47 | + @SneakyThrows |
| 48 | + private void calculateBlockRootHash(BlockItemSet block) { |
| 49 | + var blockRootHashDigest = new BlockRootHashDigest(); |
| 50 | + blockRootHashDigest.setPreviousHash(previousBlockRootHash); |
| 51 | + blockRootHashDigest.setStartOfBlockStateHash(ALL_ZERO_HASH); |
| 52 | + |
| 53 | + for (var blockItem : block.getBlockItemsList()) { |
| 54 | + switch (blockItem.getItemCase()) { |
| 55 | + case EVENT_HEADER, EVENT_TRANSACTION, ROUND_HEADER -> blockRootHashDigest.addInputBlockItem(blockItem); |
| 56 | + case BLOCK_HEADER, STATE_CHANGES, TRANSACTION_OUTPUT, TRANSACTION_RESULT -> |
| 57 | + blockRootHashDigest.addOutputBlockItem(blockItem); |
| 58 | + default -> { |
| 59 | + // other block items aren't considered input / output |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + previousBlockRootHash = Hex.decodeHex(blockRootHashDigest.digest()); |
| 65 | + } |
| 66 | + |
| 67 | + private BlockItemSet next() { |
| 68 | + var builder = BlockItemSet.newBuilder(); |
| 69 | + // block header |
| 70 | + builder.addBlockItems(BlockItem.newBuilder() |
| 71 | + .setBlockHeader(BlockHeader.newBuilder() |
| 72 | + .setBlockTimestamp(recordItemBuilder.timestamp()) |
| 73 | + .setNumber(blockNumber) |
| 74 | + .build())); |
| 75 | + // round header |
| 76 | + builder.addBlockItems(BlockItem.newBuilder() |
| 77 | + .setRoundHeader( |
| 78 | + RoundHeader.newBuilder().setRoundNumber(blockNumber + 1).build())); |
| 79 | + // event header |
| 80 | + builder.addBlockItems(BlockItem.newBuilder().setEventHeader(EventHeader.getDefaultInstance())); |
| 81 | + |
| 82 | + // transactions |
| 83 | + for (int i = 0; i < 10; i++) { |
| 84 | + builder.addAllBlockItems(transactionUnit()); |
| 85 | + } |
| 86 | + |
| 87 | + // block proof |
| 88 | + builder.addBlockItems(BlockItem.newBuilder() |
| 89 | + .setBlockProof(BlockProof.newBuilder() |
| 90 | + .setBlock(blockNumber) |
| 91 | + .setPreviousBlockRootHash(DomainUtils.fromBytes(previousBlockRootHash)) |
| 92 | + .setStartOfBlockStateRootHash(DomainUtils.fromBytes(ALL_ZERO_HASH)))); |
| 93 | + var block = builder.build(); |
| 94 | + calculateBlockRootHash(block); |
| 95 | + blockNumber++; |
| 96 | + return block; |
| 97 | + } |
| 98 | + |
| 99 | + private List<BlockItem> transactionUnit() { |
| 100 | + var recordItem = recordItemBuilder.cryptoTransfer().build(); |
| 101 | + var eventTransaction = BlockItem.newBuilder() |
| 102 | + .setEventTransaction(EventTransaction.newBuilder() |
| 103 | + .setApplicationTransaction(recordItem.getTransaction().toByteString()) |
| 104 | + .build()) |
| 105 | + .build(); |
| 106 | + var transactionResult = BlockItem.newBuilder() |
| 107 | + .setTransactionResult(TransactionResult.newBuilder() |
| 108 | + .setConsensusTimestamp(recordItem.getTransactionRecord().getConsensusTimestamp()) |
| 109 | + .setStatus(ResponseCodeEnum.SUCCESS) |
| 110 | + .setTransferList(recordItem.getTransactionRecord().getTransferList()) |
| 111 | + .build()) |
| 112 | + .build(); |
| 113 | + // for simplicity, no state changes |
| 114 | + return List.of(eventTransaction, transactionResult); |
| 115 | + } |
| 116 | +} |
0 commit comments