6
6
"encoding/json"
7
7
"errors"
8
8
"io"
9
+ "reflect"
9
10
"strings"
10
11
"sync"
11
12
"testing"
@@ -15,6 +16,7 @@ import (
15
16
abciproto "github.com/cometbft/cometbft/api/cometbft/abci/v1"
16
17
v1 "github.com/cometbft/cometbft/api/cometbft/types/v1"
17
18
"github.com/cosmos/gogoproto/proto"
19
+ gogoproto "github.com/cosmos/gogoproto/proto"
18
20
gogotypes "github.com/cosmos/gogoproto/types"
19
21
"github.com/stretchr/testify/require"
20
22
@@ -33,6 +35,9 @@ import (
33
35
"cosmossdk.io/server/v2/stf/branch"
34
36
"cosmossdk.io/server/v2/stf/mock"
35
37
consensustypes "cosmossdk.io/x/consensus/types"
38
+
39
+ "github.com/cosmos/cosmos-sdk/testutil/testdata"
40
+ sdk "github.com/cosmos/cosmos-sdk/types"
36
41
)
37
42
38
43
var (
53
58
Msg : & gogotypes.BoolValue {Value : true },
54
59
GasLimit : 0 ,
55
60
}
56
- actorName = []byte ("cookies" )
61
+ actorName = []byte ("cookies" )
62
+ testAcc = sdk .AccAddress ([]byte ("addr1_______________" ))
63
+ versionStr = "0.0.0"
57
64
)
58
65
59
66
func getQueryRouterBuilder [T any , PT interface {
@@ -583,11 +590,7 @@ func TestConsensus_Info(t *testing.T) {
583
590
require .Equal (t , res .LastBlockHeight , int64 (1 ))
584
591
}
585
592
586
- // TODO:
587
- // - GRPC request
588
- // - app request
589
- // - p2p request
590
- func TestConsensus_Query (t * testing.T ) {
593
+ func TestConsensus_QueryStore (t * testing.T ) {
591
594
c := setUpConsensus (t , 100_000 , cometmock.MockMempool [mock.Tx ]{})
592
595
593
596
// Write data to state storage
@@ -648,11 +651,149 @@ func TestConsensus_Query(t *testing.T) {
648
651
require .Equal (t , res .Value , []byte (nil ))
649
652
}
650
653
654
+ func TestConsensus_GRPCQuery (t * testing.T ) {
655
+ c := setUpConsensus (t , 100_000 , cometmock.MockMempool [mock.Tx ]{})
656
+
657
+ _ , err := c .InitChain (context .Background (), & abciproto.InitChainRequest {
658
+ Time : time .Now (),
659
+ ChainId : "test" ,
660
+ InitialHeight : 1 ,
661
+ })
662
+ require .NoError (t , err )
663
+
664
+ _ , err = c .FinalizeBlock (context .Background (), & abciproto.FinalizeBlockRequest {
665
+ Time : time .Now (),
666
+ Height : 1 ,
667
+ Txs : [][]byte {mockTx .Bytes ()},
668
+ Hash : emptyHash [:],
669
+ })
670
+ require .NoError (t , err )
671
+
672
+ // empty request
673
+ res , err := c .Query (context .Background (), & abciproto.QueryRequest {})
674
+ require .NoError (t , err )
675
+ require .Equal (t , res .Code , uint32 (1 ))
676
+ require .Contains (t , res .Log , "no query path provided" )
677
+
678
+ // query request not exist in handler map
679
+ invalidReq := testdata.EchoRequest {
680
+ Message : "echo" ,
681
+ }
682
+ invalidReqBz , err := invalidReq .Marshal ()
683
+ require .NoError (t , err )
684
+ invalidQuery := abci.QueryRequest {
685
+ Data : invalidReqBz ,
686
+ Path : "testpb.EchoRequest" ,
687
+ }
688
+ invalidRes , err := c .Query (context .TODO (), & invalidQuery )
689
+ require .Error (t , err )
690
+ require .Nil (t , invalidRes )
691
+ require .Contains (t , err .Error (), "no query handler found" )
692
+
693
+ // Valid query
694
+ req := testdata.SayHelloRequest {Name : "foo" }
695
+ reqBz , err := req .Marshal ()
696
+ require .NoError (t , err )
697
+ reqQuery := abci.QueryRequest {
698
+ Data : reqBz ,
699
+ Path : "testpb.SayHelloRequest" ,
700
+ }
701
+
702
+ resQuery , err := c .Query (context .TODO (), & reqQuery )
703
+ require .NoError (t , err )
704
+ require .Equal (t , abci .CodeTypeOK , resQuery .Code , resQuery )
705
+
706
+ var response testdata.SayHelloResponse
707
+ require .NoError (t , response .Unmarshal (resQuery .Value ))
708
+ require .Equal (t , "Hello foo!" , response .Greeting )
709
+ }
710
+
711
+ func TestConsensus_P2PQuery (t * testing.T ) {
712
+ c := setUpConsensus (t , 100_000 , cometmock.MockMempool [mock.Tx ]{})
713
+
714
+ _ , err := c .InitChain (context .Background (), & abciproto.InitChainRequest {
715
+ Time : time .Now (),
716
+ ChainId : "test" ,
717
+ InitialHeight : 1 ,
718
+ })
719
+ require .NoError (t , err )
720
+
721
+ _ , err = c .FinalizeBlock (context .Background (), & abciproto.FinalizeBlockRequest {
722
+ Time : time .Now (),
723
+ Height : 1 ,
724
+ Txs : [][]byte {mockTx .Bytes ()},
725
+ Hash : emptyHash [:],
726
+ })
727
+ require .NoError (t , err )
728
+
729
+ // empty request
730
+ res , err := c .Query (context .Background (), & abciproto.QueryRequest {})
731
+ require .NoError (t , err )
732
+ require .Equal (t , res .Code , uint32 (1 ))
733
+ require .Contains (t , res .Log , "no query path provided" )
734
+
735
+ addrQuery := abci.QueryRequest {
736
+ Path : "/p2p/filter/addr/1.1.1.1:8000" ,
737
+ }
738
+ res , err = c .Query (context .TODO (), & addrQuery )
739
+ require .NoError (t , err )
740
+ require .Equal (t , uint32 (3 ), res .Code )
741
+
742
+ idQuery := abci.QueryRequest {
743
+ Path : "/p2p/filter/id/testid" ,
744
+ }
745
+ res , err = c .Query (context .TODO (), & idQuery )
746
+ require .NoError (t , err )
747
+ require .Equal (t , uint32 (4 ), res .Code )
748
+ }
749
+
750
+ func TestConsensus_AppQuery (t * testing.T ) {
751
+ c := setUpConsensus (t , 100_000 , cometmock.MockMempool [mock.Tx ]{})
752
+
753
+ _ , err := c .InitChain (context .Background (), & abciproto.InitChainRequest {
754
+ Time : time .Now (),
755
+ ChainId : "test" ,
756
+ InitialHeight : 1 ,
757
+ })
758
+ require .NoError (t , err )
759
+
760
+ _ , err = c .FinalizeBlock (context .Background (), & abciproto.FinalizeBlockRequest {
761
+ Time : time .Now (),
762
+ Height : 1 ,
763
+ Txs : [][]byte {mockTx .Bytes ()},
764
+ Hash : emptyHash [:],
765
+ })
766
+ require .NoError (t , err )
767
+
768
+ tx := mock.Tx {
769
+ Sender : testAcc ,
770
+ Msg : & gogotypes.BoolValue {Value : true },
771
+ GasLimit : 1000 ,
772
+ }
773
+ txBytes := tx .Bytes ()
774
+
775
+ // simulate by calling Query with encoded tx
776
+ query := abci.QueryRequest {
777
+ Path : "/app/simulate" ,
778
+ Data : txBytes ,
779
+ }
780
+ queryResult , err := c .Query (context .TODO (), & query )
781
+ require .NoError (t , err )
782
+ require .True (t , queryResult .IsOK (), queryResult .Log )
783
+
784
+ // Query app version
785
+ res , err := c .Query (context .TODO (), & abci.QueryRequest {Path : "app/version" })
786
+ require .NoError (t , err )
787
+ require .True (t , res .IsOK ())
788
+ require .Equal (t , versionStr , string (res .Value ))
789
+ }
790
+
651
791
func setUpConsensus (t * testing.T , gasLimit uint64 , mempool mempool.Mempool [mock.Tx ]) * consensus [mock.Tx ] {
652
792
t .Helper ()
653
793
794
+ queryHandler := make (map [string ]appmodulev2.Handler )
654
795
msgRouterBuilder := getMsgRouterBuilder (t , func (ctx context.Context , msg * gogotypes.BoolValue ) (* gogotypes.BoolValue , error ) {
655
- return nil , nil
796
+ return msg , nil
656
797
})
657
798
658
799
queryRouterBuilder := getQueryRouterBuilder (t , func (ctx context.Context , q * consensustypes.QueryParamsRequest ) (* consensustypes.QueryParamsResponse , error ) {
@@ -669,6 +810,32 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
669
810
}, nil
670
811
})
671
812
813
+ helloFooHandler := func (ctx context.Context , msg transaction.Msg ) (msgResp transaction.Msg , err error ) {
814
+ typedReq := msg .(* testdata.SayHelloRequest )
815
+ handler := testdata.QueryImpl {}
816
+ typedResp , err := handler .SayHello (ctx , typedReq )
817
+ if err != nil {
818
+ return nil , err
819
+ }
820
+
821
+ return typedResp , nil
822
+ }
823
+
824
+ queryRouterBuilder .RegisterHandler (
825
+ proto .MessageName (& testdata.SayHelloRequest {}),
826
+ helloFooHandler ,
827
+ )
828
+
829
+ queryHandler [proto .MessageName (& testdata.SayHelloRequest {})] = appmodulev2.Handler {
830
+ Func : helloFooHandler ,
831
+ MakeMsg : func () transaction.Msg {
832
+ return reflect .New (gogoproto .MessageType (proto .MessageName (& testdata.SayHelloRequest {})).Elem ()).Interface ().(transaction.Msg )
833
+ },
834
+ MakeMsgResp : func () transaction.Msg {
835
+ return reflect .New (gogoproto .MessageType (proto .MessageName (& testdata.SayHelloResponse {})).Elem ()).Interface ().(transaction.Msg )
836
+ },
837
+ }
838
+
672
839
s , err := stf .New (
673
840
log .NewNopLogger ().With ("module" , "stf" ),
674
841
msgRouterBuilder ,
@@ -709,6 +876,16 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
709
876
nil ,
710
877
)
711
878
879
+ addrPeerFilter := func (info string ) (* abci.QueryResponse , error ) {
880
+ require .Equal (t , "1.1.1.1:8000" , info )
881
+ return & abci.QueryResponse {Code : uint32 (3 )}, nil
882
+ }
883
+
884
+ idPeerFilter := func (id string ) (* abci.QueryResponse , error ) {
885
+ require .Equal (t , "testid" , id )
886
+ return & abci.QueryResponse {Code : uint32 (4 )}, nil
887
+ }
888
+
712
889
return & consensus [mock.Tx ]{
713
890
logger : log .NewNopLogger (),
714
891
appName : "testing-app" ,
@@ -719,6 +896,10 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
719
896
txCodec : mock.TxCodec {},
720
897
chainID : "test" ,
721
898
getProtoRegistry : sync .OnceValues (proto .MergedRegistry ),
899
+ queryHandlersMap : queryHandler ,
900
+ addrPeerFilter : addrPeerFilter ,
901
+ idPeerFilter : idPeerFilter ,
902
+ version : versionStr ,
722
903
}
723
904
}
724
905
0 commit comments