Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Commit

Permalink
Use config object to encapsulate parameters and ensure immutability
Browse files Browse the repository at this point in the history
  • Loading branch information
Gal Rogozinski committed Feb 18, 2019
1 parent 4d8bee0 commit 9135f7a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/iota/iri/conf/BaseIotaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.iota.iri.IRI;
import com.iota.iri.crypto.SpongeFactory;
import com.iota.iri.model.Hash;
import com.iota.iri.model.HashFactory;
import com.iota.iri.utils.IotaUtils;
import org.apache.commons.lang3.ArrayUtils;

Expand Down Expand Up @@ -594,6 +596,11 @@ public int getMilestoneStartIndex() {
return Defaults.MILESTONE_START_INDEX;
}

@Override
public int getMaxMilestoneIndex() {
return Defaults.MAX_MILESTONE_INDEX;
}

@Override
public int getNumberOfKeysInMilestone() {
return Defaults.NUM_KEYS_IN_MILESTONE;
Expand Down Expand Up @@ -734,7 +741,7 @@ protected void setCacheSizeBytes(int cacheSizeBytes) {
}

@Override
public String getCoordinator() {
public Hash getCoordinator() {
return Defaults.COORDINATOR_ADDRESS;
}

Expand Down Expand Up @@ -873,11 +880,12 @@ public interface Defaults {
int POW_THREADS = 0;

//Coo
String COORDINATOR_ADDRESS =
"KPWCHICGJZXKE9GSUDXZYUAPLHAKAHYHDXNPHENTERYMMBQOPSQIDENXKLKCEYCPVTZQLEEJVYJZV9BWU";
Hash COORDINATOR_ADDRESS = HashFactory.ADDRESS.create(
"KPWCHICGJZXKE9GSUDXZYUAPLHAKAHYHDXNPHENTERYMMBQOPSQIDENXKLKCEYCPVTZQLEEJVYJZV9BWU");
int COORDINATOR_SECURITY_LEVEL = 1;
SpongeFactory.Mode COORDINATOR_SIGNATURE_MODE = SpongeFactory.Mode.CURLP27;
int NUM_KEYS_IN_MILESTONE = 20;
int MAX_MILESTONE_INDEX = 1 << NUM_KEYS_IN_MILESTONE;

//Snapshot
boolean LOCAL_SNAPSHOTS_ENABLED = true;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/iota/iri/conf/MilestoneConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.iota.iri.conf;

import com.iota.iri.crypto.SpongeFactory;
import com.iota.iri.model.Hash;

/**
* Configs that should be used for tracking milestones
Expand All @@ -10,7 +11,7 @@ public interface MilestoneConfig extends Config {
/**
* @return Descriptions#COORDINATOR
*/
String getCoordinator();
Hash getCoordinator();

/**
* @return {@value Descriptions#DONT_VALIDATE_TESTNET_MILESTONE_SIG}
Expand All @@ -23,6 +24,12 @@ public interface MilestoneConfig extends Config {
*/
int getNumberOfKeysInMilestone();

/**
* This is a meta-config. Its value depends on {@link #getNumberOfKeysInMilestone()}
* @return the maximal amount of possible milestones that can be issued
*/
int getMaxMilestoneIndex();

/**
* Default Value: {@value BaseIotaConfig.Defaults#COORDINATOR_SECURITY_LEVEL}
* @return {@value Descriptions#COORDINATOR_SECURITY_LEVEL}
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/iota/iri/conf/TestnetConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.beust.jcommander.ParameterException;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.iota.iri.crypto.SpongeFactory;
import com.iota.iri.model.Hash;
import com.iota.iri.model.HashFactory;

import java.util.Objects;

public class TestnetConfig extends BaseIotaConfig {

protected String coordinator = Defaults.COORDINATOR_ADDRESS;
protected Hash coordinator = Defaults.COORDINATOR_ADDRESS;
protected int numberOfKeysInMilestone = Defaults.KEYS_IN_MILESTONE;
protected int maxMilestoneIndex = Defaults.MAX_MILESTONE_INDEX;
protected int coordinatorSecurityLevel = Defaults.COORDINATOR_SECURITY_LEVEL;

protected boolean dontValidateTestnetMilestoneSig = Defaults.DONT_VALIDATE_MILESTONE_SIG;
Expand All @@ -36,14 +39,14 @@ public boolean isTestnet() {
}

@Override
public String getCoordinator() {
public Hash getCoordinator() {
return coordinator;
}

@JsonProperty
@Parameter(names = "--testnet-coordinator", description = MilestoneConfig.Descriptions.COORDINATOR)
protected void setCoordinator(String coordinator) {
this.coordinator = coordinator;
this.coordinator = HashFactory.ADDRESS.create(coordinator);
}

@Override
Expand All @@ -66,6 +69,12 @@ public int getNumberOfKeysInMilestone() {
@Parameter(names = "--milestone-keys", description = MilestoneConfig.Descriptions.NUMBER_OF_KEYS_IN_A_MILESTONE)
protected void setNumberOfKeysInMilestone(int numberOfKeysInMilestone) {
this.numberOfKeysInMilestone = numberOfKeysInMilestone;
this.maxMilestoneIndex = 1 << numberOfKeysInMilestone;
}

@Override
public int getMaxMilestoneIndex() {
return maxMilestoneIndex;
}

@Override
Expand Down Expand Up @@ -186,11 +195,13 @@ public void setDbLogPath(String dbLogPath) {
}

public interface Defaults {
String COORDINATOR_ADDRESS = "EQQFCZBIHRHWPXKMTOLMYUYPCN9XLMJPYZVFJSAY9FQHCCLWTOLLUGKKMXYFDBOOYFBLBI9WUEILGECYM";
Hash COORDINATOR_ADDRESS = HashFactory.ADDRESS.create(
"EQQFCZBIHRHWPXKMTOLMYUYPCN9XLMJPYZVFJSAY9FQHCCLWTOLLUGKKMXYFDBOOYFBLBI9WUEILGECYM");
boolean DONT_VALIDATE_MILESTONE_SIG = false;
int COORDINATOR_SECURITY_LEVEL = 1;
SpongeFactory.Mode COORDINATOR_SIGNATURE_MODE = SpongeFactory.Mode.CURLP27;
int KEYS_IN_MILESTONE = 22;
int MAX_MILESTONE_INDEX = 1 << KEYS_IN_MILESTONE;

String LOCAL_SNAPSHOTS_BASE_PATH = "testnet";
String SNAPSHOT_FILE = "/snapshotTestnet.txt";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.iota.iri.service.milestone.impl;

import com.iota.iri.BundleValidator;
import com.iota.iri.conf.ConsensusConfig;
import com.iota.iri.conf.MilestoneConfig;
import com.iota.iri.controllers.MilestoneViewModel;
import com.iota.iri.controllers.TransactionViewModel;
import com.iota.iri.crypto.Curl;
Expand Down Expand Up @@ -59,34 +59,10 @@ public class MilestoneServiceImpl implements MilestoneService {
private SnapshotService snapshotService;

/**
* Holds the coordinator address which is used to validate signatures.<br />
* Configurations for milestone
*/
private Hash coordinatorAddress;
private MilestoneConfig config;

/**
* Holds the coordinator signature mode which is used to validate signatures.<br />
*/
private SpongeFactory.Mode coordinatorSignatureMode;

/**
* Holds the coordinator security level is used to validate signatures.<br />
*/
private int coordinatorSecurityLevel;

/**
* Holds the depth of Merkle tree used for coordinator signatures.<br />
*/
private int numberOfKeysInMilestone;

/**
* Specifies if signatures should be validated.<br />
=======
* Holds the config with important milestone specific settings.<br />
>>>>>>> dev
*/
private boolean skipValidation;

private int maxMilestoneIndex;

/**
* This method initializes the instance and registers its dependencies.<br />
Expand All @@ -106,18 +82,12 @@ public class MilestoneServiceImpl implements MilestoneService {
* @return the initialized instance itself to allow chaining
*/
public MilestoneServiceImpl init(Tangle tangle, SnapshotProvider snapshotProvider, SnapshotService snapshotService,
ConsensusConfig config) {
MilestoneConfig config) {

this.tangle = tangle;
this.snapshotProvider = snapshotProvider;
this.snapshotService = snapshotService;

coordinatorAddress = HashFactory.ADDRESS.create(config.getCoordinator());
coordinatorSignatureMode = config.getCoordinatorSignatureMode();
coordinatorSecurityLevel = config.getCoordinatorSecurityLevel();
numberOfKeysInMilestone = config.getNumberOfKeysInMilestone();
skipValidation = (config.isTestnet() && config.isDontValidateTestnetMilestoneSig());
maxMilestoneIndex = 1 << config.getNumberOfKeysInMilestone();
this.config = config;

return this;
}
Expand Down Expand Up @@ -186,7 +156,7 @@ public void resetCorruptedMilestone(int index) throws MilestoneException {
public MilestoneValidity validateMilestone(TransactionViewModel transactionViewModel, int milestoneIndex)
throws MilestoneException {

if (milestoneIndex < 0 || milestoneIndex >= maxMilestoneIndex) {
if (milestoneIndex < 0 || milestoneIndex >= config.getMaxMilestoneIndex()) {
return INVALID;
}

Expand All @@ -207,6 +177,7 @@ public MilestoneValidity validateMilestone(TransactionViewModel transactionViewM
if (tail.getHash().equals(transactionViewModel.getHash())) {
//the signed transaction - which references the confirmed transactions and contains
// the Merkle tree siblings.
int coordinatorSecurityLevel = config.getCoordinatorSecurityLevel();
final TransactionViewModel siblingsTx =
bundleTransactionViewModels.get(coordinatorSecurityLevel);

Expand All @@ -218,6 +189,7 @@ public MilestoneValidity validateMilestone(TransactionViewModel transactionViewM
ByteBuffer bb = ByteBuffer.allocate(Curl.HASH_LENGTH * coordinatorSecurityLevel);
byte[] digest = new byte[Curl.HASH_LENGTH];

SpongeFactory.Mode coordinatorSignatureMode = config.getCoordinatorSignatureMode();
for (int i = 0; i < coordinatorSecurityLevel; i++) {
ISSInPlace.digest(coordinatorSignatureMode, signedHash,
ISS.NUMBER_OF_FRAGMENT_CHUNKS * i,
Expand All @@ -230,8 +202,9 @@ public MilestoneValidity validateMilestone(TransactionViewModel transactionViewM

//validate Merkle path
byte[] merkleRoot = ISS.getMerkleRoot(coordinatorSignatureMode, address,
siblingsTx.trits(), 0, milestoneIndex, numberOfKeysInMilestone);
if (skipValidation || coordinatorAddress.equals(HashFactory.ADDRESS.create(merkleRoot))) {
siblingsTx.trits(), 0, milestoneIndex, config.getNumberOfKeysInMilestone());
boolean skipValidation = config.isTestnet() && config.isDontValidateTestnetMilestoneSig();
if (skipValidation || config.getCoordinator().equals(HashFactory.ADDRESS.create(merkleRoot))) {
MilestoneViewModel newMilestoneViewModel = new MilestoneViewModel(milestoneIndex,
transactionViewModel.getHash());
newMilestoneViewModel.store(tangle);
Expand Down

0 comments on commit 9135f7a

Please sign in to comment.