Intermediate

 

What is baking & How baking works?

Welcome to our tutorial on baking and how it works in the Tezos blockchain network. In this tutorial, we will cover the process of baking, which is the process of creating and validating new blocks on the Tezos blockchain. We will explore the concept of baking, the role of bakers, the process of baking, and the rewards for baking.

By the end of this tutorial, you will have a solid understanding of what baking is, how it works and how it is essential for the Tezos blockchain to function. You will also learn how to become a baker and the rewards you can earn for participating in the baking process. This tutorial is ideal for developers, researchers and anyone interested in understanding the inner workings of the Tezos blockchain and how to participate in it.

What is baking?

To achieve its consensus, Tezos uses Liquid Proof of Stake (LPoS). That is to say that the validators/bakers of the network temporarily lock a part of their tokens (which they cannot use anymore) to obtain the right to create a block. The creator of the next block is called the baker and is chosen randomly among all the candidates, based on the number of tokens locked. In exchange for his work, the baker receives a reward in Tez.

What is delegating?

If a Tez holder does not have 6,000ꜩ or does not want to set up a computing infrastructure to bake blocks, they may delegate their coins to a baker. Delegating lets coin holders (i.e., delegators) “lend” their coins to a baker (i.e., a delegate), giving the baker a higher probability of being selected to bake and endorse blocks. In turn, bakers share the additional revenue generated from the delegated tokens with the delegators, in proportion to their participation. Note that this process does not transfer ownership of coins. Hence bakers cannot spend or control the Tez delegated to them, ensuring that bakers cannot appropriate the delegators funds.

With LPoS the number of bakers is unlimited (everyone can participate), and delegation is optional.

In Tezos we call participants in the block validation process delegates. Each delegate can:

  • act as a baker, creating and signing blocks, and/or

  • act as an endorser, approving blocks by issuing an endorsement operation (endorsements visible at the block level n + 1 relate to the block level n).

Baking and endorsement rights are attributed at random, several cycles in advance. To deter dishonest behavior (e.g. double baking or double endorsement), each delegate must place a fixed quantity of Tez as a security deposit for a limited time (five cycles), which will be confiscated if they attempt to compromise the chain.

Prospective bakers must meet the following requirements:

  • Server available round the clock and stable internet connection

  • At least 8GB of RAM

  • SSD disk (preferably with more than 100GB storage)

  • At least 6,000ꜩ as a staking balance (own funds + delegated balance)

The more Tez a baker holds in his staking balance, the higher his chances to bake blocks and earn baking rewards.

Liquid Proof-of-Stake and delegation

On Tezos, baking rights are attributed randomly but proportionally to the staking balance. A Tez holder may:

  • become a baker if they possess at least 6,000ꜩ in their own funds or by delegation

  • delegate their Tez to a baker, to participate in the consensus

In practice, certain bakers may pass on some of the rewards they receive to the individuals who delegated their Tez to them. This distribution may encourage others to delegate their Tez, thereby increasing the delegated balance of these bakers and so their staking balance, i.e. their chance of baking/endorsing.

Liquid Proof-of-Stake (LPoS) provides the option for Tez holders to retain ownership of their funds while delegating their associated (voting, baking) rights to a baker. This principle differs from the delegated Proof-of-Stake (DPoS) of other blockchains, in which participants vote for a restricted number of block validators.

How baking works?

In this section, we will discuss how baking works. The different actors of the mechanism and the technological solutions will be presented.

Actors

Two types of actors are involved in the block creation process:

  • The Delegate, can be :

    • a Baker, who participates in the consensus by creating new blocks.

    • an Endorser, who participates in the consensus by validating the blocks created by other bakers.

    • an Accuser, who participates in the consensus by monitoring bakers’ and endorsers’ works.

  • The Delegator can delegate their tokens to a Delegate.

Roles of a delegate

Baking

Baking is the act of creating, signing, and publishing blocks.

