Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CUDOS extra config verification #414

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/ordered_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures exclusivity of the pair.Key in the input list = that input list do NOT contain more then single pair with the same pair.Key value.

}

return newMap
Expand Down
129 changes: 129 additions & 0 deletions app/upgrade_network_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This effectively ensures, that the original address value does not contain preceding and trailing whitespace characters.

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
}
63 changes: 2 additions & 61 deletions cmd/fetchd/cmd/cudos_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ const (
FlagCudosMigrationConfigSha256 = "cudos-migration-config-sha256"

FlagManifestDestinationPath = "manifest-destination-path"

DestinationChainPrefix = "fetch"
)

func AddCudosFlags(startCmd *cobra.Command) {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Loading