@@ -14,126 +14,99 @@ import (
1414 "github.com/ChainSafe/gossamer/lib/crypto/ed25519"
1515 "github.com/ChainSafe/gossamer/lib/grandpa"
1616 "github.com/ChainSafe/gossamer/lib/keystore"
17-
17+ "github.com/golang/mock/gomock"
1818 "github.com/stretchr/testify/assert"
1919)
2020
2121func TestGrandpaModule_ProveFinality (t * testing.T ) {
22- testHash := common .NewHash ([]byte {0x01 , 0x02 })
23- testHashSlice := []common.Hash {testHash , testHash , testHash }
24-
25- mockBlockFinalityAPI := new (mocks.BlockFinalityAPI )
26- mockBlockAPI := new (mocks.BlockAPI )
27- mockBlockAPI .On ("SubChain" , testHash , testHash ).Return (testHashSlice , nil )
28- mockBlockAPI .On ("HasJustification" , testHash ).Return (true , nil )
29- mockBlockAPI .On ("GetJustification" , testHash ).Return ([]byte ("test" ), nil )
22+ t .Parallel ()
3023
31- mockBlockAPIHasJustErr := new (mocks.BlockAPI )
32- mockBlockAPIHasJustErr .On ("SubChain" , testHash , testHash ).Return (testHashSlice , nil )
33- mockBlockAPIHasJustErr .On ("HasJustification" , testHash ).Return (false , nil )
24+ mockError := errors .New ("test mock error" )
3425
35- mockBlockAPIGetJustErr := new (mocks.BlockAPI )
36- mockBlockAPIGetJustErr .On ("SubChain" , testHash , testHash ).Return (testHashSlice , nil )
37- mockBlockAPIGetJustErr .On ("HasJustification" , testHash ).Return (true , nil )
38- mockBlockAPIGetJustErr .On ("GetJustification" , testHash ).Return (nil , errors .New ("GetJustification error" ))
39-
40- mockBlockAPISubChainErr := new (mocks.BlockAPI )
41- mockBlockAPISubChainErr .On ("SubChain" , testHash , testHash ).Return (nil , errors .New ("SubChain error" ))
42-
43- grandpaModule := NewGrandpaModule (mockBlockAPISubChainErr , mockBlockFinalityAPI )
44- type fields struct {
45- blockAPI BlockAPI
46- blockFinalityAPI BlockFinalityAPI
47- }
48- type args struct {
49- r * http.Request
50- req * ProveFinalityRequest
51- }
52- tests := []struct {
53- name string
54- fields fields
55- args args
56- expErr error
57- exp ProveFinalityResponse
26+ tests := map [string ]struct {
27+ blockAPIBuilder func (ctrl * gomock.Controller ) BlockAPI
28+ request * ProveFinalityRequest
29+ expErr error
30+ exp ProveFinalityResponse
5831 }{
59- {
60- name : "SubChain Err" ,
61- fields : fields {
62- grandpaModule . blockAPI ,
63- grandpaModule . blockFinalityAPI ,
32+ "error during get hash by number" : {
33+ blockAPIBuilder : func ( ctrl * gomock. Controller ) BlockAPI {
34+ mockBlockAPI := NewMockBlockAPI ( ctrl )
35+ mockBlockAPI . EXPECT (). GetHashByNumber ( uint ( 1 )). Return (common. Hash {}, mockError )
36+ return mockBlockAPI
6437 },
65- args : args {
66- req : & ProveFinalityRequest {
67- blockHashStart : testHash ,
68- blockHashEnd : testHash ,
69- authorityID : uint64 (21 ),
70- },
38+ request : & ProveFinalityRequest {
39+ BlockNumber : 1 ,
7140 },
72- expErr : errors . New ( "SubChain error" ) ,
41+ expErr : mockError ,
7342 },
74- {
75- name : "OK Case" ,
76- fields : fields {
77- mockBlockAPI ,
78- mockBlockFinalityAPI ,
43+ "error during has justification" : {
44+ blockAPIBuilder : func (ctrl * gomock.Controller ) BlockAPI {
45+ mockBlockAPI := NewMockBlockAPI (ctrl )
46+ mockBlockAPI .EXPECT ().GetHashByNumber (uint (2 )).Return (common.Hash {2 }, nil )
47+ mockBlockAPI .EXPECT ().HasJustification (common.Hash {2 }).Return (false , mockError )
48+ return mockBlockAPI
7949 },
80- args : args {
81- req : & ProveFinalityRequest {
82- blockHashStart : testHash ,
83- blockHashEnd : testHash ,
84- authorityID : uint64 (21 ),
85- },
50+ request : & ProveFinalityRequest {
51+ BlockNumber : 2 ,
8652 },
87- exp : ProveFinalityResponse {
88- []uint8 {0x74 , 0x65 , 0x73 , 0x74 },
89- []uint8 {0x74 , 0x65 , 0x73 , 0x74 },
90- []uint8 {0x74 , 0x65 , 0x73 , 0x74 }},
53+ expErr : mockError ,
9154 },
92- {
93- name : "HasJustification Error" ,
94- fields : fields {
95- mockBlockAPIHasJustErr ,
96- mockBlockFinalityAPI ,
55+ "has justification is false" : {
56+ blockAPIBuilder : func (ctrl * gomock.Controller ) BlockAPI {
57+ mockBlockAPI := NewMockBlockAPI (ctrl )
58+ mockBlockAPI .EXPECT ().GetHashByNumber (uint (2 )).Return (common.Hash {2 }, nil )
59+ mockBlockAPI .EXPECT ().HasJustification (common.Hash {2 }).Return (false , nil )
60+ return mockBlockAPI
9761 },
98- args : args {
99- req : & ProveFinalityRequest {
100- blockHashStart : testHash ,
101- blockHashEnd : testHash ,
102- authorityID : uint64 (21 ),
103- },
62+ request : & ProveFinalityRequest {
63+ BlockNumber : 2 ,
10464 },
105- exp : ProveFinalityResponse ( nil ) ,
65+ exp : ProveFinalityResponse { "GRANDPA prove finality rpc failed: Block not covered by authority set changes" } ,
10666 },
107- {
108- name : "GetJustification Error" ,
109- fields : fields {
110- mockBlockAPIGetJustErr ,
111- mockBlockFinalityAPI ,
67+ "error during getJustification" : {
68+ blockAPIBuilder : func (ctrl * gomock.Controller ) BlockAPI {
69+ mockBlockAPI := NewMockBlockAPI (ctrl )
70+ mockBlockAPI .EXPECT ().GetHashByNumber (uint (3 )).Return (common.Hash {3 }, nil )
71+ mockBlockAPI .EXPECT ().HasJustification (common.Hash {3 }).Return (true , nil )
72+ mockBlockAPI .EXPECT ().GetJustification (common.Hash {3 }).Return (nil , mockError )
73+ return mockBlockAPI
11274 },
113- args : args {
114- req : & ProveFinalityRequest {
115- blockHashStart : testHash ,
116- blockHashEnd : testHash ,
117- authorityID : uint64 (21 ),
118- },
75+ request : & ProveFinalityRequest {
76+ BlockNumber : 3 ,
77+ },
78+ expErr : mockError ,
79+ },
80+ "happy path" : {
81+ blockAPIBuilder : func (ctrl * gomock.Controller ) BlockAPI {
82+ mockBlockAPI := NewMockBlockAPI (ctrl )
83+ mockBlockAPI .EXPECT ().GetHashByNumber (uint (4 )).Return (common.Hash {4 }, nil )
84+ mockBlockAPI .EXPECT ().HasJustification (common.Hash {4 }).Return (true , nil )
85+ mockBlockAPI .EXPECT ().GetJustification (common.Hash {4 }).Return ([]byte (`justification` ), nil )
86+ return mockBlockAPI
87+ },
88+ request : & ProveFinalityRequest {
89+ BlockNumber : 4 ,
11990 },
120- exp : ProveFinalityResponse ( nil ) ,
91+ exp : ProveFinalityResponse { common . BytesToHex ([] byte ( `justification` ))} ,
12192 },
12293 }
123- for _ , tt := range tests {
124- t .Run (tt .name , func (t * testing.T ) {
94+ for name , tt := range tests {
95+ tt := tt
96+ t .Run (name , func (t * testing.T ) {
97+ t .Parallel ()
98+ ctrl := gomock .NewController (t )
12599 gm := & GrandpaModule {
126- blockAPI : tt .fields .blockAPI ,
127- blockFinalityAPI : tt .fields .blockFinalityAPI ,
100+ blockAPI : tt .blockAPIBuilder (ctrl ),
128101 }
129102 res := ProveFinalityResponse (nil )
130- err := gm .ProveFinality (tt .args .r , tt .args .req , & res )
103+ err := gm .ProveFinality (nil , tt .request , & res )
104+ assert .Equal (t , tt .exp , res )
131105 if tt .expErr != nil {
132- assert .EqualError (t , err , tt .expErr .Error ())
106+ assert .ErrorContains (t , err , tt .expErr .Error ())
133107 } else {
134108 assert .NoError (t , err )
135109 }
136- assert .Equal (t , tt .exp , res )
137110 })
138111 }
139112}
0 commit comments