Bakers select and validate all transactions and add them to the blockchain. If a baker behaves dishonestly, the protocol has a built-in mechanism that can cause them to lose their security deposit.

Bakers are rewarded with Tez for securing the network.

In Tezos, the right to produce a block in cycle n is assigned to a randomly selected delegate in a randomly selected stake snapshot from cycle n-PRESERVED_CYCLES-2.

Bakers are randomly selected from the list of all nodes that have declared themselves as delegates, in proportion to the amount of Tez they have.

A baker is a delegate, while a coin holder who delegates their Tez is a delegator. Note that a baker does not necessarily need delegators to function, they can play solo, but they have fewer chances to be selected for baking.

The selected baker is allowed to create the next block and add it to the chain then communicate it to the network. They receive a certain number of Tez as a reward.

Several bakers are elected to create a block with a priority list. The one with the highest priority will try to create a block. If they fail to do so within the time limit, the hand will pass to the next baker. A block generated by the baker that does not have priority will be invalid and refused by the network.

To create a block, a baker has to freeze part of their tokens, which will be made available back to them only PRESERVED_CYCLES = 5 cycles later.

A baker can be marked as either active or passive. A passive delegate cannot be selected for baking or endorsing. A baker becomes passive in cycle n if they fail to create any block or endorsement in the past PRESERVED_CYCLES cycles, that is to say, in cycles n-1, n-2, …, n - PRESERVED_CYCLES.

Endorsing

Among the bakers, a list of endorsers is drawn up for each block. Their role is to approve the blocks created by the baker (i.e. to officially approve the block) in exchange for a reward.

To approve a block at level n, the endorsers each add a signature operation that is included in the n + 1 level block. Once the block n + 1 is baked, no more endorsement operations are accepted for block n.

Each block has 7,000 endorsement slots. There is no obligation for a baker to include endorsements with endorsing the power of more than 4,667 but the baker will receive a bonus proportional to the extra endorsing power of the endorsements included.

An endorser may obtain several endorsement slots for a given block. They will then be rewarded for each endorsement slot completed (i.e. for each slot validated).

Accusation

There are also accusers. This type of delegate monitor that:

  • a baker does not create two competing blocks at the same level

  • an endorser does not endorse a block twice or more at the same baking slot

If an accusation is correct, the accuser gets a part of the funds that were frozen by the baker or the endorser, as a reward. The remaining part is burned.

Any attempt at fraud is therefore punished.

Cycle

The Tezos consensus is organized in cycles. One cycle corresponds to BLOCKS_PER_CYLCE = 8192 blocks (≈ 2.8 days). At any point, the shell will not implicitly accept a branch (chain of blocks) whose fork point is in a cycle more than PRESERVED_CYCLES = 5 cycles in the past (≈ 14 days).

Delegates’ rights selection

At each cycle, a random seed is created. A pseudo-random number generator uses the seed to generate the priority list based on a stake snapshot 2 cycles ago.

Stake snapshots

Snapshots of delegates’ stakes are done every 512 blocks and define who can bake. The probability to sample a particular delegate is its stake in the snapshot over the total stake in that snapshot.

A stake snapshot represents the stake distribution for a given block. Stake snapshots are taken every BLOCKS_PER_STAKE_SNAPSHOT = 512 levels.

We also define:

  • BLOCKS_PER_CYLCE = 8192 blocks.

  • n, the current cycle.

  • PRESERVED_CYCLES = 5 cycles, i.e. the number of cycles during which the tokens are frozen.

Random seed

Since Tezos runs a deterministic protocol, there is no real randomness. More specifically, there is no randomness in the snapshot selection and baking/endorsement slots attribution, i.e., we know the result before the operation occurs (unlike in PoW where you can only estimate it).

The seed is created by requesting a secret number (nonce) from all delegates. All secret numbers are gathered and used to create a hash that will be used as the random seed. Since the last owner to reveal their secret already knows the other numbers, a 2-phase process called “Commit & Reveal” is necessary.

