@@ -16,65 +16,84 @@ import (
1616 "github.com/hyperledger/fabric-x-orderer/node/assembler"
1717 "github.com/hyperledger/fabric-x-orderer/node/comm/tlsgen"
1818 "github.com/hyperledger/fabric-x-orderer/node/config"
19+ "github.com/hyperledger/fabric-x-orderer/node/consensus/state"
1920 "github.com/hyperledger/fabric-x-orderer/testutil"
2021
2122 "github.com/hyperledger/fabric-protos-go-apiv2/common"
2223 "github.com/hyperledger/fabric-protos-go-apiv2/orderer"
2324 "github.com/stretchr/testify/require"
2425)
2526
26- func TestAssemblerAppendBlockAndIgnoreDuplicate (t * testing.T ) {
27+ func TestAssemblerHandlesConsenterReconnect (t * testing.T ) {
2728 ca , err := tlsgen .NewCA ()
2829 require .NoError (t , err )
2930
30- batcherStub := NewStubBatcher (t , types .ShardID (1 ), types .PartyID (1 ), ca )
31- defer batcherStub .Stop ()
31+ numParties := 4
32+ partyID := types .PartyID (1 )
33+ shardID := types .ShardID (1 )
3234
33- consenterStub := NewStubConsenter (t , types . PartyID ( 1 ) , ca )
34- defer consenterStub . Stop ()
35+ batchersStub , batcherInfos , cleanup := createStubBatchersAndInfos (t , numParties , shardID , ca )
36+ defer cleanup ()
3537
36- assembler , clean := newAssemblerTest (t , 1 , 1 , ca , batcherStub . batcherInfo , consenterStub . consenterInfo )
37- defer clean ()
38+ consenterStub := NewStubConsenter (t , partyID , ca )
39+ defer consenterStub . Shutdown ()
3840
39- // genesis block added
41+ assembler := newAssemblerTest (t , partyID , shardID , ca , batcherInfos , consenterStub .consenterInfo )
42+ defer assembler .Stop ()
43+
44+ // wait for genesis block
4045 require .Eventually (t , func () bool {
4146 return assembler .GetTxCount () == 1
4247 }, 3 * time .Second , 100 * time .Millisecond )
4348
4449 obaCreator , _ := NewOrderedBatchAttestationCreator ()
4550
46- // create batch with 1 req, send from batcher and consenter
51+ // send batch and matching decision
4752 batch1 := testutil .CreateMockBatch (1 , 1 , 1 , []int {1 })
48- batcherStub .SetNextBatch (batch1 )
49-
53+ batchersStub [0 ].SetNextBatch (batch1 )
5054 oba1 := obaCreator .Append (batch1 , 1 , 1 , 1 )
51- consenterStub .SetNextDecision (oba1 )
55+ consenterStub .SetNextDecision (oba1 .( * state. AvailableBatchOrdered ) )
5256
5357 require .Eventually (t , func () bool {
5458 return assembler .GetTxCount () == 2
5559 }, 3 * time .Second , 100 * time .Millisecond )
5660
57- // create batch with 2 reqs, send from batcher and consenter
61+ // stop consenter and send next batch
62+ consenterStub .Stop ()
63+
5864 batch2 := testutil .CreateMockBatch (1 , 1 , 2 , []int {2 , 3 })
59- batcherStub .SetNextBatch (batch2 )
65+ batchersStub [0 ].SetNextBatch (batch2 )
66+
67+ // restart consenter and send matching decision
68+ consenterStub .Restart ()
6069
6170 oba2 := obaCreator .Append (batch2 , 2 , 1 , 1 )
62- consenterStub .SetNextDecision (oba2 )
71+ consenterStub .SetNextDecision (oba2 .( * state. AvailableBatchOrdered ) )
6372
6473 require .Eventually (t , func () bool {
6574 return assembler .GetTxCount () == 4
6675 }, 3 * time .Second , 100 * time .Millisecond )
6776
68- // send duplicate batch+oba, should be ignored
69- batcherStub .SetNextBatch (batch2 )
70- consenterStub .SetNextDecision (oba2 )
77+ // send next decision and restart consenter
78+ batch3 := testutil .CreateMockBatch (1 , 1 , 3 , []int {4 })
79+ oba3 := obaCreator .Append (batch3 , 3 , 1 , 1 )
80+ consenterStub .SetNextDecision (oba3 .(* state.AvailableBatchOrdered ))
81+
82+ // wait for decistion will be sent
83+ time .Sleep (3 * time .Second )
84+
85+ consenterStub .Stop ()
86+ consenterStub .Restart ()
87+
88+ // send matching batch
89+ batchersStub [0 ].SetNextBatch (batch3 )
7190
72- require .Never (t , func () bool {
73- return assembler .GetTxCount () > 4
74- }, 3 * time .Millisecond , 100 * time .Millisecond )
91+ require .Eventually (t , func () bool {
92+ return assembler .GetTxCount () == 5
93+ }, 3 * time .Second , 100 * time .Millisecond )
7594}
7695
77- func newAssemblerTest (t * testing.T , partyID int , shardID int , ca tlsgen.CA , batcherInfo config.BatcherInfo , consenterInfo config.ConsenterInfo ) ( * assembler.Assembler , func ()) {
96+ func newAssemblerTest (t * testing.T , partyID types. PartyID , shardID types. ShardID , ca tlsgen.CA , batchersInfo [] config.BatcherInfo , consenterInfo config.ConsenterInfo ) * assembler.Assembler {
7897 genesisBlock := utils .EmptyGenesisBlock ("arma" )
7998 genesisBlock .Metadata = & common.BlockMetadata {
8099 Metadata : [][]byte {nil , nil , []byte ("dummy" ), []byte ("dummy" )},
@@ -85,19 +104,17 @@ func newAssemblerTest(t *testing.T, partyID int, shardID int, ca tlsgen.CA, batc
85104
86105 shards := []config.ShardInfo {
87106 {
88- ShardId : types .ShardID (shardID ),
89- Batchers : []config.BatcherInfo {
90- batcherInfo ,
91- },
107+ ShardId : shardID ,
108+ Batchers : batchersInfo ,
92109 },
93110 }
94111
95112 nodeConfig := & config.AssemblerNodeConfig {
96113 TLSPrivateKeyFile : ckp .Key ,
97114 TLSCertificateFile : ckp .Cert ,
98- PartyId : types . PartyID ( partyID ) ,
115+ PartyId : partyID ,
99116 Directory : t .TempDir (),
100- ListenAddress : "0 .0.0.0 :0" ,
117+ ListenAddress : "127 .0.0.1 :0" ,
101118 PrefetchBufferMemoryBytes : 1 * 1024 * 1024 * 1024 ,
102119 RestartLedgerScanTimeout : 5 * time .Second ,
103120 PrefetchEvictionTtl : time .Hour ,
@@ -111,15 +128,30 @@ func newAssemblerTest(t *testing.T, partyID int, shardID int, ca tlsgen.CA, batc
111128
112129 assemblerGRPC := node .CreateGRPCAssembler (nodeConfig )
113130
114- assembler := assembler .NewAssembler (nodeConfig , assemblerGRPC , genesisBlock , testutil .CreateLogger (t , partyID ))
131+ assembler := assembler .NewAssembler (nodeConfig , assemblerGRPC , genesisBlock , testutil .CreateLogger (t , int ( partyID ) ))
115132
116133 orderer .RegisterAtomicBroadcastServer (assemblerGRPC .Server (), assembler )
117134 go func () {
118135 err := assemblerGRPC .Start ()
119136 require .NoError (t , err )
120137 }()
121138
122- return assembler , func () {
123- assembler .Stop ()
139+ return assembler
140+ }
141+
142+ func createStubBatchersAndInfos (t * testing.T , numParties int , shardID types.ShardID , ca tlsgen.CA ) ([]* stubBatcher , []config.BatcherInfo , func ()) {
143+ var batchers []* stubBatcher
144+ var batcherInfos []config.BatcherInfo
145+
146+ for i := 1 ; i <= numParties ; i ++ {
147+ b := NewStubBatcher (t , shardID , types .PartyID (i ), ca )
148+ batchers = append (batchers , b )
149+ batcherInfos = append (batcherInfos , b .batcherInfo )
150+ }
151+
152+ return batchers , batcherInfos , func () {
153+ for _ , b := range batchers {
154+ b .Shutdown ()
155+ }
124156 }
125157}
0 commit comments