@@ -17,6 +17,7 @@ limitations under the License.
17
17
package state
18
18
19
19
import (
20
+ "bytes"
20
21
"math/rand"
21
22
"sync"
22
23
"sync/atomic"
@@ -25,6 +26,7 @@ import (
25
26
pb "github.com/golang/protobuf/proto"
26
27
"github.com/hyperledger/fabric/core/committer"
27
28
"github.com/hyperledger/fabric/gossip/comm"
29
+ common2 "github.com/hyperledger/fabric/gossip/common"
28
30
"github.com/hyperledger/fabric/gossip/gossip"
29
31
"github.com/hyperledger/fabric/gossip/proto"
30
32
"github.com/hyperledger/fabric/protos/common"
@@ -58,6 +60,9 @@ const (
58
60
// the struct to handle in memory sliding window of
59
61
// new ledger block to be acquired by hyper ledger
60
62
type GossipStateProviderImpl struct {
63
+ // Chain id
64
+ chainID string
65
+
61
66
// The gossiping service
62
67
gossip gossip.Gossip
63
68
@@ -82,18 +87,19 @@ type GossipStateProviderImpl struct {
82
87
}
83
88
84
89
// NewGossipStateProvider creates initialized instance of gossip state provider
85
- func NewGossipStateProvider (g gossip.Gossip , committer committer.Committer ) GossipStateProvider {
90
+ func NewGossipStateProvider (chainID string , g gossip.Gossip , committer committer.Committer ) GossipStateProvider {
86
91
logger , _ := logging .GetLogger ("GossipStateProvider" )
92
+ logging .SetLevel (logging .DEBUG , logger .Module )
87
93
88
94
gossipChan , _ := g .Accept (func (message interface {}) bool {
89
95
// Get only data messages
90
- return message .(* proto.GossipMessage ).GetDataMsg () != nil
96
+ return message .(* proto.GossipMessage ).IsDataMsg () &&
97
+ bytes .Equal (message .(* proto.GossipMessage ).Channel , []byte (chainID ))
91
98
}, false )
92
99
93
100
// Filter message which are only relevant for state transfer
94
101
_ , commChan := g .Accept (func (message interface {}) bool {
95
- return message .(comm.ReceivedMessage ).GetGossipMessage ().GetStateRequest () != nil ||
96
- message .(comm.ReceivedMessage ).GetGossipMessage ().GetStateResponse () != nil
102
+ return message .(comm.ReceivedMessage ).GetGossipMessage ().IsRemoteStateMessage ()
97
103
}, true )
98
104
99
105
height , err := committer .LedgerHeight ()
@@ -106,6 +112,8 @@ func NewGossipStateProvider(g gossip.Gossip, committer committer.Committer) Goss
106
112
}
107
113
108
114
s := & GossipStateProviderImpl {
115
+ chainID : chainID ,
116
+
109
117
// Instance of the gossip
110
118
gossip : g ,
111
119
@@ -131,7 +139,8 @@ func NewGossipStateProvider(g gossip.Gossip, committer committer.Committer) Goss
131
139
s .logger .Infof ("Updating node metadata information, current ledger sequence is at = %d, next expected block is = %d" , state .LedgerHeight , s .payloads .Next ())
132
140
bytes , err := state .Bytes ()
133
141
if err == nil {
134
- g .UpdateMetadata (bytes )
142
+ s .logger .Debug ("[VVV]: Updating gossip metadate state" , state )
143
+ g .UpdateChannelMetadata (bytes , common2 .ChainID (s .chainID ))
135
144
} else {
136
145
s .logger .Errorf ("Unable to serialize node meta state, error = %s" , err )
137
146
}
@@ -182,6 +191,12 @@ func (s *GossipStateProviderImpl) directMessage(msg comm.ReceivedMessage) {
182
191
return
183
192
}
184
193
194
+ if ! bytes .Equal (msg .GetGossipMessage ().Channel , []byte (s .chainID )) {
195
+ s .logger .Warning ("Received state transfer request for channel" ,
196
+ string (msg .GetGossipMessage ().Channel ), "while expecting channel" , s .chainID , "skipping request..." )
197
+ return
198
+ }
199
+
185
200
incoming := msg .GetGossipMessage ()
186
201
187
202
if incoming .GetStateRequest () != nil {
@@ -208,19 +223,17 @@ func (s *GossipStateProviderImpl) handleStateRequest(msg comm.ReceivedMessage) {
208
223
s .logger .Errorf ("Could not marshal block: %s" , err )
209
224
}
210
225
211
- if err != nil {
212
- s .logger .Errorf ("Could not calculate hash of block: %s" , err )
213
- }
214
-
215
226
response .Payloads = append (response .Payloads , & proto.Payload {
216
227
SeqNum : seqNum ,
217
228
Data : blockBytes ,
218
- // TODO: Check hash generation for given block from the ledger
219
- Hash : "" ,
229
+ Hash : string (blocks [0 ].Header .Hash ()),
220
230
})
221
231
}
222
232
// Sending back response with missing blocks
223
233
msg .Respond (& proto.GossipMessage {
234
+ Nonce : 0 ,
235
+ Tag : proto .GossipMessage_CHAN_OR_ORG ,
236
+ Channel : []byte (s .chainID ),
224
237
Content : & proto.GossipMessage_StateResponse {response },
225
238
})
226
239
}
@@ -251,6 +264,12 @@ func (s *GossipStateProviderImpl) Stop() {
251
264
252
265
// New message notification/handler
253
266
func (s * GossipStateProviderImpl ) queueNewMessage (msg * proto.GossipMessage ) {
267
+ if ! bytes .Equal (msg .Channel , []byte (s .chainID )) {
268
+ s .logger .Warning ("Received state transfer request for channel" ,
269
+ string (msg .Channel ), "while expecting channel" , s .chainID , "skipping request..." )
270
+ return
271
+ }
272
+
254
273
dataMsg := msg .GetDataMsg ()
255
274
if dataMsg != nil {
256
275
// Add new payload to ordered set
@@ -302,7 +321,7 @@ func (s *GossipStateProviderImpl) antiEntropy() {
302
321
current , _ := s .committer .LedgerHeight ()
303
322
max , _ := s .committer .LedgerHeight ()
304
323
305
- for _ , p := range s .gossip .Peers ( ) {
324
+ for _ , p := range s .gossip .PeersOfChannel ( common2 . ChainID ( s . chainID ) ) {
306
325
if state , err := FromBytes (p .Metadata ); err == nil {
307
326
if max < state .LedgerHeight {
308
327
max = state .LedgerHeight
@@ -328,7 +347,7 @@ func (s *GossipStateProviderImpl) antiEntropy() {
328
347
func (s * GossipStateProviderImpl ) requestBlocksInRange (start uint64 , end uint64 ) {
329
348
var peers []* comm.RemotePeer
330
349
// Filtering peers which might have relevant blocks
331
- for _ , value := range s .gossip .Peers ( ) {
350
+ for _ , value := range s .gossip .PeersOfChannel ( common2 . ChainID ( s . chainID ) ) {
332
351
nodeMetadata , err := FromBytes (value .Metadata )
333
352
if err == nil {
334
353
if nodeMetadata .LedgerHeight >= end {
@@ -356,13 +375,15 @@ func (s *GossipStateProviderImpl) requestBlocksInRange(start uint64, end uint64)
356
375
request .SeqNums = append (request .SeqNums , uint64 (i ))
357
376
}
358
377
359
- s .logger .Debug ("[$$$$$$$$$$$$$$$$]: Sending direct request to complete missing blocks, " , request )
378
+ s .logger .Debug ("[$$$$$$$$$$$$$$$$]: Sending direct request to complete missing blocks, " , request , "for chain" , s . chainID )
360
379
s .gossip .Send (& proto.GossipMessage {
380
+ Nonce : 0 ,
381
+ Tag : proto .GossipMessage_CHAN_OR_ORG ,
382
+ Channel : []byte (s .chainID ),
361
383
Content : & proto.GossipMessage_StateRequest {request },
362
384
}, peer )
363
385
}
364
386
365
-
366
387
// GetBlock return ledger block given its sequence number as a parameter
367
388
func (s * GossipStateProviderImpl ) GetBlock (index uint64 ) * common.Block {
368
389
// Try to read missing block from the ledger, should return no nil with
@@ -376,6 +397,7 @@ func (s *GossipStateProviderImpl) GetBlock(index uint64) *common.Block {
376
397
377
398
// AddPayload add new payload into state
378
399
func (s * GossipStateProviderImpl ) AddPayload (payload * proto.Payload ) error {
400
+ s .logger .Debug ("Adding new payload into the buffer, seqNum = " , payload .SeqNum )
379
401
return s .payloads .Push (payload )
380
402
}
381
403
@@ -390,7 +412,7 @@ func (s *GossipStateProviderImpl) commitBlock(block *common.Block, seqNum uint64
390
412
// Decode state to byte array
391
413
bytes , err := state .Bytes ()
392
414
if err == nil {
393
- s .gossip .UpdateMetadata (bytes )
415
+ s .gossip .UpdateChannelMetadata (bytes , common2 . ChainID ( s . chainID ) )
394
416
} else {
395
417
s .logger .Errorf ("Unable to serialize node meta state, error = %s" , err )
396
418
}
0 commit comments