Skip to content

Commit

Permalink
move encoding into enum
Browse files Browse the repository at this point in the history
  • Loading branch information
shemnon committed Feb 25, 2019
1 parent be17af4 commit e178baf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 87 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void encode(final String jsonInput) {
} else {
try {
// encode and write the value
writeOutput(type.getAdapter().encode(jsonInput));
writeOutput(type.encode(jsonInput));
} catch (MismatchedInputException e) {
throw new ParameterException(
spec.commandLine(),
Expand Down
46 changes: 35 additions & 11 deletions pantheon/src/main/java/tech/pegasys/pantheon/cli/rlp/RLPType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,43 @@
*/
package tech.pegasys.pantheon.cli.rlp;

/** Type of the RLP data to encode/decode */
public enum RLPType {
// Enum is used to enable the listing of the possible values in PicoCLI.
IBFT_EXTRA_DATA(new IbftExtraDataCLIAdapter());
import tech.pegasys.pantheon.consensus.ibft.IbftExtraData;
import tech.pegasys.pantheon.ethereum.core.Address;
import tech.pegasys.pantheon.util.bytes.BytesValue;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

private final JSONToRLP adapter;
/**
* Type of the RLP data to encode/decode. Enums are used by PicoCLI so that the values can be shown
* on the command line.
*/
public enum RLPType {
IBFT_EXTRA_DATA {
@Override
public BytesValue encode(final String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
TypeReference<Collection<String>> typeRef = new TypeReference<Collection<String>>() {};
Collection<String> validatorAddresse = mapper.readValue(json, typeRef);

RLPType(final JSONToRLP adapter) {
Collection<Address> addresses =
validatorAddresse.stream().map(Address::fromHexString).collect(Collectors.toList());

this.adapter = adapter;
}
return new IbftExtraData(
BytesValue.wrap(new byte[32]),
Collections.emptyList(),
Optional.empty(),
0,
addresses)
.encode();
}
};

public JSONToRLP getAdapter() {
return adapter;
}
public abstract BytesValue encode(String json) throws IOException;
}

0 comments on commit e178baf

Please sign in to comment.