-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f74262
commit f03177e
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,30 @@ | ||
package network | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/cosmos/gogoproto/proto" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// GetGenesisState retrieves the genesis state for a given module from the | ||
// underlying cosmos-sdk testutil network. | ||
func GetGenesisState[T proto.Message](t *testing.T, moduleName string, memnet InMemoryCosmosNetwork) T { | ||
t.Helper() | ||
|
||
require.NotEmptyf(t, memnet.GetNetwork(t), "in-memory network not started yet, call inMemoryNetworkWithSessions#Start() first") | ||
|
||
var genesisState T | ||
// NB: As this function is generic, it MUST use reflect in order to unmarshal | ||
// the genesis state as the codec requries a reference to a concrete type pointer. | ||
genesisStateType := reflect.TypeOf(genesisState) | ||
genesisStateValue := reflect.New(genesisStateType.Elem()) | ||
genesisStatePtr := genesisStateValue.Interface().(proto.Message) | ||
|
||
genesisStateJSON := memnet.GetNetworkConfig(t).GenesisState[moduleName] | ||
err := memnet.GetNetworkConfig(t).Codec.UnmarshalJSON(genesisStateJSON, genesisStatePtr) | ||
require.NoError(t, err) | ||
|
||
return genesisStatePtr.(T) | ||
} |