# Migrations: on-chain upgrades
Have you ever wondered how an upgrade is done in the Cosmos SDK? In this section you will find out how Cosmos SDK migrations are conducted.
Blockchains can be upgraded through a predictable process that reliably avoids forks. Discover the Cosmos comprehensive process that includes governance, data migrations, node upgrades, and more, to ensure upgrades proceed smoothly and without service disruption.
To better understand this section, first read the following sections:
At the end of the section, the code example demonstrates how you would use migration to upgrade your checkers blockchain with new features even after it has been in operation for some time.
A Cosmos SDK application running on a Cosmos blockchain can be upgraded in an orderly, on-chain fashion.
Upgrading blockchains and blockchain applications is notoriously difficult and risky. The Cosmos SDK solves the common risks and challenges.
Generally, when a blockchain is upgraded it is vital that all nodes upgrade simultaneously and at the same block height. This is difficult to achieve in a disorderly setting. If the nodes do not coordinate, then the blockchain will "fork" into two blockchains with a common history: one chain that observes the new rules, and one chain that observes the old rules. It is generally not possible for these two chains to reach a common consensus or merge in the future.
Upgrading a live chain without software support for upgrades is risky, because all the validators need to pause their state machines at the same block height and apply the upgrade before resuming. If this is not done correctly, there can be state inconsistencies, which are hard to recover from.
Smart contracts on EVM chains such as Ethereum are immutable software. They are difficult or impossible to change by definition. Various strategies based on modularity can simulate the effects of upgrading smart contracts, but all known methods have inherent limitations, in particular the difficulties, impossibility, or prohibitive costs of re-organizing data at rest. This places a significant limitation on the types of upgrades that are feasible.
A Cosmos SDK blockchain built for a specific application can be upgraded without forks. If a new version of the blockchain application uses a different data layout than exists on the chain, the existing data can be reorganized before the new version of the application comes online. The data migration is defined by the developer and runs in each node quickly and cost-effectively before the node returns to service.
# Process overview
A "plan" is an upgrade process to take place at a specific block height in the future. It includes a
SideCar process that executes when the upgrade commences, which names the plan and specifies a block height at which to execute.
Acceptance or rejection of the plan is managed through the normal governance process. A "cancel proposal" can be submitted and adopted, preventing the plan from executing. Cancellation is contingent on knowing that a given plan is a poor idea before the upgrade happens.
Info in a plan kicks off the
SideCar is a binary which nodes can run to attend to processes outside of Cosmos binaries. This can include steps such as downloading and compiling software from a certain commit in a repo.
UpgradeHandler may be executed after the
SideCar process is finished and the binary has been upgraded. It attends to on-chain activities that may be necessary before normal processing resumes. An upgrade handler may trigger a
StoreLoader prepares the on-chain state for use by the new binary. This can include reorganizing existing data. The node does not resume normal operation until the store loader has returned and the handler has completed its work.
Governance uses proposals that are voted on and adopted or rejected. An upgrade proposal takes the form of accepting or rejecting a plan that is prepared and submitted through governance. Proposals can be withdrawn before execution with cancellation proposals.
Coordinated upgrades attend to the challenging process of upgrading blockchain applications and blockchain platforms.
The main advantages of this form of coordinated upgrades are:
- Avoidance of forks: all validators move together at a pre-determined block height.
- Smooth upgrade of binaries: the new software is adopted in an automated fashion.
- Reorganizing data stores: data at rest can be reorganized as needed by processes that are not limited by factors such as a block gas limit.
# Effect of upgrades
Blockchains are paused at the block height of an adopted plan. This initiates the upgrade process. The upgrade process itself may include switching to a new binary that is relatively small to download and install, or it may include an extensive data reorganization process. The validator stops processing blocks until it completes the process in either case.
The validator resumes processing blocks when the handler is satisfied with the completeness degree of the upgrade. From a user perspective, this appears as a pause which resumes with the new version.
SideCar, handler, and store loader are application-specific. At each block, the Cosmos SDK checks for a plan that should be executed before processing block transactions. If none exists, then processing continues as usual. If a plan is scheduled to run, then the Cosmos SDK pauses normal processing and loads the
SideCar. When the SideCar is finished, it loads the handler and optionally the store loader.
Application developers build implementations of those components that are tailored to their application and use case.
For a more detailed explanation of the upgrade process, see the Cosmos SDK documentation (opens new window).
Cosmovisor is a tool that node operators can use to automate the on-chain processes described above.
- Cosmovisor runs as a small wrapper around the Cosmos SDK application binaries.
- Cosmovisor watches for any approved upgrade proposals.
- Cosmovisor can download and run the new binary if wanted.
- When the chain reaches the upgrade block, Cosmovisor also handles the storage upgrade.
See the Cosmos SDK documentation on Cosmovisor (opens new window) to learn more about this process manager for Cosmos SDK applications.
# Next up
You are now up-to-date on migrations. Look at the following code samples, or go to the next section to discover bridges in the Cosmos SDK.
The code samples you have seen previously were meant to build your checkers blockchain from the ground up. Now imagine it has been running in production for some time, with games created, played on, won, and lost. Given its success and following player feedback, you decide to introduce leaderboards. In particular:
- Any player who has ever played should have a tally of games won, lost, drawn, and forfeited.
- There should be a leaderboard that lists the players with the most wins, but in limited numbers (for instance, only the top 100 scores).
- To increase engagement, the player with the most recent score takes precedence over an older contender with an equal score.
It is not good enough to introduce a leaderboard for players currently winning and losing: you want to start with all those that played in the past. Fortunately, all past games and their outcomes have been kept in the chain state. What you need to do is go through the record, update the players with their tallies, and add a leaderboard.
Call your existing version "v1". To disambiguate, call your new one with the leaderboard "v2".
# New information
You need new data structures for v2. With Ignite CLI you have:
A way to store each player's information:
A way to identify a winning player's information in the leaderboard:
wonCountdecides the position on the leaderboard, and
dateAddedresolves ties for equal
wonCounts, with the most recent ranking higher.
Now you need a leaderboard as an array of winner information:
Because this player information and this leaderboard are stored somewhere in storage, you must introduce them in the new genesis:
Conceptually, it is the new genesis because your actual genesis file did not contain any leaderboard.
Finally, you must set a hard-coded leaderboard length:
# Leaderboard on-the-go updating
You will need to add code to v2 to update the leaderboard after a game has been determined. This means a lot of array sorting and information adjustment on the previous code.
If you want more details on how to update the leaderboard, look at Run my own chain.
# Genesis migration preparation
With on-the-go updating of the leaderboard taken care of for v2, you must place past players on the leaderboard. You need a new v2 genesis where the leaderboard has been added. First, create a new folder
x/checkers/migrations/v1tov2 to handle this task.
Create a new type to make it easier to handle your v1 genesis:
This is easy to create, as you only need to copy and paste the values of your genesis from a previous commit.
# Past player handling
Now prepare functions to progressively build the player's information, given a list of games:
# Past leaderboard
Eventually the player information is complete and it is possible to create the leaderboard for these past players. This may involve the sorting of a very large array. Perhaps it could be done in tranches:
Then process the player information:
If you want more details about the number of helper functions like
AddCandidatesAndSortAtNow, go to Run my own chain.
# Genesis migration proper
Now everything is prepared, migrate the v1 genesis:
NextGame are copied from v1 to v2. Also note that all past players are saved
now, since the time was not saved in the game when winning. If you decide to use the
Deadline, make sure that there are no times in the future.
The migration mechanism helps identify how you can upgrade your blockchain to introduce new features.