Skip to content
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

refactor(x/upgrade): create directory when dump disk #17238

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions x/upgrade/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package upgrade_test
import (
"context"
"errors"
"os"
"testing"
"time"

Expand Down Expand Up @@ -379,12 +378,6 @@ func TestDumpUpgradeInfoToFile(t *testing.T) {
t.Log("Verify upgrade height from file matches ")
require.Equal(upgradeInfo.Height, planHeight)
require.Equal(upgradeInfo.Name, plan.Name)

// clear the test file
upgradeInfoFilePath, err := s.keeper.GetUpgradeInfoPath()
require.Nil(err)
err = os.Remove(upgradeInfoFilePath)
require.Nil(err)
}

// TODO: add testcase to for `no upgrade handler is present for last applied upgrade`.
Expand Down
39 changes: 11 additions & 28 deletions x/upgrade/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strconv"
Expand Down Expand Up @@ -510,11 +509,6 @@ func (k Keeper) IsSkipHeight(height int64) bool {

// DumpUpgradeInfoToDisk writes upgrade information to UpgradeInfoFileName.
func (k Keeper) DumpUpgradeInfoToDisk(height int64, p types.Plan) error {
upgradeInfoFilePath, err := k.GetUpgradeInfoPath()
if err != nil {
return err
}

upgradeInfo := types.Plan{
Name: p.Name,
Height: height,
Expand All @@ -525,22 +519,18 @@ func (k Keeper) DumpUpgradeInfoToDisk(height int64, p types.Plan) error {
return err
}

return os.WriteFile(upgradeInfoFilePath, info, 0o600)
}

// GetUpgradeInfoPath returns the upgrade info file path
func (k Keeper) GetUpgradeInfoPath() (string, error) {
upgradeInfoFileDir := path.Join(k.getHomeDir(), "data")
if err := os.MkdirAll(upgradeInfoFileDir, os.ModePerm); err != nil {
return "", fmt.Errorf("could not create directory %q: %w", upgradeInfoFileDir, err)
upgradeInfoFilePath, upgradeInfoFileDir := k.GetUpgradeInfoPath()
if err = os.MkdirAll(upgradeInfoFileDir, os.ModePerm); err != nil {
return fmt.Errorf("could not create directory %q: %w", upgradeInfoFileDir, err)
}

return filepath.Join(upgradeInfoFileDir, types.UpgradeInfoFilename), nil
return os.WriteFile(upgradeInfoFilePath, info, 0o600)
}

// getHomeDir returns the height at which the given upgrade was executed
func (k Keeper) getHomeDir() string {
return k.homePath
// GetUpgradeInfoPath returns the upgrade info file path
func (k Keeper) GetUpgradeInfoPath() (string, string) {
upgradeInfoFileDir := filepath.Join(k.homePath, "data")
return filepath.Join(upgradeInfoFileDir, types.UpgradeInfoFilename), upgradeInfoFileDir
}

// ReadUpgradeInfoFromDisk returns the name and height of the upgrade which is
Expand All @@ -550,11 +540,7 @@ func (k Keeper) getHomeDir() string {
func (k Keeper) ReadUpgradeInfoFromDisk() (types.Plan, error) {
var upgradeInfo types.Plan

upgradeInfoPath, err := k.GetUpgradeInfoPath()
if err != nil {
return upgradeInfo, err
}

upgradeInfoPath, _ := k.GetUpgradeInfoPath()
data, err := os.ReadFile(upgradeInfoPath)
if err != nil {
// if file does not exist, assume there are no upgrades
Expand All @@ -565,11 +551,8 @@ func (k Keeper) ReadUpgradeInfoFromDisk() (types.Plan, error) {
return upgradeInfo, err
}

if err := json.Unmarshal(data, &upgradeInfo); err != nil {
return upgradeInfo, err
}

return upgradeInfo, nil
err = json.Unmarshal(data, &upgradeInfo)
return upgradeInfo, err
}

// SetDowngradeVerified updates downgradeVerified.
Expand Down