Cosmos-sdk: Composable Initchainers & Beginblockers

Created on 9 Mar 2018  路  3Comments  路  Source: cosmos/cosmos-sdk

There are three types defined in types/abci.go. EndBlocker returns new validator set so have to be treated carefully, but other two types are the functions that return empty response(see types.proto). Because of it, it is likely that there will be a boilerplate code for registering InitChainers from multiple modules.

app.initChainer1 = x1.InitChainer(mapper1, mapper2)
app.initChainer2 = x2.InitChainer(mapper1, mapper2)

app.SetInitChainer(func (ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
    app.initChainer1(ctx, req)
    app.initChainer2(ctx, req)
    return abci.ResponseInitChain{}
})

To make this code more readable, ComposeInitChainers and ComposeBeginBlockers are proposed.

app.SetInitChainer(sdk.ComposeInitChainers(
    x1.InitChainer(mapper1, mapper2),
    x2.InitChainer(mapper1, mapper2),
))
proposal

Most helpful comment

I'm still a cosmos-sdk noob so my perspective is limited, but I'm in favor of APIs to make the code succinct like this, as long as it doesn't make it less readable.

Could it make sense to do this as a SetInitChainers method?

app.SetInitChainers(
    x1.InitChainer(mapper1, mapper2),
    x2.InitChainer(mapper1, mapper2),
)

All 3 comments

Although a cool idea in isolation not sure whether it fits nicely or not with the rest of the SDK - I'm thinking that I prefer consistency over compression of code here for the SetXxxer functions - what if there is some application where the operations within initChain do not explicitly fit the formula you've provided? -> it could be misleading to developer into thinking they are constrained by using sdk.ComposeInitChainers - Additionally there are lots of other SetXxxer functions in the SDK which the app needs to define which cannot be compressed into a "function that returns a Xxxer function" - and it's kind of weird to have some SetXxxer take a function like ComposeInitChainers where as others take the fully exposed function.... Just thinking out loud here

I'm still a cosmos-sdk noob so my perspective is limited, but I'm in favor of APIs to make the code succinct like this, as long as it doesn't make it less readable.

Could it make sense to do this as a SetInitChainers method?

app.SetInitChainers(
    x1.InitChainer(mapper1, mapper2),
    x2.InitChainer(mapper1, mapper2),
)

could be cool

Was this page helpful?
0 / 5 - 0 ratings