Skip to content

Commit

Permalink
Gaia upgrade demo (#184)
Browse files Browse the repository at this point in the history
* integrate upgrade in app.go

* integrated gaia with upgrade

* Update deps to point to regen/upgrade-modules pr

* added docs for upgrade-demo

* Complete upgrade-demo docs, app cleanup

* Update go.mod to most recent

* Moved the demo upgrade handler from app.go into demo docs
  • Loading branch information
ethanfrey authored and fedekunze committed Nov 19, 2019
1 parent 9100142 commit e8db770
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
)

const appName = "GaiaApp"
Expand All @@ -50,11 +52,12 @@ var (
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.NewAppModuleBasic(paramsclient.ProposalHandler, distr.ProposalHandler),
gov.NewAppModuleBasic(paramsclient.ProposalHandler, distr.ProposalHandler, upgradeclient.ProposalHandler),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
supply.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
)

Expand Down Expand Up @@ -105,6 +108,7 @@ type GaiaApp struct {
govKeeper gov.Keeper
crisisKeeper crisis.Keeper
paramsKeeper params.Keeper
upgradeKeeper upgrade.Keeper
evidenceKeeper *evidence.Keeper

// the module manager
Expand All @@ -125,11 +129,10 @@ func NewGaiaApp(
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetAppVersion(version.Version)

keys := sdk.NewKVStoreKeys(
bam.MainStoreKey, auth.StoreKey, staking.StoreKey, supply.StoreKey,
mint.StoreKey, distr.StoreKey, slashing.StoreKey, gov.StoreKey,
params.StoreKey, evidence.StoreKey,
bam.MainStoreKey, auth.StoreKey, staking.StoreKey,
supply.StoreKey, mint.StoreKey, distr.StoreKey, slashing.StoreKey,
gov.StoreKey, params.StoreKey, evidence.StoreKey, upgrade.StoreKey,
)
tKeys := sdk.NewTransientStoreKeys(staking.TStoreKey, params.TStoreKey)

Expand Down Expand Up @@ -167,6 +170,7 @@ func NewGaiaApp(
app.cdc, keys[slashing.StoreKey], &stakingKeeper, slashingSubspace, slashing.DefaultCodespace,
)
app.crisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.supplyKeeper, auth.FeeCollectorName)
app.upgradeKeeper = upgrade.NewKeeper(keys[upgrade.StoreKey], app.cdc)

// create evidence keeper with evidence router
app.evidenceKeeper = evidence.NewKeeper(
Expand All @@ -180,7 +184,8 @@ func NewGaiaApp(
govRouter := gov.NewRouter()
govRouter.AddRoute(gov.RouterKey, gov.ProposalHandler).
AddRoute(params.RouterKey, params.NewParamChangeProposalHandler(app.paramsKeeper)).
AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.distrKeeper))
AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.distrKeeper)).
AddRoute(upgrade.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.upgradeKeeper))
app.govKeeper = gov.NewKeeper(
app.cdc, keys[gov.StoreKey], govSubspace,
app.supplyKeeper, &stakingKeeper, gov.DefaultCodespace, govRouter,
Expand All @@ -205,14 +210,15 @@ func NewGaiaApp(
mint.NewAppModule(app.mintKeeper),
slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper),
staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper),
upgrade.NewAppModule(app.upgradeKeeper),
evidence.NewAppModule(*app.evidenceKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
// there is nothing left over in the validator fee pool, so as to keep the
// CanWithdrawInvariant invariant.
app.mm.SetOrderBeginBlockers(mint.ModuleName, distr.ModuleName, slashing.ModuleName)

app.mm.SetOrderBeginBlockers(upgrade.ModuleName, mint.ModuleName, distr.ModuleName, slashing.ModuleName)
app.mm.SetOrderEndBlockers(crisis.ModuleName, gov.ModuleName, staking.ModuleName)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down
121 changes: 121 additions & 0 deletions docs/upgrade-demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Upgrade demo

Compile gaia from https://github.com/regen-friends/gaia/tree/gaia-upgrade-rebased using `make install`

## Prepare demo genesis

Setup local testnet. You can use the following script for this.

```
# You can run all of these commands from your home directory
cd $HOME
# Remove any existing gaiacli and gaiad folders - check there is no useful data there first
rm -rf .gaiacli/ .gaiad/
# Initialize the genesis.json file that will help you to bootstrap the network
gaiad init --chain-id=testing testing
# Create a key to hold your validator account
gaiacli keys add validator
# Add that key into the genesis.app_state.accounts array in the genesis file
# NOTE: this command lets you set the number of coins. Make sure this account has some coins
# with the genesis.app_state.staking.params.bond_denom denom, the default is staking
gaiad add-genesis-account $(gaiacli keys show validator -a) 1000000000stake,1000000000validatortoken
# Generate the transaction that creates your validator
gaiad gentx --name validator
# Add the generated bonding transaction to the genesis file
gaiad collect-gentxs
```

**Important** Change `voting_params.voting_period` in `.gaiad/config/genesis.json` to a reduced time ~5 minutes(300000000000)

## Start the network and trigger upgrade

```
# Start the testnet
gaiad start
# Set up the cli config
gaiacli config trust-node true
gaiacli config chain-id testing
# Create a proposal
gaiacli tx gov submit-proposal software-upgrade test1 --title "test1" --description "upgrade" --from validator --upgrade-height 200 --deposit 10000000stake -y
# Ensure voting_start_time is set (sufficient deposit)
gaiacli query gov proposal 1
# Vote the proposal
gaiacli tx gov vote 1 yes --from validator -y
#Wait for the voting period to pass...
sleep 500
# Query the proposal after voting time ends to see if it passed using
gaiacli query gov proposal 1
# Query the pending plan
gaiacli query upgrade plan
# Verify the bonus account (see commented upgrade handler) is empty
gaiacli query account cosmos18cgkqduwuh253twzmhedesw3l7v3fm37sppt58
```

## Performing an upgrade

Assuming you voted properly above, the proposal will pass, and the chain will stop at given upgrade height.
You can stop and start the original binary all you want, but it will refuse to run after the upgrade height.
We need a new binary with the upgrade handler installed. The logs should look something like:

```
E[2019-11-05|12:44:18.913] UPGRADE "test1" NEEDED at height: 200: module=main
E[2019-11-05|12:44:18.914] CONSENSUS FAILURE!!!
...
```

Note that the process just hangs, doesn't exit to avoid restart loops. You must manually kill the process and replace it with a new
binary. Do so now with `Ctrl+C` or `killall gaiad`.

In `gaia/app/app.go`, add the following lines after line 169 (oncd `app.upgradeKeeper` is initialized). Make sure the upgrade title in handler matches the title from proposal.

```go
app.upgradeKeeper.SetUpgradeHandler("test1", func(ctx sdk.Context, plan upgrade.Plan) {
// Add some coins to a random account
addr, err := sdk.AccAddressFromBech32("cosmos18cgkqduwuh253twzmhedesw3l7v3fm37sppt58")
if err != nil {
panic(err)
}
_, err = app.bankKeeper.AddCoins(ctx, addr, sdk.Coins{sdk.Coin{Denom: "stake", Amount: sdk.NewInt(345600000)}})
if err != nil {
panic(err)
}
})
```

Note that we panic on any error - this would cause the upgrade to fail if the migration could not be run, and no node
would advance - allowing a manual recovery. If we ignored the errors, then we would proceed with an incomplete upgrade
and have a very difficult time every recovering the proper state.

Now, compile the new binary and run the upgraded code to complete the upgrade:

```
# Create a new binary of gaia with added upgrade handler using
make install
# Restart the chain using new binary, you should see the chain resume from the upgrade height
# Like `I[2019-11-05|12:48:15.184] applying upgrade "test1" at height: 200 module=main`
gaiad start
# Verify no more pending plan
gaiacli query upgrade plan
# You can query the block header of the completed upgrade
gaiacli query upgrade applied test1
# Verify the bonus account (see commented upgrade handler) is now rich
gaiacli query account cosmos18cgkqduwuh253twzmhedesw3l7v3fm37sppt58
```
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,22 @@ github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6
github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962 h1:eUm8ma4+yPknhXtkYlWh3tMkE6gBjXZToDned9s2gbQ=
github.com/rcrowley/go-metrics v0.0.0-20190706150252-9beb055b7962/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101124434-7307c3cb6533 h1:ZZ9jT0fFL7uRsKn3xR1NOhbgoxx6x6PgRJUq7o1do0Q=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101124434-7307c3cb6533/go.mod h1:PuN72vbZxlorpnHmoIl6RjzVMXEOMejmwORJlZ74K7Q=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101134638-82d72de4f16a h1:WBBi+9EevKwpkNbK+9/U+XLiOixCxS3bhTHaSahiWRU=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101134638-82d72de4f16a/go.mod h1:PuN72vbZxlorpnHmoIl6RjzVMXEOMejmwORJlZ74K7Q=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101140248-3ec02d7b49ad h1:U+6LzpXFwKUx0wyf8zcBcdkOW9aorTmMLvK7OMy/S84=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101140248-3ec02d7b49ad/go.mod h1:PuN72vbZxlorpnHmoIl6RjzVMXEOMejmwORJlZ74K7Q=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101140703-25ed8b852bbd h1:5zStI/Dp6qTir5qP2uFTbmcx4HZYAoqxVXna9pIqLlY=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101140703-25ed8b852bbd/go.mod h1:PuN72vbZxlorpnHmoIl6RjzVMXEOMejmwORJlZ74K7Q=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101144209-073fd9b4c070 h1:BmxgddMZ95diElMYqC143f/GZP9mMKQpyO1ejHFcIu4=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101144209-073fd9b4c070/go.mod h1:PuN72vbZxlorpnHmoIl6RjzVMXEOMejmwORJlZ74K7Q=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101144426-83bfdc19c405 h1:4HmsC3hkkumwnNUfylZkLcarkwmRA6uhonhQcZgme/s=
github.com/regen-friends/cosmos-sdk v0.25.1-0.20191101144426-83bfdc19c405/go.mod h1:PuN72vbZxlorpnHmoIl6RjzVMXEOMejmwORJlZ74K7Q=
github.com/regen-network/cosmos-sdk v0.25.1-0.20191101102816-9a016d3082fb h1:naQ1WMC5h0uGrzzfwTrdWqv6kuot+j4Pt8cTeHR1wuM=
github.com/regen-network/cosmos-sdk v0.25.1-0.20191101102816-9a016d3082fb/go.mod h1:PuN72vbZxlorpnHmoIl6RjzVMXEOMejmwORJlZ74K7Q=
github.com/regen-network/cosmos-sdk v0.25.1-0.20191105115944-73304609a9f2 h1:7vQv/vms+TMUQkM8uCKIvFhp2wu5rZXJZ10PCQbgxmM=
github.com/regen-network/cosmos-sdk v0.25.1-0.20191105115944-73304609a9f2/go.mod h1:jfj68M7UAsSvG+XmGU5zVfSij5fzlOdiQc1NKt+YK2A=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
Expand Down

0 comments on commit e8db770

Please sign in to comment.