Skip to content

Commit

Permalink
remove redundant methods in forkId and modify fork collection to use …
Browse files Browse the repository at this point in the history
…a list throughout

Signed-off-by: SteveM <SteveM@myEtherWallet.com>
  • Loading branch information
SteveMieskoski committed Nov 30, 2019
1 parent 9c946a8 commit 35f012f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.hyperledger.besu.config.JsonUtil.normalizeKeys;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Streams;
import com.google.common.io.Resources;
Expand Down Expand Up @@ -137,17 +133,17 @@ public long getTimestamp() {
return parseLong("timestamp", JsonUtil.getValueAsString(configRoot, "timestamp", "0x0"));
}

// TODO look into how to handle invalid or exceptional (i.e. null or "") fork values
public List<Long> getForks() {
return JsonUtil.getObjectNode(configRoot, "config").stream()
.flatMap(node ->
Streams.stream(node.fieldNames())
.filter(name -> !name.toLowerCase().equals("chainid"))
.filter(name -> node.get(name).canConvertToLong())
.filter(name -> name.contains("block"))
.map(name -> node.get(name).asLong())
)
.collect(toList());
.flatMap(
node ->
Streams.stream(node.fieldNames())
.filter(name -> !name.toLowerCase().equals("chainid"))
.filter(name -> node.get(name).canConvertToLong())
.filter(name -> name.contains("block"))
.map(name -> node.get(name).asLong()))
.distinct()
.collect(toList());
}

