Skip to content

Commit

Permalink
Dorado migration (#204)
Browse files Browse the repository at this point in the history
* fix: bump go requirements to 1.17 to allow wasmvm to build

* feat: add dorado-migrate command

* chores: fmt

* feat: add initial nanonomx supply to dorado-migrate command

* docs: remove redundant info
  • Loading branch information
daeMOn63 authored Mar 22, 2022
1 parent 1bde317 commit 725b727
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 12 deletions.
202 changes: 202 additions & 0 deletions cmd/fetchd/cmd/doradomigrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package cmd

import (
"encoding/json"
"fmt"
"time"

"github.com/spf13/cobra"

tmjson "github.com/tendermint/tendermint/libs/json"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
authz "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
legacyibc "github.com/cosmos/ibc-go/v2/modules/core/legacy/v100"
"github.com/fetchai/fetchd/app"
)

const (
flagGenesisTime = "genesis-time"
flagInitialHeight = "initial-height"
flagIBCMaxExpectedTimePerBlock = "max-expected-time-per-block"
flagNanonomxSupply = "nanonomx-supply"
flagFoundationAddress = "foundation-address"
)

// AddDoradoMigrateCmd returns a command to migrate genesis to Dorado version.
func AddDoradoMigrateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dorado-migrate <genesis-file>",
Short: "Migrate fetchAI mainnet genesis from the Capricorn version to the Dorado version",
Long: `Migrate fetchAI mainnet genesis from the Capricorn version to the Dorado version.
It does the following operations:
- migrate IBC module state from v1 to v2
- init authz module state
- add initial nanonomx supply to fetch foundation account
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
importGenesis := args[0]

genDoc, err := tmtypes.GenesisDocFromFile(importGenesis)
if err != nil {
return fmt.Errorf("failed to load genesis file at %q: %w", importGenesis, err)
}

// set genesis time
genesisTime, err := cmd.Flags().GetString(flagGenesisTime)
if err != nil {
return fmt.Errorf("failed to read %q flag: %w", flagGenesisTime, err)
}
if genesisTime != "" {
var t time.Time

err := t.UnmarshalText([]byte(genesisTime))
if err != nil {
return errors.Wrap(err, "failed to unmarshal genesis time")
}

genDoc.GenesisTime = t
}

// set initial height
initialHeight, err := cmd.Flags().GetInt64(flagInitialHeight)
if err != nil {
return fmt.Errorf("failed to retrieve flag %q: %w", flagInitialHeight, err)
}
// only set initial height if it was given
// otherwise, keep the initial_height from exported genesis
// that should already be set to last committed block+1
if initialHeight > 0 {
genDoc.InitialHeight = initialHeight
}

// set new chain-id
chainID, err := cmd.Flags().GetString(flags.FlagChainID)
if err != nil {
return fmt.Errorf("failed to read %q flag: %w", flags.FlagChainID, err)
}
if chainID != "" {
genDoc.ChainID = chainID
}

var appState types.AppMap
if err := json.Unmarshal(genDoc.AppState, &appState); err != nil {
return errors.Wrap(err, "failed to JSON unmarshal initial genesis state")
}

// ------------ Start of custom migration operations ------------

// Migrate ibc state from v1 to v2
maxExpectedTimePerBlock, err := cmd.Flags().GetUint64(flagIBCMaxExpectedTimePerBlock)
if err != nil {
return fmt.Errorf("failed to read %q flag: %w", flagIBCMaxExpectedTimePerBlock, err)
}
appState, err = legacyibc.MigrateGenesis(appState, clientCtx, *genDoc, maxExpectedTimePerBlock)
if err != nil {
return errors.Wrap(err, "failed to IBC migrate genesis")
}

// Init Authz module state
authzbz, err := clientCtx.Codec.MarshalJSON(authz.DefaultGenesisState())
if err != nil {
return errors.Wrap(err, "failed to marshal authz state")
}
appState[authz.ModuleName] = authzbz

// Add initial nanonomx supply
foundationAddrStr, err := cmd.Flags().GetString(flagFoundationAddress)
if err != nil {
return fmt.Errorf("failed to read %q flag: %w", flagFoundationAddress, err)
}
foundationAddr, err := sdk.AccAddressFromBech32(foundationAddrStr)
if err != nil {
return fmt.Errorf("failed to parse bech32 foundation address: %w", err)
}
nanonomxStr, err := cmd.Flags().GetString(flagNanonomxSupply)
if err != nil {
return fmt.Errorf("failed to read %q flag: %w", flagNanonomxSupply, err)
}
nanonomxCoin, err := sdk.ParseCoinNormalized(nanonomxStr)
if err != nil {
return fmt.Errorf("failed to parse nanonmox coin: %w", err)
}

appState, err = mintTokens(appState, clientCtx.Codec, foundationAddr, nanonomxCoin)
if err != nil {
return fmt.Errorf("failed to mint tokens: %w", err)
}

// ------------ End of custom migration operations ------------

// Validate state (same as fetchd validate-genesis cmd)
if err := app.ModuleBasics.ValidateGenesis(clientCtx.Codec, clientCtx.TxConfig, appState); err != nil {
return fmt.Errorf("failed to validate state: %w", err)
}

// build and print the new genesis state
genDoc.AppState, err = json.Marshal(appState)
if err != nil {
return errors.Wrap(err, "failed to JSON marshal migrated genesis state")
}
bz, err := tmjson.Marshal(genDoc)
if err != nil {
return errors.Wrap(err, "failed to marshal genesis doc")
}
sortedBz, err := sdk.SortJSON(bz)
if err != nil {
return errors.Wrap(err, "failed to sort JSON genesis doc")
}

fmt.Println(string(sortedBz))

return nil
},
}

cmd.Flags().String(flagGenesisTime, "", "override genesis_time with this flag")
cmd.Flags().Int64(flagInitialHeight, 0, "override initial_height with this flag")
cmd.Flags().String(flags.FlagChainID, "fetchhub-4", "override chain_id with this flag")
// see https://github.com/cosmos/ibc-go/blob/v2.0.3/modules/core/03-connection/types/connection.pb.go#L359-L362
cmd.Flags().Uint64(flagIBCMaxExpectedTimePerBlock, 30000000000, "value for ibc.connection_genesis.params.max_expected_time_per_block (nanoseconds)")
cmd.Flags().String(flagNanonomxSupply, "1000000000000000000nanonomx", "initial nanonomx supply")
cmd.Flags().String(flagFoundationAddress, "fetch1c2wlfqn6eqqknpwcr0na43m9k6hux94dp6fx4y", "fetch.ai foundation address")

return cmd
}

// mintTokens add given coins to given address and increase total supply accordingly.
func mintTokens(state types.AppMap, cdc codec.JSONCodec, addr sdk.AccAddress, coin sdk.Coin) (types.AppMap, error) {
bankState := banktypes.GetGenesisStateFromAppState(cdc, state)

var updated bool
for i, balance := range bankState.Balances {
if balance.GetAddress().Equals(addr) {
bankState.Balances[i].Coins = bankState.Balances[i].Coins.Add(coin)
bankState.Supply = bankState.Supply.Add(coin)

updated = true
break
}
}

if !updated {
return nil, fmt.Errorf("account %s not found", addr.String())
}

bankStateBz, err := cdc.MarshalJSON(bankState)
if err != nil {
return nil, fmt.Errorf("failed to marshal bank genesis state: %w", err)
}
state[banktypes.ModuleName] = bankStateBz

return state, nil
}
1 change: 1 addition & 0 deletions cmd/fetchd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
config.Cmd(),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
AddStakeReconciliationMigrateCmd(),
AddDoradoMigrateCmd(),
genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
genutilcli.ValidateGenesisCmd(app.ModuleBasics),
AddGenesisAccountCmd(app.DefaultNodeHome),
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ require (
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
golang.org/x/net v0.0.0-20211208012354-db4efeb81f4b // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220118154757-00ab72f36ad5 // indirect
google.golang.org/genproto v0.0.0-20220302033224-9aa15565e42a // indirect
google.golang.org/grpc v1.44.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
Expand All @@ -128,4 +128,4 @@ replace google.golang.org/grpc => google.golang.org/grpc v1.33.2

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

replace github.com/cosmos/cosmos-sdk => github.com/fetchai/cosmos-sdk v0.17.8-0.20220301120338-2e922587eecd
replace github.com/cosmos/cosmos-sdk => github.com/fetchai/cosmos-sdk v0.17.8-0.20220301154645-3d22bf5d9e04
17 changes: 9 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGE
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fetchai/cosmos-sdk v0.17.8-0.20220301120338-2e922587eecd h1:Jm3CXSFIW/hv08qEqw4Ue1fjkuKe+TXlqj6bPZ3ILAY=
github.com/fetchai/cosmos-sdk v0.17.8-0.20220301120338-2e922587eecd/go.mod h1:cgt9slB9JrOW/x54syIPZq3vzu1xtk2qTFcbV8HCLZU=
github.com/fetchai/cosmos-sdk v0.17.8-0.20220301154645-3d22bf5d9e04 h1:YhIt95OlYphJ1ixK7L1Aobzfe06hE1ERWVSEye34mTo=
github.com/fetchai/cosmos-sdk v0.17.8-0.20220301154645-3d22bf5d9e04/go.mod h1:xyuwOrMZClaCy6p8mPAu5anqmgPWrQkLb/pYjwox1mM=
github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
Expand Down Expand Up @@ -607,7 +607,6 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
Expand Down Expand Up @@ -811,7 +810,6 @@ github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB8
github.com/prometheus/common v0.14.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s=
github.com/prometheus/common v0.15.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s=
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
github.com/prometheus/common v0.29.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
github.com/prometheus/common v0.30.0/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4=
github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
Expand Down Expand Up @@ -1152,8 +1150,9 @@ golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211208012354-db4efeb81f4b h1:MWaHNqZy3KTpuTMAGvv+Kw+ylsEpmyJZizz1dqxnu28=
golang.org/x/net v0.0.0-20211208012354-db4efeb81f4b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -1280,11 +1279,13 @@ golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -1488,8 +1489,8 @@ google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12/go.mod h1:5CzLGKJ6
google.golang.org/genproto v0.0.0-20211203200212-54befc351ae9/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211206160659-862468c7d6e0/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220118154757-00ab72f36ad5 h1:zzNejm+EgrbLfDZ6lu9Uud2IVvHySPl8vQzf04laR5Q=
google.golang.org/genproto v0.0.0-20220118154757-00ab72f36ad5/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220302033224-9aa15565e42a h1:uqouglH745GoGeZ1YFZbPBiu961tgi/9Qm5jaorajjQ=
google.golang.org/genproto v0.0.0-20220302033224-9aa15565e42a/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
Expand Down

0 comments on commit 725b727

Please sign in to comment.