Developers Forum for XinFin XDC Network

Rushabh Parmar
Rushabh Parmar

Posted on

Proof of Stake on XDC Network: How Staking Decides Who Secures the Chain

XDC Network is secured by Proof of Stake. In simple words, the more XDC staked behind a node, the higher its priority to produce blocks. This article explains how Proof of Stake works on XDC and how the staking contract's vote() function lets anyone add stake to a node.

Proof of Stake: the basics

Every blockchain needs a group of nodes that check transactions and add new blocks. These nodes are called validators. The question is: who gets to be a validator?

  • In Proof of Work (like BTC), miners compete using computing power and electricity.
  • In Proof of Stake, validators are chosen based on how many coins are staked, meaning locked up, behind them.

Staking works like a security deposit. A validator with a lot of XDC at stake has a strong reason to keep the network healthy, because its own value depends on it. That is what makes Proof of Stake secure without burning energy.

XDC Network uses XDPoS (Delegated Proof of Stake). This means:

  • A node owner stakes XDC to register their node as a candidate.
  • Any XDC holder can also delegate their XDC to a candidate. On XDC, this is done with the vote() function.
  • A candidate's total stake is the owner's stake plus all the XDC delegated to it.

More stake means higher priority

This is the most important rule to understand.

At every epoch (900 blocks, about every 30 minutes), the network:

  1. Looks at every candidate in the staking contract.
  2. Ranks them by total stake, from highest to lowest.
  3. Selects the top 108 to produce blocks for that epoch.

Candidates below the top 108 wait as standby until their stake ranks high enough.

A simple example. Imagine there were only 3 seats:

Candidate Total stake Result
A 15,000,000 XDC Selected (#1)
B 12,000,000 XDC Selected (#2)
C 11,000,000 XDC Selected (#3)
D 10,500,000 XDC Standby (#4)

If someone uses vote() to add 1,000,000 XDC to candidate D, its total becomes 11,500,000 XDC. D now ranks #3 and is selected, and C moves to standby.

The same rule applies to everyone:

  • Higher stake = higher priority.
  • There is no upper limit on stake, so there is no ceiling on how high a candidate can rank.
  • Rankings are checked again every epoch, so priority always follows the current stake.

The staking contract

All staking on XDC Network happens in one smart contract called XDCValidator. It lives at this address:

0x0000000000000000000000000000000000000088

This contract keeps a list of nodes (called candidates) and records how much XDC is staked to each one.

What does vote() do?

vote() lets you stake your XDC to a candidate.

When you call it:

  • You choose a candidate address.
  • You send some XDC with the call.
  • The contract adds your XDC to that candidate's total stake.
  • The contract remembers that this XDC belongs to you.

That's all it does. Your XDC stays in the contract under your name, and you can take it back later.

Here is the function from the contract:

function vote(address _candidate) external payable onlyValidVoterCap onlyValidCandidate(_candidate) {
    // add your XDC to the candidate's total stake
    validatorsState[_candidate].cap = validatorsState[_candidate].cap.add(msg.value);

    // save you as a voter for this candidate
    if (validatorsState[_candidate].voters[msg.sender] == 0) {
        voters[_candidate].push(msg.sender);
    }

    // record how much XDC you have staked
    validatorsState[_candidate].voters[msg.sender] = validatorsState[_candidate].voters[msg.sender].add(msg.value);

    emit Vote(msg.sender, _candidate, msg.value);
}
Enter fullscreen mode Exit fullscreen mode

The rules

The function checks only two things:

  • You must send at least 25,000 XDC in each call.
  • The address must be a registered candidate.

There is no maximum. You can stake as much XDC as you like, and you can call vote() as many times as you want. Each call simply adds to the total.

You also don't need KYC to vote.

How to use vote() on XDCScan

  1. Open the Write Contract page of the staking contract.
  2. Click Connect to Web3 and connect your wallet (XDCPay or MetaMask on XDC Network).
  3. Find the vote function.
  4. In payableAmount, enter how much XDC you want to stake (at least 25,000).
  5. In _candidate, paste the candidate address. Use the 0x format, not xdc.
  6. Click Write and confirm in your wallet.

To check the candidate's new total, go to the Read Contract page and use getCandidateCap with the same address.

Taking your XDC back

You can withdraw your stake in two steps:

  1. Call unvote(candidate, amount) to remove your stake. The XDC is then locked for about 10 days (432,000 blocks).
  2. After 10 days, call withdraw(blockNumber, index) to receive it in your wallet. You can find the block number with getWithdrawBlockNumbers().

If you are the owner of the candidate, you must keep at least 10,000,000 XDC staked. You can only unvote the amount above that.

Quick tips

  • Always use the vote() function. Don't send XDC directly to the contract address.
  • Double-check the candidate address before you confirm.
  • Remember the 10-day lock when you unvote.

Coming soon: XDC 3.0

The XDC 3.0 upgrade goes live in the first week of January. After the upgrade, rewards will be distributed every epoch.

Have questions? Ask in the comments.

Discussion (0)