This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
[PIE-1810] Update export subcommand to export blocks in rlp format #1852
Merged
mbaxter
merged 7 commits into
PegaSysEng:master
from
mbaxter:PIE-1810/export-blocks-to-rlp
Aug 14, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f43299
Rearrange import / export utilities for symmetry, export rlp format
mbaxter 4391172
Update subcommand tests
mbaxter 9614d6c
Error out if there is no data to export
mbaxter eb840cf
Pass blockchain to BlockExporter utility
mbaxter 3208f46
Add tests for rlp block exporter
mbaxter 2aa726d
Merge branch 'master' into PIE-1810/export-blocks-to-rlp
mbaxter 38c4361
Remove redundant check
mbaxter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
pantheon/src/main/java/tech/pegasys/pantheon/chainexport/BlockExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2018 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.chainexport; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import tech.pegasys.pantheon.ethereum.chain.Blockchain; | ||
import tech.pegasys.pantheon.ethereum.core.Block; | ||
import tech.pegasys.pantheon.ethereum.core.BlockHeader; | ||
import tech.pegasys.pantheon.ethereum.core.Hash; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
/** Pantheon Block Export Util. */ | ||
public abstract class BlockExporter { | ||
private static final Logger LOG = LogManager.getLogger(); | ||
private final Blockchain blockchain; | ||
|
||
protected BlockExporter(final Blockchain blockchain) { | ||
this.blockchain = blockchain; | ||
} | ||
|
||
/** | ||
* Export blocks that are stored in Pantheon's block storage. | ||
* | ||
* @param outputFile the path at which to save the exported block data | ||
* @param maybeStartBlock the starting index of the block list to export (inclusive) | ||
* @param maybeEndBlock the ending index of the block list to export (exclusive), if not specified | ||
* a single block will be export | ||
* @throws IOException if an I/O error occurs while writing data to disk | ||
*/ | ||
public void exportBlocks( | ||
final File outputFile, | ||
final Optional<Long> maybeStartBlock, | ||
final Optional<Long> maybeEndBlock) | ||
throws IOException { | ||
|
||
// Get range to export | ||
final long startBlock = maybeStartBlock.orElse(BlockHeader.GENESIS_BLOCK_NUMBER); | ||
final long endBlock = maybeEndBlock.orElse(blockchain.getChainHeadBlockNumber() + 1L); | ||
checkArgument(startBlock >= 0 && endBlock >= 0, "Start and end blocks must be greater than 0."); | ||
checkArgument(startBlock < endBlock, "Start block must be less than end block"); | ||
|
||
// Append to file if a range is specified | ||
final boolean append = maybeStartBlock.isPresent(); | ||
FileOutputStream outputStream = new FileOutputStream(outputFile, append); | ||
|
||
LOG.info( | ||
"Exporting blocks [{},{}) to file {} (appending: {})", | ||
startBlock, | ||
endBlock, | ||
outputFile.toString(), | ||
Boolean.toString(append)); | ||
|
||
long blockNumber = 0L; | ||
for (long i = startBlock; i < endBlock; i++) { | ||
Optional<Hash> blockHash = blockchain.getBlockHashByNumber(i); | ||
if (blockHash.isEmpty()) { | ||
LOG.warn("Unable to export blocks [{} - {}). Blocks not found.", i, endBlock); | ||
break; | ||
} | ||
|
||
final Block block = blockchain.getBlockByHash(blockHash.get()); | ||
blockNumber = block.getHeader().getNumber(); | ||
if (blockNumber % 100 == 0) { | ||
LOG.info("Export at block {}", blockNumber); | ||
} | ||
|
||
exportBlock(outputStream, block); | ||
} | ||
|
||
outputStream.close(); | ||
LOG.info("Export complete at block {}", blockNumber); | ||
} | ||
|
||
protected abstract void exportBlock(final FileOutputStream outputStream, final Block block) | ||
throws IOException; | ||
} |
35 changes: 35 additions & 0 deletions
35
pantheon/src/main/java/tech/pegasys/pantheon/chainexport/RlpBlockExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.chainexport; | ||
|
||
import tech.pegasys.pantheon.ethereum.chain.Blockchain; | ||
import tech.pegasys.pantheon.ethereum.core.Block; | ||
import tech.pegasys.pantheon.ethereum.rlp.RLP; | ||
import tech.pegasys.pantheon.util.bytes.BytesValue; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
|
||
public class RlpBlockExporter extends BlockExporter { | ||
|
||
public RlpBlockExporter(final Blockchain blockchain) { | ||
super(blockchain); | ||
} | ||
|
||
@Override | ||
protected void exportBlock(final FileOutputStream outputStream, final Block block) | ||
throws IOException { | ||
final BytesValue rlp = RLP.encode(block::writeTo); | ||
outputStream.write(rlp.getArrayUnsafe()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: Are you writing binary data in the file ? Is it what we want ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, this matches the format the |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
pantheon/src/main/java/tech/pegasys/pantheon/cli/subcommands/blocks/BlockExportFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.pantheon.cli.subcommands.blocks; | ||
|
||
public enum BlockExportFormat { | ||
RLP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[optional] maybe a check arg that startblock <= chainHead?