Skip to content

Instantly share code, notes, and snippets.

@spoo-bar
Last active May 12, 2024 11:39
Show Gist options
  • Save spoo-bar/e00f6059704b28391f625e41dc240c93 to your computer and use it in GitHub Desktop.
Save spoo-bar/e00f6059704b28391f625e41dc240c93 to your computer and use it in GitHub Desktop.
A guide to run a cosmos sdk based chain on Windows natively

Cosmos on Windows

By the end of this guide, you will be running a Cosmos chain on your local machine. We will start with the assumption that you are on a clean fresh new Windows10/11 system.

We will be using Powershell as our terminal and not Command Prompt. I highly recommend you install Windows Terminal app. You can download it here from the Windows Store

❗This guide has not yet been tested with chains which use x/wasmd (Cosmwasm).

There are two ways to get Cosmos chains running on Windows.

  1. WSL - This uses Windows Subsystem for Linux. You could use Ubuntu or Debian or whatever you choose from within Windows. Since most Cosmos projects are already compatible with Linux systems, its likely that you do not need to do much to get things up and running. However, you are not actually running Linux natively. There is a performance hit. You might not notice this if you use vim or even vscode. However, if you want to use IDEs like Jetbrain's GoLand, the IO gets slow enough to be noticeable.
  2. Native Windows - The initial setup is not straightforward (but thats why you got this guide). You need to do it only once. And after setup, its just life as usual to continue your work.

I prefer native setup as that provides better long term work environment. However, this guide helps you set up both ways, afterall, you know your requirements best.

WSL

Install Ubuntu

Go to Windows Store and install Ubuntu After the distro has been installed, open WSL in terminal and run

sudo apt update
sudo apt install build-essential

Install GoLang

You can download Go for Ubuntu here: https://go.dev/doc/install. Make sure you follow the Linux installation instructions, and NOT the Windows instructions.

Build Cosmos SDK

You can now go to your desired directory and run the following commands to download the repository locally and build the binary.

git clone https://github.com/cosmos/cosmos-sdk.git
cd cosmos-sdk
make build
cd build

You will now find you compiled binary for the cosmos chain here as simd.

You can find the detalis for running the chain here.

Native Windows

Install git

You can download Git for Windows here: https://git-scm.com/downloads Go through the installer and post installation verify that git has been installed by running the following command.

git version

Install GoLang

You can download Go for Windows here: https://go.dev/doc/install Go through the installer and post installation verify that Go has been installed by running the following command in terminal

go version

Clone cosmos-sdk

In your terminal app, navigate to the folder where you want to download the code and run the following

git clone https://github.com/cosmos/cosmos-sdk.git

This might take a while. Once done close the terminal.

Install linux binaries

To build cosmos-sdk app, you will need to install a few linux binaries like make, gcc and sed, we will install Mingw-w64 via MSYS2. It provides native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries.

Go to Msys2 website and download and run the installer. You can follow the setup instructions on the website until Step 6.

Now you will have some of your your windows compatible binaries installed. To make them accessible via you default terminal, we will need to add the bin folders to Windows PATH environment variable.

  1. In the Windows search bar, type 'settings' to open your Windows Settings.

  2. Search for Edit environment variables for your account

  3. Go to Advanced tab and click on Environment Variables

  4. Double click on Path under User variables

  5. In the new window, click on Edit and in the new dialog box, click on Add and add the following paths

    C:\msys64\mingw64\bin

    C:\msys64\usr\bin

  6. Select OK to save the updated PATH.

Install make and sed

  1. Search and open "MSYS2 MSYS"
  2. Run pacman -S sed
  3. Run pacman -S make

Make sure your PATH entry matches the Mingw-w64 binary location where the installed binaries are located.

Build chain binary

Open new terminal instance and go to the root of the cosmos-sdk ripository. Run make build It takes a while, but it should now work. You can find your binary at ..\cosmos-sdk\build\simd.exe From the root of the repository run .\build\simd.exe version and it should show the sdk version

Here you have a cosmos chain windows binary 🎉

Run the chain

In the root of your repository, create a file called "localnet.ps1". Paste the following contents into the file and run .\localnet.ps1 And your chain should be running now

###############################################################################
###                               Localnet.ps1                              ###
###############################################################################
###                                                                         ###
### How to use?                                                             ###
###                                                                         ###
### `.\localnet.ps1`         runs the entire script including clearing old  ###
### data and re running the default setup and then starts the chain.        ###
###                                                                         ###
### `.\localnet.ps1 -Run`    skips the cleanup & setup and directly starts  ###
###  the chain from existing data and continues from old height.            ###
###############################################################################

