Rename Fields in ForkIdManager (#2091)

Signed-off-by: Ratan Rai Sur <ratan.r.sur@gmail.com>
pull/2099/head
Ratan (Rai) Sur 4 years ago committed by GitHub
parent c7e72434b9
commit 3d4a2974ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      besu/src/test/java/org/hyperledger/besu/ForkIdsTest.java
  2. 5
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/EthProtocolManager.java
  3. 43
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/ForkIdManager.java
  4. 3
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/ForkIdBackwardCompatibilityTest.java
  5. 6
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/manager/EIP2124Test.java

@ -168,7 +168,7 @@ public class ForkIdsTest {
.map( .map(
block -> { block -> {
blockNumber.set(block); blockNumber.set(block);
return forkIdManager.computeForkId(); return forkIdManager.getForkIdForChainHead();
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());

@ -275,7 +275,8 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver {
} }
final Capability cap = connection.capability(getSupportedProtocol()); final Capability cap = connection.capability(getSupportedProtocol());
final ForkId latestForkId = cap.getVersion() >= 64 ? forkIdManager.computeForkId() : null; final ForkId latestForkId =
cap.getVersion() >= 64 ? forkIdManager.getForkIdForChainHead() : null;
// TODO: look to consolidate code below if possible // TODO: look to consolidate code below if possible
// making status non-final and implementing it above would be one way. // making status non-final and implementing it above would be one way.
final StatusMessage status = final StatusMessage status =
@ -361,6 +362,6 @@ public class EthProtocolManager implements ProtocolManager, MinedBlockObserver {
} }
public List<Bytes> getForkIdAsBytesList() { public List<Bytes> getForkIdAsBytesList() {
return forkIdManager.computeForkId().getForkIdAsBytesList(); return forkIdManager.getForkIdForChainHead().getForkIdAsBytesList();
} }
} }

@ -34,9 +34,9 @@ import org.apache.tuweni.bytes.Bytes32;
public class ForkIdManager { public class ForkIdManager {
private final Hash genesisHash; private final Hash genesisHash;
private final List<ForkId> forkAndHashList; private final List<ForkId> forkIds;
private final List<Long> forks; private final List<Long> forkBlockNumbers;
private final LongSupplier chainHeadSupplier; private final LongSupplier chainHeadSupplier;
private final long forkNext; private final long forkNext;
private final boolean onlyZerosForkBlocks; private final boolean onlyZerosForkBlocks;
@ -50,9 +50,9 @@ public class ForkIdManager {
checkNotNull(nonFilteredForks); checkNotNull(nonFilteredForks);
this.chainHeadSupplier = blockchain::getChainHeadBlockNumber; this.chainHeadSupplier = blockchain::getChainHeadBlockNumber;
this.genesisHash = blockchain.getGenesisBlock().getHash(); this.genesisHash = blockchain.getGenesisBlock().getHash();
this.forkAndHashList = new ArrayList<>(); this.forkIds = new ArrayList<>();
this.legacyEth64 = legacyEth64; this.legacyEth64 = legacyEth64;
this.forks = this.forkBlockNumbers =
nonFilteredForks.stream() nonFilteredForks.stream()
.filter(fork -> fork > 0L) .filter(fork -> fork > 0L)
.distinct() .distinct()
@ -60,27 +60,26 @@ public class ForkIdManager {
.collect(Collectors.toUnmodifiableList()); .collect(Collectors.toUnmodifiableList());
this.onlyZerosForkBlocks = nonFilteredForks.stream().allMatch(value -> 0L == value); this.onlyZerosForkBlocks = nonFilteredForks.stream().allMatch(value -> 0L == value);
this.forkNext = createForkIds(); this.forkNext = createForkIds();
this.highestKnownFork = !forks.isEmpty() ? forks.get(forks.size() - 1) : 0L; this.highestKnownFork =
!forkBlockNumbers.isEmpty() ? forkBlockNumbers.get(forkBlockNumbers.size() - 1) : 0L;
} }
public ForkId computeForkId() { public ForkId getForkIdForChainHead() {
if (legacyEth64) { if (legacyEth64) {
return forkAndHashList.isEmpty() ? null : forkAndHashList.get(forkAndHashList.size() - 1); return forkIds.isEmpty() ? null : forkIds.get(forkIds.size() - 1);
} }
final long head = chainHeadSupplier.getAsLong(); final long head = chainHeadSupplier.getAsLong();
for (final ForkId forkId : forkAndHashList) { for (final ForkId forkId : forkIds) {
if (head < forkId.getNext()) { if (head < forkId.getNext()) {
return forkId; return forkId;
} }
} }
return forkAndHashList.isEmpty() return forkIds.isEmpty() ? new ForkId(genesisHashCrc, 0) : forkIds.get(forkIds.size() - 1);
? new ForkId(genesisHashCrc, 0)
: forkAndHashList.get(forkAndHashList.size() - 1);
} }
@VisibleForTesting @VisibleForTesting
List<ForkId> getForkAndHashList() { List<ForkId> getForkIds() {
return this.forkAndHashList; return this.forkIds;
} }
public static ForkId readFrom(final RLPInput in) { public static ForkId readFrom(final RLPInput in) {
@ -136,16 +135,16 @@ public class ForkIdManager {
} }
private boolean isHashKnown(final Bytes forkHash) { private boolean isHashKnown(final Bytes forkHash) {
return forkAndHashList.stream().map(ForkId::getHash).anyMatch(hash -> hash.equals(forkHash)); return forkIds.stream().map(ForkId::getHash).anyMatch(hash -> hash.equals(forkHash));
} }
private boolean isForkKnown(final Long nextFork) { private boolean isForkKnown(final Long nextFork) {
return highestKnownFork < nextFork return highestKnownFork < nextFork
|| forkAndHashList.stream().map(ForkId::getNext).anyMatch(fork -> fork.equals(nextFork)); || forkIds.stream().map(ForkId::getNext).anyMatch(fork -> fork.equals(nextFork));
} }
private boolean isRemoteAwareOfPresent(final Bytes forkHash, final Long nextFork) { private boolean isRemoteAwareOfPresent(final Bytes forkHash, final Long nextFork) {
for (final ForkId j : forkAndHashList) { for (final ForkId j : forkIds) {
if (forkHash.equals(j.getHash())) { if (forkHash.equals(j.getHash())) {
if (nextFork.equals(j.getNext())) { if (nextFork.equals(j.getNext())) {
return true; return true;
@ -164,20 +163,20 @@ public class ForkIdManager {
crc.update(genesisHash.toArray()); crc.update(genesisHash.toArray());
genesisHashCrc = getCurrentCrcHash(crc); genesisHashCrc = getCurrentCrcHash(crc);
final List<Bytes> forkHashes = new ArrayList<>(List.of(genesisHashCrc)); final List<Bytes> forkHashes = new ArrayList<>(List.of(genesisHashCrc));
forks.forEach( forkBlockNumbers.forEach(
fork -> { fork -> {
updateCrc(crc, fork); updateCrc(crc, fork);
forkHashes.add(getCurrentCrcHash(crc)); forkHashes.add(getCurrentCrcHash(crc));
}); });
// This loop is for all the fork hashes that have an associated "next fork" // This loop is for all the fork hashes that have an associated "next fork"
for (int i = 0; i < forks.size(); i++) { for (int i = 0; i < forkBlockNumbers.size(); i++) {
forkAndHashList.add(new ForkId(forkHashes.get(i), forks.get(i))); forkIds.add(new ForkId(forkHashes.get(i), forkBlockNumbers.get(i)));
} }
long forkNext = 0; long forkNext = 0;
if (!forks.isEmpty()) { if (!forkBlockNumbers.isEmpty()) {
forkNext = forkAndHashList.get(forkAndHashList.size() - 1).getNext(); forkNext = forkIds.get(forkIds.size() - 1).getNext();
forkAndHashList.add(new ForkId(forkHashes.get(forkHashes.size() - 1), 0)); forkIds.add(new ForkId(forkHashes.get(forkHashes.size() - 1), 0));
} }
return forkNext; return forkNext;
} }

@ -122,6 +122,7 @@ public class ForkIdBackwardCompatibilityTest {
legacyEth64 legacyEth64
? new LegacyForkIdManager(mockBlockchain(genesisHash, head), forks).getLatestForkId() ? new LegacyForkIdManager(mockBlockchain(genesisHash, head), forks).getLatestForkId()
: null; : null;
assertThat(forkIdManager.computeForkId()).isEqualTo(legacyEth64 ? legacyForkId : wantForkId); assertThat(forkIdManager.getForkIdForChainHead())
.isEqualTo(legacyEth64 ? legacyForkId : wantForkId);
} }
} }

@ -588,10 +588,10 @@ public class EIP2124Test {
LOG.info("Running test case {}", name); LOG.info("Running test case {}", name);
final ForkIdManager forkIdManager = final ForkIdManager forkIdManager =
new ForkIdManager(mockBlockchain(network.hash, head), network.forks, false); new ForkIdManager(mockBlockchain(network.hash, head), network.forks, false);
wantForkId.ifPresent(forkId -> assertThat(forkIdManager.computeForkId()).isEqualTo(forkId)); wantForkId.ifPresent(
forkId -> assertThat(forkIdManager.getForkIdForChainHead()).isEqualTo(forkId));
wantForkIds.ifPresent( wantForkIds.ifPresent(
forkIds -> forkIds -> assertThat(forkIdManager.getForkIds()).containsExactlyElementsOf(forkIds));
assertThat(forkIdManager.getForkAndHashList()).containsExactlyElementsOf(forkIds));
wantPeerCheckCase.ifPresent( wantPeerCheckCase.ifPresent(
peerCheckCase -> peerCheckCase ->
assertThat( assertThat(

Loading…
Cancel
Save