From e68df1947be97804f230bc0cda1c36b1e6f4eae2 Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Mon, 18 Nov 2024 12:16:05 +0100 Subject: [PATCH] fix: create validator (#70) --- types/types.go | 88 ++++++++++++++++++++----------- utils/create-validator.go | 36 ++++++++++--- utils/download-and-run-version.go | 7 ++- utils/update-genesis.go | 20 ++++--- 4 files changed, 102 insertions(+), 49 deletions(-) diff --git a/types/types.go b/types/types.go index 7378de3..59ac86b 100644 --- a/types/types.go +++ b/types/types.go @@ -4,6 +4,7 @@ import ( "encoding/json" evidencetypes "cosmossdk.io/x/evidence/types" + feegranttypes "cosmossdk.io/x/feegrant" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" authz "github.com/cosmos/cosmos-sdk/x/authz" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -14,6 +15,7 @@ import ( slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" + feeibctypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types" transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types" @@ -53,9 +55,19 @@ type Genesis struct { type Consensus struct { // genutiltypes.ConsensusGenesis + // Validators []GenesisValidator `json:"validators"` Params *ConsensusParams `json:"params"` } +type GenesisValidator struct { + cometbfttypes.GenesisValidator + + Address string `json:"address"` + PubKey string `json:"pub_key"` + Power string `json:"power"` + Name string `json:"name"` +} + type ConsensusParams struct { cometbfttypes.ConsensusParams @@ -93,41 +105,53 @@ type ABCIParams struct { } type AppState struct { - Amm Amm `json:"amm"` - AssetProfile AssetProfile `json:"assetprofile"` - Auth Auth `json:"auth"` - AuthZ authz.GenesisState `json:"authz"` - Bank banktypes.GenesisState `json:"bank"` - Burner burnertypes.GenesisState `json:"burner"` - Capability Capability `json:"capability"` - Commitment Commitment `json:"commitment"` - Crisis crisistypes.GenesisState `json:"crisis"` - Distribution Distribution `json:"distribution"` - Epochs Epochs `json:"epochs"` - Estaking Estaking `json:"estaking"` - Evidence EvidenceState `json:"evidence"` - Genutil Genutil `json:"genutil"` - Gov Gov `json:"gov"` - Ibc Ibc `json:"ibc"` - LeverageLP LeverageLP `json:"leveragelp"` - Perpetual Perpetual `json:"perpetual"` - Masterchef Masterchef `json:"masterchef"` - Mint Mint `json:"mint"` - Oracle Oracle `json:"oracle"` - Parameter Parameter `json:"parameter"` - Params interface{} `json:"params"` - PoolAccounted PoolAccounted `json:"poolaccounted"` - Slashing Slashing `json:"slashing"` - StableStake StableStake `json:"stablestake"` - Staking Staking `json:"staking"` - Tier Tier `json:"tier"` - Tokenomics Tokenomics `json:"tokenomics"` - Transfer transfertypes.GenesisState `json:"transfer"` - TransferHook transferhooktypes.GenesisState `json:"transferhook"` - Upgrade struct{} `json:"upgrade"` + Amm Amm `json:"amm"` + AssetProfile AssetProfile `json:"assetprofile"` + Auth Auth `json:"auth"` + AuthZ authz.GenesisState `json:"authz"` + Bank banktypes.GenesisState `json:"bank"` + Burner burnertypes.GenesisState `json:"burner"` + Capability Capability `json:"capability"` + Commitment Commitment `json:"commitment"` + Crisis crisistypes.GenesisState `json:"crisis"` + Distribution Distribution `json:"distribution"` + Epochs Epochs `json:"epochs"` + Estaking Estaking `json:"estaking"` + Evidence EvidenceState `json:"evidence"` + Feegrant feegranttypes.GenesisState `json:"feegrant"` + Feeibc Feeibc `json:"feeibc"` + Genutil Genutil `json:"genutil"` + Gov Gov `json:"gov"` + Group interface{} `json:"group"` + Ibc Ibc `json:"ibc"` + Interchainaccounts interface{} `json:"interchainaccounts"` + LeverageLP LeverageLP `json:"leveragelp"` + Perpetual Perpetual `json:"perpetual"` + Masterchef Masterchef `json:"masterchef"` + Mint Mint `json:"mint"` + Oracle Oracle `json:"oracle"` + Parameter Parameter `json:"parameter"` + Params interface{} `json:"params"` + PoolAccounted PoolAccounted `json:"poolaccounted"` + Slashing Slashing `json:"slashing"` + StableStake StableStake `json:"stablestake"` + Staking Staking `json:"staking"` + Tier Tier `json:"tier"` + Tokenomics Tokenomics `json:"tokenomics"` + Transfer transfertypes.GenesisState `json:"transfer"` + TransferHook transferhooktypes.GenesisState `json:"transferhook"` + Upgrade interface{} `json:"upgrade"` + Vesting interface{} `json:"vesting"` // Include other fields as needed } +type Feeibc struct { + feeibctypes.GenesisState + + IdentifiedFees []interface{} `json:"identified_fees"` + ForwardRelayers []interface{} `json:"forward_relayers"` +} + type PoolAccounted struct { accountedpooltypes.GenesisState diff --git a/utils/create-validator.go b/utils/create-validator.go index 1250d7f..a5e4ac7 100644 --- a/utils/create-validator.go +++ b/utils/create-validator.go @@ -2,24 +2,46 @@ package utils import ( "log" + "os" "os/exec" "github.com/elys-network/post-upgrade-snapshot-generator/types" ) func CreateValidator(cmdPath, name, selfDelegation, moniker, pubkey, homePath, keyringBackend, chainId, node, broadcastMode string) { + // Create a temporary file + tmpFile, err := os.CreateTemp("", "validator.json-*") + if err != nil { + return + } + tmpFilePath := tmpFile.Name() + defer os.Remove(tmpFilePath) // Clean up + + // prepare the validator.json file + validatorJSON := `{ + "pubkey": ` + pubkey + `, + "amount": "` + selfDelegation + `uelys", + "moniker": "` + moniker + `", + "identity": "bob", + "website": "https://example.com", + "security": "secury@example.com", + "details": "details", + "commission-rate": "0.05", + "commission-max-rate": "0.50", + "commission-max-change-rate": "0.01", + "min-self-delegation": "1" + }` + if _, err := tmpFile.Write([]byte(validatorJSON)); err != nil { + log.Fatalf(types.ColorRed+"Failed to write to file: %v", err) + } + tmpFile.Close() + // Command and arguments args := []string{ "tx", "staking", "create-validator", - "--amount", selfDelegation + "uelys", - "--pubkey", pubkey, - "--moniker", moniker, - "--commission-rate", "0.05", - "--commission-max-rate", "0.50", - "--commission-max-change-rate", "0.01", - "--min-self-delegation", "1", + tmpFilePath, "--from", name, "--keyring-backend", keyringBackend, "--chain-id", chainId, diff --git a/utils/download-and-run-version.go b/utils/download-and-run-version.go index a3a7dbd..c748ab0 100644 --- a/utils/download-and-run-version.go +++ b/utils/download-and-run-version.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "os" "os/exec" @@ -24,7 +23,7 @@ func DownloadAndRunVersion(binaryPathOrURL string, skipDownload bool) (path stri // Check if the path exists if _, err = os.Stat(path); os.IsNotExist(err) { - err = errors.New(fmt.Sprintf("binary file does not exist at the specified path: %v", path)) + err = fmt.Errorf("binary file does not exist at the specified path: %v", path) return } @@ -55,7 +54,7 @@ func DownloadAndRunVersion(binaryPathOrURL string, skipDownload bool) (path stri // Check if the path exists if _, err = os.Stat(path); os.IsNotExist(err) { - err = errors.New(fmt.Sprintf("binary file does not exist at the specified path: %v", path)) + err = fmt.Errorf("binary file does not exist at the specified path: %v", path) } return @@ -69,7 +68,7 @@ func DownloadAndRunVersion(binaryPathOrURL string, skipDownload bool) (path stri defer resp.Body.Close() // Create a temporary file - tmpFile, err := ioutil.TempFile("", "binary-*") + tmpFile, err := os.CreateTemp("", "binary-*") if err != nil { return } diff --git a/utils/update-genesis.go b/utils/update-genesis.go index 79be131..46a856c 100644 --- a/utils/update-genesis.go +++ b/utils/update-genesis.go @@ -48,10 +48,18 @@ func UpdateGenesis(validatorBalance, homePath, genesisFilePath string) { genesis.AppState.Bank.Supply = genesis.AppState.Bank.Supply.Sub(coinsToRemove...) // add node 1 supply - genesis.AppState.Bank.Supply = genesis.AppState.Bank.Supply.Add(sdk.NewCoin("uelys", newValidatorBalance)).Add(sdk.NewCoin("ibc/2180E84E20F5679FCC760D8C165B60F42065DEF7F46A72B447CFF1B7DC6C0A65", newValidatorBalance)).Add(sdk.NewCoin("ibc/E2D2F6ADCC68AA3384B2F5DFACCA437923D137C14E86FB8A10207CF3BED0C8D4", newValidatorBalance)).Add(sdk.NewCoin("ibc/B4314D0E670CB43C88A5DCA09F76E5E812BD831CC2FEC6E434C9E5A9D1F57953", newValidatorBalance)) + genesis.AppState.Bank.Supply = genesis.AppState.Bank.Supply. + Add(sdk.NewCoin("uelys", newValidatorBalance)). + Add(sdk.NewCoin("ibc/2180E84E20F5679FCC760D8C165B60F42065DEF7F46A72B447CFF1B7DC6C0A65", newValidatorBalance)). + Add(sdk.NewCoin("ibc/E2D2F6ADCC68AA3384B2F5DFACCA437923D137C14E86FB8A10207CF3BED0C8D4", newValidatorBalance)). + Add(sdk.NewCoin("ibc/B4314D0E670CB43C88A5DCA09F76E5E812BD831CC2FEC6E434C9E5A9D1F57953", newValidatorBalance)) // add node 2 supply - genesis.AppState.Bank.Supply = genesis.AppState.Bank.Supply.Add(sdk.NewCoin("uelys", newValidatorBalance)).Add(sdk.NewCoin("ibc/2180E84E20F5679FCC760D8C165B60F42065DEF7F46A72B447CFF1B7DC6C0A65", newValidatorBalance)).Add(sdk.NewCoin("ibc/E2D2F6ADCC68AA3384B2F5DFACCA437923D137C14E86FB8A10207CF3BED0C8D4", newValidatorBalance)).Add(sdk.NewCoin("ibc/B4314D0E670CB43C88A5DCA09F76E5E812BD831CC2FEC6E434C9E5A9D1F57953", newValidatorBalance)) + genesis.AppState.Bank.Supply = genesis.AppState.Bank.Supply. + Add(sdk.NewCoin("uelys", newValidatorBalance)). + Add(sdk.NewCoin("ibc/2180E84E20F5679FCC760D8C165B60F42065DEF7F46A72B447CFF1B7DC6C0A65", newValidatorBalance)). + Add(sdk.NewCoin("ibc/E2D2F6ADCC68AA3384B2F5DFACCA437923D137C14E86FB8A10207CF3BED0C8D4", newValidatorBalance)). + Add(sdk.NewCoin("ibc/B4314D0E670CB43C88A5DCA09F76E5E812BD831CC2FEC6E434C9E5A9D1F57953", newValidatorBalance)) // Add new validator account and balance genesis.AppState.Auth.Accounts = append(genesis.AppState.Auth.Accounts, genesisInit.AppState.Auth.Accounts...) @@ -86,7 +94,10 @@ func UpdateGenesis(validatorBalance, homePath, genesisFilePath string) { genesis.AppState.Distribution = genesisInit.AppState.Distribution // temporary fix for distribution params - genesis.AppState.Distribution.FeePool.CommunityPool = sdk.NewDecCoins(sdk.NewDecCoin("ueden", math.NewInt(595021147500)), sdk.NewDecCoin("uedenb", math.NewInt(1983399876344))) + genesis.AppState.Distribution.FeePool.CommunityPool = sdk.NewDecCoins( + sdk.NewDecCoin("ueden", math.NewInt(595021147500)), + sdk.NewDecCoin("uedenb", math.NewInt(1983399876344)), + ) log.Printf("community pool: %v", genesis.AppState.Distribution.FeePool.CommunityPool) @@ -130,9 +141,6 @@ func UpdateGenesis(validatorBalance, homePath, genesisFilePath string) { // update masterchef genesis.AppState.Masterchef = genesisInit.AppState.Masterchef - // temporary fix - // genesis.InitialHeight = "0" - outputFilePath := homePath + "/config/genesis.json" if err := WriteGenesisFile(outputFilePath, genesis); err != nil { log.Fatalf(types.ColorRed+"Error writing genesis file: %v", err)