|
| 1 | +/* |
| 2 | + * Copyright contributors to Hyperledger 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.tests.acceptance; |
| 16 | + |
| 17 | +import static org.apache.logging.log4j.util.LoaderUtil.getClassLoader; |
| 18 | + |
| 19 | +import org.hyperledger.besu.plugin.services.storage.DataStorageFormat; |
| 20 | +import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase; |
| 21 | +import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.nio.file.StandardCopyOption; |
| 29 | + |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +public class QuorumIBFTMigrationTest extends AcceptanceTestBase { |
| 33 | + |
| 34 | + public static void copyKeyFilesToNodeDataDirs(final BesuNode... nodes) throws IOException { |
| 35 | + for (BesuNode node : nodes) { |
| 36 | + copyKeyFile(node, "key"); |
| 37 | + copyKeyFile(node, "key.pub"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private static void copyKeyFile(final BesuNode node, final String keyFileName) |
| 42 | + throws IOException { |
| 43 | + String resourceFileName = "qbft/migration-ibft1/" + node.getName() + keyFileName; |
| 44 | + try (InputStream keyFileStream = getClassLoader().getResourceAsStream(resourceFileName)) { |
| 45 | + if (keyFileStream == null) { |
| 46 | + throw new IOException("Resource not found: " + resourceFileName); |
| 47 | + } |
| 48 | + Path targetPath = node.homeDirectory().resolve(keyFileName); |
| 49 | + Files.createDirectories(targetPath.getParent()); |
| 50 | + Files.copy(keyFileStream, targetPath, StandardCopyOption.REPLACE_EXISTING); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static void runBesuCommand(final Path dataPath) throws IOException, InterruptedException { |
| 55 | + ProcessBuilder processBuilder = |
| 56 | + new ProcessBuilder( |
| 57 | + "../../build/install/besu/bin/besu", |
| 58 | + "--genesis-file", |
| 59 | + "src/test/resources/qbft/migration-ibft1/qbft-migration.json", |
| 60 | + "--data-path", |
| 61 | + dataPath.toString(), |
| 62 | + "--data-storage-format", |
| 63 | + "FOREST", |
| 64 | + "blocks", |
| 65 | + "import", |
| 66 | + "src/test/resources/qbft/migration-ibft1/ibft.blocks"); |
| 67 | + |
| 68 | + processBuilder.directory(new File(System.getProperty("user.dir"))); |
| 69 | + processBuilder.inheritIO(); // This will redirect the output to the console |
| 70 | + |
| 71 | + Process process = processBuilder.start(); |
| 72 | + int exitCode = process.waitFor(); |
| 73 | + if (exitCode == 0) { |
| 74 | + System.out.println("Import command executed successfully."); |
| 75 | + } else { |
| 76 | + throw new RuntimeException("Import command execution failed with exit code: " + exitCode); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void shouldImportIBFTBlocksAndTransitionToQBFT() throws Exception { |
| 82 | + |
| 83 | + // Create a mix of Bonsai and Forest DB nodes |
| 84 | + final BesuNode minerNode1 = |
| 85 | + besu.createQbftMigrationNode("miner1", false, DataStorageFormat.FOREST); |
| 86 | + final BesuNode minerNode2 = |
| 87 | + besu.createQbftMigrationNode("miner2", false, DataStorageFormat.FOREST); |
| 88 | + final BesuNode minerNode3 = |
| 89 | + besu.createQbftMigrationNode("miner3", false, DataStorageFormat.FOREST); |
| 90 | + final BesuNode minerNode4 = |
| 91 | + besu.createQbftMigrationNode("miner4", false, DataStorageFormat.FOREST); |
| 92 | + final BesuNode minerNode5 = |
| 93 | + besu.createQbftMigrationNode("miner5", false, DataStorageFormat.FOREST); |
| 94 | + |
| 95 | + // Copy key files to the node datadirs |
| 96 | + // Use the key files saved in resources directory |
| 97 | + copyKeyFilesToNodeDataDirs(minerNode1, minerNode2, minerNode3, minerNode4, minerNode5); |
| 98 | + |
| 99 | + // start one node and import blocks from import file |
| 100 | + // Use import file, genesis saved in resources directory |
| 101 | + |
| 102 | + runBesuCommand(minerNode1.homeDirectory()); |
| 103 | + |
| 104 | + // After the import is done, start the rest of the nodes using the same genesis and respective |
| 105 | + // node keys |
| 106 | + |
| 107 | + cluster.start(minerNode1, minerNode2, minerNode3, minerNode4, minerNode5); |
| 108 | + |
| 109 | + // Check that the chain is progressing as expected |
| 110 | + cluster.verify(blockchain.reachesHeight(minerNode2, 1, 120)); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void tearDownAcceptanceTestBase() { |
| 115 | + cluster.stop(); |
| 116 | + super.tearDownAcceptanceTestBase(); |
| 117 | + } |
| 118 | +} |
0 commit comments