Skip to content

Commit

Permalink
SetupConfig function for Unit Tests (#519)
Browse files Browse the repository at this point in the history
Closes: #XXX

## Context and purpose of the change
When working with addresses in unit tests, we need to update the sdk config to have a stride address prefix. The function to set these config settings was already in our test utils, but it was limited to our keeper test framework. This PR makes the setup function public so it can be leveraged by non-keeper tests.

## Brief Changelog
* Moved config settings out of `init` and into a `SetupConfig` function
* Added config function to apptesting so it's easily importable
* Added function to generate test addresses

## Usage
```go
import "github.com/Stride-Labs/stride/v4/app/apptesting"

apptesting.SetupConfig()
validAddr, invalidAddr := apptesting.GenerateTestAddrs()
```

## Author's Checklist

I have...

- [ ] Run and PASSED locally all GAIA integration tests
- [ ] If the change is contentful, I either:
    - [ ] Added a new unit test OR 
    - [ ] Added test cases to existing unit tests
- [ ] OR this change is a trivial rework / code cleanup without any test coverage

If skipped any of the tests above, explain.


## Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] manually tested (if applicable)
- [ ] confirmed the author wrote unit tests for new logic
- [ ] reviewed documentation exists and is accurate


## Documentation and Release Note

  - [ ] Does this pull request introduce a new feature or user-facing behavior changes? 
  - [ ] Is a relevant changelog entry added to the `Unreleased` section in `CHANGELOG.md`?
  - [ ] This pull request updates existing proto field values (and require a backend and frontend migration)? 
  - [ ] Does this pull request change existing proto field names (and require a frontend migration)?
  How is the feature or change documented? 
      - [ ] not applicable
      - [ ] jira ticket `XXX` 
      - [ ] specification (`x/<module>/spec/`) 
      - [ ] README.md 
      - [ ] not documented
  • Loading branch information
sampocs authored Dec 19, 2022
1 parent d1938b9 commit 4996b7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/apptesting/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,16 @@ func (s *AppTestHelper) ConfirmUpgradeSucceededs(upgradeName string, upgradeHeig
s.App.BeginBlocker(contextAtUpgrade, beginBlockRequest)
})
}

// Generates a valid and invalid test address (used for non-keeper tests)
func GenerateTestAddrs() (string, string) {
pk1 := ed25519.GenPrivKey().PubKey()
validAddr := sdk.AccAddress(pk1.Address()).String()
invalidAddr := sdk.AccAddress("invalid").String()
return validAddr, invalidAddr
}

// Modifies sdk config to have stride address prefixes (used for non-keeper tests)
func SetupConfig() {
app.SetupConfig()
}
7 changes: 7 additions & 0 deletions app/test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

cmdcfg "github.com/Stride-Labs/stride/v4/cmd/strided/config"
)

const Bech32Prefix = "stride"

func init() {
SetupConfig()
}

func SetupConfig() {
config := sdk.GetConfig()
valoper := sdk.PrefixValidator + sdk.PrefixOperator
valoperpub := sdk.PrefixValidator + sdk.PrefixOperator + sdk.PrefixPublic
config.SetBech32PrefixForAccount(Bech32Prefix, Bech32Prefix+sdk.PrefixPublic)
config.SetBech32PrefixForValidator(Bech32Prefix+valoper, Bech32Prefix+valoperpub)
cmdcfg.SetAddressPrefixes(config)
}

// Initializes a new StrideApp without IBC functionality
Expand Down

0 comments on commit 4996b7b

Please sign in to comment.