@@ -11,17 +11,14 @@ import (
11
11
12
12
"github.com/golang/protobuf/proto"
13
13
"github.com/hyperledger/fabric/common/mocks/ledger"
14
- "github.com/hyperledger/fabric/core/chaincode/shim"
15
14
"github.com/hyperledger/fabric/core/endorser"
16
15
"github.com/hyperledger/fabric/core/endorser/mocks"
17
16
endorsement "github.com/hyperledger/fabric/core/handlers/endorsement/api"
18
17
. "github.com/hyperledger/fabric/core/handlers/endorsement/api/state"
19
18
"github.com/hyperledger/fabric/core/transientstore"
20
- "github.com/hyperledger/fabric/protos/common"
21
19
"github.com/hyperledger/fabric/protos/ledger/rwset"
22
20
"github.com/hyperledger/fabric/protos/peer"
23
21
transientstore2 "github.com/hyperledger/fabric/protos/transientstore"
24
- "github.com/hyperledger/fabric/protoutil"
25
22
"github.com/pkg/errors"
26
23
"github.com/stretchr/testify/assert"
27
24
"github.com/stretchr/testify/mock"
@@ -38,21 +35,13 @@ func TestPluginEndorserNotFound(t *testing.T) {
38
35
pluginEndorser := endorser .NewPluginEndorser (& endorser.PluginSupport {
39
36
PluginMapper : pluginMapper ,
40
37
})
41
- resp , err := pluginEndorser .EndorseWithPlugin (endorser.Context {
42
- Response : & peer.Response {},
43
- PluginName : "notfound" ,
44
- })
45
- assert .Nil (t , resp )
38
+ endorsement , prpBytes , err := pluginEndorser .EndorseWithPlugin ("notfound" , "" , nil , nil )
39
+ assert .Nil (t , endorsement )
40
+ assert .Nil (t , prpBytes )
46
41
assert .Contains (t , err .Error (), "plugin with name notfound wasn't found" )
47
42
}
48
43
49
44
func TestPluginEndorserGreenPath (t * testing.T ) {
50
- proposal , _ , err := protoutil .CreateChaincodeProposal (common .HeaderType_ENDORSER_TRANSACTION , "mychannel" , & peer.ChaincodeInvocationSpec {
51
- ChaincodeSpec : & peer.ChaincodeSpec {
52
- ChaincodeId : & peer.ChaincodeID {Name : "mycc" },
53
- },
54
- }, []byte {1 , 2 , 3 })
55
- assert .NoError (t , err )
56
45
expectedSignature := []byte {5 , 4 , 3 , 2 , 1 }
57
46
expectedProposalResponsePayload := []byte {1 , 2 , 3 }
58
47
pluginMapper := & mocks.PluginMapper {}
@@ -73,48 +62,35 @@ func TestPluginEndorserGreenPath(t *testing.T) {
73
62
PluginMapper : pluginMapper ,
74
63
TransientStoreRetriever : mockTransientStoreRetriever ,
75
64
})
76
- ctx := endorser.Context {
77
- Response : & peer.Response {},
78
- PluginName : "plugin" ,
79
- Proposal : proposal ,
80
- ChaincodeID : & peer.ChaincodeID {
81
- Name : "mycc" ,
82
- },
83
- Channel : "mychannel" ,
84
- }
85
65
86
66
// Scenario I: Call the endorsement for the first time
87
- resp , err := pluginEndorser .EndorseWithPlugin (ctx )
67
+ endorsement , prpBytes , err := pluginEndorser .EndorseWithPlugin ("plugin" , "mychannel" , nil , nil )
88
68
assert .NoError (t , err )
89
- assert .NotNil (t , resp )
90
- assert .Equal (t , expectedSignature , resp .Endorsement .Signature )
91
- assert .Equal (t , expectedProposalResponsePayload , resp .Payload )
69
+ assert .Equal (t , expectedSignature , endorsement .Signature )
70
+ assert .Equal (t , expectedProposalResponsePayload , prpBytes )
92
71
// Ensure both state and SigningIdentityFetcher were passed to Init()
93
72
plugin .AssertCalled (t , "Init" , & endorser.ChannelState {QueryCreator : queryCreator , Store : mockTransientStore }, sif )
94
73
95
74
// Scenario II: Call the endorsement again a second time.
96
75
// Ensure the plugin wasn't instantiated again - which means the same instance
97
76
// was used to service the request.
98
77
// Also - check that the Init() wasn't called more than once on the plugin.
99
- resp , err = pluginEndorser .EndorseWithPlugin (ctx )
78
+ endorsement , prpBytes , err = pluginEndorser .EndorseWithPlugin ("plugin" , "mychannel" , nil , nil )
100
79
assert .NoError (t , err )
101
- assert .NotNil (t , resp )
102
- assert .Equal (t , expectedSignature , resp .Endorsement .Signature )
103
- assert .Equal (t , expectedProposalResponsePayload , resp .Payload )
80
+ assert .Equal (t , expectedSignature , endorsement .Signature )
81
+ assert .Equal (t , expectedProposalResponsePayload , prpBytes )
104
82
pluginFactory .AssertNumberOfCalls (t , "New" , 1 )
105
83
plugin .AssertNumberOfCalls (t , "Init" , 1 )
106
84
107
85
// Scenario III: Call the endorsement with a channel-less context.
108
86
// The init method should be called again, but this time - a channel state object
109
87
// should not be passed into the init.
110
- ctx .Channel = ""
111
88
pluginFactory .On ("New" ).Return (plugin ).Once ()
112
89
plugin .On ("Init" , mock .Anything ).Return (nil ).Once ()
113
- resp , err = pluginEndorser .EndorseWithPlugin (ctx )
90
+ endorsement , prpBytes , err = pluginEndorser .EndorseWithPlugin ("plugin" , "" , nil , nil )
114
91
assert .NoError (t , err )
115
- assert .NotNil (t , resp )
116
- assert .Equal (t , expectedSignature , resp .Endorsement .Signature )
117
- assert .Equal (t , expectedProposalResponsePayload , resp .Payload )
92
+ assert .Equal (t , expectedSignature , endorsement .Signature )
93
+ assert .Equal (t , expectedProposalResponsePayload , prpBytes )
118
94
plugin .AssertCalled (t , "Init" , sif )
119
95
}
120
96
@@ -136,73 +112,15 @@ func TestPluginEndorserErrors(t *testing.T) {
136
112
TransientStoreRetriever : mockTransientStoreRetriever ,
137
113
})
138
114
139
- // Scenario I: Failed initializing plugin
115
+ // Failed initializing plugin
140
116
t .Run ("PluginInitializationFailure" , func (t * testing.T ) {
141
117
plugin .On ("Init" , mock .Anything , mock .Anything ).Return (errors .New ("plugin initialization failed" )).Once ()
142
- resp , err := pluginEndorser .EndorseWithPlugin (endorser.Context {
143
- PluginName : "plugin" ,
144
- Channel : "mychannel" ,
145
- Response : & peer.Response {},
146
- })
147
- assert .Nil (t , resp )
118
+ endorsement , prpBytes , err := pluginEndorser .EndorseWithPlugin ("plugin" , "mychannel" , nil , nil )
119
+ assert .Nil (t , endorsement )
120
+ assert .Nil (t , prpBytes )
148
121
assert .Contains (t , err .Error (), "plugin initialization failed" )
149
122
})
150
123
151
- // Scenario II: an empty proposal is passed in the context, and parsing fails
152
- t .Run ("EmptyProposal" , func (t * testing.T ) {
153
- plugin .On ("Init" , mock .Anything , mock .Anything ).Return (nil ).Once ()
154
- ctx := endorser.Context {
155
- Response : & peer.Response {},
156
- PluginName : "plugin" ,
157
- ChaincodeID : & peer.ChaincodeID {
158
- Name : "mycc" ,
159
- },
160
- Proposal : & peer.Proposal {},
161
- Channel : "mychannel" ,
162
- }
163
- resp , err := pluginEndorser .EndorseWithPlugin (ctx )
164
- assert .Nil (t , resp )
165
- assert .Contains (t , err .Error (), "could not compute proposal hash" )
166
- })
167
-
168
- // Scenario III: The proposal's header is invalid
169
- t .Run ("InvalidHeader in the proposal" , func (t * testing.T ) {
170
- ctx := endorser.Context {
171
- Response : & peer.Response {},
172
- PluginName : "plugin" ,
173
- ChaincodeID : & peer.ChaincodeID {
174
- Name : "mycc" ,
175
- },
176
- Proposal : & peer.Proposal {
177
- Header : []byte {1 , 2 , 3 },
178
- },
179
- Channel : "mychannel" ,
180
- }
181
- resp , err := pluginEndorser .EndorseWithPlugin (ctx )
182
- assert .Nil (t , resp )
183
- assert .Contains (t , err .Error (), "failed parsing header" )
184
- })
185
-
186
- // Scenario IV: The proposal's response status code indicates an error
187
- t .Run ("ResponseStatusContainsError" , func (t * testing.T ) {
188
- r := & peer.Response {
189
- Status : shim .ERRORTHRESHOLD ,
190
- Payload : []byte {1 , 2 , 3 },
191
- Message : "bla bla" ,
192
- }
193
- resp , err := pluginEndorser .EndorseWithPlugin (endorser.Context {
194
- Response : r ,
195
- })
196
- assert .Equal (t , & peer.ProposalResponse {Response : r }, resp )
197
- assert .NoError (t , err )
198
- })
199
-
200
- // Scenario V: The proposal's response is nil
201
- t .Run ("ResponseIsNil" , func (t * testing.T ) {
202
- resp , err := pluginEndorser .EndorseWithPlugin (endorser.Context {})
203
- assert .Nil (t , resp )
204
- assert .Contains (t , err .Error (), "response is nil" )
205
- })
206
124
}
207
125
208
126
func transientStoreRetriever () * mocks.TransientStoreRetriever {
@@ -281,22 +199,6 @@ func TestTransientStore(t *testing.T) {
281
199
TransientStoreRetriever : storeRetriever ,
282
200
})
283
201
284
- proposal , _ , err := protoutil .CreateChaincodeProposal (common .HeaderType_ENDORSER_TRANSACTION , "mychannel" , & peer.ChaincodeInvocationSpec {
285
- ChaincodeSpec : & peer.ChaincodeSpec {
286
- ChaincodeId : & peer.ChaincodeID {Name : "mycc" },
287
- },
288
- }, []byte {1 , 2 , 3 })
289
- assert .NoError (t , err )
290
- ctx := endorser.Context {
291
- Response : & peer.Response {},
292
- PluginName : "plugin" ,
293
- Proposal : proposal ,
294
- ChaincodeID : & peer.ChaincodeID {
295
- Name : "mycc" ,
296
- },
297
- Channel : "mychannel" ,
298
- }
299
-
300
202
rws := & rwset.TxPvtReadWriteSet {
301
203
NsPvtRwset : []* rwset.NsPvtReadWriteSet {
302
204
{
@@ -316,11 +218,11 @@ func TestTransientStore(t *testing.T) {
316
218
317
219
transientStore .On ("GetTxPvtRWSetByTxid" , mock .Anything , mock .Anything ).Return (scanner , nil )
318
220
319
- resp , err := pluginEndorser .EndorseWithPlugin (ctx )
221
+ _ , prpBytes , err := pluginEndorser .EndorseWithPlugin ("plugin" , "mychannel" , nil , nil )
320
222
assert .NoError (t , err )
321
223
322
224
txrws := & rwset.TxPvtReadWriteSet {}
323
- err = proto .Unmarshal (resp . Payload , txrws )
225
+ err = proto .Unmarshal (prpBytes , txrws )
324
226
assert .NoError (t , err )
325
227
assert .True (t , proto .Equal (rws , txrws ))
326
228
scanner .AssertCalled (t , "Close" )
0 commit comments