@@ -17,6 +17,7 @@ limitations under the License.
17
17
package multichain
18
18
19
19
import (
20
+ "fmt"
20
21
"reflect"
21
22
"testing"
22
23
"time"
@@ -39,19 +40,24 @@ import (
39
40
"github.com/stretchr/testify/assert"
40
41
)
41
42
42
- var conf , singleMSPConf * genesisconfig.Profile
43
- var genesisBlock , singleMSPGenesisBlock * cb.Block
43
+ var conf , singleMSPConf , noConsortiumConf * genesisconfig.Profile
44
+ var genesisBlock , singleMSPGenesisBlock , noConsortiumGenesisBlock * cb.Block
44
45
var mockSigningIdentity msp.SigningIdentity
45
46
47
+ const NoConsortiumChain = "NoConsortiumChain"
48
+
46
49
func init () {
47
- conf = genesisconfig .Load (genesisconfig .SampleInsecureProfile )
48
50
logging .SetLevel (logging .DEBUG , "" )
49
- genesisBlock = provisional .New (conf ).GenesisBlock ()
50
51
mockSigningIdentity , _ = mmsp .NewNoopMsp ().GetDefaultSigningIdentity ()
51
52
53
+ conf = genesisconfig .Load (genesisconfig .SampleInsecureProfile )
54
+ genesisBlock = provisional .New (conf ).GenesisBlock ()
55
+
52
56
singleMSPConf = genesisconfig .Load (genesisconfig .SampleSingleMSPSoloProfile )
53
- logging .SetLevel (logging .DEBUG , "" )
54
57
singleMSPGenesisBlock = provisional .New (singleMSPConf ).GenesisBlock ()
58
+
59
+ noConsortiumConf = genesisconfig .Load ("SampleNoConsortium" )
60
+ noConsortiumGenesisBlock = provisional .New (noConsortiumConf ).GenesisBlockForChannel (NoConsortiumChain )
55
61
}
56
62
57
63
func mockCrypto () * mockCryptoHelper {
@@ -113,10 +119,7 @@ func TestGetConfigTx(t *testing.T) {
113
119
rl .Append (block )
114
120
115
121
pctx := getConfigTx (rl )
116
-
117
- if ! reflect .DeepEqual (ctx , pctx ) {
118
- t .Fatalf ("Did not select most recent config transaction" )
119
- }
122
+ assert .Equal (t , pctx , ctx , "Did not select most recent config transaction" )
120
123
}
121
124
122
125
// Tests a chain which contains blocks with multi-transactions mixed with config txs, and a single tx which is not a config tx, none count as config blocks so nil should return
@@ -129,29 +132,21 @@ func TestGetConfigTxFailure(t *testing.T) {
129
132
}))
130
133
}
131
134
rl .Append (ledger .CreateNextBlock (rl , []* cb.Envelope {makeNormalTx (provisional .TestChainID , 11 )}))
132
- defer func () {
133
- if recover () == nil {
134
- t .Fatalf ("Should have panic-ed because there was no config tx" )
135
- }
136
- }()
137
- getConfigTx (rl )
135
+ assert .Panics (t , func () { getConfigTx (rl ) }, "Should have panicked because there was no config tx" )
138
136
137
+ block := ledger .CreateNextBlock (rl , []* cb.Envelope {makeNormalTx (provisional .TestChainID , 12 )})
138
+ block .Metadata .Metadata [cb .BlockMetadataIndex_LAST_CONFIG ] = []byte ("bad metadata" )
139
+ assert .Panics (t , func () { getConfigTx (rl ) }, "Should have panicked because of bad last config metadata" )
139
140
}
140
141
141
142
// This test checks to make sure the orderer refuses to come up if it cannot find a system channel
142
143
func TestNoSystemChain (t * testing.T ) {
143
- defer func () {
144
- if recover () == nil {
145
- t .Fatalf ("Should have panicked when starting without a system chain" )
146
- }
147
- }()
148
-
149
144
lf := ramledger .New (10 )
150
145
151
146
consenters := make (map [string ]Consenter )
152
147
consenters [conf .Orderer .OrdererType ] = & mockConsenter {}
153
148
154
- NewManagerImpl (lf , consenters , mockCrypto ())
149
+ assert . Panics ( t , func () { NewManagerImpl (lf , consenters , mockCrypto ()) }, "Should have panicked when starting without a system chain" )
155
150
}
156
151
157
152
// This test checks to make sure that the orderer refuses to come up if there are multiple system channels
@@ -172,6 +167,74 @@ func TestMultiSystemChannel(t *testing.T) {
172
167
assert .Panics (t , func () { NewManagerImpl (lf , consenters , mockCrypto ()) }, "Two system channels should have caused panic" )
173
168
}
174
169
170
+ // This test checks to make sure that the orderer creates different type of filters given different type of channel
171
+ func TestFilterCreation (t * testing.T ) {
172
+ lf := ramledger .New (10 )
173
+ rl , err := lf .GetOrCreate (provisional .TestChainID )
174
+ if err != nil {
175
+ panic (err )
176
+ }
177
+ err = rl .Append (genesisBlock )
178
+ if err != nil {
179
+ panic (err )
180
+ }
181
+
182
+ // Creating a non-system chain to test that NewManagerImpl could handle the diversity
183
+ rl , err = lf .GetOrCreate (NoConsortiumChain )
184
+ if err != nil {
185
+ panic (err )
186
+ }
187
+ err = rl .Append (noConsortiumGenesisBlock )
188
+ if err != nil {
189
+ panic (err )
190
+ }
191
+
192
+ consenters := make (map [string ]Consenter )
193
+ consenters [conf .Orderer .OrdererType ] = & mockConsenter {}
194
+
195
+ manager := NewManagerImpl (lf , consenters , mockCrypto ())
196
+
197
+ _ , ok := manager .GetChain (provisional .TestChainID )
198
+ assert .True (t , ok , "Should have found chain: %d" , provisional .TestChainID )
199
+
200
+ chainSupport , ok := manager .GetChain (NoConsortiumChain )
201
+ assert .True (t , ok , "Should have retrieved chain: %d" , NoConsortiumChain )
202
+
203
+ messages := make ([]* cb.Envelope , conf .Orderer .BatchSize .MaxMessageCount )
204
+ for i := 0 ; i < int (conf .Orderer .BatchSize .MaxMessageCount ); i ++ {
205
+ messages [i ] = & cb.Envelope {
206
+ Payload : utils .MarshalOrPanic (& cb.Payload {
207
+ Header : & cb.Header {
208
+ ChannelHeader : utils .MarshalOrPanic (& cb.ChannelHeader {
209
+ // For testing purpose, we are injecting configTx into non-system channel.
210
+ // Set Type to HeaderType_ORDERER_TRANSACTION to verify this message is NOT
211
+ // filtered by SystemChainFilter, so we know we are creating correct type
212
+ // of filter for the chain.
213
+ Type : int32 (cb .HeaderType_ORDERER_TRANSACTION ),
214
+ ChannelId : NoConsortiumChain ,
215
+ }),
216
+ SignatureHeader : utils .MarshalOrPanic (& cb.SignatureHeader {}),
217
+ },
218
+ Data : []byte (fmt .Sprintf ("%d" , i )),
219
+ }),
220
+ }
221
+
222
+ assert .True (t , chainSupport .Enqueue (messages [i ]), "Should have successfully enqueued message" )
223
+ }
224
+
225
+ it , _ := rl .Iterator (& ab.SeekPosition {Type : & ab.SeekPosition_Specified {Specified : & ab.SeekSpecified {Number : 1 }}})
226
+ select {
227
+ case <- it .ReadyChan ():
228
+ block , status := it .Next ()
229
+ assert .Equal (t , cb .Status_SUCCESS , status , "Could not retrieve block" )
230
+ for i := 0 ; i < int (conf .Orderer .BatchSize .MaxMessageCount ); i ++ {
231
+ assert .Equal (t , messages [i ], utils .ExtractEnvelopeOrPanic (block , i ), "Block contents wrong at index %d" , i )
232
+ }
233
+ case <- time .After (time .Second ):
234
+ t .Fatalf ("Block 1 not produced after timeout" )
235
+ }
236
+ }
237
+
175
238
// This test essentially brings the entire system up and is ultimately what main.go will replicate
176
239
func TestManagerImpl (t * testing.T ) {
177
240
lf , rl := NewRAMLedgerAndFactory (10 )
@@ -182,15 +245,10 @@ func TestManagerImpl(t *testing.T) {
182
245
manager := NewManagerImpl (lf , consenters , mockCrypto ())
183
246
184
247
_ , ok := manager .GetChain ("Fake" )
185
- if ok {
186
- t .Errorf ("Should not have found a chain that was not created" )
187
- }
248
+ assert .False (t , ok , "Should not have found a chain that was not created" )
188
249
189
250
chainSupport , ok := manager .GetChain (provisional .TestChainID )
190
-
191
- if ! ok {
192
- t .Fatalf ("Should have gotten chain which was initialized by ramledger" )
193
- }
251
+ assert .True (t , ok , "Should have gotten chain which was initialized by ramledger" )
194
252
195
253
messages := make ([]* cb.Envelope , conf .Orderer .BatchSize .MaxMessageCount )
196
254
for i := 0 ; i < int (conf .Orderer .BatchSize .MaxMessageCount ); i ++ {
@@ -205,21 +263,16 @@ func TestManagerImpl(t *testing.T) {
205
263
select {
206
264
case <- it .ReadyChan ():
207
265
block , status := it .Next ()
208
- if status != cb .Status_SUCCESS {
209
- t .Fatalf ("Could not retrieve block" )
210
- }
266
+ assert .Equal (t , cb .Status_SUCCESS , status , "Could not retrieve block" )
211
267
for i := 0 ; i < int (conf .Orderer .BatchSize .MaxMessageCount ); i ++ {
212
- if ! reflect .DeepEqual (utils .ExtractEnvelopeOrPanic (block , i ), messages [i ]) {
213
- t .Errorf ("Block contents wrong at index %d" , i )
214
- }
268
+ assert .Equal (t , messages [i ], utils .ExtractEnvelopeOrPanic (block , i ), "Block contents wrong at index %d" , i )
215
269
}
216
270
case <- time .After (time .Second ):
217
271
t .Fatalf ("Block 1 not produced after timeout" )
218
272
}
219
273
}
220
274
221
275
func TestNewChannelConfig (t * testing.T ) {
222
-
223
276
lf , _ := NewRAMLedgerAndFactoryWithMSP ()
224
277
225
278
consenters := make (map [string ]Consenter )
@@ -228,7 +281,7 @@ func TestNewChannelConfig(t *testing.T) {
228
281
229
282
t .Run ("BadPayload" , func (t * testing.T ) {
230
283
_ , err := manager .NewChannelConfig (& cb.Envelope {Payload : []byte ("bad payload" )})
231
- assert .Error (t , err , "Should bot be able to create new channel config from bad payload." )
284
+ assert .Error (t , err , "Should not be able to create new channel config from bad payload." )
232
285
})
233
286
234
287
for _ , tc := range []struct {
0 commit comments