From 2ac11e8e179282520f1dffa49e82fd4b6312d9d7 Mon Sep 17 00:00:00 2001 From: Jiri Date: Fri, 25 Oct 2024 20:47:54 +0200 Subject: [PATCH] Verification --- app/app.go | 6 ++ app/ordered_map.go | 2 +- app/upgrade_network_config.go | 129 ++++++++++++++++++++++++++++++++++ cmd/fetchd/cmd/cudos_merge.go | 63 +---------------- 4 files changed, 138 insertions(+), 62 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..6e3a58c1 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" ) @@ -549,3 +550,131 @@ 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.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 {