Skip to content

Commit

Permalink
Using EnumMap in chain config for increased performance and code main…
Browse files Browse the repository at this point in the history
…tenance.
  • Loading branch information
AlexandraRoatis committed Oct 9, 2019
1 parent c3328ad commit c7d6615
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import org.aion.equihash.OptimizedEquiValidator;
Expand Down Expand Up @@ -127,9 +127,9 @@ public BlockHeaderValidator createBlockHeaderValidator() {
new EnergyConsumedRule(),
new SignatureRule());

Map<Byte, List<BlockHeaderRule>> unityRules = new HashMap<>();
unityRules.put(BlockSealType.SEAL_POW_BLOCK.getSealId(), powRules);
unityRules.put(BlockSealType.SEAL_POS_BLOCK.getSealId(), posRules);
Map<BlockSealType, List<BlockHeaderRule>> unityRules = new EnumMap<>(BlockSealType.class);
unityRules.put(BlockSealType.SEAL_POW_BLOCK, powRules);
unityRules.put(BlockSealType.SEAL_POS_BLOCK, posRules);

return new BlockHeaderValidator(unityRules);
}
Expand All @@ -138,8 +138,8 @@ public ParentBlockHeaderValidator createSealParentBlockHeaderValidator() {
List<DependentBlockHeaderRule> posRules =
Arrays.asList(new StakingSeedRule(), new StakingBlockTimeStampRule());

Map<Byte, List<DependentBlockHeaderRule>> unityRules = new HashMap<>();
unityRules.put(BlockSealType.SEAL_POS_BLOCK.getSealId(), posRules);
Map<BlockSealType, List<DependentBlockHeaderRule>> unityRules = new EnumMap<>(BlockSealType.class);
unityRules.put(BlockSealType.SEAL_POS_BLOCK, posRules);

return new ParentBlockHeaderValidator(unityRules);
}
Expand All @@ -149,8 +149,8 @@ public GrandParentBlockHeaderValidator createPreUnityGrandParentHeaderValidator(
List<GrandParentDependantBlockHeaderRule> powRules =
Collections.singletonList(new AionDifficultyRule(this));

Map<Byte, List<GrandParentDependantBlockHeaderRule>> unityRules = new HashMap<>();
unityRules.put(BlockSealType.SEAL_POW_BLOCK.getSealId(), powRules);
Map<BlockSealType, List<GrandParentDependantBlockHeaderRule>> unityRules = new EnumMap<>(BlockSealType.class);
unityRules.put(BlockSealType.SEAL_POW_BLOCK, powRules);

return new GrandParentBlockHeaderValidator(unityRules);
}
Expand All @@ -165,9 +165,9 @@ public GrandParentBlockHeaderValidator createUnityGrandParentHeaderValidator() {
List<GrandParentDependantBlockHeaderRule> posRules =
Collections.singletonList(new UnityDifficultyRule(this));

Map<Byte, List<GrandParentDependantBlockHeaderRule>> unityRules = new HashMap<>();
unityRules.put(BlockSealType.SEAL_POW_BLOCK.getSealId(), powRules);
unityRules.put(BlockSealType.SEAL_POS_BLOCK.getSealId(), posRules);
Map<BlockSealType, List<GrandParentDependantBlockHeaderRule>> unityRules = new EnumMap<>(BlockSealType.class);
unityRules.put(BlockSealType.SEAL_POW_BLOCK, powRules);
unityRules.put(BlockSealType.SEAL_POS_BLOCK, posRules);

return new GrandParentBlockHeaderValidator(unityRules);
}
Expand All @@ -181,9 +181,9 @@ public ParentBlockHeaderValidator createChainParentBlockHeaderValidator() {
getConstants().getEnergyDivisorLimitLong(),
getConstants().getEnergyLowerBoundLong()));

Map<Byte, List<DependentBlockHeaderRule>> unityRules = new HashMap<>();
unityRules.put(BlockSealType.SEAL_POW_BLOCK.getSealId(), rules);
unityRules.put(BlockSealType.SEAL_POS_BLOCK.getSealId(), rules);
Map<BlockSealType, List<DependentBlockHeaderRule>> unityRules = new EnumMap<>(BlockSealType.class);
unityRules.put(BlockSealType.SEAL_POW_BLOCK, rules);
unityRules.put(BlockSealType.SEAL_POS_BLOCK, rules);