private String getRequiredString(final String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.CRC32;

public class ForkIdManager {
Expand All @@ -41,7 +39,7 @@ public class ForkIdManager {
private ForkId lastKnownEntry = null;
private ArrayDeque<ForkId> forkAndHashList;

public ForkIdManager(final Hash genesisHash, final Set<Long> forks, final Long currentHead) {
public ForkIdManager(final Hash genesisHash, final List<Long> forks, final Long currentHead) {
this.genesisHash = genesisHash;
this.currentHead = currentHead;
if (forks != null) {
Expand All @@ -56,17 +54,15 @@ static ForkIdManager buildCollection(
if (forks == null) {
return new ForkIdManager(genesisHash, null, blockchain.getChainHeadBlockNumber());
} else {
Set<Long> forkSet = new LinkedHashSet<>(forks);
return new ForkIdManager(genesisHash, forkSet, blockchain.getChainHeadBlockNumber());
return new ForkIdManager(genesisHash, forks, blockchain.getChainHeadBlockNumber());
}
};

public static ForkIdManager buildCollection(final Hash genesisHash, final List<Long> forks) {
if (forks == null) {
return new ForkIdManager(genesisHash, null, Long.MAX_VALUE);
} else {
Set<Long> forkSet = new LinkedHashSet<>(forks);
return new ForkIdManager(genesisHash, forkSet, Long.MAX_VALUE);
return new ForkIdManager(genesisHash, forks, Long.MAX_VALUE);
}
};

Expand Down Expand Up @@ -100,8 +96,6 @@ public static ForkId readFrom(final RLPInput in) {
return new ForkId(hash, next);
}



/**
* EIP-2124 behaviour
*
Expand Down Expand Up @@ -146,11 +140,11 @@ public boolean peerCheck(final ForkId forkId) {
/**
* Non EIP-2124 behaviour
*
* @param peerGenesisOrCheckSumHash Hash or checksum to be validated.
* @param peerGenesisHash Hash to be validated.
* @return boolean
*/
public boolean peerCheck(final Bytes32 peerGenesisOrCheckSumHash) {
return !peerGenesisOrCheckSumHash.equals(genesisHash);
public boolean peerCheck(final Bytes32 peerGenesisHash) {
return !peerGenesisHash.equals(genesisHash);
}

private boolean isHashKnown(final BytesValue forkHash) {
Expand Down Expand Up @@ -189,8 +183,8 @@ private boolean isRemoteAwareOfPresent(final BytesValue forkHash, final Long nex
return false;
}

// TODO: sort these when the list of forks is first gathered
private ArrayDeque<ForkId> collectForksAndHashes(final Set<Long> forks, final Long currentHead) {
// TODO: should sort these when first gathering the list of forks to ensure order
private ArrayDeque<ForkId> collectForksAndHashes(final List<Long> forks, final Long currentHead) {
boolean first = true;
ArrayDeque<ForkId> forkList = new ArrayDeque<>();
Iterator<Long> iterator = forks.iterator();
Expand Down Expand Up @@ -235,7 +229,8 @@ private void updateCrc(final Long block) {
}

private BytesValue updateCrc(final String hash) {
byte[] byteRepresentation = hexStringToByteArray(hash);
BytesValue bv = BytesValue.fromHexString(hash);
byte[] byteRepresentation = bv.extractArray();
crc.update(byteRepresentation, 0, byteRepresentation.length);
return getCurrentCrcHash();
}
Expand All @@ -244,8 +239,6 @@ private BytesValue getCurrentCrcHash() {
return BytesValues.ofUnsignedInt(crc.getValue());
}

// TODO use Hash class instead of string for checksum. convert to or from string only when needed
// ^ the crc is not hashed/checksum-ed any further so the hash class is not suited for this case
public static class ForkId {
BytesValue hash;
BytesValue next;
Expand All @@ -258,12 +251,12 @@ public static class ForkId {
}

ForkId(final String hash, final String next) {
this.hash = BytesValue.wrap(hexStringToByteArray(hash));
this.hash = BytesValue.fromHexString((hash.length() % 2 == 0 ? "" : "0") + hash);
if (this.hash.size() < 4) {
this.hash = padToEightBytes(this.hash);
}
if (next.equals("") || next.equals("0x")) {
this.next = BytesValue.wrap(hexStringToByteArray("0x"));
this.next = BytesValue.EMPTY;
} else if (next.startsWith("0x")) {
long asLong = Long.parseLong(next.replaceFirst("0x", ""), 16);
this.next = BytesValues.trimLeadingZeros(BytesValue.wrap(longToBigEndian(asLong)));
Expand All @@ -274,7 +267,7 @@ public static class ForkId {
}

ForkId(final String hash, final long next) {
this.hash = BytesValue.wrap(hexStringToByteArray(hash));
this.hash = BytesValue.fromHexString(hash);
this.next = BytesValue.wrap(longToBigEndian(next));
createForkIdRLP();
}
Expand Down Expand Up @@ -340,7 +333,7 @@ public List<ForkId> asList() {
private static BytesValue padToEightBytes(final BytesValue hash) {
if (hash.size() < 4) {
BytesValue padded =
BytesValues.concatenate(hash, BytesValue.wrap(hexStringToByteArray("0x00")));
BytesValues.concatenate(hash, BytesValue.fromHexString("0x00"));
return padToEightBytes(padded);
} else {
return hash;
Expand Down Expand Up @@ -368,17 +361,7 @@ public int hashCode() {
}
}

// TODO: Ask / look to see if there is a helper for these below <----------
private static byte[] hexStringToByteArray(final String s) {
String string = "";
if (s.startsWith("0x")) {
string = s.replaceFirst("0x", "");
}
string = (string.length() % 2 == 0 ? "" : "0") + string;
return decodeHexString(string);
}

// next three methods adopted from:
// next two methods adopted from:
// https://github.com/bcgit/bc-java/blob/master/core/src/main/java/org/bouncycastle/util/Pack.java
private static byte[] longToBigEndian(final long n) {
byte[] bs = new byte[8];
Expand All @@ -394,30 +377,4 @@ private static void intToBigEndian(final int n, final byte[] bs, int off) {
bs[++off] = (byte) (n >>> 8);
bs[++off] = (byte) (n);
}

private static byte[] decodeHexString(final String hexString) {
if (hexString.length() % 2 == 1) {
throw new IllegalArgumentException("Invalid hexadecimal String supplied.");
}

byte[] bytes = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i += 2) {
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
}
return bytes;
}

private static byte hexToByte(final String hexString) {
int firstDigit = toDigit(hexString.charAt(0));
int secondDigit = toDigit(hexString.charAt(1));
return (byte) ((firstDigit << 4) + secondDigit);
}

private static int toDigit(final char hexChar) {
int digit = Character.digit(hexChar, 16);
if (digit == -1) {
throw new IllegalArgumentException("Invalid Hexadecimal Character: " + hexChar);
}
return digit;
}
}
Loading

0 comments on commit 35f012f

Please sign in to comment.