Skip to content

Commit

Permalink
regfactor: CUDOS prefix removed from config and parsed from genesis (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MissingNO57 authored Oct 8, 2024
1 parent 151d49c commit c91059e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 51 deletions.
114 changes: 78 additions & 36 deletions app/upgrade_cudos.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func convertAddressPrefix(addr string, newPrefix string) (string, error) {
return newAddress, nil
}

func convertAddressToRaw(addr string, cudosCfg *CudosMergeConfig) (sdk.AccAddress, error) {
func ensureCudosconvertAddressToRaw(addr string, genesisData *GenesisData) (sdk.AccAddress, error) {
prefix, decodedAddrData, err := bech32.DecodeAndConvert(addr)

if prefix != cudosCfg.config.SourceChainAddressPrefix {
if prefix != genesisData.prefix {
return nil, fmt.Errorf("unknown prefix: %s", prefix)
}

Expand Down Expand Up @@ -107,6 +107,7 @@ type GenesisData struct {
totalSupply sdk.Coins
blockHeight int64
chainId string
prefix string

accounts *OrderedMap[string, *AccountInfo]
contracts *OrderedMap[string, *ContractInfo]
Expand Down Expand Up @@ -222,8 +223,43 @@ func CudosMergeUpgradeHandler(app *App, ctx sdk.Context, cudosCfg *CudosMergeCon
return nil
}

func getAccPrefix(jsonData map[string]interface{}) (string, error) {
// Map to verify that account exists in auth module
auth := jsonData[authtypes.ModuleName].(map[string]interface{})
accounts := auth["accounts"].([]interface{})

lastErr := fmt.Errorf("unknown error")
for _, acc := range accounts {
accMap, ok := acc.(map[string]interface{})
if !ok {
return "", fmt.Errorf("account entry is not a valid map")
}

accountInfo, err := parseGenesisAccount(accMap)
if err != nil {
lastErr = fmt.Errorf("failed to parse account: %w", err)
continue
}

prefix, _, err := bech32.DecodeAndConvert(accountInfo.address)
if err != nil {
lastErr = fmt.Errorf("failed to decode address %s: %w", accountInfo.address, err)
continue
}

// Return immediately if a valid prefix is found
if prefix != "" {
return prefix, nil
}
}

// If no valid prefix was found, return the last encountered error
return "", fmt.Errorf("failed to get prefix: %w", lastErr)
}

func parseGenesisData(jsonData map[string]interface{}, genDoc *tmtypes.GenesisDoc, cudosCfg *CudosMergeConfig, manifest *UpgradeManifest) (*GenesisData, error) {
genesisData := GenesisData{}
var err error

totalSupply, err := parseGenesisTotalSupply(jsonData)
if err != nil {
Expand All @@ -232,13 +268,17 @@ func parseGenesisData(jsonData map[string]interface{}, genDoc *tmtypes.GenesisDo
genesisData.totalSupply = totalSupply
genesisData.blockHeight = genDoc.InitialHeight
genesisData.chainId = genDoc.ChainID
genesisData.prefix, err = getAccPrefix(jsonData)
if err != nil {
return nil, fmt.Errorf("failed to get prefix: %w", err)
}

genesisData.contracts, err = parseGenesisWasmContracts(jsonData)
if err != nil {
return nil, fmt.Errorf("failed to get contracts: %w", err)
}

genesisData.ibcAccounts, err = parseGenesisIBCAccounts(jsonData, cudosCfg)
genesisData.ibcAccounts, err = parseGenesisIBCAccounts(jsonData, cudosCfg, genesisData.prefix)
if err != nil {
return nil, fmt.Errorf("failed to get ibc accounts: %w", err)
}
Expand Down Expand Up @@ -318,7 +358,7 @@ type AccountInfo struct {
rawAccData map[string]interface{}
}

func parseGenesisBaseVesting(baseVestingAccData map[string]interface{}, accountInfo *AccountInfo, cudosCfg *CudosMergeConfig) error {
func parseGenesisBaseVesting(baseVestingAccData map[string]interface{}, accountInfo *AccountInfo) error {
// Parse specific base vesting account types
accountInfo.endTime = cast.ToInt64(baseVestingAccData["end_time"].(string))

Expand All @@ -330,15 +370,15 @@ func parseGenesisBaseVesting(baseVestingAccData map[string]interface{}, accountI

// Parse inner base account
baseAccData := baseVestingAccData["base_account"].(map[string]interface{})
err = parseGenesisBaseAccount(baseAccData, accountInfo, cudosCfg)
err = parseGenesisBaseAccount(baseAccData, accountInfo)
if err != nil {
return err
}

return nil
}

func parseGenesisBaseAccount(baseAccData map[string]interface{}, accountInfo *AccountInfo, cudosCfg *CudosMergeConfig) error {
func parseGenesisBaseAccount(baseAccData map[string]interface{}, accountInfo *AccountInfo) error {
accountInfo.address = baseAccData["address"].(string)

// Parse pubkey
Expand All @@ -355,7 +395,8 @@ func parseGenesisBaseAccount(baseAccData map[string]interface{}, accountInfo *Ac
accountInfo.pubkey = AccPubKey

// Get raw address
accRawAddr, err := convertAddressToRaw(accountInfo.address, cudosCfg)
_, accRawAddr, err := bech32.DecodeAndConvert(accountInfo.address)

accountInfo.rawAddress = accRawAddr
if err != nil {
return err
Expand All @@ -364,110 +405,110 @@ func parseGenesisBaseAccount(baseAccData map[string]interface{}, accountInfo *Ac
return nil
}

func parseGenesisDelayedVestingAccount(accMap map[string]interface{}, accountInfo *AccountInfo, cudosCfg *CudosMergeConfig) error {
func parseGenesisDelayedVestingAccount(accMap map[string]interface{}, accountInfo *AccountInfo) error {
// Specific delayed vesting stuff
// Nothing

baseVestingAccData := accMap["base_vesting_account"].(map[string]interface{})
err := parseGenesisBaseVesting(baseVestingAccData, accountInfo, cudosCfg)
err := parseGenesisBaseVesting(baseVestingAccData, accountInfo)
if err != nil {
return err
}

return nil
}

func parseGenesisContinuousVestingAccount(accMap map[string]interface{}, accountInfo *AccountInfo, cudosCfg *CudosMergeConfig) error {
func parseGenesisContinuousVestingAccount(accMap map[string]interface{}, accountInfo *AccountInfo) error {
// Specific continuous vesting stuff

accountInfo.startTime = cast.ToInt64(accMap["start_time"].(string))

baseVestingAccData := accMap["base_vesting_account"].(map[string]interface{})
err := parseGenesisBaseVesting(baseVestingAccData, accountInfo, cudosCfg)
err := parseGenesisBaseVesting(baseVestingAccData, accountInfo)
if err != nil {
return err
}

return nil
}

func parseGenesisPermanentLockedAccount(accMap map[string]interface{}, accountInfo *AccountInfo, cudosCfg *CudosMergeConfig) error {
func parseGenesisPermanentLockedAccount(accMap map[string]interface{}, accountInfo *AccountInfo) error {
baseVestingAccData := accMap["base_vesting_account"].(map[string]interface{})
err := parseGenesisBaseVesting(baseVestingAccData, accountInfo, cudosCfg)
err := parseGenesisBaseVesting(baseVestingAccData, accountInfo)
if err != nil {
return err
}

return nil
}

func parseGenesisPeriodicVestingAccount(accMap map[string]interface{}, accountInfo *AccountInfo, cudosCfg *CudosMergeConfig) error {
func parseGenesisPeriodicVestingAccount(accMap map[string]interface{}, accountInfo *AccountInfo) error {
// Specific periodic stuff
accountInfo.startTime = cast.ToInt64(accMap["start_time"].(string))

// parse periods
// Do we care?

baseVestingAccData := accMap["base_vesting_account"].(map[string]interface{})
err := parseGenesisBaseVesting(baseVestingAccData, accountInfo, cudosCfg)
err := parseGenesisBaseVesting(baseVestingAccData, accountInfo)
if err != nil {
return err
}

return nil
}

func parseGenesisModuleAccount(accMap map[string]interface{}, accountInfo *AccountInfo, cudosCfg *CudosMergeConfig) error {
func parseGenesisModuleAccount(accMap map[string]interface{}, accountInfo *AccountInfo) error {
// Specific module account values
accountInfo.name = accMap["name"].(string)

// parse inner base account
baseAccData := accMap["base_account"].(map[string]interface{})
err := parseGenesisBaseAccount(baseAccData, accountInfo, cudosCfg)
err := parseGenesisBaseAccount(baseAccData, accountInfo)
if err != nil {
return err
}

return nil
}

func parseGenesisAccount(accMap map[string]interface{}, cudosCfg *CudosMergeConfig) (*AccountInfo, error) {
func parseGenesisAccount(accMap map[string]interface{}) (*AccountInfo, error) {
accountInfo := AccountInfo{balance: sdk.NewCoins(), migrated: false, rawAccData: accMap}
accType := accMap["@type"]

// Extract base account and special values
if accType == ModuleAccount {
err := parseGenesisModuleAccount(accMap, &accountInfo, cudosCfg)
err := parseGenesisModuleAccount(accMap, &accountInfo)
if err != nil {
return nil, err
}
accountInfo.accountType = ModuleAccountType
} else if accType == DelayedVestingAccount {
err := parseGenesisDelayedVestingAccount(accMap, &accountInfo, cudosCfg)
err := parseGenesisDelayedVestingAccount(accMap, &accountInfo)
if err != nil {
return nil, err
}
accountInfo.accountType = DelayedVestingAccountType
} else if accType == ContinuousVestingAccount {
err := parseGenesisContinuousVestingAccount(accMap, &accountInfo, cudosCfg)
err := parseGenesisContinuousVestingAccount(accMap, &accountInfo)
if err != nil {
return nil, err
}
accountInfo.accountType = ContinuousVestingAccountType
} else if accType == PermanentLockedAccount {
err := parseGenesisPermanentLockedAccount(accMap, &accountInfo, cudosCfg)
err := parseGenesisPermanentLockedAccount(accMap, &accountInfo)
if err != nil {
return nil, err
}
accountInfo.accountType = PermanentLockedAccountType
} else if accType == PeriodicVestingAccount {
err := parseGenesisPeriodicVestingAccount(accMap, &accountInfo, cudosCfg)
err := parseGenesisPeriodicVestingAccount(accMap, &accountInfo)
if err != nil {
return nil, err
}
accountInfo.accountType = PeriodicVestingAccountType
} else if accType == BaseAccount {
err := parseGenesisBaseAccount(accMap, &accountInfo, cudosCfg)
err := parseGenesisBaseAccount(accMap, &accountInfo)
if err != nil {
return nil, err
}
Expand All @@ -489,7 +530,7 @@ func parseGenesisAccounts(jsonData map[string]interface{}, contractAccountMap *O

for _, acc := range accounts {
accMap := acc.(map[string]interface{})
accountInfo, err := parseGenesisAccount(accMap, cudosCfg)
accountInfo, err := parseGenesisAccount(accMap)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -944,7 +985,7 @@ func createGenesisDelegations(ctx sdk.Context, app *App, genesisData *GenesisDat
}
} else {
// Regular case
delegatorRawAddr, err = convertAddressToRaw(delegatorAddr, cudosCfg)
delegatorRawAddr, err = ensureCudosconvertAddressToRaw(delegatorAddr, genesisData)
if err != nil {
return err
}
Expand Down Expand Up @@ -1049,14 +1090,15 @@ func convertBalance(balance sdk.Coins, cudosCfg *CudosMergeConfig) (sdk.Coins, e
return resBalance, nil
}

func ensureAccount(addrStr string, genesisAccountsMap *OrderedMap[string, *AccountInfo], reason string, cudosCfg *CudosMergeConfig, manifest *UpgradeManifest) error {
func ensureAccount(addrStr string, genesisAccountsMap *OrderedMap[string, *AccountInfo], reason string, manifest *UpgradeManifest) error {
// Create new account if it doesn't exist
if genesisAccountsMap.Has(addrStr) {
// Already exist
return nil
}

accRawAddress, err := convertAddressToRaw(addrStr, cudosCfg)
_, accRawAddress, err := bech32.DecodeAndConvert(addrStr)

if err != nil {
return err
}
Expand Down Expand Up @@ -1103,7 +1145,7 @@ func fillGenesisBalancesToAccountsMap(jsonData map[string]interface{}, genesisAc

if !convertedBalance.IsZero() {
// Create new account if it doesn't exist
err := ensureAccount(addrStr, genesisAccountsMap, "bank_balance_no_auth_acc", cudosCfg, manifest)
err := ensureAccount(addrStr, genesisAccountsMap, "bank_balance_no_auth_acc", manifest)
if err != nil {
return err
}
Expand Down Expand Up @@ -1155,7 +1197,7 @@ type IBCInfo struct {
portId string
}

func parseGenesisIBCAccounts(jsonData map[string]interface{}, cudosCfg *CudosMergeConfig) (*OrderedMap[string, *IBCInfo], error) {
func parseGenesisIBCAccounts(jsonData map[string]interface{}, cudosCfg *CudosMergeConfig, prefix string) (*OrderedMap[string, *IBCInfo], error) {
ibcAccountMap := NewOrderedMap[string, *IBCInfo]()

ibc, ok := jsonData[ibccore.ModuleName].(map[string]interface{})
Expand Down Expand Up @@ -1190,7 +1232,7 @@ func parseGenesisIBCAccounts(jsonData map[string]interface{}, cudosCfg *CudosMer
}

rawAddr := ibctransfertypes.GetEscrowAddress(portId, channelId)
channelAddr, err := sdk.Bech32ifyAddressBytes(cudosCfg.config.SourceChainAddressPrefix, rawAddr)
channelAddr, err := sdk.Bech32ifyAddressBytes(prefix, rawAddr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1530,7 +1572,7 @@ func moveGenesisBalance(genesisData *GenesisData, fromAddress, toAddress string,
}

// Create to account if it doesn't exist
err := ensureAccount(toAddress, genesisData.accounts, "balance_movement_destination", cudosCfg, manifest)
err := ensureAccount(toAddress, genesisData.accounts, "balance_movement_destination", manifest)
if err != nil {
return err
}
Expand All @@ -1556,9 +1598,9 @@ func moveGenesisBalance(genesisData *GenesisData, fromAddress, toAddress string,
return nil
}

func createGenesisBalance(genesisData *GenesisData, toAddress string, amount sdk.Coins, memo string, manifest *UpgradeManifest, cudosCfg *CudosMergeConfig) error {
func createGenesisBalance(genesisData *GenesisData, toAddress string, amount sdk.Coins, memo string, manifest *UpgradeManifest) error {
// Create to account if it doesn't exist
err := ensureAccount(toAddress, genesisData.accounts, "balance_creation_destination", cudosCfg, manifest)
err := ensureAccount(toAddress, genesisData.accounts, "balance_creation_destination", manifest)
if err != nil {
return err
}
Expand Down Expand Up @@ -1788,14 +1830,14 @@ func MigrateGenesisAccounts(genesisData *GenesisData, ctx sdk.Context, app *App,
err = migrateToAccount(ctx, app, "mint_module", commissionRawAcc, sdk.NewCoins(), totalCommission, "total_commission", manifest)

extraSupplyInCudos := cudosCfg.config.TotalCudosSupply.Sub(genesisData.totalSupply.AmountOf(cudosCfg.config.OriginalDenom))
extraSupplyCudosAddress, err := convertAddressPrefix(cudosCfg.config.ExtraSupplyFetchAddr, cudosCfg.config.SourceChainAddressPrefix)
extraSupplyCudosAddress, err := convertAddressPrefix(cudosCfg.config.ExtraSupplyFetchAddr, genesisData.prefix)
if err != nil {
return err
}

extraSupplyInCudosCoins := sdk.NewCoins(sdk.NewCoin(cudosCfg.config.OriginalDenom, extraSupplyInCudos))

err = createGenesisBalance(genesisData, extraSupplyCudosAddress, extraSupplyInCudosCoins, "extra_supply", manifest, cudosCfg)
err = createGenesisBalance(genesisData, extraSupplyCudosAddress, extraSupplyInCudosCoins, "extra_supply", manifest)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions app/upgrade_cudos_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func withdrawAccumulatedCommissions(genesisData *GenesisData, cudosCfg *CudosMer
for _, validatorAddress := range genesisData.distributionInfo.validatorAccumulatedCommissions.Keys() {
accumulatedCommission := genesisData.distributionInfo.validatorAccumulatedCommissions.MustGet(validatorAddress)

accountAddress, err := convertAddressPrefix(validatorAddress, cudosCfg.config.SourceChainAddressPrefix)
accountAddress, err := convertAddressPrefix(validatorAddress, genesisData.prefix)
if err != nil {
return err
}
Expand All @@ -440,7 +440,7 @@ func withdrawValidatorOutstandingRewards(genesisData *GenesisData, cudosCfg *Cud
for _, validatorAddress := range genesisData.distributionInfo.outstandingRewards.Keys() {
outstandingRewards := genesisData.distributionInfo.outstandingRewards.MustGet(validatorAddress)

accountAddress, err := convertAddressPrefix(validatorAddress, cudosCfg.config.SourceChainAddressPrefix)
accountAddress, err := convertAddressPrefix(validatorAddress, genesisData.prefix)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit c91059e

Please sign in to comment.