return new ParentBlockHeaderValidator(unityRules);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -372,11 +373,9 @@ public BlockHeaderValidator createBlockHeaderValidator() {
.getMaximumExtraDataSize()),
new EnergyConsumedRule());

Map<Byte, List<BlockHeaderRule>> unityRules = new HashMap<>();
unityRules.put(
BlockSealType.SEAL_POW_BLOCK.getSealId(), powRules);
unityRules.put(
BlockSealType.SEAL_POS_BLOCK.getSealId(), posRules);
Map<BlockSealType, List<BlockHeaderRule>> unityRules = new EnumMap<>(BlockSealType.class);
unityRules.put(BlockSealType.SEAL_POW_BLOCK, powRules);
unityRules.put(BlockSealType.SEAL_POS_BLOCK, posRules);

return new BlockHeaderValidator(unityRules);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

import java.util.Map;
import org.aion.mcf.blockchain.BlockHeader;
import org.aion.mcf.blockchain.BlockHeader.BlockSealType;
import org.slf4j.Logger;

public class BlockHeaderValidator {

private Map<Byte, List<BlockHeaderRule>> chainRules;
private Map<BlockSealType, List<BlockHeaderRule>> chainRules;

public BlockHeaderValidator(Map<Byte, List<BlockHeaderRule>> rules) {
public BlockHeaderValidator(Map<BlockSealType, List<BlockHeaderRule>> rules) {
if (rules == null) {
throw new NullPointerException("The blockHeaderRule can not be null");
}
Expand All @@ -27,7 +28,7 @@ public boolean validate(BlockHeader header, Logger logger) {
return false;
}

List<BlockHeaderRule> rules = chainRules.get(header.getSealType().getSealId());
List<BlockHeaderRule> rules = chainRules.get(header.getSealType());
if (rules == null) {
return false;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import java.util.List;
import java.util.Map;
import org.aion.mcf.blockchain.BlockHeader;
import org.aion.mcf.blockchain.BlockHeader.BlockSealType;
import org.slf4j.Logger;

public class GrandParentBlockHeaderValidator {

private Map<Byte, List<GrandParentDependantBlockHeaderRule>> chainRules;
private Map<BlockSealType, List<GrandParentDependantBlockHeaderRule>> chainRules;

public GrandParentBlockHeaderValidator(
Map<Byte, List<GrandParentDependantBlockHeaderRule>> rules) {
Map<BlockSealType, List<GrandParentDependantBlockHeaderRule>> rules) {
if (rules == null) {
throw new NullPointerException();
}
Expand All @@ -35,7 +36,7 @@ public boolean validate(
return false;
}

List<GrandParentDependantBlockHeaderRule> rules = chainRules.get(current.getSealType().getSealId());
List<GrandParentDependantBlockHeaderRule> rules = chainRules.get(current.getSealType());

if (rules == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import java.util.List;
import java.util.Map;
import org.aion.mcf.blockchain.BlockHeader;
import org.aion.mcf.blockchain.BlockHeader.BlockSealType;
import org.slf4j.Logger;

/** validation rules depending on parent's block header */
public class ParentBlockHeaderValidator {

private Map<Byte, List<DependentBlockHeaderRule>> chainRules;
private Map<BlockSealType, List<DependentBlockHeaderRule>> chainRules;

public ParentBlockHeaderValidator(Map<Byte, List<DependentBlockHeaderRule>> rules) {
public ParentBlockHeaderValidator(Map<BlockSealType, List<DependentBlockHeaderRule>> rules) {
if (rules == null) {
throw new NullPointerException();
}
Expand All @@ -39,7 +40,7 @@ public boolean validate(
return false;
}

List<DependentBlockHeaderRule> rules = chainRules.get(header.getSealType().getSealId());
List<DependentBlockHeaderRule> rules = chainRules.get(header.getSealType());

if (rules == null) {
return false;
Expand Down

0 comments on commit c7d6615

Please sign in to comment.