Understanding Polkadot's Staking Mechanism and Validator System
Polkadot uses a Nominated Proof-of-Stake (NPoS) consensus mechanism where validators secure the network and nominators support validators with their stake. This system ensures network security while allowing token holders to participate in consensus without running validator nodes.
Nominated Proof-of-Stake is a variation of Proof-of-Stake that separates the roles of block production and stake delegation:
Validators are responsible for producing blocks, validating parachain blocks, and participating in consensus.
Nominators support validators by staking their tokens and share in the rewards and risks.
Polkadot uses a sophisticated algorithm to select validators that maximizes network security and decentralization:
Let's explore the key staking operations using Polkadot.js API:
// Staking operations with Polkadot.js API
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/keyring';
const api = await ApiPromise.create({ provider: new WsProvider('wss://rpc.polkadot.io') });
const keyring = new Keyring({ type: 'sr25519' });
// 1. Bond tokens for staking
const bondTx = api.tx.staking.bond(
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', // controller account
1000000000000, // amount to bond
'Staked' // reward destination
);
// 2. Nominate validators
const nominateTx = api.tx.staking.nominate([
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
'5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty'
]);
// 3. Set controller account
const setControllerTx = api.tx.staking.setController(
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'
);
// 4. Unbond tokens
const unbondTx = api.tx.staking.unbond(500000000000);
// 5. Withdraw unbonded tokens
const withdrawTx = api.tx.staking.withdrawUnbonded(0);The staking system includes both rewards for good behavior and penalties for bad behavior:
Validators and nominators earn rewards for participating in consensus:
Validators and nominators face penalties for malicious behavior:
Polkadot uses eras and sessions to manage validator sets and reward distribution:
// Era and session information
// Get current era
const currentEra = await api.query.staking.currentEra();
console.log('Current era:', currentEra.toString());
// Get active validators for current era
const activeValidators = await api.query.session.validators();
console.log('Active validators:', activeValidators.toHuman());
// Get validator info
const validatorInfo = await api.query.staking.validators('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
console.log('Validator info:', validatorInfo.toHuman());
// Get nominator info
const nominatorInfo = await api.query.staking.nominators('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
console.log('Nominator info:', nominatorInfo.toHuman());
// Get staking rewards for an era
const rewards = await api.query.staking.erasRewardPoints(currentEra);
console.log('Era rewards:', rewards.toHuman());Understanding the economics of running a validator is crucial for network participants:
In this chapter, we've explored Polkadot's staking and validator system:
In the next chapter, we'll explore Polkadot's governance system, learning how network participants can propose and vote on changes to the protocol.