Skip to content

Commit

Permalink
[Testing] chore: add testkeyring pkg (#237)
Browse files Browse the repository at this point in the history
* chore: add testkeyring pkg

* chore: self-review improvements

* chore: review feedback improvements

* chore: review feedback improvements

Co-authored-by: Daniel Olshansky <[email protected]>

* wip

* chore: chore: review feedback improvements

Co-authored-by: Daniel Olshansky <[email protected]>

---------

Co-authored-by: Daniel Olshansky <[email protected]>
  • Loading branch information
bryanchriswhite and Olshansk authored Dec 5, 2023
1 parent 3b85dc4 commit 7f1c5af
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 6 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,14 @@ go_mockgen: ## Use `mockgen` to generate mocks used for testing purposes of all
go generate ./pkg/relayer/interface.go
go generate ./pkg/crypto/rings/interface.go

.PHONY: go_fixturegen
go_fixturegen: ## Generate fixture data for unit tests
.PHONY: go_testgen_fixtures
go_testgen_fixtures: ## Generate fixture data for unit tests
go generate ./pkg/relayer/miner/miner_test.go

.PHONY: go_testgen_accounts
go_testgen_accounts: ## Generate test accounts for usage in test environments
go generate ./testutil/testkeyring/keyring.go

.PHONY: go_develop
go_develop: proto_regen go_mockgen ## Generate protos and mocks

Expand Down
2 changes: 1 addition & 1 deletion pkg/relayer/miner/gen/gen_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func init() {
// random bytes.
// Output file is truncated and overwritten if it already exists.
//
// To regenerate all fixtures, use `make go_fixturegen`; to regenerate only this
// To regenerate all fixtures, use `make go_testgen_fixtures`; to regenerate only this
// test's fixtures run `go generate ./pkg/relayer/miner/miner_test.go`.
func main() {
flag.Parse()
Expand Down
2 changes: 1 addition & 1 deletion pkg/relayer/miner/gen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var (
`// DO NOT EDIT: this file was generated by gen/gen_fixtures.go,
// changes made will be overwritten upon regeneration.
//
// To regenerate all fixtures, use make go_fixturegen; to regenerate only this
// To regenerate all fixtures, use make go_testgen_fixture; to regenerate only this
// test's fixtures run go generate ./pkg/relayer/miner/miner_test.go.
package miner_test
Expand Down
2 changes: 1 addition & 1 deletion pkg/relayer/miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const testDifficulty = 16
// it pipes pre-mined relay fixtures. It asserts that the observable only emits
// mined relays with difficulty equal to or greater than testDifficulty.
//
// To regenerate all fixtures, use `make go_fixturegen`; to regenerate only this
// To regenerate all fixtures, use `make go_testgen_fixtures`; to regenerate only this
// test's fixtures run `go generate ./pkg/relayer/miner/miner_test.go`.
func TestMiner_MinedRelays(t *testing.T) {
var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/relayer/miner/relay_fixtures_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// DO NOT EDIT: this file was generated by gen/gen_fixtures.go,
// changes made will be overwritten upon regeneration.
//
// To regenerate all fixtures, use make go_fixturegen; to regenerate only this
// To regenerate all fixtures, use make go_testgen_fixture; to regenerate only this
// test's fixtures run go generate ./pkg/relayer/miner/miner_test.go.
package miner_test

Expand Down
118 changes: 118 additions & 0 deletions testutil/testkeyring/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package testkeyring

import (
"encoding/base64"
"encoding/json"
"sync/atomic"

sdktypes "github.com/cosmos/cosmos-sdk/types"
)

// PreGeneratedAccount holds the address and mnemonic of an account which was
// pre-generated by the `gen_accounts` package. It is intended to be used ONLY
// in tests. It is useful for scenarios where it is necessary to know the address
// of a specific key in a separate context from that of keyring and/or module
// genesis state construction.
type PreGeneratedAccount struct {
Address sdktypes.AccAddress `json:"address"`
Mnemonic string `json:"mnemonic"`
}

// PreGeneratedAccountIterator is an iterator over the pre-generated accounts.
// A new iterator populated with existing pre-generated accounts can be created
// with the PreGeneratedAccounts function. Alternatively, a new iterator can be
// created with the NewPreGeneratedAccountIterator function, to which a list of
// pre-generated accounts to be iterated over must be provided.
type PreGeneratedAccountIterator struct {
accounts []*PreGeneratedAccount
nextIndex uint32
}

// PreGeneratedAccountAtIndex returns the pre-generated account at the given index.
// It returns nil and false if the index is out of range.
func PreGeneratedAccountAtIndex(index uint32) (_ *PreGeneratedAccount, ok bool) {
if preGeneratedAccounts.nextIndex >= uint32(len(preGeneratedAccounts.accounts)) {
return nil, false
}

return preGeneratedAccounts.accounts[index], true
}

// MustPreGeneratedAccountAtIndex returns the pre-generated account at the given index.
// It panics on error; i.e. if the index is out of range.
func MustPreGeneratedAccountAtIndex(index uint32) *PreGeneratedAccount {
account, ok := PreGeneratedAccountAtIndex(index)
if !ok {
panic("index out of range of pre-generated accounts list")
}
return account
}

// PreGeneratedAccounts returns a new PreGeneratedAccountIterator with the
// accounts which were pre-generated by the `gen_accounts` package (i.e. accounts_table.go).
func PreGeneratedAccounts() *PreGeneratedAccountIterator {
return preGeneratedAccounts.Clone()
}

// NewPreGeneratedAccountIterator returns a new PreGeneratedAccountIterator with
// the given accounts. It is primarily used by the generated code to initially
// construct the preGeneratedAccounts variable.
func NewPreGeneratedAccountIterator(accounts ...*PreGeneratedAccount) *PreGeneratedAccountIterator {
return &PreGeneratedAccountIterator{
accounts: accounts,
nextIndex: 0,
}
}

// Next returns the next account in the iterator. It is safe to call
// concurrently and is guaranteed to return a unique account on each call.
// If the iterator index goes out of range, it returns nil and false.
func (iter *PreGeneratedAccountIterator) Next() (_ *PreGeneratedAccount, ok bool) {
// NB: instead of loading and incrementing in separate steps, just increment
// and use nextIndex-1 for the current index.
nextIndex := atomic.AddUint32(&iter.nextIndex, 1)
currentIndex := nextIndex - 1

if currentIndex > uint32(len(iter.accounts)) {
return nil, false
}

return iter.accounts[currentIndex], true
}

// Clone returns a new PreGeneratedAccountIterator with the same accounts as the
// receiver but with its nextIndex reset to 0.
func (iter *PreGeneratedAccountIterator) Clone() *PreGeneratedAccountIterator {
return NewPreGeneratedAccountIterator(iter.accounts...)
}

// Marshal returns the base64 and JSON encoded account string.
func (pga *PreGeneratedAccount) Marshal() (string, error) {
accountJson, err := json.Marshal(pga)
if err != nil {
return "", err
}

accountStr := base64.StdEncoding.EncodeToString(accountJson)
return accountStr, nil
}

// UnmarshalString parses the given base64 and JSON encoded account string into
// the PreGeneratedAccount receiver.
func (pga *PreGeneratedAccount) UnmarshalString(encodedAccountStr string) error {
accountJson, err := base64.StdEncoding.DecodeString(encodedAccountStr)
if err != nil {
return err
}
return json.Unmarshal(accountJson, pga)
}

// mustParsePreGeneratedAccount parses the given base64 and JSON encoded account
// string into a PreGeneratedAccount. It panics on error.
func mustParsePreGeneratedAccount(accountStr string) *PreGeneratedAccount {
account := new(PreGeneratedAccount)
if err := account.UnmarshalString(accountStr); err != nil {
panic(err)
}
return account
}
Loading

0 comments on commit 7f1c5af

Please sign in to comment.