Leader Election

Overview

Leader election mechanisms are used to determine which operator (or Performer) executes a Task at any given time. These mechanisms ensure the orderly allocation of tasks and optimal system performance while avoiding conflicts. Conflicts can occur when multiple operators try to execute the same task simultaneously, leading to duplicate efforts or inconsistent system states.

Developers can write their own algorithm to determine the Task Performer.

Leader Election Mechanisms

Below are different leader election mechanisms commonly used in decentralized networks:

  1. Round Robin

    • Description: The "Task Performer" is chosen in a round-robin manner, where operators take turns performing tasks in a fixed sequence based on their IDs. The system cycles through the list of operators, ensuring an even distribution of tasks.

    • Implementation: This can be implemented by taking the block number modulo the number of operators (plus one). The result gives a number in the range [1..count], corresponding to the chosen performer's ID.

  2. Prevrandao Selection

    • Description: The "Task Performer" is chosen randomly and deterministically from the pool of nodes. This method ensures that no operator is favored over another, and it can prevent systematic bias in task distribution.

    • Implementation: A random number generator can be used to select a task performer from the pool of operators.

  3. Stake Weighted Leader Selection

    • Description: The "Task Performer" is selected based on a weighted probability. It ensures that operators with higher Voting Power (or other assigned weights) have a greater likelihood of being chosen while still maintaining an element of randomness.

    • Implementation: This can be implemented by retrieving the list of all the active operators, Performing a weighted random selection to determine the Task Performer.

  4. Priority-based Selection

    • Description: Operators are assigned different priority levels, and the highest priority operator is chosen to perform the task. This priority can be based on factors such as reputation, available resources, or past performance.

    • Implementation: A priority queue can be used to select the operator with the highest priority for the task.

  5. Leader Election via Consensus

    • Description: In this approach, operators participate in a consensus mechanism, such as RAFT, to elect a leader to perform the task. This is commonly used in distributed systems, where the leader coordinates task execution and ensures system consistency.

    • Implementation: Operators vote on who should be the leader, and the one with the majority of votes is chosen to perform the task.


Custom Leader Election Logic

When a Task execution is triggered, the Operator node broadcasts a custom message to the P2P network. Upon receiving this message, the peers run a leader election algorithm to select the operator responsible for executing the task.

  1. Publishing the Task Request

Publish the task request as a Custom Message to the P2P network using the JSON-RPC method.

async function publishTask(taskData) {  
  const rpcUrl = "http://127.0.0.1:8545"; // Replace with your RPC server URL
  const hexData = Buffer.from(taskData, "utf8").toString("hex");
  const payload = {
    jsonrpc: "2.0",
    method: "sendCustomMessage",
    params: [`0x${hexData}`],
    id: 1,
  };

  const response = await axios.post(rpcUrl, payload);
}
  1. Invoke the Leader Election Algorithm

When a peer receives a custom message, the /p2p/message endpoint triggers the leader election algorithm to determine which peer will execute the task.

// Endpoint to process custom P2P messages
app.post('/p2p/message', (req, res) => {
  const { data } = req.body;
  const electedLeader = leaderElectionAlgorithm(nodes);
  if (electedLeader === nodeAccount.address) {
       //execute task
  }
});

Last updated