The flipperLoop function aims to continually push the system towards consensus on a particular value, using various mechanisms and tools.
Parameters:
Node* u: The primary node initiating the consensus round.
int value0: The initial value proposed for consensus.
const std::vector<Node*>& network: The entire network of nodes.
Internal Variables:
value: Current value being considered.
lastValue: Last value that was considered.
cnt: Counter to keep track of consecutive times a majority value has been identified.
e: Efficiency factor, a dynamic parameter to determine sampling size and other related parameters.
d: Map to keep track of votes for different values.
checkpointInterval: Interval at which the value is stored as a checkpoint.
lastCheckpoint: Last stored checkpoint value.
Dynamic Variables:
The updateParams lambda function updates the following:
alpha: Calculated from e, used to influence other parameters.
beta: Calculated from e, used to influence other parameters.
k: Calculated from e, determines the sampling size.
Mechanism:
Initialization: Set initial parameters and dynamic variables.
Invalid State Check: If value becomes 2 (indicating an invalid state), it continues to the next iteration.
Pre-prepare Phase: The primary node u proposes a value (proposedValue).
Prepare Phase:
The network is sampled to select a subset K of nodes.
These nodes are queried in parallel for their values, resulting in a set of values P.
Commit Phase:
Majority check is performed on the set P using majorityVote. If a majority value is found, it updates the count and current proposed value.
If a majority is not found, the counter cnt is reset.
Reply Phase:
If the counter cnt reaches a threshold (e.g., 5), the current value is accepted, displayed, and the loop exits.
Dynamic Adjustment:
The efficiency factor e is adjusted to improve the consensus process's efficiency and responsiveness.
If there are frequent changes in the primary node (view changes) or if the consensus process is too slow, the efficiency factor is adjusted downward or upward, respectively. This ensures the system adapts to its operational context.
Feedback Loop:
Placeholder flags frequentViewChanges and slowConsensus are given, which, in a real-world application, would be populated based on system observations. They determine how the efficiency factor e is adjusted.
Checkpoint Mechanism:
At regular intervals (checkpointInterval), the current value is stored as a checkpoint (lastCheckpoint).
View Change Mechanism:
If the primary node is suspected by more than 1/3 of the network, a view change is initiated. The primary node is then changed. (Note: the actual implementation details for selecting a new primary and handling the view change are not provided in the code).
Loops:
The entire mechanism continually tries to reach consensus on the proposed value. The loop iterates until the cnt threshold is reached for a particular value, indicating that consensus has been achieved on that value.
#include"node_and_adjustments.h"#include<cmath>Node::Node(int id): id(id) {}std::string Node::signMessage(const std::string& message) {return"signature_for_"+ message; // Mock signature}bool Node::verifySignature(const std::string& message,const std::string& signature) {returnsignMessage(message) == signature;}voidupdateParams(double& e, std::unordered_map<int,int>& d,int& value) {staticconstdouble upperThreshold =1.5;staticconstdouble lowerThreshold =0.5;staticconstdouble adjustmentRate =1.01;staticconstdouble decreaseRate =0.99;staticconstdouble smoothingFactor =0.1; e = std::min(1.5, e * adjustmentRate); // Feedback from the systembool frequentViewChanges =false; // Placeholder: This should be determined based on actual system feedbackbool slowConsensus =false; // Placeholder: This should be determined based on actual system feedbackif (frequentViewChanges) { e *= decreaseRate; } elseif (slowConsensus) { e *= adjustmentRate; } else { e *= adjustmentRate; } e = (1- smoothingFactor) * e + smoothingFactor * std::min(upperThreshold, e); e = std::clamp(e, lowerThreshold, upperThreshold);}boolisSuperMajority(int votes,int total) {return votes > (2* total) /3;}