@@ -15,12 +15,19 @@ import (
1515 "github.com/celestiaorg/celestia-node/api/rpc"
1616 "github.com/celestiaorg/celestia-node/api/rpc/client"
1717 "github.com/celestiaorg/celestia-node/nodebuilder"
18+ "github.com/celestiaorg/celestia-node/nodebuilder/das"
1819 dasMock "github.com/celestiaorg/celestia-node/nodebuilder/das/mocks"
20+ "github.com/celestiaorg/celestia-node/nodebuilder/fraud"
1921 fraudMock "github.com/celestiaorg/celestia-node/nodebuilder/fraud/mocks"
22+ "github.com/celestiaorg/celestia-node/nodebuilder/header"
2023 headerMock "github.com/celestiaorg/celestia-node/nodebuilder/header/mocks"
2124 "github.com/celestiaorg/celestia-node/nodebuilder/node"
25+ nodeMock "github.com/celestiaorg/celestia-node/nodebuilder/node/mocks"
26+ "github.com/celestiaorg/celestia-node/nodebuilder/p2p"
2227 p2pMock "github.com/celestiaorg/celestia-node/nodebuilder/p2p/mocks"
28+ "github.com/celestiaorg/celestia-node/nodebuilder/share"
2329 shareMock "github.com/celestiaorg/celestia-node/nodebuilder/share/mocks"
30+ statemod "github.com/celestiaorg/celestia-node/nodebuilder/state"
2431 stateMock "github.com/celestiaorg/celestia-node/nodebuilder/state/mocks"
2532 "github.com/celestiaorg/celestia-node/state"
2633)
@@ -58,33 +65,67 @@ func TestRPCCallsUnderlyingNode(t *testing.T) {
5865 require .Equal (t , expectedBalance , balance )
5966}
6067
68+ // api contains all modules that are made available as the node's
69+ // public API surface (except for the `node` module as the node
70+ // module contains a method `Info` that is also contained in the
71+ // p2p module).
72+ type api interface {
73+ fraud.Module
74+ header.Module
75+ statemod.Module
76+ share.Module
77+ das.Module
78+ p2p.Module
79+ }
80+
6181func TestModulesImplementFullAPI (t * testing.T ) {
62- api := reflect .TypeOf (new (client.API )).Elem ()
82+ api := reflect .TypeOf (new (api )).Elem ()
83+ nodeapi := reflect .TypeOf (new (node.Module )).Elem () // TODO @renaynay: explain
6384 client := reflect .TypeOf (new (client.Client )).Elem ()
6485 for i := 0 ; i < client .NumField (); i ++ {
6586 module := client .Field (i )
66- // the "closers" field is not an actual module
67- if module .Name == "closer" {
87+ switch module .Name {
88+ case "closer" :
89+ // the "closers" field is not an actual module
6890 continue
69- }
70- internal , ok := module .Type .FieldByName ("Internal" )
71- require .True (t , ok , "module %s's API does not have an Internal field" , module .Name )
72- for j := 0 ; j < internal .Type .NumField (); j ++ {
73- impl := internal .Type .Field (j )
74- method , _ := api .MethodByName (impl .Name )
75- require .Equal (t , method .Type , impl .Type , "method %s does not match" , impl .Name )
91+ case "Node" :
92+ // node module contains a duplicate method to the p2p module
93+ // and must be tested separately.
94+ internal , ok := module .Type .FieldByName ("Internal" )
95+ require .True (t , ok , "module %s's API does not have an Internal field" , module .Name )
96+ for j := 0 ; j < internal .Type .NumField (); j ++ {
97+ impl := internal .Type .Field (j )
98+ method , _ := nodeapi .MethodByName (impl .Name )
99+ require .Equal (t , method .Type , impl .Type , "method %s does not match" , impl .Name )
100+ }
101+ default :
102+ internal , ok := module .Type .FieldByName ("Internal" )
103+ require .True (t , ok , "module %s's API does not have an Internal field" , module .Name )
104+ for j := 0 ; j < internal .Type .NumField (); j ++ {
105+ impl := internal .Type .Field (j )
106+ method , _ := api .MethodByName (impl .Name )
107+ require .Equal (t , method .Type , impl .Type , "method %s does not match" , impl .Name )
108+ }
76109 }
77110 }
78111}
79112
80113func TestAllReturnValuesAreMarshalable (t * testing.T ) {
81- ra := reflect .TypeOf (new (client. API )).Elem ()
114+ ra := reflect .TypeOf (new (api )).Elem ()
82115 for i := 0 ; i < ra .NumMethod (); i ++ {
83116 m := ra .Method (i )
84117 for j := 0 ; j < m .Type .NumOut (); j ++ {
85118 implementsMarshaler (t , m .Type .Out (j ))
86119 }
87120 }
121+ // NOTE: see comment above api interface definition.
122+ na := reflect .TypeOf (new (node.Module )).Elem ()
123+ for i := 0 ; i < na .NumMethod (); i ++ {
124+ m := na .Method (i )
125+ for j := 0 ; j < m .Type .NumOut (); j ++ {
126+ implementsMarshaler (t , m .Type .Out (j ))
127+ }
128+ }
88129}
89130
90131func implementsMarshaler (t * testing.T , typ reflect.Type ) {
@@ -145,6 +186,7 @@ func setupNodeWithModifiedRPC(t *testing.T) (*nodebuilder.Node, *mockAPI) {
145186 headerMock .NewMockModule (ctrl ),
146187 dasMock .NewMockModule (ctrl ),
147188 p2pMock .NewMockModule (ctrl ),
189+ nodeMock .NewMockModule (ctrl ),
148190 }
149191
150192 // given the behavior of fx.Invoke, this invoke will be called last as it is added at the root
@@ -156,6 +198,7 @@ func setupNodeWithModifiedRPC(t *testing.T) (*nodebuilder.Node, *mockAPI) {
156198 srv .RegisterService ("header" , mockAPI .Header )
157199 srv .RegisterService ("das" , mockAPI .Das )
158200 srv .RegisterService ("p2p" , mockAPI .P2P )
201+ srv .RegisterService ("node" , mockAPI .Node )
159202 })
160203 nd := nodebuilder .TestNode (t , node .Full , invokeRPC )
161204 // start node
@@ -175,4 +218,5 @@ type mockAPI struct {
175218 Header * headerMock.MockModule
176219 Das * dasMock.MockModule
177220 P2P * p2pMock.MockModule
221+ Node * nodeMock.MockModule
178222}
0 commit comments