Skip to content

Commit

Permalink
feat: Refact. of NetworkConfig + dropping usage of panic(...) (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbukva authored Sep 12, 2024
1 parent 9f32ea8 commit 96e3f66
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 261 deletions.
74 changes: 8 additions & 66 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -731,30 +730,24 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
manifest := NewUpgradeManifest()

if app.cudosPath == "" {
panic("cudos path not set")
return nil, fmt.Errorf("cudos path not set")
}

networkInfo, ok := NetworkInfos[ctx.ChainID()]
if !ok {
panic("Network info not found for chain id: " + ctx.ChainID())
return nil, fmt.Errorf("Network info not found for chain id: " + ctx.ChainID())
}

err := app.DeleteContractStates(ctx, &networkInfo, manifest)
if err != nil {
return nil, err
}

// Call the separate function to handle the admin upgrade
err = app.UpgradeContractAdmins(ctx, &networkInfo, manifest)
if err != nil {
return nil, err
}

err = app.ProcessReconciliation(ctx, &networkInfo, manifest)
if err != nil {
return nil, err
}

err = app.ChangeContractLabels(ctx, &networkInfo, manifest)
if err != nil {
return nil, err
Expand All @@ -765,73 +758,22 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
return nil, err
}

_, genDoc, err := genutiltypes.GenesisStateFromGenFile(app.cudosPath)
if err != nil {
panic(fmt.Errorf("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 {
panic(fmt.Errorf("failed to unmarshal app state: %w", err))
}

genesisData, err := parseGenesisData(jsonData, networkInfo)
if err != nil {
panic(err)
}

err = GenesisUpgradeWithdrawIBCChannelsBalances(genesisData, networkInfo, manifest)
if err != nil {
panic(fmt.Errorf("failed to withdraw IBC channels balances: %w", err))
}

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

delegatedBalanceMap, err := withdrawGenesisStakingDelegations(genesisData, networkInfo, manifest)
if err != nil {
panic(fmt.Errorf("failed to withdraw genesis staked tokens: %w", err))
}

err = withdrawGenesisDistributionRewards(genesisData, networkInfo, manifest)
if err != nil {
panic(fmt.Errorf("failed to withdraw genesis rewards: %w", err))
}

err = WithdrawGenesisGravity(genesisData, networkInfo, manifest)
if err != nil {
panic(fmt.Errorf("failed to withdraw gravity: %w", err))
}

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

err = createGenesisDelegations(ctx, app, delegatedBalanceMap, networkInfo, manifest)
if err != nil {
panic(fmt.Errorf("failed process delegations: %w", err))
}

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

err = VerifySupply(genesisData, networkInfo, manifest)
err = CudosMergeUpgradeHandler(app, ctx, networkInfo.CudosMerge, manifest)
if err != nil {
panic(fmt.Errorf("failed to verify supply: %w", err))
return nil, err
}

// Save the manifest
err = app.SaveManifest(manifest, plan.Name)
if err != nil {
panic(err)
return nil, err
}

// TODO(pb): ! Drop this in release version !
panic("Debug interruption")

// End of migration
Expand Down
75 changes: 39 additions & 36 deletions app/upgrade_0_11_4_reconciliation.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ func (app *App) ChangeContractLabel(ctx types.Context, contractAddr *string, new
contractAddrKey := append(wasmTypes.ContractKeyPrefix, *addr...)
(*store).Set(contractAddrKey, contractBz)

if manifest.Contracts == nil {
manifest.Contracts = new(Contracts)
}

manifest.Contracts.LabelUpdated = append(manifest.Contracts.LabelUpdated, ContractValueUpdate{*contractAddr, oldLabel, *newLabel})

return nil
}

Expand Down Expand Up @@ -129,17 +134,22 @@ func (app *App) ChangeContractVersion(ctx types.Context, contractAddr *string, n
To: newVersion,
}

if manifest.Contracts == nil {
manifest.Contracts = new(Contracts)
}

manifest.Contracts.VersionUpdated = append(manifest.Contracts.VersionUpdated, manifestVersionUpdate)

return nil
}

func (app *App) ChangeContractLabels(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error {
contracts := []struct{ addr, newLabel *string }{
{addr: &networkInfo.Contracts.Reconciliation.Addr, newLabel: networkInfo.Contracts.Reconciliation.NewLabel},
}
contracts := []IContractLabel{networkInfo.Contracts.Reconciliation}
for _, contract := range contracts {
err := app.ChangeContractLabel(ctx, contract.addr, contract.newLabel, manifest)
if contract == nil {
continue
}
err := app.ChangeContractLabel(ctx, contract.GetPrimaryContractAddr(), contract.GetNewLabel(), manifest)
if err != nil {
return err
}
Expand All @@ -149,15 +159,12 @@ func (app *App) ChangeContractLabels(ctx types.Context, networkInfo *NetworkConf
}

func (app *App) ChangeContractVersions(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error {
contracts := []struct {
addr *string
newVersion *ContractVersion
}{
{addr: &networkInfo.Contracts.Reconciliation.Addr, newVersion: networkInfo.Contracts.Reconciliation.NewContractVersion},
}
contracts := []IContractVersion{networkInfo.Contracts.Reconciliation}
for _, contract := range contracts {

err := app.ChangeContractVersion(ctx, contract.addr, contract.newVersion, manifest)
if contract == nil {
continue
}
err := app.ChangeContractVersion(ctx, contract.GetPrimaryContractAddr(), contract.GetNewVersion(), manifest)
if err != nil {
return err
}
Expand Down Expand Up @@ -375,7 +382,7 @@ func DropHexPrefix(hexEncodedData string) string {
return hexEncodedData
}

func (app *App) UpgradeContractAdmin(ctx types.Context, newAdmin *string, contractAddr *string, manifest *UpgradeManifest) error {
func (app *App) UpgradeContractAdmin(ctx types.Context, contractAddr *string, newAdmin *string, manifest *UpgradeManifest) error {
if newAdmin == nil || contractAddr == nil {
return nil
}
Expand All @@ -399,18 +406,22 @@ func (app *App) UpgradeContractAdmin(ctx types.Context, newAdmin *string, contra
contractAddrKey := append(wasmTypes.ContractKeyPrefix, *addr...)
(*store).Set(contractAddrKey, contractBz)

if manifest.Contracts == nil {
manifest.Contracts = new(Contracts)
}

manifest.Contracts.AdminUpdated = append(manifest.Contracts.AdminUpdated, ContractValueUpdate{*contractAddr, oldAdmin, *newAdmin})

return nil
}

func (app *App) UpgradeContractAdmins(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error {
contracts := []struct{ Addr, NewAdmin *string }{
{Addr: &networkInfo.Contracts.Reconciliation.Addr, NewAdmin: networkInfo.Contracts.Reconciliation.NewAdmin},
{Addr: &networkInfo.Contracts.TokenBridge.Addr, NewAdmin: networkInfo.Contracts.TokenBridge.NewAdmin},
}

contracts := []IContractAdmin{networkInfo.Contracts.Reconciliation, networkInfo.Contracts.TokenBridge}
for _, contract := range contracts {
err := app.UpgradeContractAdmin(ctx, contract.NewAdmin, contract.Addr, manifest)
if contract == nil {
continue
}
err := app.UpgradeContractAdmin(ctx, contract.GetPrimaryContractAddr(), contract.GetNewAdminAddr(), manifest)
if err != nil {
return err
}
Expand All @@ -435,30 +446,22 @@ func (app *App) DeleteContractState(ctx types.Context, contractAddr string, mani
for ; iter.Valid(); iter.Next() {
prefixStore.Delete(iter.Key())
}

if manifest.Contracts == nil {
manifest.Contracts = new(Contracts)
}

manifest.Contracts.StateCleaned = append(manifest.Contracts.StateCleaned, contractAddr)

return nil
}

func (app *App) DeleteContractStates(ctx types.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error {
var contractsToWipe []string

if networkInfo.Contracts.Reconciliation != nil {
contractsToWipe = append(contractsToWipe, networkInfo.Contracts.Reconciliation.Addr)
}

if networkInfo.Contracts.Almanac != nil {
contractsToWipe = append(contractsToWipe,
networkInfo.Contracts.Almanac.ProdAddr,
networkInfo.Contracts.Almanac.DevAddr,
)
}

if networkInfo.Contracts.AName != nil {
contractsToWipe = append(contractsToWipe,
networkInfo.Contracts.AName.ProdAddr,
networkInfo.Contracts.AName.DevAddr,
)
}
contractsToWipe = networkInfo.Contracts.Reconciliation.GetContracts(contractsToWipe)
contractsToWipe = networkInfo.Contracts.Almanac.GetContracts(contractsToWipe)
contractsToWipe = networkInfo.Contracts.AName.GetContracts(contractsToWipe)

for _, contract := range contractsToWipe {
err := app.DeleteContractState(ctx, contract, manifest)
Expand Down
Loading

0 comments on commit 96e3f66

Please sign in to comment.