-
Notifications
You must be signed in to change notification settings - Fork 130
[PIE-1809] Add import chain json utility #1832
[PIE-1809] Add import chain json utility #1832
Conversation
@@ -256,4 +322,9 @@ private void outputBlock(final List<Block> blocks) { | |||
} | |||
return metricsService; | |||
} | |||
|
|||
@FunctionalInterface | |||
public interface ChainImporterFactory { |
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.
Why does this need to be an explicit functional interface? Could we not use a Supplier
?
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.
We could use Function<PantheonController<T>, ChainImporter<T>>
instead. But, I prefer making explicit interfaces for reasons of readability and discoverability. It's a pattern we use pretty extensively in the codebase.
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.
Interesting, I find the explicit interface less readable because the parameter looks like it could be a full blown object with more methods. Not a blocking issue though
"Unable to create block using current consensus engine: " | ||
+ genesisConfigOptions.getConsensusEngine()); | ||
} | ||
return maybeBlock.get(); |
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) use orElseThrow
and inline maybeBlock
} | ||
|
||
public void importChain(final String chainJson) throws IOException { | ||
checkDatabaseIsEmpty(); |
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) this sounds stricter than it is, how about warnIfDatabaseNotEmpty
?
@@ -0,0 +1,33 @@ | |||
/* |
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.
Is there a way to parse the blocks directly into a sequence without the need of a separate class?
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.
Maybe? But I think the structure is pretty clear as is and it provides a place to add additional data if we need it in the future.
public void importChain(final String chainJson) throws IOException { | ||
checkDatabaseIsEmpty(); | ||
|
||
ChainData chainData = mapper.readValue(chainJson, ChainData.class); |
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.
Regarding the comment on the ChainData
file. This reads like there's probably more to ChainData
than just a sequence of blocks
public void importChain(final String chainJson) throws IOException { | ||
checkDatabaseIsEmpty(); | ||
|
||
ChainData chainData = mapper.readValue(chainJson, ChainData.class); |
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.
can this be final
?
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.
yep - I always miss the finals 😄
} | ||
|
||
// Check that imported blocks form the canonical chain, if not print a warning | ||
this.checkImportedBlocksAreOnCanonicalChain(importedBlocks); |
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) The comment could probably be removed (and thus not at risk of getting outdated) if the method was something like warnIfImportedBlocksNotOnCanonicalChain
pantheon/src/main/java/tech/pegasys/pantheon/chainimport/ChainImporter.java
Outdated
Show resolved
Hide resolved
+ System.lineSeparator() | ||
+ "This command imports blocks from a file into the database." | ||
+ System.lineSeparator() | ||
+ " --from=<FILE> File containing blocks to import" | ||
+ " --format=<format> The type of data to be imported (default: RLP)." |
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.
Is there somewhere the user can see that JSON is a valid option to this flag?
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.
Good call - will update!
description = "The type of data to be imported (default: ${DEFAULT-VALUE}).", | ||
hidden = true, | ||
description = | ||
"The type of data to be imported, possible values are: ${COMPLETION-CANDIDATES} (default: ${DEFAULT-VALUE}).", |
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.
I didn't know ${COMPLETION-CANDIDATES}
existed. Looks like I should open another PR to add this to other options as well.
PR description
Add a CLI utility for importing blocks from a json file containing minimally specified block and transaction data.
Example command:
Example import file:
Example output: