Skip to content

Commit

Permalink
Refacor
Browse files Browse the repository at this point in the history
  • Loading branch information
MissingNO57 committed Oct 4, 2024
1 parent 615a29b commit 886b30a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
9 changes: 9 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,15 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
return nil, fmt.Errorf("cudos path not set")
}

actualGenesisSha256Hex, err := GenerateSHA256FromFile(app.cudosGenesisPath)
if err != nil {
return nil, fmt.Errorf("failed to generate sha256 out of genesis file %v: %w", app.cudosGenesisPath, err)
}
if app.cudosGenesisSha256 != actualGenesisSha256Hex {
return nil, fmt.Errorf("sha256 failed to verify: genesis file \"%v\" hash %v does not match expected hash %v", app.cudosGenesisPath, actualGenesisSha256Hex, app.cudosGenesisSha256)
}
manifest.GenesisFileSha256 = actualGenesisSha256Hex

networkInfo, err := getNetworkInfo(app, ctx, manifest)
if err != nil {
return nil, err
Expand Down
39 changes: 15 additions & 24 deletions app/upgrade_cudos.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,86 +144,77 @@ type GenesisData struct {

func CudosMergeUpgradeHandler(app *App, ctx sdk.Context, cudosCfg *CudosMergeConfig, manifest *UpgradeManifest) error {
if cudosCfg == nil {
return fmt.Errorf("cudos CudosMergeConfig not provided (null pointer passed in)")
return fmt.Errorf("cudos merge: cudos CudosMergeConfig not provided (null pointer passed in)")
}

if app.cudosGenesisPath == "" {
return fmt.Errorf("cudos path not set")
return fmt.Errorf("cudos merge: cudos path not set")
}

actualGenesisSha256Hex, err := GenerateSHA256FromFile(app.cudosGenesisPath)
if err != nil {
return fmt.Errorf("failed to generate sha256 out of genesis file %v: %w", app.cudosGenesisPath, err)
}
if app.cudosGenesisSha256 != actualGenesisSha256Hex {
return fmt.Errorf("sha256 failed to verify: genesis file \"%v\" hash %v does not match expected hash %v", app.cudosGenesisPath, actualGenesisSha256Hex, app.cudosGenesisSha256)
}
manifest.GenesisFileSha256 = actualGenesisSha256Hex

_, genDoc, err := genutiltypes.GenesisStateFromGenFile(app.cudosGenesisPath)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
return fmt.Errorf("cudos merge: failed to unmarshal genesis state: %w", err)
}

// unmarshal the app state
var jsonData map[string]interface{}
if err = json.Unmarshal(genDoc.AppState, &jsonData); err != nil {
return fmt.Errorf("failed to unmarshal app state: %w", err)
return fmt.Errorf("cudos merge: failed to unmarshal app state: %w", err)
}

genesisData, err := parseGenesisData(jsonData, cudosCfg, manifest)
if err != nil {
return err
return fmt.Errorf("cudos merge: failed to parse genesis data: %w", err)
}

err = genesisUpgradeWithdrawIBCChannelsBalances(genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed to withdraw IBC channels balances: %w", err)
return fmt.Errorf("cudos merge: failed to withdraw IBC channels balances: %w", err)
}

err = withdrawGenesisContractBalances(genesisData, manifest, cudosCfg)
if err != nil {
return fmt.Errorf("failed to withdraw genesis contracts balances: %w", err)
return fmt.Errorf("cudos merge: failed to withdraw genesis contracts balances: %w", err)
}

err = withdrawGenesisStakingDelegations(app, genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed to withdraw genesis staked tokens: %w", err)
return fmt.Errorf("cudos merge: failed to withdraw genesis staked tokens: %w", err)
}

err = withdrawGenesisDistributionRewards(app, genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed to withdraw genesis rewards: %w", err)
return fmt.Errorf("cudos merge: failed to withdraw genesis rewards: %w", err)
}

err = withdrawGenesisGravity(genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed to withdraw gravity: %w", err)
return fmt.Errorf("cudos merge: failed to withdraw gravity: %w", err)
}

err = DoGenesisAccountMovements(genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed to move funds: %w", err)
return fmt.Errorf("cudos merge: failed to move funds: %w", err)
}

err = MigrateGenesisAccounts(genesisData, ctx, app, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed process accounts: %w", err)
return fmt.Errorf("cudos merge: failed process accounts: %w", err)
}

err = createGenesisDelegations(ctx, app, genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed process delegations: %w", err)
return fmt.Errorf("cudos merge: failed process delegations: %w", err)
}

err = fundCommunityPool(ctx, app, genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed to fund community pool: %w", err)
return fmt.Errorf("cudos merge: failed to fund community pool: %w", err)
}

err = verifySupply(genesisData, cudosCfg, manifest)
if err != nil {
return fmt.Errorf("failed to verify supply: %w", err)
return fmt.Errorf("cudos merge: failed to verify supply: %w", err)
}

return nil
Expand Down

0 comments on commit 886b30a

Please sign in to comment.