|
| 1 | +/* |
| 2 | + * Copyright contributors to Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.chainimport; |
| 16 | + |
| 17 | +import org.hyperledger.besu.controller.BesuController; |
| 18 | +import org.hyperledger.besu.ethereum.ProtocolContext; |
| 19 | +import org.hyperledger.besu.ethereum.core.Block; |
| 20 | +import org.hyperledger.besu.ethereum.core.BlockBody; |
| 21 | +import org.hyperledger.besu.ethereum.core.BlockHeader; |
| 22 | +import org.hyperledger.besu.ethereum.core.BlockHeaderFunctions; |
| 23 | +import org.hyperledger.besu.ethereum.core.BlockImporter; |
| 24 | +import org.hyperledger.besu.ethereum.core.TransactionReceipt; |
| 25 | +import org.hyperledger.besu.ethereum.mainnet.BlockImportResult; |
| 26 | +import org.hyperledger.besu.ethereum.mainnet.BodyValidationMode; |
| 27 | +import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; |
| 28 | +import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; |
| 29 | +import org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions; |
| 30 | +import org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput; |
| 31 | +import org.hyperledger.besu.ethereum.rlp.RLPInput; |
| 32 | +import org.hyperledger.besu.util.era1.Era1BlockIndex; |
| 33 | +import org.hyperledger.besu.util.era1.Era1ExecutionBlockBody; |
| 34 | +import org.hyperledger.besu.util.era1.Era1ExecutionBlockHeader; |
| 35 | +import org.hyperledger.besu.util.era1.Era1ExecutionBlockReceipts; |
| 36 | +import org.hyperledger.besu.util.era1.Era1Reader; |
| 37 | +import org.hyperledger.besu.util.era1.Era1ReaderListener; |
| 38 | +import org.hyperledger.besu.util.snappy.SnappyFactory; |
| 39 | + |
| 40 | +import java.io.Closeable; |
| 41 | +import java.io.FileInputStream; |
| 42 | +import java.io.IOException; |
| 43 | +import java.nio.file.Path; |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.List; |
| 46 | +import java.util.concurrent.CompletableFuture; |
| 47 | +import java.util.concurrent.ExecutionException; |
| 48 | +import java.util.concurrent.Future; |
| 49 | +import java.util.concurrent.TimeUnit; |
| 50 | +import java.util.concurrent.TimeoutException; |
| 51 | + |
| 52 | +import org.apache.tuweni.bytes.Bytes; |
| 53 | +import org.slf4j.Logger; |
| 54 | +import org.slf4j.LoggerFactory; |
| 55 | + |
| 56 | +/** |
| 57 | + * Tool for importing era1-encoded block data, headers, and transaction receipts from era1 files. |
| 58 | + */ |
| 59 | +public class Era1BlockImporter implements Closeable { |
| 60 | + private static final Logger LOG = LoggerFactory.getLogger(Era1BlockImporter.class); |
| 61 | + |
| 62 | + private static final int ERA1_BLOCK_COUNT_MAX = 8192; |
| 63 | + private static final int IMPORT_COUNT_FOR_LOG_UPDATE = 1000; |
| 64 | + |
| 65 | + /** Default Constructor. */ |
| 66 | + public Era1BlockImporter() {} |
| 67 | + |
| 68 | + /** |
| 69 | + * Imports the blocks, headers, and transaction receipts from the file found at the supplied path |
| 70 | + * |
| 71 | + * @param controller The BesuController |
| 72 | + * @param path The path |
| 73 | + * @throws IOException IOException |
| 74 | + * @throws ExecutionException ExecutionException |
| 75 | + * @throws InterruptedException InterruptedException |
| 76 | + * @throws TimeoutException TimeoutException |
| 77 | + */ |
| 78 | + public void importBlocks(final BesuController controller, final Path path) |
| 79 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 80 | + final ProtocolSchedule protocolSchedule = controller.getProtocolSchedule(); |
| 81 | + final BlockHeaderFunctions blockHeaderFunctions = |
| 82 | + ScheduleBasedBlockHeaderFunctions.create(protocolSchedule); |
| 83 | + final ProtocolContext context = controller.getProtocolContext(); |
| 84 | + |
| 85 | + Era1Reader reader = new Era1Reader(new SnappyFactory()); |
| 86 | + |
| 87 | + final List<Future<BlockHeader>> headersFutures = new ArrayList<>(ERA1_BLOCK_COUNT_MAX); |
| 88 | + final List<Future<BlockBody>> bodiesFutures = new ArrayList<>(ERA1_BLOCK_COUNT_MAX); |
| 89 | + final List<Future<List<TransactionReceipt>>> receiptsFutures = |
| 90 | + new ArrayList<>(ERA1_BLOCK_COUNT_MAX); |
| 91 | + reader.read( |
| 92 | + new FileInputStream(path.toFile()), |
| 93 | + new Era1ReaderListener() { |
| 94 | + |
| 95 | + @Override |
| 96 | + public void handleExecutionBlockHeader( |
| 97 | + final Era1ExecutionBlockHeader executionBlockHeader) { |
| 98 | + headersFutures.add( |
| 99 | + CompletableFuture.supplyAsync( |
| 100 | + () -> |
| 101 | + BlockHeader.readFrom( |
| 102 | + new BytesValueRLPInput( |
| 103 | + Bytes.wrap(executionBlockHeader.header()), false), |
| 104 | + blockHeaderFunctions))); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void handleExecutionBlockBody(final Era1ExecutionBlockBody executionBlockBody) { |
| 109 | + bodiesFutures.add( |
| 110 | + CompletableFuture.supplyAsync( |
| 111 | + () -> |
| 112 | + BlockBody.readWrappedBodyFrom( |
| 113 | + new BytesValueRLPInput(Bytes.wrap(executionBlockBody.block()), false), |
| 114 | + blockHeaderFunctions, |
| 115 | + true))); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void handleExecutionBlockReceipts( |
| 120 | + final Era1ExecutionBlockReceipts executionBlockReceipts) { |
| 121 | + receiptsFutures.add( |
| 122 | + CompletableFuture.supplyAsync( |
| 123 | + () -> { |
| 124 | + RLPInput input = |
| 125 | + new BytesValueRLPInput( |
| 126 | + Bytes.wrap(executionBlockReceipts.receipts()), false); |
| 127 | + final List<TransactionReceipt> receiptsForBlock = new ArrayList<>(); |
| 128 | + input.readList((in) -> receiptsForBlock.add(TransactionReceipt.readFrom(in))); |
| 129 | + return receiptsForBlock; |
| 130 | + })); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void handleBlockIndex(final Era1BlockIndex blockIndex) { |
| 135 | + // not really necessary, do nothing |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + LOG.info("Read {} blocks, now importing", headersFutures.size()); |
| 140 | + |
| 141 | + Block block = null; |
| 142 | + for (int i = 0; i < headersFutures.size(); i++) { |
| 143 | + BlockHeader blockHeader = headersFutures.get(i).get(10, TimeUnit.SECONDS); |
| 144 | + BlockImporter blockImporter = |
| 145 | + protocolSchedule.getByBlockHeader(blockHeader).getBlockImporter(); |
| 146 | + block = new Block(blockHeader, bodiesFutures.get(i).get(10, TimeUnit.SECONDS)); |
| 147 | + |
| 148 | + BlockImportResult importResult = |
| 149 | + blockImporter.importBlockForSyncing( |
| 150 | + context, |
| 151 | + block, |
| 152 | + receiptsFutures.get(i).get(10, TimeUnit.SECONDS), |
| 153 | + HeaderValidationMode.NONE, |
| 154 | + HeaderValidationMode.NONE, |
| 155 | + BodyValidationMode.NONE, |
| 156 | + false); |
| 157 | + if (importResult.getStatus() != BlockImportResult.BlockImportStatus.IMPORTED) { |
| 158 | + LOG.warn( |
| 159 | + "Failed to import block {} due to {}", |
| 160 | + blockHeader.getNumber(), |
| 161 | + importResult.getStatus()); |
| 162 | + } else if (i % IMPORT_COUNT_FOR_LOG_UPDATE == 0) { |
| 163 | + LOG.info("{}/{} blocks imported", i, headersFutures.size()); |
| 164 | + } |
| 165 | + } |
| 166 | + LOG.info("Done importing {} blocks", headersFutures.size()); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void close() throws IOException {} |
| 171 | +} |
0 commit comments