Commit 68965ff1 authored by mikael.beyene's avatar mikael.beyene
Browse files

Add checks for target scale factor

parent 02fb5946
......@@ -17,6 +17,7 @@
package edu.kit.aifb.cii.pow;
import edu.kit.aifb.cii.pow.cmd.Options;
import picocli.CommandLine;
import java.math.BigInteger;
......@@ -40,7 +41,7 @@ public class App {
if (options == null) return;
// Try different targets. The maximum target is Integer.MAX (i.e., 2^32 -1)
int target = (int) (Integer.MAX_VALUE * options.difficultyPercentage);
int target = (int) (Integer.MAX_VALUE * options.targetScaleFactor);
// this is the first block
Block genesis = new Block.Builder()
......
......@@ -15,7 +15,7 @@
*
*/
package edu.kit.aifb.cii.pow;
package edu.kit.aifb.cii.pow.cmd;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
......@@ -27,7 +27,8 @@ import picocli.CommandLine.Option;
)
public class Options {
@Option(names = {"-d", "--difficulty-percentage"}, required = true,
description = "Scaling factor that is applied to Integer.MAX in order to compute target")
public double difficultyPercentage;
@Option(names = {"-t", "--target-scale-factor"}, required = true,
description = "Scaling factor that is applied to Integer.MAX in order to compute target. " +
"Needs to be larger than 0 and less or equal than 1.", converter = TargetScaleFactorConverter.class)
public double targetScaleFactor;
}
\ No newline at end of file
/*
* Copyright 2021 Mikael Beyene
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package edu.kit.aifb.cii.pow.cmd;
import picocli.CommandLine;
import picocli.CommandLine.ITypeConverter;
import picocli.CommandLine.TypeConversionException;
public class TargetScaleFactorConverter implements ITypeConverter<Double> {
@Override
public Double convert(String value) throws Exception {
double d;
try {
d = Double.parseDouble(value);
} catch (NumberFormatException e) {
String message = String.format("Argument '%s' is not a valid double.", value);
throw new TypeConversionException(message);
}
if (d > 1) {
String message = "Target scale factor needs to be less or equal to 1: " + value;
throw new TypeConversionException(message);
} else if (d == 0 || d < 0) {
String message = "Target scale factor needs to be greater than 0: " + value;
throw new TypeConversionException(message);
}
return d;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment