Set up and use Proof of Stake (PoS)

Overview

This guide explains how to set up a Proof of Stake (PoS) network with the Ting Application Chain, how to stake funds for nodes to become validators, and how to unstake funds.

It is highly encouraged to read and complete the Local Setup / Cloud Setup sections before following this PoS guide. Those sections outline the steps needed to start a PoS cluster with the Ting Application Chain.

Currently, there is no limit to the number of validators that can stake funds on the Staking Smart Contract.

Staking Smart Contract

The repo for the Staking Smart Contract is located here: https://github.com/0xPolygon/staking-contracts

It contains testing scripts, ABI files and the Staking Smart Contract itself.

Setting up an N node cluster

Setting up a network with the Ting Application Chain is covered in the Local Setup / Cloud Setup sections.

The only difference between setting up a PoS and PoA cluster is in the genesis generation part.

When generating the genesis file for a PoS cluster, add the --pos flag:

command
polygon-edge genesis --pos ...

Setting the length of an epoch

Epochs are covered in detail in the Epoch Blocks section.

To set the size of an epoch for a cluster (in blocks), specify the --epoch-size flag when generating the genesis file:

This sets the epoch size to 50 blocks in the genesis file. The default value for the epoch size is 100000 blocks.

circle-info

Lowering the epoch length

As outlined in the Epoch Blocks section, epoch blocks are used to update the validator sets for nodes.

The default epoch length (100000 blocks) may be a long time to wait for validator set updates. Considering new blocks are added at ~2s intervals, it would take ~55.5 hours for the validator set to possibly change.

Setting a lower value for the epoch length ensures that the validator set is updated more frequently.

Using the Staking Smart Contract scripts

1

Prerequisites

The Staking Smart Contract repo is a Hardhat project and requires NPM.

Initialize it in the repo main directory:

2

Setting up the provided helper scripts

Scripts for interacting with the deployed Staking Smart Contract are located in the Staking Smart Contract repo: https://github.com/0xPolygon/staking-contracts

Create an .env file in the Smart Contracts repo with the following parameters:

Where the parameters are:

  • JSONRPC_URL — the JSON-RPC endpoint for the running node

  • PRIVATE_KEYS — private keys of the staker address

  • STAKING_CONTRACT_ADDRESS — the address of the staking smart contract (default 0x0000000000000000000000000000000000001001)

3

Staking funds

circle-info

Staking address

The Staking Smart Contract is pre-deployed at address 0x0000000000000000000000000000000000001001.

Any interaction with the staking mechanism is done through the Staking Smart Contract at that address.

To learn more about the Staking Smart Contract, visit the Staking Smart Contract section.

To become part of the validator set, an address needs to stake an amount above a threshold. The default threshold for joining the validator set is 1 ETH.

Staking is done by calling the stake method of the Staking Smart Contract with a value >= 1 ETH.

After the .env file is set up and a chain is started in PoS mode, stake with:

The stake script stakes a default amount of 1 ETH (changeable in scripts/stake.ts). If the staked funds are >= 1 ETH, the validator set on the contract is updated and the address becomes part of the validator set starting from the next epoch.

4

Unstaking funds

Addresses that have staked can only unstake all of their funds at once.

After the .env file is set up and a chain is started in PoS mode, unstake with:

5

Fetching the list of stakers

All addresses that stake funds are stored in the Staking Smart Contract.

After the .env file is set up and a chain is started in PoS mode, fetch the list of validators with: