Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: servicer module implementation #19

Closed
wants to merge 9 commits into from
Closed

Conversation

bryanchriswhite
Copy link
Contributor

@bryanchriswhite bryanchriswhite commented Sep 14, 2023

This is a port of the servicer from #9 to a cosmos module.

@@ -0,0 +1,22 @@
syntax = "proto3";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this and import from the session module

@@ -0,0 +1,6 @@
package types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try import "github.com/cometbft/cometbft/rpc/core" instead and see if that works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a block type in that package but I do see one in github.com/cometbft/cometbft/types. 👍

return AppModule{
AppModuleBasic: NewAppModuleBasic(cdc),
keeper: keeper,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,

relayer: relayer,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a little while talking to ChatGPT about keepers vs modules and this was the ultimate conclusion:

Conclusion:
In summary, add to the keeper when dealing with business logic and state management and add to the module when dealing with user interfaces, message handling, and blockchain lifecycle events. It promotes a clean separation of concerns, where the module manages interactions and the keeper manages state.

I'm still working through this code but consider if this should be part of the keeper (i.e. for state management and state transitions) instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at one of the most complex modules in the cosmos-sdk (staking) and even they don't update the module but only the keeper:

https://github.com/cosmos/cosmos-sdk/blob/main/x/staking/module.go#L100
https://github.com/cosmos/cosmos-sdk/blob/main/x/staking/keeper/keeper.go#L29

}

func NewAppModule(
cdc codec.Codec,
keeper keeper.Keeper,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
// TODO_THIS_COMMIT: use an enum or something
clientCtx cosmosClient.Context,
factory tx.Factory,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we already have the clientCtx, can we just initialize txf via txf, err := tx.NewFactoryCLI(clientCtx, flags())?

@@ -146,3 +154,10 @@ func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}

// AddModuleInitFlags implements servertypes.ModuleInitFlags interface.
func AddModuleInitFlags(startCmd *cobra.Command) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is x/poktroll still a "special" module even after the changes?

Is there a way to move all this logic out into app.go?

}

if servicerEnabled || applicationEnabled || portalEnabled {
modules = append(modules, actorModule)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, I was thinking each service can act as multiple actors. I think it would also simplify the logic by initializing all of them (none of the if else statements) and it would just be a noop inside the actual module if the flagis off.

serverCtx := server.GetServerContextFromCmd(cmd)
// TODO_THIS_COMMIT: factor out keys to constants.
serverCtx.Viper.Set("actorMode", "servicer")
serverCtx.Viper.Set("clientCtx", initClientCtx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're doing here and seems like a good first approach (assuming we clean up the code), but I do want to try to avoid using viper for it.

An idea (I don' know if it works) is to potentially leverage app.go:

func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
	clientCtx := apiSvr.ClientCtx
	// Register new tx routes from grpc-gateway.
	authtx.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
	// Register new tendermint queries routes from grpc-gateway.
	tmservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
	// Register node gRPC service for grpc-gateway.
	nodeservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

	// Register grpc-gateway routes for all modules.
	ModuleBasics.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)

	// register app's OpenAPI routes.
	docs.RegisterOpenAPIService(Name, apiSvr.Router)
}

What if we add a customservicerService.RegisterRelayRoutes(clientCtx, cmdContext), and have it idiometically manage things under the hood?

This isn't a very well formed idea yet, so I'm still thinking through things...

relays utils.Observable[*types.Relay]
sessions utils.Observable[*types.Session]
cometClient cosmosClient.TendermintRPC
txConfig cosmosClient.TxConfig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really believe all of these should be part of the keeper (if we do keep them) and the minder is just an instance there as well.

return err
}

txBcastResult, err := m.cometClient.BroadcastTxCommit(context.TODO(), txBz)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does seem to be the right approach here: https://docs.cosmos.network/v0.50/core/transactions#cometbft-rpc

// Sign tx!!
if err := tx.Sign(
m.factory,
"SOME KEY NAME!!!!",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should come from the keyrig

@bryanchriswhite
Copy link
Contributor Author

Superseded by #24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants