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(nexus)!: expose IsChainRegistered query to wasm #2182

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ type QueryRequest struct {
}

// NewQueryPlugins returns a new instance of the custom query plugins
func NewQueryPlugins(msgIDGenerator nexustypes.MsgIDGenerator) *wasmkeeper.QueryPlugins {
nexusWasmQuerier := nexusKeeper.NewWasmQuerier(msgIDGenerator)
func NewQueryPlugins(nexus nexustypes.Nexus) *wasmkeeper.QueryPlugins {
nexusWasmQuerier := nexusKeeper.NewWasmQuerier(nexus)

return &wasmkeeper.QueryPlugins{
Custom: func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
Expand Down
35 changes: 24 additions & 11 deletions app/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/axelarnetwork/axelar-core/cmd/axelard/cmd"
"github.com/axelarnetwork/axelar-core/testutils/fake"
"github.com/axelarnetwork/axelar-core/testutils/rand"
nexus "github.com/axelarnetwork/axelar-core/x/nexus/exported"
nexusmock "github.com/axelarnetwork/axelar-core/x/nexus/types/mock"
"github.com/axelarnetwork/utils/funcs"
. "github.com/axelarnetwork/utils/test"
Expand Down Expand Up @@ -334,21 +335,21 @@ func TestMaxSizeOverrideForClient(t *testing.T) {

func TestQueryPlugins(t *testing.T) {
var (
msgIDGenerator *nexusmock.MsgIDGeneratorMock
req json.RawMessage
ctx sdk.Context
nexusK *nexusmock.NexusMock
req json.RawMessage
ctx sdk.Context
)

Given("the tx id generator", func() {
Given("the nexus keeper", func() {
ctx = sdk.NewContext(nil, tmproto.Header{}, false, log.TestingLogger())
msgIDGenerator = &nexusmock.MsgIDGeneratorMock{}
nexusK = &nexusmock.NexusMock{}
}).
Branch(
When("request is invalid", func() {
req = []byte("{\"invalid\"}")
}).
Then("it should return an error", func(t *testing.T) {
_, err := app.NewQueryPlugins(msgIDGenerator).Custom(ctx, req)
_, err := app.NewQueryPlugins(nexusK).Custom(ctx, req)

assert.ErrorContains(t, err, "invalid Custom query request")
}),
Expand All @@ -357,7 +358,7 @@ func TestQueryPlugins(t *testing.T) {
req = []byte("{\"unknown\":{}}")
}).
Then("it should return an error", func(t *testing.T) {
_, err := app.NewQueryPlugins(msgIDGenerator).Custom(ctx, req)
_, err := app.NewQueryPlugins(nexusK).Custom(ctx, req)

assert.ErrorContains(t, err, "unknown Custom query request")
}),
Expand All @@ -366,26 +367,38 @@ func TestQueryPlugins(t *testing.T) {
req = []byte("{\"nexus\":{}}")
}).
Then("it should return an error", func(t *testing.T) {
_, err := app.NewQueryPlugins(msgIDGenerator).Custom(ctx, req)
_, err := app.NewQueryPlugins(nexusK).Custom(ctx, req)

assert.ErrorContains(t, err, "unknown Nexus query request")
}),

When("request is a nexus wasm TxID query", func() {
req = []byte("{\"nexus\":{\"tx_hash_and_nonce\":{}}}")
}).
Then("it should return an error", func(t *testing.T) {
Then("it should return a TxHashAndNonce response", func(t *testing.T) {
txHash := [32]byte(rand.Bytes(32))
index := uint64(rand.PosI64())
msgIDGenerator.CurrIDFunc = func(ctx sdk.Context) ([32]byte, uint64) {
nexusK.CurrIDFunc = func(ctx sdk.Context) ([32]byte, uint64) {
return txHash, index
}

actual, err := app.NewQueryPlugins(msgIDGenerator).Custom(ctx, req)
actual, err := app.NewQueryPlugins(nexusK).Custom(ctx, req)

assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf("{\"tx_hash\":%s,\"nonce\":%d}", funcs.Must(json.Marshal(txHash)), index), string(actual))
}),
When("request is a nexus wasm IsChainRegistered query", func() {
req = []byte("{\"nexus\":{\"is_chain_registered\":{\"chain\": \"chain-0\"}}}")
}).
Then("it should return a IsChainRegisteredResponse", func(t *testing.T) {
nexusK.GetChainFunc = func(ctx sdk.Context, chain nexus.ChainName) (nexus.Chain, bool) {
return nexus.Chain{}, true
}
actual, err := app.NewQueryPlugins(nexusK).Custom(ctx, req)

assert.NoError(t, err)
assert.Equal(t, "{\"is_registered\":true}", string(actual))
}),
).
Run(t)

Expand Down
11 changes: 10 additions & 1 deletion x/nexus/exported/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,20 @@ func (bz *WasmBytes) UnmarshalJSON(data []byte) error {

// WasmQueryRequest is the request for wasm contracts to query
type WasmQueryRequest struct {
TxHashAndNonce *struct{} `json:"tx_hash_and_nonce,omitempty"`
TxHashAndNonce *struct{} `json:"tx_hash_and_nonce,omitempty"`
IsChainRegistered *IsChainRegisteredRequest `json:"is_chain_registered,omitempty"`
}

// WasmQueryTxHashAndNonceResponse is the response for the TxHashAndNonce query
type WasmQueryTxHashAndNonceResponse struct {
TxHash [32]byte `json:"tx_hash,omitempty"` // the hash of the current transaction
Nonce uint64 `json:"nonce,omitempty"` // the nonce of the current execution, which increments with each entry of any wasm execution
}

type IsChainRegisteredRequest struct {
Chain string `json:"chain"`
}

type WasmQueryIsChainRegisteredResponse struct {
IsRegistered bool `json:"is_registered"`
}
26 changes: 19 additions & 7 deletions x/nexus/keeper/wasm_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,36 @@

// WasmQuerier is a querier for the wasm contracts
type WasmQuerier struct {
msgIDGenerator types.MsgIDGenerator
nexus types.Nexus
}

// NewWasmQuerier creates a new WasmQuerier
func NewWasmQuerier(msgIDGenerator types.MsgIDGenerator) *WasmQuerier {
return &WasmQuerier{msgIDGenerator}
func NewWasmQuerier(nexus types.Nexus) *WasmQuerier {
return &WasmQuerier{nexus}

Check warning on line 21 in x/nexus/keeper/wasm_querier.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/keeper/wasm_querier.go#L20-L21

Added lines #L20 - L21 were not covered by tests
}

// Query handles the wasm queries for the nexus module
func (q WasmQuerier) Query(ctx sdk.Context, req exported.WasmQueryRequest) ([]byte, error) {
if req.TxHashAndNonce != nil {
txHash, nonce := q.msgIDGenerator.CurrID(ctx)
switch {
case req.TxHashAndNonce != nil:
txHash, nonce := q.nexus.CurrID(ctx)

Check warning on line 28 in x/nexus/keeper/wasm_querier.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/keeper/wasm_querier.go#L26-L28

Added lines #L26 - L28 were not covered by tests

return funcs.Must(json.Marshal(exported.WasmQueryTxHashAndNonceResponse{
TxHash: txHash,
Nonce: nonce,
})), nil
}
case req.IsChainRegistered != nil:
chainName := exported.ChainName(req.IsChainRegistered.Chain)
haiyizxx marked this conversation as resolved.
Show resolved Hide resolved
if err := chainName.Validate(); err != nil {
return nil, err

Check warning on line 37 in x/nexus/keeper/wasm_querier.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/keeper/wasm_querier.go#L34-L37

Added lines #L34 - L37 were not covered by tests
}

_, registered := q.nexus.GetChain(ctx, chainName)
return funcs.Must(json.Marshal(exported.WasmQueryIsChainRegisteredResponse{
IsRegistered: registered,
})), nil

Check warning on line 43 in x/nexus/keeper/wasm_querier.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/keeper/wasm_querier.go#L40-L43

Added lines #L40 - L43 were not covered by tests

return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown Nexus query request"}
default:
return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown Nexus query request"}

Check warning on line 46 in x/nexus/keeper/wasm_querier.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/keeper/wasm_querier.go#L45-L46

Added lines #L45 - L46 were not covered by tests
}
}
1 change: 1 addition & 0 deletions x/nexus/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Nexus interface {
RouteMessage(ctx sdk.Context, id string, routingCtx ...exported.RoutingContext) error
DequeueRouteMessage(ctx sdk.Context) (exported.GeneralMessage, bool)
IsAssetRegistered(ctx sdk.Context, chain exported.Chain, denom string) bool
CurrID(ctx sdk.Context) ([32]byte, uint64)
}

// MsgIDGenerator provides functionality to generate msg IDs
Expand Down
44 changes: 44 additions & 0 deletions x/nexus/types/mock/expected_keepers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading