From 84fb92b01b782785b25912abe3aa51495b56a685 Mon Sep 17 00:00:00 2001 From: Steve <1848680+misko9@users.noreply.github.com> Date: Wed, 25 Sep 2024 12:16:27 -0600 Subject: [PATCH] feat(thorchain): v50 support, update bank's genesis supply when adding bond module balance (#1267) --- chain/thorchain/thornode.go | 48 +++++++++++++++++-- ...ochains.go => chainspec_exochains_test.go} | 0 ...orchain.go => chainspec_thorchain_test.go} | 2 +- .../thorchain/{helper.go => helper_test.go} | 0 .../thorchain/{setup.go => setup_test.go} | 0 5 files changed, 45 insertions(+), 5 deletions(-) rename examples/thorchain/{chainspec_exochains.go => chainspec_exochains_test.go} (100%) rename examples/thorchain/{chainspec_thorchain.go => chainspec_thorchain_test.go} (99%) rename examples/thorchain/{helper.go => helper_test.go} (100%) rename examples/thorchain/{setup.go => setup_test.go} (100%) diff --git a/chain/thorchain/thornode.go b/chain/thorchain/thornode.go index b7c8d9309..1e764e747 100644 --- a/chain/thorchain/thornode.go +++ b/chain/thorchain/thornode.go @@ -23,6 +23,7 @@ import ( rpchttp "github.com/cometbft/cometbft/rpc/client/http" coretypes "github.com/cometbft/cometbft/rpc/core/types" libclient "github.com/cometbft/cometbft/rpc/jsonrpc/client" + sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" @@ -357,7 +358,7 @@ func (tn *ChainNode) SetTestConfig(ctx context.Context) error { // Enable public REST API api["enable"] = true - api["swagger"] = false + api["swagger"] = true api["address"] = "tcp://0.0.0.0:1317" api["enabled-unsafe-cors"] = true @@ -721,13 +722,13 @@ EOF stdout, _, err := tn.Exec(ctx, command, tn.Chain.Config().Env) if err != nil { - return err + return fmt.Errorf("create key sdtout:\n%s\n, error: %w", string(stdout), err) } if tn.Validator && tn.ValidatorMnemonic == "" { var createKeyOutput map[string]string if err := json.Unmarshal(stdout, &createKeyOutput); err != nil { - return err + return fmt.Errorf("unmarshal create key sdtout:\n%s\n, error: %w", string(stdout), err) } tn.ValidatorMnemonic = createKeyOutput["mnemonic"] @@ -1071,7 +1072,7 @@ func (tn *ChainNode) AddBondModule(ctx context.Context) error { Coins: []CoinBalance{ { Denom: "rune", - Amount: fmt.Sprintf("%d0000000000000", tn.Chain.(*Thorchain).NumValidators), + Amount: fmt.Sprintf("%d00000000", tn.Chain.(*Thorchain).NumValidators), }, }, } @@ -1095,6 +1096,45 @@ func (tn *ChainNode) AddBondModule(ctx context.Context) error { } } + // Now update bank's supply with new balance if on v50 + if tn.IsAboveSDK47(ctx) { + bankSupplyPath := "app_state.bank.supply" + splitPath = strings.Split(bankSupplyPath, ".") + path = make([]interface{}, len(splitPath)) + for i, component := range splitPath { + if v, err := strconv.Atoi(component); err == nil { + path[i] = v + } else { + path[i] = component + } + } + supplyI, err := dyno.GetSlice(g, path...) + if err != nil { + return fmt.Errorf("failed to get key %s in genesis json", bankSupplyPath) + } + + for i, coinI := range supplyI { + coin := coinI.(map[string]interface{}) + if coin["denom"].(string) == "rune" { + originalAmount, ok := sdkmath.NewIntFromString(coin["amount"].(string)) + if !ok { + return fmt.Errorf("failed to convert bank's supply of rune to sdk Int from string") + } + addAmount, ok := sdkmath.NewIntFromString(bondBalance.Coins[0].Amount) + if !ok { + return fmt.Errorf("failed to convert bond amount of rune to sdk Int from string") + } + coin["amount"] = originalAmount.Add(addAmount).String() + supplyI[i] = coin + break + } + } + + if err := dyno.Set(g, supplyI, path...); err != nil { + return fmt.Errorf("failed to set bank's new supply") + } + } + genbz, err = json.Marshal(g) if err != nil { return fmt.Errorf("failed to marshal genesis bytes to json: %w", err) diff --git a/examples/thorchain/chainspec_exochains.go b/examples/thorchain/chainspec_exochains_test.go similarity index 100% rename from examples/thorchain/chainspec_exochains.go rename to examples/thorchain/chainspec_exochains_test.go diff --git a/examples/thorchain/chainspec_thorchain.go b/examples/thorchain/chainspec_thorchain_test.go similarity index 99% rename from examples/thorchain/chainspec_thorchain.go rename to examples/thorchain/chainspec_thorchain_test.go index de750d711..4398e269e 100644 --- a/examples/thorchain/chainspec_thorchain.go +++ b/examples/thorchain/chainspec_thorchain_test.go @@ -30,7 +30,7 @@ func ThorchainDefaultChainSpec(testName string, numVals int, numFn int, ethRoute name := common.THORChain.String() // Must use this name for test chainImage := ibc.NewDockerImage("thorchain", "local", "1025:1025") genesisKVMods := []thorchain.GenesisKV{ - thorchain.NewGenesisKV("app_state.bank.params.default_send_enabled", false), // disable bank module transfers + thorchain.NewGenesisKV("app_state.bank.params.default_send_enabled", true), // disable bank module transfers thorchain.NewGenesisKV("app_state.thorchain.reserve", "22000000000000000"), // mint to reserve for mocknet (220M) thorchain.NewGenesisKV("app_state.thorchain.chain_contracts", []ChainContract{ { diff --git a/examples/thorchain/helper.go b/examples/thorchain/helper_test.go similarity index 100% rename from examples/thorchain/helper.go rename to examples/thorchain/helper_test.go diff --git a/examples/thorchain/setup.go b/examples/thorchain/setup_test.go similarity index 100% rename from examples/thorchain/setup.go rename to examples/thorchain/setup_test.go