The random seed for cycle n is a 256-bit number generated at the very end of cycle n-1 from the previous seed and the nonces that delegates commit during cycle n-2.

One out of every BLOCKS_PER_COMMITMENT (= 32 blocks) can contain a commitment. There are therefore, at most BLOCKS_PER_CYCLE / BLOCKS_PER_COMMITMENT (= 128 commitments) per cycle. The commitment is generated by the baker who produces the block and is included in the block header.

In summary, the seed for cycle n is a hash created from:

  • the seed of cycle n-1, a constant.

  • every nonce that was revealed in cycle n-1

This seed is used to randomly select:

  • a stake snapshot from cycle n-2

  • delegates in the selected snapshot

Baker and endorser selection

The generated list of priorities identifies which delegate has the responsibility to bake a block and which delegates have to endorse this new block. It is a round-robin process that cycles on the list of priorities until the end of the cycle (4096 blocks).

The minimum time delay before baking a block

The minimum time delay between two blocks is 30 seconds, reached if the baker is of priority 0 and has gathered at least 66.7% of the endorsements. If the priority 0 baker fails to bake, the priority 1 baker may then bake after a certain time, defined by a so-called round duration function. To be able to bake as rapidly as possible, the baker must gain enough endorsements (66.7%, that is, endorsements with endorsing power of at least 4,667 out of 7,000). Rounds follow the structure below:

Round duration thus increases linearly with DELAY_INCREMENT_PER_ROUND = 15s, MINIMAL_BLOCK_DELAY = 30s,r = round.

Any block baked ahead of the minimum time delay is automatically rejected under the protocol’s rules.

Remote signing

Remote signing software is one way to reduce the exposure of private keys during baking operations.

In the block validation process (baking/endorsement), the software interacts with a private key management system (e.g. Ledger Nano S, a remote machine communicating via a secure channel).

Remote signing involves physically separating baking/endorsement requests originating from the network and intended for a given delegate, from that delegate’s signing operation. The Octez client implements several built-in signing mechanisms (unix, tcp, http, and https) that use different communication channels to send signing requests for the baker.

The following events occur during a remote signing operation:

1. The node sends a signing request to the remote signing software that interacts with the private key management system.

2. The remote signing software verifies what the operation involves − usually the creation of a block or an endorsement (The third, rarer option is a nonce revelation.) − then sends it to the key management system, which signs the operation.

3. The signature is then validated by the remote signing software and sent back to the Tezos node, which propagates the operation to the network.

The key should preferably be held in a hardware vault so it is never directly accessible on the network. Moreover, it is more secure to ensure that the hardware vault authorizes only block signing rather than the signing of just any transaction (i.e. such as not to create a blind signing mechanism). For example, Ledger Nano S together with Kiln meets these two conditions.

In summary

  • At each cycle, a priority list of bakers is chosen and up to 7,000 delegates are chosen as endorsers. The endorsers are thus delegates who will validate the block of bakers. The bakers are elected pseudo-randomly in proportion to the quantity of Tez that they possess.

  • To become a delegate, one must freeze a part of their tokens, at least 6000ꜩ, which will be made available to him only 5 cycles later (14 days).

  • A baker is a delegate and the one who delegates their Tez is a delegator. Note that a baker does not necessarily need delegators to operate, they can play solo, but they will be less likely to be selected to create the block because they will have fewer funds at their disposal.

  • Several bakers are added to a priority list. The one with the highest priority will try to create a block. If they fail to do so within the time limit, the hand will pass to the next baker. And so on.

  • The first baker who manages to create the block will be able to add it to the blockchain. They will receive a certain number of Tez as a reward. In turn, bakers share the additional revenue generated from the delegated tokens with the delegators, in proportion to their participation.

  • The accusers monitor the process and punish the cheaters.

With this, you complete this workshop successfully!!