Param(
    # If specified, the localnet setup is bypassed and the chain is directly run
    [Parameter(Mandatory=$false)]
    [Switch]$Run
)

###############################################################################
###                               Script Vars                               ###
###############################################################################
$CHAIN_ID = "localnet"
$CHAIN_DIR = "data"
$BUILD_DIR = "build"
$BINARY = "simd.exe"
$BINARY_PATH = Join-Path -Path (Join-Path -Path $PWD -ChildPath $BUILD_DIR) -ChildPath $BINARY
$CHAIN_DATA = Join-Path -Path (Join-Path -Path $PWD -ChildPath $CHAIN_DIR) -ChildPath $CHAIN_ID

## If the param is set, chain setup is not run
if($Run -eq $false)
{

###############################################################################
###                         Application Data Vars                           ###
###############################################################################
$MNEMONIC_1 = "guard cream sadness conduct invite crumble clock pudding hole grit liar hotel maid produce squeeze return argue turtle know drive eight casino maze host"
$MNEMONIC_2 = "friend excite rough reopen cover wheel spoon convince island path clean monkey play snow number walnut pull lock shoot hurry dream divide concert discover"
$MNEMONIC_3 = "fuel obscure melt april direct second usual hair leave hobby beef bacon solid drum used law mercy worry fat super must ritual bring faculty"
$GENESIS_COINS = "10000000000000000000000000stake"
$STAKE_AMOUNT = "1000000000stake"



###############################################################################
###                              Setup Chain                                ###
###############################################################################
Write-Host "`nChain : " $CHAIN_ID -ForegroundColor Green

Remove-Item $CHAIN_DATA -Recurse
$CHAIN_DATA = New-Item -ItemType Directory -Force -Path $CHAIN_DATA

Write-Host "`nWriting chain data at : " $CHAIN_DATA -ForegroundColor Green

Write-Host "`nInitializing chain : " $CHAIN_ID -ForegroundColor Green

Start-Process -NoNewWindow -Wait -FilePath $BINARY_PATH -ArgumentList "--home $CHAIN_DATA  init $CHAIN_ID --chain-id $CHAIN_ID -o"

Write-Host "`nAdding genesis accounts" -ForegroundColor Green

$MNEMONIC_1 | & $BINARY_PATH --home $CHAIN_DATA keys add user --recover --keyring-backend test
$MNEMONIC_2 | & $BINARY_PATH --home $CHAIN_DATA keys add user2 --recover --keyring-backend test
$MNEMONIC_3 | & $BINARY_PATH --home $CHAIN_DATA keys add validator --recover --keyring-backend test

$USER = & $BINARY_PATH --home $CHAIN_DATA keys show user --keyring-backend test -a

Start-Process -NoNewWindow -Wait -FilePath $BINARY_PATH -ArgumentList "--home $CHAIN_DATA add-genesis-account $USER $GENESIS_COINS --keyring-backend test"

$USER2 = & $BINARY_PATH --home $CHAIN_DATA keys show user2 --keyring-backend test -a

Start-Process -NoNewWindow -Wait -FilePath $BINARY_PATH -ArgumentList "--home $CHAIN_DATA add-genesis-account $USER2 $GENESIS_COINS --keyring-backend test"

$VALIDATOR = & $BINARY_PATH --home $CHAIN_DATA keys show validator --keyring-backend test -a

Start-Process -NoNewWindow -Wait -FilePath $BINARY_PATH -ArgumentList "--home $CHAIN_DATA add-genesis-account $VALIDATOR $GENESIS_COINS --keyring-backend test"

Write-Host "`nCreating gentx" -ForegroundColor Green

Start-Process -NoNewWindow -Wait -FilePath $BINARY_PATH -ArgumentList "--home $CHAIN_DATA --chain-id $CHAIN_ID gentx validator $STAKE_AMOUNT --keyring-backend test"

Write-Host "`nCollecting gentxs" -ForegroundColor Green

Start-Process -NoNewWindow -Wait -FilePath $BINARY_PATH -ArgumentList "--home $CHAIN_DATA collect-gentxs" 

}

###############################################################################
###                              Start Chain                                ###
###############################################################################

Write-Host "`nStarting chain : " $CHAIN_ID -ForegroundColor Green

Start-Process -NoNewWindow -Wait -FilePath $BINARY_PATH -ArgumentList "start --home $CHAIN_DATA"

If you have any queries, do reach out :) https://www.spoorthi.dev/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment