Skip to content

Commit

Permalink
fix: create validator (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond authored Nov 18, 2024
1 parent ed2e531 commit e68df19
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 49 deletions.
88 changes: 56 additions & 32 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
36 changes: 29 additions & 7 deletions utils/create-validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions utils/download-and-run-version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
20 changes: 14 additions & 6 deletions utils/update-genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e68df19

Please sign in to comment.