Loading stateStore data in module constructor

Hey folks,

in my module’s constructor I want to load configuration data for the module. The idea is that this data is being fetched from the blockchain itself, hence the approach to load it from stateStore. In case, there is no data in the stateStore yet (no transaction processed yet which would have set stateStore data), some default configuration shall be used.

export const getChainState = async (
  stateStore: StateStore
): Promise<ObjInterface | undefined> => {
  const result = await stateStore.chain.get("CHAIN_KEY_PREFIX:CHAIN_KEY");

  if (!result) {
    return undefined;

  return codec.decode<ObjInterface>(ObjSchema, result);

In the documentation it says about stateStore:

Inside of a module, the stateStore is available for reducers and all lifecycle hooks.

… so the stateStore is actually not available in the constructor of the module …

So what should I do?

I would use this part to return it:

 if (!result) {
    return configuration/default state;

An other way if you really want to add it to the stateStore as soon as possible, it could be done in the beforeBlockApply().

To invoke getChainState at all, one needs to pass the stateStore as an argument, that’s why I can’t invoke in the constructor. Your recommendation to return the default config in the if-block is a welcomed addition, but it does not solve my issue.

I had the beforeBlockApply() method in mind, but this seems like a bad practice, because whichever code block I am adding is executed on every block forever. The same would be for a routine implemented on every reducer … it would work but seems wrong.

I wonder what the reasoning was / is to let a module access stateStore in reducers and lifecycle hooks but not its constructor.

Can you explain your case a little bit ?

Loading module configuration from the chain itself is a weird thing to do.
Modules are on-chain logics, so the only behaviours supposed to be affected by on-chain configuration are block lifecycles, reducers and assets (aka custom transactions).
All those things have access to the stateStore.
If you want to configure something else from the chain itself, it’s most likely wrong.