-
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
Open
anhductn2001
wants to merge
5
commits into
main
Choose a base branch
from
ducnt/switchDA
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9339871
fix: compare wrong type
anhductn2001 72d9e6a
fix: Avail logic
anhductn2001 e4f5928
feat: switch DAs
anhductn2001 3317939
move gov and update config-file step out of switch clause
anhductn2001 3c28674
check if target da is same as previous
anhductn2001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package da | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "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" | ||
| datalayer "github.com/dymensionxyz/roller/data_layer" | ||
| "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 | ||
| } | ||
|
|
||
| if strings.ToLower(string(rollappConfig.DA.Backend)) == env { | ||
| pterm.Info.Println("You are switching to the same DA as previous!") | ||
| return | ||
| } | ||
|
|
||
| var dalayer datalayer.DataLayer | ||
| 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) | ||
|
|
||
| default: | ||
| pterm.Error.Println("switch does not support da: ", env) | ||
| return | ||
| } | ||
|
|
||
| submited, _ := pterm.DefaultInteractiveConfirm.WithDefaultText("Have you submitted to gov yet?").Show() | ||
| if !submited { | ||
| // 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(fmt.Sprintf(`"%s"`, env)) | ||
| 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) | ||
|
Comment on lines
+100
to
+120
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. over all good, but there are too many inputs for the user to provide, might need ux improvements in the future |
||
| } | ||
|
|
||
| daConfig := dalayer.GetSequencerDAConfig(consts.NodeType.Sequencer) | ||
| rollappConfig.DA.Backend = consts.DAType(env) | ||
|
|
||
| dymintConfigPath := sequtils.GetDymintFilePath(home) | ||
|
|
||
| pterm.Info.Println("updating dymint configuration") | ||
|
|
||
| _ = tomlconfig.UpdateFieldInFile( | ||
| dymintConfigPath, | ||
| "da_layer", | ||
| []string{string(env)}, | ||
| ) | ||
|
|
||
| _ = 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.") | ||
|
|
||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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