From a90e7a7f75b31e82039560651bf4637f4a7ea688 Mon Sep 17 00:00:00 2001 From: Jiri Date: Thu, 17 Oct 2024 16:34:34 +0200 Subject: [PATCH] manifest data cmd --- app/upgrade_v_11_4_manifest.go | 25 +++++++ cmd/fetchd/cmd/cudos_merge.go | 133 +++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) diff --git a/app/upgrade_v_11_4_manifest.go b/app/upgrade_v_11_4_manifest.go index a9e19718..27e03a32 100644 --- a/app/upgrade_v_11_4_manifest.go +++ b/app/upgrade_v_11_4_manifest.go @@ -6,6 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "io" "os" "path" ) @@ -254,6 +255,30 @@ func SaveManifestToPath(manifest *UpgradeManifest, manifestFilePath string) erro return nil } +func LoadManifestFromPath(manifestFilePath string) (*UpgradeManifest, error) { + var manifest UpgradeManifest + + // Open the file + file, err := os.Open(manifestFilePath) + if err != nil { + return nil, fmt.Errorf("failed to open file \"%s\": %w", manifestFilePath, err) + } + defer file.Close() + + // Read the file contents + fileContents, err := io.ReadAll(file) + if err != nil { + return nil, fmt.Errorf("failed to read file \"%s\": %w", manifestFilePath, err) + } + + // Unmarshal the JSON content into the manifest + if err := json.Unmarshal(fileContents, &manifest); err != nil { + return nil, fmt.Errorf("failed to unmarshal manifest from file \"%s\": %w", manifestFilePath, err) + } + + return &manifest, nil +} + func RegisterVestingCollision(manifest *UpgradeManifest, originalAccount *AccountInfo, targetAccountFunds types.Coins, targetAccount authtypes.AccountI) error { if manifest.VestingCollision == nil { manifest.VestingCollision = &UpgradeVestingCollision{} diff --git a/cmd/fetchd/cmd/cudos_merge.go b/cmd/fetchd/cmd/cudos_merge.go index 5c427565..3f741382 100644 --- a/cmd/fetchd/cmd/cudos_merge.go +++ b/cmd/fetchd/cmd/cudos_merge.go @@ -116,6 +116,30 @@ It utilizes the provided network merge config and genesis JSON files to perform networkMergeCmd.AddCommand(cmd) } +func AddCommandManifestAddressInfo(networkMergeCmd *cobra.Command) { + cmd := &cobra.Command{ + Use: "manifest-address-info [manifest_file_path] [address]", + Short: "Extracts balance information for a specific address", + Long: `This command retrieves all balance information for a given address from almanac, including the amount delegated to validators, and rewards.`, + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := client.GetClientContextFromCmd(cmd) + + manifestFilePath := args[0] + address := args[1] + + // Call a function to extract address info + err := AlmanacAddressInfo(manifestFilePath, address, ctx) + if err != nil { + return err + } + return nil + }, + } + + networkMergeCmd.AddCommand(cmd) +} + func utilNetworkMergeCommand() *cobra.Command { cmd := &cobra.Command{ Use: "network-merge", @@ -127,6 +151,8 @@ func utilNetworkMergeCommand() *cobra.Command { AddCommandVerify(cmd) AddCommandExtractAddressInfo(cmd) + AddCommandManifestAddressInfo(cmd) + return cmd } @@ -414,3 +440,110 @@ func printAccInfo(genesisData *app.GenesisData, address string, ctx client.Conte return nil } + +type ManifestData struct { + InitialBalances *app.OrderedMap[string, app.UpgradeBalances] + MovedBalances *app.OrderedMap[string, app.UpgradeBalances] +} + +func parseManifestData(manifest *app.UpgradeManifest) (*ManifestData, error) { + manifestData := ManifestData{} + + manifestData.InitialBalances = app.NewOrderedMap[string, app.UpgradeBalances]() + for _, initialBalanceEntry := range manifest.InitialBalances { + manifestData.InitialBalances.SetNew(initialBalanceEntry.Address, initialBalanceEntry) + } + + manifestData.MovedBalances = app.NewOrderedMap[string, app.UpgradeBalances]() + for _, movedBalanceEntry := range manifest.MovedBalances { + manifestData.MovedBalances.SetNew(movedBalanceEntry.Address, movedBalanceEntry) + } + + return &manifestData, nil +} + +func printBalancesEntry(upgradeBalances app.UpgradeBalances, ctx client.Context) error { + if !upgradeBalances.BankBalance.IsZero() { + err := ctx.PrintString(fmt.Sprintf("Bank balance: %s\n", upgradeBalances.BankBalance)) + if err != nil { + return err + } + } + + if !upgradeBalances.VestedBalance.IsZero() { + err := ctx.PrintString(fmt.Sprintf("Vested balance: %s\n", upgradeBalances.VestedBalance)) + if err != nil { + return err + } + } + + if !upgradeBalances.BondedStakingBalance.IsZero() { + err := ctx.PrintString(fmt.Sprintf("Bonded staking balance: %s\n", upgradeBalances.BondedStakingBalance)) + if err != nil { + return err + } + } + + if !upgradeBalances.UnbondedStakingBalance.IsZero() { + err := ctx.PrintString(fmt.Sprintf("Unbonded staking balance: %s\n", upgradeBalances.UnbondedStakingBalance)) + if err != nil { + return err + } + } + + if !upgradeBalances.UnbondingStakingBalance.IsZero() { + err := ctx.PrintString(fmt.Sprintf("Unbonding staking balance: %s\n", upgradeBalances.UnbondingStakingBalance)) + if err != nil { + return err + } + } + + if !upgradeBalances.DistributionRewards.IsZero() { + err := ctx.PrintString(fmt.Sprintf("Distribution rewards: %s\n", upgradeBalances.DistributionRewards)) + if err != nil { + return err + } + } + + return nil +} + +func AlmanacAddressInfo(almanacFilePath string, address string, ctx client.Context) error { + manifest := app.NewUpgradeManifest() + + manifest, err := app.LoadManifestFromPath(almanacFilePath) + if err != nil { + return err + } + + manifestData, err := parseManifestData(manifest) + if err != nil { + return err + } + + err = ctx.PrintString("Initial balances:\n") + if err != nil { + return err + } + + if InitialBalances, exists := manifestData.InitialBalances.Get(address); exists { + err = printBalancesEntry(InitialBalances, ctx) + if err != nil { + return err + } + } + + err = ctx.PrintString("Moved balances:\n") + if err != nil { + return err + } + + if MovedBalances, exists := manifestData.MovedBalances.Get(address); exists { + err = printBalancesEntry(MovedBalances, ctx) + if err != nil { + return err + } + } + + return nil +}