-
Notifications
You must be signed in to change notification settings - Fork 77
feat: support switch DA to Avail #1321
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
9339871
72d9e6a
e4f5928
3317939
3c28674
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package da | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/x/params/client/utils" | ||
| "github.com/pterm/pterm" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| initconfig "github.com/dymensionxyz/roller/cmd/config/init" | ||
| "github.com/dymensionxyz/roller/cmd/consts" | ||
| "github.com/dymensionxyz/roller/data_layer/avail" | ||
| "github.com/dymensionxyz/roller/utils/config/tomlconfig" | ||
| "github.com/dymensionxyz/roller/utils/filesystem" | ||
| "github.com/dymensionxyz/roller/utils/genesis" | ||
| "github.com/dymensionxyz/roller/utils/gov" | ||
| "github.com/dymensionxyz/roller/utils/rollapp" | ||
| "github.com/dymensionxyz/roller/utils/roller" | ||
| sequtils "github.com/dymensionxyz/roller/utils/sequencer" | ||
| ) | ||
|
|
||
| func Cmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "switch-da", | ||
| Short: "Switch to another DA.", | ||
| Long: ``, | ||
| Args: cobra.MaximumNArgs(0), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| envs := []string{"avail"} // TODO: support more DAs | ||
| env, _ := pterm.DefaultInteractiveSelect. | ||
| WithDefaultText("select the DA you want to switch"). | ||
| WithOptions(envs). | ||
| Show() | ||
|
|
||
| err := initconfig.AddFlags(cmd) | ||
| if err != nil { | ||
| pterm.Error.Println("failed to switch DA: ", err) | ||
| return | ||
| } | ||
|
|
||
| home, err := filesystem.ExpandHomePath( | ||
| cmd.Flag(initconfig.GlobalFlagNames.Home).Value.String(), | ||
| ) | ||
| if err != nil { | ||
| pterm.Error.Println("failed to switch DA: ", err) | ||
| return | ||
| } | ||
|
|
||
| rollappConfig, err := roller.LoadConfig(home) | ||
| if err != nil { | ||
| pterm.Error.Println("failed to load roller config: ", err) | ||
| return | ||
| } | ||
|
|
||
| switch env { | ||
anhductn2001 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case "avail": | ||
| raResp, err := rollapp.GetMetadataFromChain(rollappConfig.RollappID, rollappConfig.HubData) | ||
| if err != nil { | ||
| pterm.Error.Println("failed to get metadata from chain: ", err) | ||
| return | ||
| } | ||
|
|
||
| drsVersion, err := genesis.GetDrsVersionFromGenesis(home, raResp) | ||
| if err != nil { | ||
| pterm.Error.Println("failed to get drs version from genesis: ", err) | ||
| return | ||
| } | ||
|
|
||
| drsVersionInt, err := strconv.ParseInt(drsVersion, 10, 64) | ||
| if err != nil { | ||
| pterm.Error.Println("failed to get drs version from genesis: ", err) | ||
| return | ||
| } | ||
|
|
||
| if !rollapp.IsDaConfigNewFormat(drsVersionInt, strings.ToLower(raResp.Rollapp.VmType)) { | ||
| pterm.Error.Println("required Rollapp DRS version of at least", drsVersionInt) | ||
| return | ||
| } | ||
|
|
||
| dalayer := avail.NewAvail(home) | ||
|
|
||
| submited, _ := pterm.DefaultInteractiveConfirm.WithDefaultText("Have you submitted to gov yet?").Show() | ||
| if !submited { | ||
anhductn2001 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Create Gov | ||
| keyName, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("Enter your key name").Show() | ||
| keyring, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("Enter your keyring-backend").Show() | ||
| title, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("Enter your Proposal Title").Show() | ||
| description, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("Enter your Proposal Description").Show() | ||
| deposit, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("Enter your Proposal Deposit").Show() | ||
| newDAParam := json.RawMessage(`"avail"`) | ||
| txHash, err := gov.ParamChangeProposal(home, keyName, keyring, | ||
| &utils.ParamChangeProposalJSON{ | ||
| Title: title, | ||
| Description: description, | ||
| Changes: utils.ParamChangesJSON{ | ||
| utils.NewParamChangeJSON("rollappparams", "da", newDAParam), | ||
| }, | ||
| Deposit: deposit + rollappConfig.Denom, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| pterm.Error.Println("failed to submit proposal", err) | ||
| return | ||
| } | ||
| pterm.Info.Println("Proposal Tx hash: ", txHash) | ||
| } | ||
|
|
||
| daConfig := dalayer.GetSequencerDAConfig(consts.NodeType.Sequencer) | ||
|
||
| rollappConfig.DA.Backend = "avail" | ||
|
|
||
| dymintConfigPath := sequtils.GetDymintFilePath(home) | ||
|
|
||
| pterm.Info.Println("updating dymint configuration") | ||
|
|
||
| _ = tomlconfig.UpdateFieldInFile( | ||
|
||
| dymintConfigPath, | ||
| "da_layer", | ||
| []string{string("avail")}, | ||
| ) | ||
|
|
||
| _ = tomlconfig.UpdateFieldInFile( | ||
| dymintConfigPath, | ||
| "da_config", | ||
| daConfig, | ||
| ) | ||
|
|
||
| if err := roller.WriteConfig(rollappConfig); err != nil { | ||
| pterm.Error.Println("failed to write roller config", err) | ||
| return | ||
| } | ||
|
|
||
| pterm.Info.Println("the config update process is complete! Now you need to restart the nodes before the proposal passes.") | ||
|
|
||
| default: | ||
| pterm.Error.Println("switch does not support da: ", env) | ||
| return | ||
| } | ||
|
|
||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package gov | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
|
|
||
| paramsutils "github.com/cosmos/cosmos-sdk/x/params/client/utils" | ||
| "github.com/dymensionxyz/roller/cmd/consts" | ||
| bashutils "github.com/dymensionxyz/roller/utils/bash" | ||
| "github.com/pterm/pterm" | ||
| ) | ||
|
|
||
| type CosmosTx struct { | ||
| TxHash string `json:"txhash"` | ||
| Code int `json:"code"` | ||
| RawLog string `json:"raw_log"` | ||
| } | ||
|
|
||
| // ParamChangeProposal submits a param change proposal to the chain, signed by keyName. | ||
| func ParamChangeProposal(home, keyName, keyring string, prop *paramsutils.ParamChangeProposalJSON) (string, error) { | ||
| content, err := json.Marshal(prop) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| hash := sha256.Sum256(content) | ||
| proposalFilename := fmt.Sprintf("%x.json", hash) | ||
| proposalPath := filepath.Join(home, proposalFilename) | ||
|
|
||
| err = os.WriteFile(proposalPath, content, 0o644) | ||
| if err != nil { | ||
| pterm.Error.Printf("Error writing prop file: %v\n", err) | ||
| return "", err | ||
| } | ||
|
|
||
| cmd := exec.Command( | ||
| consts.Executables.RollappEVM, | ||
| "tx", "gov", "submit-legacy-proposal", | ||
| "param-change", | ||
| proposalPath, | ||
| "--gas", "auto", | ||
| "--from", keyName, | ||
| "--keyring-backend", keyring, | ||
| "--output", "json", | ||
| "-y", | ||
| ) | ||
|
|
||
| out, err := bashutils.ExecCommandWithStdout(cmd) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| output := CosmosTx{} | ||
| err = json.Unmarshal(out.Bytes(), &output) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if output.Code != 0 { | ||
| return output.TxHash, fmt.Errorf("transaction failed with code %d: %s", output.Code, output.RawLog) | ||
| } | ||
|
|
||
| return output.TxHash, nil | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
envis not descriptive