Skip to content

Commit

Permalink
Use uint instead of floating-point for thresholds
Browse files Browse the repository at this point in the history
Fixes #1112
  • Loading branch information
AndrejMitrovic authored and linked0 committed Aug 14, 2020
1 parent 331e518 commit 274b73b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions source/agora/common/Config.d
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public struct NodeConfig
/// Maximum number of nodes to include in an autogenerated quorum set
public uint max_quorum_nodes = 7;

/// Threshold to use in the autogenerated quorum
public double quorum_threshold = 0.80;
/// Threshold to use in the autogenerated quorum. Between 1 and 100.
public uint quorum_threshold = 80;
}

/// Admin API config
Expand Down Expand Up @@ -319,8 +319,8 @@ private NodeConfig parseNodeConfig (Node* node, const ref CommandLine cmdln)
auto port = get!(ushort, "node", "port")(cmdln, node);
auto validator_cycle = get!(uint, "node", "validator_cycle")(cmdln, node);
auto max_quorum_nodes = get!(uint, "node", "max_quorum_nodes")(cmdln, node);
auto quorum_threshold = get!(double, "node", "quorum_threshold")(cmdln, node);
assert(quorum_threshold > 0.0 && quorum_threshold <= 1.0);
auto quorum_threshold = get!(uint, "node", "quorum_threshold")(cmdln, node);
assert(quorum_threshold >= 1 && quorum_threshold <= 100);

NodeConfig makeConf (KeyPair key_pair)
{
Expand Down
12 changes: 6 additions & 6 deletions source/agora/consensus/Quorum.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public struct QuorumParams
/// Maximum number of nodes to include in a quorum.
const MaxQuorumNodes = 7;

/// Threshold to use for the quorum configuration (e.g. 0.80 = 80%)
const double QuorumThreshold = 0.80;
/// Threshold percentage to use for the quorum configuration
const uint QuorumThreshold = 80;
}

/*******************************************************************************
Expand Down Expand Up @@ -117,7 +117,7 @@ public QuorumConfig buildQuorumConfig ( const ref PublicKey key,

quorum.nodes.sort;
quorum.threshold = max(1, cast(uint)ceil(
params.QuorumThreshold * quorum.nodes.length));
(params.QuorumThreshold * double(0.01)) * quorum.nodes.length));
return quorum;
}

Expand Down Expand Up @@ -335,7 +335,7 @@ unittest
// using various different quorum parameter configurations
unittest
{
QuorumParams qp_1 = { MaxQuorumNodes : 4, QuorumThreshold : 0.80 };
QuorumParams qp_1 = { MaxQuorumNodes : 4, QuorumThreshold : 80 };
auto keys = getKeys(10);
auto quorums_1 = buildTestQuorums(Amount.MinFreezeAmount.repeat(10), keys,
hashFull(1), qp_1);
Expand All @@ -346,7 +346,7 @@ unittest
test!"=="(countNodeInclusions(quorums_1, keys),
[3, 3, 2, 4, 4, 3, 3, 8, 6, 4]);

QuorumParams qp_2 = { MaxQuorumNodes : 8, QuorumThreshold : 0.80 };
QuorumParams qp_2 = { MaxQuorumNodes : 8, QuorumThreshold : 80 };
auto quorums_2 = buildTestQuorums(Amount.MinFreezeAmount.repeat(10), keys,
hashFull(1), qp_2);
verifyQuorumsSanity(quorums_2);
Expand All @@ -356,7 +356,7 @@ unittest
test!"=="(countNodeInclusions(quorums_2, keys),
[8, 7, 3, 9, 8, 10, 8, 10, 10, 7]);

QuorumParams qp_3 = { MaxQuorumNodes : 8, QuorumThreshold : 0.60 };
QuorumParams qp_3 = { MaxQuorumNodes : 8, QuorumThreshold : 60 };
auto quorums_3 = buildTestQuorums(Amount.MinFreezeAmount.repeat(10), keys,
hashFull(1), qp_3);
verifyQuorumsSanity(quorums_3);
Expand Down
6 changes: 3 additions & 3 deletions source/agora/consensus/data/ConsensusParams.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public immutable class ConsensusParams
public uint MaxQuorumNodes;

/// The threshold to use for the generated quorums
public double QuorumThreshold;
public uint QuorumThreshold;

/// The Genesis block of the chain
public Block Genesis;
Expand All @@ -49,7 +49,7 @@ public immutable class ConsensusParams
***************************************************************************/

public this (immutable(Block) genesis, uint validator_cycle = 1008,
uint max_quorum_nodes = 7, double quorum_threshold = 0.80)
uint max_quorum_nodes = 7, uint quorum_threshold = 80)
{
this.Genesis = genesis;
this.ValidatorCycle = validator_cycle;
Expand All @@ -60,7 +60,7 @@ public immutable class ConsensusParams
/// Default for unittest, uses the test genesis block
version (unittest) public this (
uint validator_cycle = 1008, uint max_quorum_nodes = 7,
double quorum_threshold = 0.80)
uint quorum_threshold = 80)
{
import agora.consensus.data.genesis.Test : GenesisBlock;
this(GenesisBlock, validator_cycle, max_quorum_nodes, quorum_threshold);
Expand Down
2 changes: 1 addition & 1 deletion source/agora/test/Base.d
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ public struct TestConf
uint max_quorum_nodes = 7;

/// Overrides the default quorum threshold
double quorum_threshold = 0.8;
uint quorum_threshold = 80;

/// whether to set up the peers in the config
bool configure_network = true;
Expand Down

0 comments on commit 274b73b

Please sign in to comment.