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

[Documentation]: How To Register Module Interfaces With BaseApp #18865

Closed
bonedaddy opened this issue Dec 21, 2023 · 4 comments
Closed

[Documentation]: How To Register Module Interfaces With BaseApp #18865

bonedaddy opened this issue Dec 21, 2023 · 4 comments
Assignees
Labels
T:Docs Changes and features related to documentation.

Comments

@bonedaddy
Copy link

Summary

Summary

I'm unable to find documentation that covers how to properly register a module's interface within baseapp such that all message types supported by a module have their message handlers available. Is there existing documentation that details this, or alternatively existing unit tests that detail the steps that need to be taken?

Overview

As part of #18402 I'm attempting to write unit tests covering the integration of x/circuit within baseapp, including authorizing authorities to trip circuits, and actual tripping of circuits. Currently when attempting to trip circuits I run into the following error which seems to indicate that baseapp is unaware of the handler for types.MsgTripCircuitBreaker.

baseapp_test.go:864: response tx_results:<code:6 log:"no message handler found for *types.MsgTripCircuitBreaker: unknown request" codespace:"sdk" > consensus_param_updates:<> app_hash:",Bd\235\314\275\036\276!ZR\252\343\304Q\177\337\267\201\375\377\274\261^\324\306B\260j\006\275U"

The test I have written is as follows

func TestBaseAppCircuitBreaker_TripCircuit(t *testing.T) {

	// prepare base app suite

	anteKey := []byte("ante-key")
	deliverKey := []byte("deliver-key")
	circuitAppModule := circuit.AppModuleBasic{}

	anteOpt := func(bapp *baseapp.BaseApp) {
		bapp.SetAnteHandler(anteHandlerTxTest(t, capKey1, anteKey))
	}
	storeService := runtime.NewKVStoreService(capKey1)

	suite := NewBaseAppSuite(t, anteOpt)
	baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), CounterServerImpl{t, capKey1, deliverKey})

	// prepare circuit module

	encCfg := moduletestutil.MakeTestEncodingConfig(circuitAppModule)
	ac := addresscodec.NewBech32Codec("cosmos")
	k := circuitkeeper.NewKeeper(encCfg.Codec, storeService, authtypes.NewModuleAddress("gov").String(), ac)
	circuitAppModule.RegisterInterfaces(suite.cdc.InterfaceRegistry())
	circuittypes.RegisterInterfaces(suite.cdc.InterfaceRegistry())
	suite.baseApp.SetCircuitBreaker(&k)

	// initialize baseapp
	_, err := suite.baseApp.InitChain(&abci.RequestInitChain{
		ConsensusParams: &cmtproto.ConsensusParams{},
	})
	require.NoError(t, err)

	// create message to trip circuit for /MsgUrl
	tx := newTxTripCircuit(t, suite.txConfig, authtypes.NewModuleAddress("gov").String(), "/MsgUrl")
	txBytes, err := suite.txConfig.TxEncoder()(tx)
	require.NoError(t, err)
	res, err := suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{
		Height: 1,
		Txs:    [][]byte{txBytes},
	})
	require.NoError(t, err)
	t.Logf("response %+v\n", res)
}

func newTxTripCircuit(t *testing.T, cfg client.TxConfig, authority string, url string) signing.Tx {
	t.Helper()
	msgs := make([]sdk.Msg, 0)
	msgs = append(msgs, &types.MsgTripCircuitBreaker{Authority: authority, MsgTypeUrls: []string{url}})

	builder := cfg.NewTxBuilder()
	err := builder.SetMsgs(msgs...)
	require.NoError(t, err)
	setTxSignature(t, builder, uint64(1))

	return builder.GetTx()
}
@bonedaddy bonedaddy added the T:Docs Changes and features related to documentation. label Dec 21, 2023
@facundomedica
Copy link
Member

I wasn't able to replicate, I cloned your branch (rangesecurity#1) and added the test you shared but it passed. Maybe the PR isn't up-to-date? Let me know

@bonedaddy
Copy link
Author

bonedaddy commented Jan 2, 2024

@facundomedica Apologies for the delayed response, and thank you for taking a look. I have another branch I'm working on to debug the error, additionally the no message handler message from baseapp doesn't appear to actually trigger a failure. I updated the unit test to cause a fatal error when the resposen from FinalizeBlock contains the message no message handler found, so it should now correctly display the message.

Reproduction steps:

$> git clone https://github.com/rangesecurity/cosmos-sdk
$> cd cosmos-sdk
$> git checkout debug/circuit-expiration
$> go test -v -run=TestBaseAppCircuitBreaker_TripCircuit ./...

@facundomedica
Copy link
Member

@bonedaddy you are missing:

circuittypes.RegisterMsgServer(suite.baseApp.MsgServiceRouter(), circuitkeeper.NewMsgServerImpl(k))

So circuit breaker was being set but you weren't registering the message handler for x/circuit related messages.

@bonedaddy
Copy link
Author

@facundomedica tyvm 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T:Docs Changes and features related to documentation.
Projects
None yet
Development

No branches or pull requests

2 participants