cardano course
  • Welcome!
  • Video lessons
  • Handbook
    • Module 1. Building and running the node
      • Building the node
      • Running the node and connecting to a network
    • Module 2. Basic operations
      • Creating keys and addresses
      • Creating a simple transaction
      • Registering the stake address and delegating
    • Module 3. Protocol parameters and configuration files
      • Protocol parameters
      • Byron genesis file
      • Shelley genesis file
      • Alonzo genesis file
      • Conway genesis file
      • Node configuration file
      • Topology file
    • Module 4. Peer-to-peer (P2P) networking
    • Module 5. Creating a stake pool
      • The setup
      • Generating keys
      • Registering a stake pool
      • Runtime system options
      • Running the nodes
      • Stake snapshots
      • Upgrading cardano-node and cardano-cli
      • Pool operations and maintenance
      • Retiring a stake pool
    • Module 6. Monitoring the nodes
    • Module 7. Cardano governance
      • Update proposals
      • Polls
    • Module 8. Setting up a local cluster
      • Creating a local cluster
      • Spending the genesis UTXO
      • From Byron to Shelley
      • Moving funds to a Shelley address
      • Creating a first stake pool
      • Bringing decentralization parameter down to .80
      • From Shelley to Alonzo
      • Creating a second stake pool before moving to the Babbage era
      • Redelegating genesis keys
      • Vasil hard fork
      • Creating a local cluster with the mkfiles script
    • Module 9. Simple scripts and Plutus scripts
    • Module 10. New tracing system
    • Module 11. Running the SMASH server
    • Module 12. Running the token metadata server
  • Curated playlist
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Handbook
  2. Module 2. Basic operations

Registering the stake address and delegating

In this tutorial, you will learn how to delegate stake and get rewarded for it.

First, you need to register your stake key on the blockchain. This action incurs a deposit of two ada according to the protocol parameters:

cat pparams.json | grep stakeAddressDeposit
    "stakeAddressDeposit": 2000000,

To register the stake key, you first need to produce a registration certificate. Take a look at the cardano-cli stake-address command:

cardano-cli stake-address

Available commands:
  key-gen                  Create a stake address key pair
  build                    Build a stake address
  key-hash                 Print the hash of a stake address key.
  registration-certificate Create a stake address registration certificate
  deregistration-certificate
                           Create a stake address deregistration certificate
  delegation-certificate   Create a stake address delegation certificate

You'd be interested to use registration-certificate:

cardano-cli stake-address registration-certificate \
--stake-verification-key-file stake.vkey \
--out-file registration.cert

This is how it looks like:

cat registration.cert

{
    "type": "CertificateShelley",
    "description": "Stake Address Registration Certificate",
    "cborHex": "82008200581cb6590a7ef994cc48cb4d980ee85f56d6b3a24cfd5e594cc644f761d9"
}

Now you need to submit your certificate to the blockchain. Let's use the build command. This time, you need to use a few more options to build the transaction. We will use --certificate-file to include the registration certificate, and --witness-override to specify that this will be signed by two witnesses – payment.skey and stake.skey:

cardano-cli transaction build --babbage-era \
--testnet-magic 2 \
--witness-override 2 \
--tx-in $(cardano-cli query utxo --address $(cat paymentwithstake.addr) --testnet-magic 2 --out-file  /dev/stdout | jq -r 'keys[1]') \
--change-address $(cat paymentwithstake.addr) \
--certificate-file registration.cert \
--out-file tx.raw

Sign it using both keys:

cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file payment.skey \
--signing-key-file stake.skey \
--testnet-magic 2 \
--out-file tx.signed
cardano-cli transaction submit \
--testnet-magic 2 \
--tx-file tx.signed 

You can now delegate your stake. For this, you need to create a delegation certificate. Let's take another look at the cardano-cli stake-address command:

cardano-cli stake-addres


Available commands:
  key-gen                  Create a stake address key pair
  build                    Build a stake address
  key-hash                 Print the hash of a stake address key.
  registration-certificate Create a stake address registration certificate
  deregistration-certificate
                           Create a stake address deregistration certificate
  delegation-certificate   Create a stake address delegation certificate

To produce the delegation certificate, you need to know the ID of the pool that you will delegate to. The example below delegates to the pool that processed the registration certificate:

cardano-cli stake-address delegation-certificate \
--stake-verification-key-file stake.vkey \
--stake-pool-id ff7b882facd434ac990c4293aa60f3b8a8016e7ad51644939597e90c \
--out-file delegation.cert

Build, sign, and submit the transaction with the certificate:

cardano-cli transaction build --babbage-era \
--testnet-magic 2 \
--witness-override 2 \
--tx-in $(cardano-cli query utxo --address $(cat paymentwithstake.addr) --testnet-magic 2 --out-file  /dev/stdout | jq -r 'keys[1]') \
--change-address $(cat paymentwithstake.addr) \
--certificate-file delegation.cert \
--out-file tx.raw
cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file payment.skey \
--signing-key-file stake.skey \
--testnet-magic 2 \
--out-file tx.signed
cardano-cli transaction submit \
--testnet-magic 2 \
--tx-file tx.signed 

The delegation cycle

The delegation cycle consists of four epochs.

The cycle begins when a stakeholder delegates their stake to a stake pool. This process involves the creation of a delegation certificate, which is subsequently included in a transaction and registered on the blockchain. In the example below, this occurs at any slot within Epoch N.

Then, the protocol captures a stake snapshot (stakedist) at the last slot of Epoch N,, recording three important pieces of information:

  1. The balance of each stake key registered on the blockchain

  2. To which stake pool each stake key is delegated

  3. The parameters (pool cost, margin, pledge, etc) that each stake pool has set.

This snapshot is then used at the end of Epoch N+1 to randomly select the slot leaders for Epoch N+2. This process is the essence of Ouroboros as a proof-of-stake consensus algorithm: the bigger the stake, the more chances to become a slot leader.

During Epoch N+2, stake pools produce the blocks they are entitled to based on the slot leader election. Naturally, stake pools with greater control of stake will be entitled to a larger number of blocks.

In the transition between Epoch N+2 and Epoch N+3, a new snapshot registers the collected rewards. The protocol uses the stake distribution recorded at the end of Epoch N to calculate how much of the rewards belong to each stake key.

Finally, during the transition between Epoch N+3 and Epoch N+4, the protocol distributes rewards to all stake keys. These rewards appear in wallets at the beginning of Epoch N+4, are credited to the wallet's balance, and are considered part of the stake for the snapshot taken at the final slot of Epoch N+4.

PreviousCreating a simple transactionNextModule 3. Protocol parameters and configuration files

Last updated 1 year ago

Was this helpful?

Page cover image