Skip to content

Commit

Permalink
Verification
Browse files Browse the repository at this point in the history
  • Loading branch information
MissingNO57 committed Oct 25, 2024
1 parent 4fe46ad commit 2ac11e8
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 62 deletions.
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)
}

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 {
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

0 comments on commit 2ac11e8

Please sign in to comment.