From b46801ad80624d8d09b1fee4a707ec80ccf28eca Mon Sep 17 00:00:00 2001 From: Jiri Vestfal <45360788+MissingNO57@users.noreply.github.com> Date: Fri, 25 Oct 2024 21:13:29 +0200 Subject: [PATCH] feat: CUDOS extra config verification (#414) --- app/app.go | 6 ++ app/ordered_map.go | 2 +- app/upgrade_network_config.go | 146 ++++++++++++++++++++++++++++++++-- cmd/fetchd/cmd/cudos_merge.go | 63 +-------------- 4 files changed, 148 insertions(+), 69 deletions(-) diff --git a/app/app.go b/app/app.go index 012042c9..b3f9d3b0 100644 --- a/app/app.go +++ b/app/app.go @@ -829,6 +829,12 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) { } cudosConfig := NewCudosMergeConfig(networkInfo.CudosMerge) + + err = VerifyConfig(cudosConfig, cudosGenesisData.Prefix, AccountAddressPrefix) + if err != nil { + return nil, err + } + err = CudosMergeUpgradeHandler(app, ctx, cudosConfig, cudosGenesisData, manifest) if err != nil { return nil, err diff --git a/app/ordered_map.go b/app/ordered_map.go index cc600df2..a66298be 100644 --- a/app/ordered_map.go +++ b/app/ordered_map.go @@ -33,7 +33,7 @@ func NewOrderedMapFromPairs[K comparable, V any](pairs []Pair[K, V]) *OrderedMap newMap := NewOrderedMap[K, V]() for _, pair := range pairs { - newMap.Set(pair.Key, pair.Value) + newMap.SetNew(pair.Key, pair.Value) } return newMap diff --git a/app/upgrade_network_config.go b/app/upgrade_network_config.go index b8cbd3dd..5bf224e0 100644 --- a/app/upgrade_network_config.go +++ b/app/upgrade_network_config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32" "io/ioutil" "os" ) @@ -341,13 +342,13 @@ func LoadAndVerifyNetworkConfigFromFile(configFilePath string, expectedSha256Hex } type CudosMergeConfigJSON struct { - IbcTargetAddr string `json:"ibc_target_addr"` // Cudos address - RemainingStakingBalanceAddr string `json:"remaining_staking_balance_addr"` // Cudos account for remaining bonded and not-bonded pool balances - RemainingGravityBalanceAddr string `json:"remaining_gravity_balance_addr"` // Cudos address - RemainingDistributionBalanceAddr string `json:"remaining_distribution_balance_addr"` // Cudos address - ContractDestinationFallbackAddr string `json:"contract_destination_fallback_addr"` // Cudos address - CommunityPoolBalanceDestAddr string `json:"community_pool_balance_dest_addr"` // Cudos address, funds are moved to destination chain community pool if not set - GenericModuleRemainingBalance string `json:"generic_module_remaining_balance"` // Cudos address for all leftover funds remaining on module accounts after the processing + IbcTargetAddr string `json:"ibc_target_addr"` // Cudos address + RemainingStakingBalanceAddr string `json:"remaining_staking_balance_addr"` // Cudos account for remaining bonded and not-bonded pool balances + RemainingGravityBalanceAddr string `json:"remaining_gravity_balance_addr"` // Cudos address + RemainingDistributionBalanceAddr string `json:"remaining_distribution_balance_addr"` // Cudos address + ContractDestinationFallbackAddr string `json:"contract_destination_fallback_addr"` // Cudos address + CommunityPoolBalanceDestAddr string `json:"community_pool_balance_dest_addr,omitempty"` // Cudos address, funds are moved to destination chain community pool if not set + GenericModuleRemainingBalance string `json:"generic_module_remaining_balance"` // Cudos address for all leftover funds remaining on module accounts after the processing CommissionFetchAddr string `json:"commission_fetch_addr"` // Fetch address for commission ExtraSupplyFetchAddr string `json:"extra_supply_fetch_addr"` // Fetch address for extra supply @@ -549,3 +550,134 @@ func (c *ProdDevContract) GetContracts(contracts []string) []string { return contracts } + +func verifyAddress(address string, expectedPrefix *string) error { + prefix, decodedAddrData, err := bech32.DecodeAndConvert(address) + if err != nil { + return err + } + if expectedPrefix != nil && prefix != *expectedPrefix { + return fmt.Errorf("expected address prefix %s, got %s", *expectedPrefix, prefix) + } + + reconstructedAddr, err := bech32.ConvertAndEncode(prefix, decodedAddrData) + if err != nil { + return err + } + if address != reconstructedAddr { + return fmt.Errorf("Invalid address '%s'", address) + } + + return nil +} + +func VerifyConfig(cudosCfg *CudosMergeConfig, sourceAddrPrefix string, DestAddrPrefix string) error { + expectedSourceValoperPrefix := sourceAddrPrefix + ValAddressPrefix + expectedDestValoperPrefix := DestAddrPrefix + ValAddressPrefix + + for i := range cudosCfg.ValidatorsMap.Iterate() { + srcValidator, DestValidator := i.Key, i.Value + err := verifyAddress(srcValidator, &expectedSourceValoperPrefix) + if err != nil { + return err + } + err = verifyAddress(DestValidator, &expectedDestValoperPrefix) + if err != nil { + return err + } + } + + for _, notDelegatedAccount := range cudosCfg.NotDelegatedAccounts.Keys() { + err := verifyAddress(notDelegatedAccount, &sourceAddrPrefix) + if err != nil { + return err + } + } + + for _, notVestedAccount := range cudosCfg.NotVestedAccounts.Keys() { + err := verifyAddress(notVestedAccount, &sourceAddrPrefix) + if err != nil { + return err + } + } + + for _, movement := range cudosCfg.Config.MovedAccounts { + err := verifyAddress(movement.SourceAddress, &sourceAddrPrefix) + if err != nil { + return err + } + err = verifyAddress(movement.DestinationAddress, &sourceAddrPrefix) + if err != nil { + return err + } + if movement.SourceAddress == movement.DestinationAddress { + return fmt.Errorf("movement source and destination address is the same for %s", movement.SourceAddress) + } + if movement.Amount != nil && movement.Amount.IsNegative() { + return fmt.Errorf("negative amount %s for movement from account %s to %s", movement.Amount, movement.SourceAddress, movement.DestinationAddress) + } + } + + err := verifyAddress(cudosCfg.Config.IbcTargetAddr, &sourceAddrPrefix) + if err != nil { + return fmt.Errorf("ibc targer address error: %v", err) + } + + err = verifyAddress(cudosCfg.Config.RemainingStakingBalanceAddr, &sourceAddrPrefix) + if err != nil { + return fmt.Errorf("remaining staking balance address error: %v", err) + } + + err = verifyAddress(cudosCfg.Config.RemainingGravityBalanceAddr, &sourceAddrPrefix) + if err != nil { + return fmt.Errorf("remaining gravity balance address error: %v", err) + } + + err = verifyAddress(cudosCfg.Config.RemainingDistributionBalanceAddr, &sourceAddrPrefix) + if err != nil { + return fmt.Errorf("remaining distribution balance address error: %v", err) + } + + err = verifyAddress(cudosCfg.Config.ContractDestinationFallbackAddr, &sourceAddrPrefix) + if err != nil { + return fmt.Errorf("contract destination fallback address error: %v", err) + } + + err = verifyAddress(cudosCfg.Config.GenericModuleRemainingBalance, &sourceAddrPrefix) + if err != nil { + return fmt.Errorf("remaining general module balance address error: %v", err) + } + + // Community pool address is optional + if cudosCfg.Config.CommunityPoolBalanceDestAddr != "" { + err = verifyAddress(cudosCfg.Config.CommunityPoolBalanceDestAddr, &sourceAddrPrefix) + if err != nil { + return fmt.Errorf("community pool balance destination address error: %v", err) + } + } + + err = verifyAddress(cudosCfg.Config.CommissionFetchAddr, &DestAddrPrefix) + if err != nil { + return fmt.Errorf("comission address error: %v", err) + } + + err = verifyAddress(cudosCfg.Config.ExtraSupplyFetchAddr, &DestAddrPrefix) + if err != nil { + return fmt.Errorf("extra supply address error: %v", err) + } + + err = verifyAddress(cudosCfg.Config.VestingCollisionDestAddr, &sourceAddrPrefix) + if err != nil { + return fmt.Errorf("vesting collision destination address error: %v", err) + } + + if len(cudosCfg.Config.BalanceConversionConstants) == 0 { + return fmt.Errorf("list of conversion constants is empty") + } + + if len(cudosCfg.Config.BackupValidators) == 0 { + return fmt.Errorf("list of backup validators is empty") + } + + return nil +} diff --git a/cmd/fetchd/cmd/cudos_merge.go b/cmd/fetchd/cmd/cudos_merge.go index 8ad563f4..06e1788c 100644 --- a/cmd/fetchd/cmd/cudos_merge.go +++ b/cmd/fetchd/cmd/cudos_merge.go @@ -22,8 +22,6 @@ const ( FlagCudosMigrationConfigSha256 = "cudos-migration-config-sha256" FlagManifestDestinationPath = "manifest-destination-path" - - DestinationChainPrefix = "fetch" ) func AddCudosFlags(startCmd *cobra.Command) { @@ -213,58 +211,9 @@ func VerifyConfigFile(configFilePath string, GenesisFilePath string, ctx client. return fmt.Errorf("destination chain id is empty") } - // Verify addresses - err = app.VerifyAddressPrefix(cudosConfig.Config.IbcTargetAddr, genesisData.Prefix) - if err != nil { - return fmt.Errorf("ibc targer address error: %v", err) - } - - err = app.VerifyAddressPrefix(cudosConfig.Config.RemainingStakingBalanceAddr, genesisData.Prefix) - if err != nil { - return fmt.Errorf("remaining staking balance address error: %v", err) - } - - err = app.VerifyAddressPrefix(cudosConfig.Config.RemainingGravityBalanceAddr, genesisData.Prefix) - if err != nil { - return fmt.Errorf("remaining gravity balance address error: %v", err) - } - - err = app.VerifyAddressPrefix(cudosConfig.Config.RemainingDistributionBalanceAddr, genesisData.Prefix) - if err != nil { - return fmt.Errorf("remaining distribution balance address error: %v", err) - } - - err = app.VerifyAddressPrefix(cudosConfig.Config.ContractDestinationFallbackAddr, genesisData.Prefix) - if err != nil { - return fmt.Errorf("contract destination fallback address error: %v", err) - } - - err = app.VerifyAddressPrefix(cudosConfig.Config.GenericModuleRemainingBalance, genesisData.Prefix) - if err != nil { - return fmt.Errorf("remaining general module balance address error: %v", err) - } - - // Community pool address is optional - if cudosConfig.Config.CommunityPoolBalanceDestAddr != "" { - err = app.VerifyAddressPrefix(cudosConfig.Config.CommunityPoolBalanceDestAddr, genesisData.Prefix) - if err != nil { - return fmt.Errorf("community pool balance destination address error: %v", err) - } - } - - err = app.VerifyAddressPrefix(cudosConfig.Config.CommissionFetchAddr, DestinationChainPrefix) - if err != nil { - return fmt.Errorf("comission address error: %v", err) - } - - err = app.VerifyAddressPrefix(cudosConfig.Config.ExtraSupplyFetchAddr, DestinationChainPrefix) + err = app.VerifyConfig(cudosConfig, genesisData.Prefix, app.AccountAddressPrefix) if err != nil { - return fmt.Errorf("extra supply address error: %v", err) - } - - err = app.VerifyAddressPrefix(cudosConfig.Config.VestingCollisionDestAddr, genesisData.Prefix) - if err != nil { - return fmt.Errorf("vesting collision destination address error: %v", err) + return err } // Verify extra supply @@ -273,14 +222,6 @@ func VerifyConfigFile(configFilePath string, GenesisFilePath string, ctx client. return fmt.Errorf("total supply %s from config is smaller than total supply %s in genesis", cudosConfig.Config.TotalCudosSupply.String(), bondDenomSourceTotalSupply.String()) } - if len(cudosConfig.Config.BalanceConversionConstants) == 0 { - return fmt.Errorf("list of conversion constants is empty") - } - - if len(cudosConfig.Config.BackupValidators) == 0 { - return fmt.Errorf("list of backup validators is empty") - } - logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) err = app.ProcessSourceNetworkGenesis(logger, cudosConfig, genesisData, manifest) if err != nil {