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

Commit

Permalink
refactor milestone signature parameters to be configurable (#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
alon-e authored and GalRogozinski committed Feb 27, 2019
1 parent f0be37b commit 9bb3b92
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 72 deletions.
42 changes: 31 additions & 11 deletions src/main/java/com/iota/iri/conf/BaseIotaConfig.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.iota.iri.conf;

import com.iota.iri.IRI;
import com.iota.iri.utils.IotaUtils;

import java.util.ArrayList;
import java.util.List;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.fasterxml.jackson.annotation.JsonIgnore;
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;

import java.util.ArrayList;
import java.util.List;

/*
Note: the fields in this class are being deserialized from Jackson so they must follow Java Bean convention.
Meaning that every field must have a getter that is prefixed with `get` unless it is a boolean and then it should be
Expand Down Expand Up @@ -595,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 @@ -735,10 +741,20 @@ protected void setCacheSizeBytes(int cacheSizeBytes) {
}

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

@Override
public int getCoordinatorSecurityLevel() {
return Defaults.COORDINATOR_SECURITY_LEVEL;
}

@Override
public SpongeFactory.Mode getCoordinatorSignatureMode() {
return Defaults.COORDINATOR_SIGNATURE_MODE;
}

@Override
public boolean isDontValidateTestnetMilestoneSig() {
return false;
Expand Down Expand Up @@ -864,8 +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 All @@ -888,7 +908,7 @@ public interface Defaults {
"/previousEpochsSpentAddresses3.txt";
long GLOBAL_SNAPSHOT_TIME = 1545469620;
int MILESTONE_START_INDEX = 933_210;
int NUM_KEYS_IN_MILESTONE = 20;
int MAX_ANALYZED_TXS = 20_000;

}
}
33 changes: 32 additions & 1 deletion src/main/java/com/iota/iri/conf/MilestoneConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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 @@ -8,16 +11,44 @@ public interface MilestoneConfig extends Config {
/**
* @return Descriptions#COORDINATOR
*/
String getCoordinator();
Hash getCoordinator();

/**
* @return {@value Descriptions#DONT_VALIDATE_TESTNET_MILESTONE_SIG}
*/
boolean isDontValidateTestnetMilestoneSig();

/**
* Default Value: {@value BaseIotaConfig.Defaults#NUM_KEYS_IN_MILESTONE}
* @return {@value Descriptions#NUMBER_OF_KEYS_IN_A_MILESTONE}
*/
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}
*/
int getCoordinatorSecurityLevel();

/**
* Default Value: {@link BaseIotaConfig.Defaults#COORDINATOR_SIGNATURE_MODE}
* @return {@value Descriptions#COORDINATOR_SIGNATURE_MODE}
*/
SpongeFactory.Mode getCoordinatorSignatureMode();

