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

[WiP] Merger #380

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
73 changes: 51 additions & 22 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ type App struct {
interfaceRegistry types.InterfaceRegistry

invCheckPeriod uint
cudosPath string

// keys to access the substores
keys map[string]*sdk.KVStoreKey
Expand Down Expand Up @@ -256,7 +257,7 @@ type App struct {
// NewSimApp returns a reference to an initialized SimApp.
func New(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
skipUpgradeHeights map[int64]bool, homePath string, invCheckPeriod uint, encodingConfig appparams.EncodingConfig, enabledProposals []wasm.ProposalType,
skipUpgradeHeights map[int64]bool, homePath string, invCheckPeriod uint, cudosPath string, encodingConfig appparams.EncodingConfig, enabledProposals []wasm.ProposalType,
appOpts servertypes.AppOptions, wasmOpts []wasm.Option, baseAppOptions ...func(*baseapp.BaseApp),
) *App {

Expand Down Expand Up @@ -285,6 +286,7 @@ func New(
appCodec: appCodec,
interfaceRegistry: interfaceRegistry,
invCheckPeriod: invCheckPeriod,
cudosPath: cudosPath,
keys: keys,
tkeys: tkeys,
memKeys: memKeys,
Expand Down Expand Up @@ -369,7 +371,7 @@ func New(

app.GovKeeper = *govKeeper.SetHooks(
govtypes.NewMultiGovHooks(
// register the governance hooks
// register the governance hooks
),
)

Expand Down Expand Up @@ -718,41 +720,68 @@ func (app *App) GetSubspace(moduleName string) paramstypes.Subspace {
return subspace
}

func (app *App) PerformASIUpgradeTasks(ctx sdk.Context, networkInfo *NetworkConfig, manifest *UpgradeManifest) error {
err := app.DeleteContractStates(ctx, networkInfo, manifest)
if err != nil {
return err
}

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

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

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

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

return nil
}

func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
app.UpgradeKeeper.SetUpgradeHandler("v0.11.3", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return app.mm.RunMigrations(ctx, cfg, fromVM)
})

app.UpgradeKeeper.SetUpgradeHandler("v0.11.4", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
manifest := NewUpgradeManifest()

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

err := app.DeleteContractStates(ctx, &networkInfo, manifest)
// "fetchd-v0.11.3-8-g929563a"
genesis, err := ReadGenesisFile(app.cudosPath)
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
}
// Create the map to store balances by address
balanceMap, _ := GetBankBalances(genesis)

err = app.ProcessReconciliation(ctx, &networkInfo, manifest)
if err != nil {
return nil, err
// Print the balance map
for addr, coins := range balanceMap {
fmt.Printf("Address: %s\n", addr)
for _, coin := range coins {
fmt.Printf(" Coin: %s, Amount: %s\n", coin.Denom, coin.Amount.String())
}
}

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

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

err = app.ChangeContractVersions(ctx, &networkInfo, manifest)
// Perform ASI upgrade tasks
err = app.PerformASIUpgradeTasks(ctx, &networkInfo, manifest)
if err != nil {
return nil, err
}
Expand Down
89 changes: 89 additions & 0 deletions app/cudos_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package app

import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
"io/ioutil"
"log"
"os"
)

// ParseJSON recursively parses JSON data into a map.
func ParseJSON(data []byte) (map[string]interface{}, error) {
var result map[string]interface{}
err := json.Unmarshal(data, &result)
if err != nil {
return nil, err
}
return result, nil
}

// ReadJSONFile reads a JSON file and returns its content as a byte slice.
func ReadJSONFile(filePath string) ([]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

byteValue, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}

return byteValue, nil
}

func ReadGenesisFile(filePath string) (map[string]interface{}, error) {
jsonData, err := ReadJSONFile(filePath)
if err != nil {
log.Fatalf("Failed to read JSON file: %v", err)
}

parsedData, err := ParseJSON(jsonData)
if err != nil {
log.Fatalf("Failed to parse JSON data: %v", err)
}

return parsedData, nil
}

func GetBankBalances(genesis map[string]interface{}) (map[string][]sdk.Coin, error) {

// Create the map to store balances by address
balanceMap := make(map[string][]sdk.Coin)

// Unsafe way to access and iterate over app_state -> bank -> balances
appState := genesis["app_state"].(map[string]interface{})
bank := appState["bank"].(map[string]interface{})
balances := bank["balances"].([]interface{})

for _, res := range balances {
address := res.(map[string]interface{})["address"].(string)
balance := res.(map[string]interface{})["coins"]

// Create a list to store coins for this address
var coins []sdk.Coin

for _, coinsRes := range balance.([]interface{}) {
denom := coinsRes.(map[string]interface{})["denom"].(string)
amount := coinsRes.(map[string]interface{})["amount"].(string)
// Convert amount to sdk.Int
amountInt, ok := sdk.NewIntFromString(amount)
if !ok {
panic("Failed to convert amount to sdk.Int: %s" + amount)
}
// Append coin to the list
coin := sdk.Coin{
Denom: denom,
Amount: amountInt,
}
coins = append(coins, coin)
}

// Store the list of coins in the map
balanceMap[address] = coins
}

return balanceMap, nil
}
12 changes: 12 additions & 0 deletions cmd/fetchd/cmd/cudos_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmd

import "github.com/spf13/cobra"

// Module init related flags
const (
FlagCudosPath = "cudos-path"
)

func AddCudosFlags(startCmd *cobra.Command) {
startCmd.Flags().String(FlagCudosPath, "", "Cudos genesis path")
}
9 changes: 9 additions & 0 deletions cmd/fetchd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
func addModuleInitFlags(startCmd *cobra.Command) {
crisis.AddModuleInitFlags(startCmd)
wasm.AddModuleInitFlags(startCmd)
AddCudosFlags(startCmd)
}

func queryCommand() *cobra.Command {
Expand Down Expand Up @@ -247,6 +248,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a
logger, db, traceStore, true, skipUpgradeHeights,
cast.ToString(appOpts.Get(flags.FlagHome)),
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
cast.ToString(appOpts.Get(FlagCudosPath)),
a.encCfg,
app.GetEnabledProposals(),
appOpts,
Expand Down Expand Up @@ -277,6 +279,11 @@ func (a appCreator) appExport(
return servertypes.ExportedApp{}, errors.New("application home not set")
}

cudosPath, ok := appOpts.Get(FlagCudosPath).(string)
if !ok || cudosPath == "" {
return servertypes.ExportedApp{}, errors.New("cudos path not set")
}

var emptyWasmOpts []wasm.Option
if height != -1 {
anApp = app.New(
Expand All @@ -287,6 +294,7 @@ func (a appCreator) appExport(
map[int64]bool{},
homePath,
uint(1),
cudosPath,
a.encCfg,
app.GetEnabledProposals(),
appOpts,
Expand All @@ -305,6 +313,7 @@ func (a appCreator) appExport(
map[int64]bool{},
homePath,
uint(1),
cudosPath,
a.encCfg,
app.GetEnabledProposals(),
appOpts,
Expand Down
Loading