Skip to content

Commit

Permalink
chore: add GetGenesis test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 21, 2023
1 parent a8a9cf9 commit 11c019d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions testutil/network/genesis.go
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)
}

0 comments on commit 11c019d

Please sign in to comment.