@@ -5,25 +5,27 @@ import (
5
5
"encoding/json"
6
6
"fmt"
7
7
"testing"
8
+ "time"
8
9
9
- "github.com/CosmWasm/wasmd/app"
10
- "google.golang.org/protobuf/runtime/protoiface"
11
-
10
+ wasmvmtypes "github.com/CosmWasm/wasmvm/types"
11
+ "github.com/cosmos/cosmos-sdk/codec"
12
12
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
13
+ "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
13
14
"github.com/cosmos/cosmos-sdk/store"
14
- authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
15
- banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
16
- "github.com/golang/protobuf/proto"
17
- dbm "github.com/tendermint/tm-db"
18
-
19
- wasmvmtypes "github.com/CosmWasm/wasmvm/types"
20
15
sdk "github.com/cosmos/cosmos-sdk/types"
21
16
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
22
17
"github.com/cosmos/cosmos-sdk/types/query"
18
+ authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
19
+ banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
20
+ stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
23
21
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
22
+ "github.com/gogo/protobuf/proto"
24
23
"github.com/stretchr/testify/assert"
25
24
"github.com/stretchr/testify/require"
25
+ tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
26
+ dbm "github.com/tendermint/tm-db"
26
27
28
+ "github.com/CosmWasm/wasmd/app"
27
29
"github.com/CosmWasm/wasmd/x/wasm/keeper"
28
30
"github.com/CosmWasm/wasmd/x/wasm/keeper/wasmtesting"
29
31
"github.com/CosmWasm/wasmd/x/wasm/types"
@@ -486,6 +488,71 @@ func TestQueryErrors(t *testing.T) {
486
488
}
487
489
}
488
490
491
+ func TestAcceptListStargateQuerier (t * testing.T ) {
492
+ wasmApp := app .SetupWithEmptyStore (t )
493
+ ctx := wasmApp .NewUncachedContext (false , tmproto.Header {ChainID : "foo" , Height : 1 , Time : time .Now ()})
494
+ wasmApp .StakingKeeper .SetParams (ctx , stakingtypes .DefaultParams ())
495
+
496
+ addrs := app .AddTestAddrs (wasmApp , ctx , 2 , sdk .NewInt (1_000_000 ))
497
+ accepted := keeper.AcceptedStargateQueries {
498
+ "/cosmos.auth.v1beta1.Query/Account" : & authtypes.QueryAccountResponse {},
499
+ "/no/route/to/this" : & authtypes.QueryAccountResponse {},
500
+ }
501
+
502
+ marshal := func (pb proto.Message ) []byte {
503
+ b , err := proto .Marshal (pb )
504
+ require .NoError (t , err )
505
+ return b
506
+ }
507
+
508
+ specs := map [string ]struct {
509
+ req * wasmvmtypes.StargateQuery
510
+ expErr bool
511
+ expResp string
512
+ }{
513
+ "in accept list - success result" : {
514
+ req : & wasmvmtypes.StargateQuery {
515
+ Path : "/cosmos.auth.v1beta1.Query/Account" ,
516
+ Data : marshal (& authtypes.QueryAccountRequest {Address : addrs [0 ].String ()}),
517
+ },
518
+ expResp : fmt .Sprintf (`{"account":{"@type":"/cosmos.auth.v1beta1.BaseAccount","address":%q,"pub_key":null,"account_number":"1","sequence":"0"}}` , addrs [0 ].String ()),
519
+ },
520
+ "in accept list - error result" : {
521
+ req : & wasmvmtypes.StargateQuery {
522
+ Path : "/cosmos.auth.v1beta1.Query/Account" ,
523
+ Data : marshal (& authtypes.QueryAccountRequest {Address : sdk .AccAddress (ed25519 .GenPrivKey ().PubKey ().Address ()).String ()}),
524
+ },
525
+ expErr : true ,
526
+ },
527
+ "not in accept list" : {
528
+ req : & wasmvmtypes.StargateQuery {
529
+ Path : "/cosmos.bank.v1beta1.Query/AllBalances" ,
530
+ Data : marshal (& banktypes.QueryAllBalancesRequest {Address : addrs [0 ].String ()}),
531
+ },
532
+ expErr : true ,
533
+ },
534
+ "unknown route" : {
535
+ req : & wasmvmtypes.StargateQuery {
536
+ Path : "/no/route/to/this" ,
537
+ Data : marshal (& banktypes.QueryAllBalancesRequest {Address : addrs [0 ].String ()}),
538
+ },
539
+ expErr : true ,
540
+ },
541
+ }
542
+ for name , spec := range specs {
543
+ t .Run (name , func (t * testing.T ) {
544
+ q := keeper .AcceptListStargateQuerier (accepted , wasmApp .GRPCQueryRouter (), wasmApp .AppCodec ())
545
+ gotBz , gotErr := q (ctx , spec .req )
546
+ if spec .expErr {
547
+ require .Error (t , gotErr )
548
+ return
549
+ }
550
+ require .NoError (t , gotErr )
551
+ assert .JSONEq (t , spec .expResp , string (gotBz ), string (gotBz ))
552
+ })
553
+ }
554
+ }
555
+
489
556
type mockWasmQueryKeeper struct {
490
557
GetContractInfoFn func (ctx sdk.Context , contractAddress sdk.AccAddress ) * types.ContractInfo
491
558
QueryRawFn func (ctx sdk.Context , contractAddress sdk.AccAddress , key []byte ) []byte
@@ -548,13 +615,13 @@ func (m bankKeeperMock) GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk
548
615
return m .GetAllBalancesFn (ctx , addr )
549
616
}
550
617
551
- func TestConvertProtoToJSONMarshal (t * testing.T ) {
618
+ func TestCo3nvertProtoToJSONMarshal (t * testing.T ) {
552
619
testCases := []struct {
553
620
name string
554
621
queryPath string
555
- protoResponseStruct proto. Message
622
+ protoResponseStruct codec. ProtoMarshaler
556
623
originalResponse string
557
- expectedProtoResponse proto. Message
624
+ expectedProtoResponse codec. ProtoMarshaler
558
625
expectedError bool
559
626
}{
560
627
{
@@ -573,28 +640,26 @@ func TestConvertProtoToJSONMarshal(t *testing.T) {
573
640
name : "invalid proto response struct" ,
574
641
queryPath : "/cosmos.bank.v1beta1.Query/AllBalances" ,
575
642
originalResponse : "0a090a036261721202333012050a03666f6f" ,
576
- protoResponseStruct : protoiface . MessageV1 ( nil ) ,
643
+ protoResponseStruct : & authtypes. QueryAccountResponse {} ,
577
644
expectedError : true ,
578
645
},
579
646
}
580
647
581
648
for _ , tc := range testCases {
582
649
t .Run (fmt .Sprintf ("Case %s" , tc .name ), func (t * testing.T ) {
583
- // set up app for testing
584
- wasmApp := app .SetupWithEmptyStore (t )
585
-
586
650
originalVersionBz , err := hex .DecodeString (tc .originalResponse )
587
651
require .NoError (t , err )
652
+ appCodec := app .MakeEncodingConfig ().Marshaler
588
653
589
- jsonMarshalledResponse , err := keeper .ConvertProtoToJSONMarshal (tc .protoResponseStruct , originalVersionBz , wasmApp . AppCodec () )
654
+ jsonMarshalledResponse , err := keeper .ConvertProtoToJSONMarshal (appCodec , tc .protoResponseStruct , originalVersionBz )
590
655
if tc .expectedError {
591
656
require .Error (t , err )
592
657
return
593
658
}
594
659
require .NoError (t , err )
595
660
596
661
// check response by json marshalling proto response into json response manually
597
- jsonMarshalExpectedResponse , err := wasmApp . AppCodec () .MarshalJSON (tc .expectedProtoResponse )
662
+ jsonMarshalExpectedResponse , err := appCodec .MarshalJSON (tc .expectedProtoResponse )
598
663
require .NoError (t , err )
599
664
require .JSONEq (t , string (jsonMarshalledResponse ), string (jsonMarshalExpectedResponse ))
600
665
})
@@ -609,8 +674,8 @@ func TestDeterministicJsonMarshal(t *testing.T) {
609
674
originalResponse string
610
675
updatedResponse string
611
676
queryPath string
612
- responseProtoStruct interface {}
613
- expectedProto func () proto. Message
677
+ responseProtoStruct codec. ProtoMarshaler
678
+ expectedProto func () codec. ProtoMarshaler
614
679
}{
615
680
/**
616
681
*
@@ -638,7 +703,7 @@ func TestDeterministicJsonMarshal(t *testing.T) {
638
703
"0a530a202f636f736d6f732e617574682e763162657461312e426173654163636f756e74122f0a2d636f736d6f733166387578756c746e3873717a687a6e72737a3371373778776171756867727367366a79766679122d636f736d6f733166387578756c746e3873717a687a6e72737a3371373778776171756867727367366a79766679" ,
639
704
"/cosmos.auth.v1beta1.Query/Account" ,
640
705
& authtypes.QueryAccountResponse {},
641
- func () proto. Message {
706
+ func () codec. ProtoMarshaler {
642
707
account := authtypes.BaseAccount {
643
708
Address : "cosmos1f8uxultn8sqzhznrsz3q77xwaquhgrsg6jyvfy" ,
644
709
}
@@ -653,23 +718,23 @@ func TestDeterministicJsonMarshal(t *testing.T) {
653
718
654
719
for _ , tc := range testCases {
655
720
t .Run (fmt .Sprintf ("Case %s" , tc .name ), func (t * testing.T ) {
656
- wasmApp := app .SetupWithEmptyStore ( t )
721
+ appCodec := app .MakeEncodingConfig (). Marshaler
657
722
658
723
originVersionBz , err := hex .DecodeString (tc .originalResponse )
659
724
require .NoError (t , err )
660
- jsonMarshalledOriginalBz , err := keeper .ConvertProtoToJSONMarshal (tc .responseProtoStruct , originVersionBz , wasmApp . AppCodec () )
725
+ jsonMarshalledOriginalBz , err := keeper .ConvertProtoToJSONMarshal (appCodec , tc .responseProtoStruct , originVersionBz )
661
726
require .NoError (t , err )
662
727
663
728
newVersionBz , err := hex .DecodeString (tc .updatedResponse )
664
729
require .NoError (t , err )
665
- jsonMarshalledUpdatedBz , err := keeper .ConvertProtoToJSONMarshal (tc .responseProtoStruct , newVersionBz , wasmApp . AppCodec () )
730
+ jsonMarshalledUpdatedBz , err := keeper .ConvertProtoToJSONMarshal (appCodec , tc .responseProtoStruct , newVersionBz )
666
731
require .NoError (t , err )
667
732
668
733
// json marshalled bytes should be the same since we use the same proto struct for unmarshalling
669
734
require .Equal (t , jsonMarshalledOriginalBz , jsonMarshalledUpdatedBz )
670
735
671
736
// raw build also make same result
672
- jsonMarshalExpectedResponse , err := wasmApp . AppCodec () .MarshalJSON (tc .expectedProto ())
737
+ jsonMarshalExpectedResponse , err := appCodec .MarshalJSON (tc .expectedProto ())
673
738
require .NoError (t , err )
674
739
require .Equal (t , jsonMarshalledUpdatedBz , jsonMarshalExpectedResponse )
675
740
})
0 commit comments