Skip to content

Commit

Permalink
add stateDir property to fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Apr 29, 2024
1 parent 41e4b31 commit 72438ea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
34 changes: 20 additions & 14 deletions app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ func SetupHandlers(app *App) {
// add each store upgrade to this slice with a monotonically increasing index.
// you must use a index that is greater than any migration that has ever been run.
allStoreUpgrades := upgradeTracker{
{
idx: 1000,
upgrade: storetypes.StoreUpgrades{
Added: []string{authoritytypes.ModuleName},
upgrades: []upgradeTrackerItem{

Check warning on line 37 in app/setup_handlers.go

View check run for this annotation

Codecov / codecov/patch

app/setup_handlers.go#L36-L37

Added lines #L36 - L37 were not covered by tests
{
idx: 1000,
upgrade: storetypes.StoreUpgrades{
Added: []string{authoritytypes.ModuleName},
},
},

Check warning on line 43 in app/setup_handlers.go

View check run for this annotation

Codecov / codecov/patch

app/setup_handlers.go#L39-L43

Added lines #L39 - L43 were not covered by tests
},
{
idx: 2000,
upgrade: storetypes.StoreUpgrades{
Added: []string{lightclienttypes.ModuleName},
{
idx: 2000,
upgrade: storetypes.StoreUpgrades{
Added: []string{lightclienttypes.ModuleName},
},
},
},
stateFileDir: DefaultNodeHome,

Check warning on line 51 in app/setup_handlers.go

View check run for this annotation

Codecov / codecov/patch

app/setup_handlers.go#L45-L51

Added lines #L45 - L51 were not covered by tests
}

// if this is a develop release, use the special develop store loader functionality
Expand Down Expand Up @@ -82,11 +85,14 @@ type upgradeTrackerItem struct {
upgrade storetypes.StoreUpgrades
}

type upgradeTracker []upgradeTrackerItem
type upgradeTracker struct {
upgrades []upgradeTrackerItem
stateFileDir string
}

func (t upgradeTracker) getDevelopStoreUpgrades() *storetypes.StoreUpgrades {
neededUpgrades := &storetypes.StoreUpgrades{}
stateFilePath := path.Join(DefaultNodeHome, "storeupgradetracker")
stateFilePath := path.Join(t.stateFileDir, "storeupgradetracker")

currentIdx := int64(0)
if stateFileContents, err := os.ReadFile(stateFilePath); err == nil {
Expand All @@ -99,7 +105,7 @@ func (t upgradeTracker) getDevelopStoreUpgrades() *storetypes.StoreUpgrades {
}

maxIdx := currentIdx
for _, item := range t {
for _, item := range t.upgrades {
idx := item.idx
upgrade := item.upgrade
if idx <= currentIdx {
Expand All @@ -110,7 +116,7 @@ func (t upgradeTracker) getDevelopStoreUpgrades() *storetypes.StoreUpgrades {
neededUpgrades.Renamed = append(neededUpgrades.Renamed, upgrade.Renamed...)
maxIdx = idx
}
err := os.WriteFile(stateFilePath, []byte(strconv.FormatInt(maxIdx, 10)), os.ModePerm)
err := os.WriteFile(stateFilePath, []byte(strconv.FormatInt(maxIdx, 10)), 0o660)

Check failure on line 119 in app/setup_handlers.go

View workflow job for this annotation

GitHub Actions / lint

G306: Expect WriteFile permissions to be 0600 or less (gosec)
if err != nil {
panic(fmt.Sprintf("unable to write upgrade state file: %v", err))

Check warning on line 121 in app/setup_handlers.go

View check run for this annotation

Codecov / codecov/patch

app/setup_handlers.go#L121

Added line #L121 was not covered by tests
}
Expand All @@ -131,7 +137,7 @@ func (t upgradeTracker) getDevelopStoreLoader() baseapp.StoreLoader {

func (t upgradeTracker) mergeAllUpgrades() *storetypes.StoreUpgrades {
upgrades := &storetypes.StoreUpgrades{}
for _, item := range t {
for _, item := range t.upgrades {
upgrade := item.upgrade
upgrades.Added = append(upgrades.Added, upgrade.Added...)
upgrades.Deleted = append(upgrades.Deleted, upgrade.Deleted...)
Expand Down
27 changes: 17 additions & 10 deletions app/setup_handlers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"os"
"testing"
"time"

Expand All @@ -16,19 +17,25 @@ func TestStoreUpgradeTracker(t *testing.T) {
// use timestamp for tracker idx so migrations work regardless of system state
now := time.Now().UnixMicro()

tmpdir, err := os.MkdirTemp("", "storeupgradetracker-*")
r.NoError(err)

allStoreUpgrades := upgradeTracker{
{
idx: now,
upgrade: storetypes.StoreUpgrades{
Added: []string{authoritytypes.ModuleName},
upgrades: []upgradeTrackerItem{
{
idx: 1000,
upgrade: storetypes.StoreUpgrades{
Added: []string{authoritytypes.ModuleName},
},
},
},
{
idx: now + 1,
upgrade: storetypes.StoreUpgrades{
Added: []string{lightclienttypes.ModuleName},
{
idx: 2000,
upgrade: storetypes.StoreUpgrades{
Added: []string{lightclienttypes.ModuleName},
},
},
},
stateFileDir: tmpdir,
}

r.Len(allStoreUpgrades.mergeAllUpgrades().Added, 2)
Expand All @@ -49,7 +56,7 @@ func TestStoreUpgradeTracker(t *testing.T) {

// now add a upgrade and ensure that it gets run without running
// the other upgrades
allStoreUpgrades = append(allStoreUpgrades, upgradeTrackerItem{
allStoreUpgrades.upgrades = append(allStoreUpgrades.upgrades, upgradeTrackerItem{
idx: now + 3,
upgrade: storetypes.StoreUpgrades{
Deleted: []string{"example"},
Expand Down

0 comments on commit 72438ea

Please sign in to comment.