From 274b73b446c804e4cc87ae24410098a61ee4b937 Mon Sep 17 00:00:00 2001 From: Andrej Mitrovic Date: Thu, 13 Aug 2020 18:22:30 +0900 Subject: [PATCH] Use uint instead of floating-point for thresholds Fixes #1112 --- source/agora/common/Config.d | 8 ++++---- source/agora/consensus/Quorum.d | 12 ++++++------ source/agora/consensus/data/ConsensusParams.d | 6 +++--- source/agora/test/Base.d | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/source/agora/common/Config.d b/source/agora/common/Config.d index edfeacf74ec..ae3c304871d 100644 --- a/source/agora/common/Config.d +++ b/source/agora/common/Config.d @@ -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 @@ -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) { diff --git a/source/agora/consensus/Quorum.d b/source/agora/consensus/Quorum.d index 751507955e0..34039856a54 100644 --- a/source/agora/consensus/Quorum.d +++ b/source/agora/consensus/Quorum.d @@ -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; } /******************************************************************************* @@ -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; } @@ -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); @@ -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); @@ -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); diff --git a/source/agora/consensus/data/ConsensusParams.d b/source/agora/consensus/data/ConsensusParams.d index 564371a42cd..2ebbf41e401 100644 --- a/source/agora/consensus/data/ConsensusParams.d +++ b/source/agora/consensus/data/ConsensusParams.d @@ -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; @@ -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; @@ -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); diff --git a/source/agora/test/Base.d b/source/agora/test/Base.d index 21312999197..10549ea6f80 100644 --- a/source/agora/test/Base.d +++ b/source/agora/test/Base.d @@ -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;