interface Descriptions {

String COORDINATOR = "The address of the coordinator";
String COORDINATOR_SECURITY_LEVEL = "The security level used in coordinator signatures";
String COORDINATOR_SIGNATURE_MODE = "The signature mode used in coordinator signatures";
String DONT_VALIDATE_TESTNET_MILESTONE_SIG = "Disable coordinator validation on testnet";
String NUMBER_OF_KEYS_IN_A_MILESTONE = "The depth of the Merkle tree which in turn determines the number of" +
"leaves (private keys) that the coordinator can use to sign a message.";
}
}
6 changes: 0 additions & 6 deletions src/main/java/com/iota/iri/conf/SnapshotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ public interface SnapshotConfig extends Config {
*/
String getLocalSnapshotsBasePath();

/**
* @return {@value Descriptions#NUMBER_OF_KEYS_IN_A_MILESTONE}
*/
int getNumberOfKeysInMilestone();

/**
* @return {@value Descriptions#PREVIOUS_EPOCH_SPENT_ADDRESSES_FILE}
Expand Down Expand Up @@ -94,8 +90,6 @@ interface Descriptions {
String SNAPSHOT_SIGNATURE_FILE = "Path to the file that contains a signature for the snapshot file.";
String MILESTONE_START_INDEX = "The start index of the milestones. This index is encoded in each milestone " +
"transaction by the coordinator.";
String NUMBER_OF_KEYS_IN_A_MILESTONE = "The depth of the Merkle tree which in turn determines the number of" +
"leaves (private keys) that the coordinator can use to sign a message.";
String PREVIOUS_EPOCH_SPENT_ADDRESSES_FILE = "The file that contains the list of all used addresses " +
"from previous epochs";
String SPENT_ADDRESSES_DB_PATH = "The folder where the spent addresses DB saves its data.";
Expand Down
75 changes: 58 additions & 17 deletions src/main/java/com/iota/iri/conf/TestnetConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
import com.beust.jcommander.Parameter;
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;
protected String snapshotFile = Defaults.SNAPSHOT_FILE;
protected String snapshotSignatureFile = Defaults.SNAPSHOT_SIG;
protected long snapshotTime = Defaults.SNAPSHOT_TIME;
protected int mwm = Defaults.MWM;
protected int milestoneStartIndex = Defaults.MILESTONE_START_INDEX;
protected int numberOfKeysInMilestone = Defaults.KEYS_IN_MILESTONE;
protected int transactionPacketSize = Defaults.PACKET_SIZE;
protected int requestHashSize = Defaults.REQUEST_HASH_SIZE;
protected SpongeFactory.Mode coordinatorSignatureMode = Defaults.COORDINATOR_SIGNATURE_MODE;

public TestnetConfig() {
super();
Expand All @@ -32,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 @@ -53,6 +60,45 @@ protected void setDontValidateTestnetMilestoneSig(boolean dontValidateTestnetMil
this.dontValidateTestnetMilestoneSig = dontValidateTestnetMilestoneSig;
}

@Override
public int getNumberOfKeysInMilestone() {
return numberOfKeysInMilestone;
}

@JsonProperty("NUMBER_OF_KEYS_IN_A_MILESTONE")
@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
public int getCoordinatorSecurityLevel() {
return coordinatorSecurityLevel;
}

@JsonProperty("COORDINATOR_SECURITY_LEVEL")
@Parameter(names = "--testnet-coordinator-security-level", description = MilestoneConfig.Descriptions.COORDINATOR_SECURITY_LEVEL)
protected void setCoordinatorSecurityLevel(int coordinatorSecurityLevel) {
this.coordinatorSecurityLevel = coordinatorSecurityLevel;
}

@Override
public SpongeFactory.Mode getCoordinatorSignatureMode() {
return coordinatorSignatureMode;
}

@JsonProperty("COORDINATOR_SIGNATURE_MODE")
@Parameter(names = "--testnet-coordinator-signature-mode", description = MilestoneConfig.Descriptions.COORDINATOR_SIGNATURE_MODE)
protected void setCoordinatorSignatureMode(SpongeFactory.Mode coordinatorSignatureMode) {
this.coordinatorSignatureMode = coordinatorSignatureMode;
}

@Override
public String getSnapshotFile() {
return snapshotFile;
Expand Down Expand Up @@ -108,17 +154,6 @@ protected void setMilestoneStartIndex(int milestoneStartIndex) {
this.milestoneStartIndex = milestoneStartIndex;
}

@Override
public int getNumberOfKeysInMilestone() {
return numberOfKeysInMilestone;
}

@JsonProperty("NUMBER_OF_KEYS_IN_A_MILESTONE")
@Parameter(names = "--milestone-keys", description = SnapshotConfig.Descriptions.NUMBER_OF_KEYS_IN_A_MILESTONE)
protected void setNumberOfKeysInMilestone(int numberOfKeysInMilestone) {
this.numberOfKeysInMilestone = numberOfKeysInMilestone;
}

@Override
public int getTransactionPacketSize() {
return transactionPacketSize;
Expand Down Expand Up @@ -160,18 +195,24 @@ 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";
int REQUEST_HASH_SIZE = 49;
String SNAPSHOT_SIG = "/snapshotTestnet.sig";
int SNAPSHOT_TIME = 1522306500;
int MWM = 9;
int MILESTONE_START_INDEX = 434525;
int KEYS_IN_MILESTONE = 22;
int PACKET_SIZE = 1653;
String DB_PATH = "testnetdb";
String DB_LOG_PATH = "testnetdb.log";

}
}
2 changes: 1 addition & 1 deletion src/main/java/com/iota/iri/service/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ private AbstractResponse getNodeInfoStatement() throws Exception{
instance.tipsViewModel.size(),
instance.transactionRequester.numberOfTransactionsToRequest(),
features,
instance.configuration.getCoordinator());
instance.configuration.getCoordinator().toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.iota.iri.controllers.MilestoneViewModel;
import com.iota.iri.controllers.TransactionViewModel;
import com.iota.iri.crypto.SpongeFactory;
import com.iota.iri.model.Hash;

import java.util.Optional;
Expand Down Expand Up @@ -34,13 +33,11 @@ public interface MilestoneService {
*
* @param transactionViewModel transaction that shall be analyzed
* @param milestoneIndex milestone index of the transaction (see {@link #getMilestoneIndex(TransactionViewModel)})
* @param mode mode that gets used for the signature verification
* @param securityLevel security level that gets used for the signature verification
* @return validity status of the transaction regarding its role as a milestone
* @throws MilestoneException if anything unexpected goes wrong while validating the milestone transaction
*/
MilestoneValidity validateMilestone(TransactionViewModel transactionViewModel, int milestoneIndex,
SpongeFactory.Mode mode, int securityLevel) throws MilestoneException;
MilestoneValidity validateMilestone(TransactionViewModel transactionViewModel, int milestoneIndex)
throws MilestoneException;

/**
* Updates the milestone index of all transactions that belong to a milestone.<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.iota.iri.controllers.AddressViewModel;
import com.iota.iri.controllers.MilestoneViewModel;
import com.iota.iri.controllers.TransactionViewModel;
import com.iota.iri.crypto.SpongeFactory;
import com.iota.iri.model.Hash;
import com.iota.iri.model.HashFactory;
import com.iota.iri.service.milestone.LatestMilestoneTracker;
Expand Down Expand Up @@ -140,7 +139,7 @@ public LatestMilestoneTrackerImpl init(Tangle tangle, SnapshotProvider snapshotP
this.milestoneService = milestoneService;
this.milestoneSolidifier = milestoneSolidifier;

coordinatorAddress = HashFactory.ADDRESS.create(config.getCoordinator());
coordinatorAddress = config.getCoordinator();

bootstrapLatestMilestoneValue();

Expand Down Expand Up @@ -201,7 +200,7 @@ public boolean processMilestoneCandidate(TransactionViewModel transaction) throw
return true;
}

switch (milestoneService.validateMilestone(transaction, milestoneIndex, SpongeFactory.Mode.CURLP27, 1)) {
switch (milestoneService.validateMilestone(transaction, milestoneIndex)) {
case VALID:
if (milestoneIndex > latestMilestoneIndex) {
setLatestMilestone(transaction.getHash(), milestoneIndex);
Expand Down
Loading

0 comments on commit 9bb3b92

Please sign in to comment.