Coverage Summary for Class: ActivationConfig (org.ethereum.config.blockchain.upgrades)

Class Method, % Line, %
ActivationConfig 100% (7/7) 83.3% (30/36)
ActivationConfig$ForBlock 100% (4/4) 100% (6/6)
Total 100% (11/11) 85.7% (36/42)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package org.ethereum.config.blockchain.upgrades; 20  21 import com.typesafe.config.Config; 22 import com.typesafe.config.ConfigException.WrongType; 23 import com.typesafe.config.ConfigValue; 24  25 import java.util.*; 26 import java.util.stream.Collectors; 27  28 public class ActivationConfig { 29  private static final String PROPERTY_ACTIVATION_HEIGHTS = "hardforkActivationHeights"; 30  private static final String PROPERTY_CONSENSUS_RULES = "consensusRules"; 31  32  private final Map<ConsensusRule, Long> activationHeights; 33  34  public ActivationConfig(Map<ConsensusRule, Long> activationHeights) { 35  if (activationHeights.size() != ConsensusRule.values().length) { 36  List<ConsensusRule> missing = new ArrayList<>(Arrays.asList(ConsensusRule.values())); 37  missing.removeAll(activationHeights.keySet()); 38  throw new IllegalArgumentException(String.format( 39  "The configuration must contain all consensus rule values but is missing [%s]", 40  missing.stream().map(ConsensusRule::getConfigKey).collect(Collectors.joining(", ")) 41  )); 42  } 43  44  this.activationHeights = activationHeights; 45  } 46  47  public boolean isActive(ConsensusRule consensusRule, long blockNumber) { 48  long activationHeight = activationHeights.get(consensusRule); 49  return 0 <= activationHeight && activationHeight <= blockNumber; 50  } 51  52  private boolean isActivating(ConsensusRule consensusRule, long blockNumber) { 53  long activationHeight = activationHeights.get(consensusRule); 54  return activationHeight == blockNumber; 55  } 56  57  public ForBlock forBlock(long blockNumber) { 58  return new ForBlock(blockNumber); 59  } 60  61  public static ActivationConfig read(Config config) { 62  Map<NetworkUpgrade, Long> networkUpgrades = new EnumMap<>(NetworkUpgrade.class); 63  Config networkUpgradesConfig = config.getConfig(PROPERTY_ACTIVATION_HEIGHTS); 64  for (Map.Entry<String, ConfigValue> e : networkUpgradesConfig.entrySet()) { 65  NetworkUpgrade networkUpgrade = NetworkUpgrade.named(e.getKey()); 66  long activationHeight = networkUpgradesConfig.getLong(networkUpgrade.getName()); 67  networkUpgrades.put(networkUpgrade, activationHeight); 68  } 69  70  Map<ConsensusRule, Long> activationHeights = new EnumMap<>(ConsensusRule.class); 71  Config consensusRulesConfig = config.getConfig(PROPERTY_CONSENSUS_RULES); 72  for (Map.Entry<String, ConfigValue> e : consensusRulesConfig.entrySet()) { 73  ConsensusRule consensusRule = ConsensusRule.fromConfigKey(e.getKey()); 74  long activationHeight = parseActivationHeight(networkUpgrades, consensusRulesConfig, consensusRule); 75  activationHeights.put(consensusRule, activationHeight); 76  } 77  78  return new ActivationConfig(activationHeights); 79  } 80  81  private static long parseActivationHeight( 82  Map<NetworkUpgrade, Long> networkUpgrades, 83  Config consensusRulesConfig, 84  ConsensusRule consensusRule) { 85  try { 86  return consensusRulesConfig.getLong(consensusRule.getConfigKey()); 87  } catch (WrongType ex) { 88  NetworkUpgrade networkUpgrade = NetworkUpgrade.named(consensusRulesConfig.getString(consensusRule.getConfigKey())); 89  if (!networkUpgrades.containsKey(networkUpgrade)) { 90  throw new IllegalArgumentException( 91  String.format("Unknown activation height for network upgrade %s", networkUpgrade.getName()) 92  ); 93  } 94  95  return networkUpgrades.get(networkUpgrade); 96  } 97  } 98  99  public class ForBlock { 100  private final long blockNumber; 101  102  private ForBlock(long blockNumber) { 103  this.blockNumber = blockNumber; 104  } 105  106  public boolean isActive(ConsensusRule consensusRule) { 107  return ActivationConfig.this.isActive(consensusRule, blockNumber); 108  } 109  110  public boolean isActivating(ConsensusRule consensusRule) { 111  return ActivationConfig.this.isActivating(consensusRule, blockNumber); 112  } 113  } 114 }