Skip to content

Commit 86cd87e

Browse files
committed
[FAB-2198] Gossip envelope refactoring - End
This commit finishes the refactoring of the gossip message to support envelopes with payloads and signatures. It adds a new message type that embeds both an envelope and both a GossipMessage: SignedGossipMessage. since it embeds the GossipMessage, it inherits all of GossipMessage's methods. Signed-off-by: Yacov Manevich <[email protected]> Change-Id: I7b9f8ec8e33353d194de4cb266bb7d3124830378
1 parent b7b5c4e commit 86cd87e

26 files changed

+586
-594
lines changed

gossip/comm/comm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Comm interface {
3131
GetPKIid() common.PKIidType
3232

3333
// Send sends a message to remote peers
34-
Send(msg *proto.GossipMessage, peers ...*RemotePeer)
34+
Send(msg *proto.SignedGossipMessage, peers ...*RemotePeer)
3535

3636
// Probe probes a remote node and returns nil if its responsive
3737
Probe(peer *RemotePeer) error

gossip/comm/comm_impl.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
195195
conn.pkiID = pkiID
196196
conn.logger = c.logger
197197

198-
h := func(m *proto.GossipMessage) {
198+
h := func(m *proto.SignedGossipMessage) {
199199
c.logger.Debug("Got message:", m)
200200
c.msgPublisher.DeMultiplex(&ReceivedMessageImpl{
201-
conn: conn,
202-
lock: conn,
203-
GossipMessage: m,
201+
conn: conn,
202+
lock: conn,
203+
SignedGossipMessage: m,
204204
})
205205
}
206206
conn.handler = h
@@ -211,15 +211,15 @@ func (c *commImpl) createConnection(endpoint string, expectedPKIID common.PKIidT
211211
return nil, err
212212
}
213213

214-
func (c *commImpl) Send(msg *proto.GossipMessage, peers ...*RemotePeer) {
214+
func (c *commImpl) Send(msg *proto.SignedGossipMessage, peers ...*RemotePeer) {
215215
if c.isStopping() || len(peers) == 0 {
216216
return
217217
}
218218

219219
c.logger.Debug("Entering, sending", msg, "to ", len(peers), "peers")
220220

221221
for _, peer := range peers {
222-
go func(peer *RemotePeer, msg *proto.GossipMessage) {
222+
go func(peer *RemotePeer, msg *proto.SignedGossipMessage) {
223223
c.sendToEndpoint(peer, msg)
224224
}(peer, msg)
225225
}
@@ -247,7 +247,7 @@ func (c *commImpl) isPKIblackListed(p common.PKIidType) bool {
247247
return false
248248
}
249249

250-
func (c *commImpl) sendToEndpoint(peer *RemotePeer, msg *proto.GossipMessage) {
250+
func (c *commImpl) sendToEndpoint(peer *RemotePeer, msg *proto.SignedGossipMessage) {
251251
if c.isStopping() {
252252
return
253253
}
@@ -388,7 +388,7 @@ func (c *commImpl) authenticateRemotePeer(stream stream) (common.PKIidType, erro
388388
remoteAddress := extractRemoteAddress(stream)
389389
remoteCertHash := extractCertificateHashFromContext(ctx)
390390
var err error
391-
var cMsg *proto.GossipMessage
391+
var cMsg *proto.SignedGossipMessage
392392
var signer proto.Signer
393393

394394
// If TLS is detected, sign the hash of our cert to bind our TLS cert
@@ -475,11 +475,11 @@ func (c *commImpl) GossipStream(stream proto.Gossip_GossipStreamServer) error {
475475
return nil
476476
}
477477

478-
h := func(m *proto.GossipMessage) {
478+
h := func(m *proto.SignedGossipMessage) {
479479
c.msgPublisher.DeMultiplex(&ReceivedMessageImpl{
480-
conn: conn,
481-
lock: conn,
482-
GossipMessage: m,
480+
conn: conn,
481+
lock: conn,
482+
SignedGossipMessage: m,
483483
})
484484
}
485485

@@ -506,8 +506,8 @@ func (c *commImpl) disconnect(pkiID common.PKIidType) {
506506
c.connStore.closeByPKIid(pkiID)
507507
}
508508

509-
func readWithTimeout(stream interface{}, timeout time.Duration, address string) (*proto.GossipMessage, error) {
510-
incChan := make(chan *proto.GossipMessage, 1)
509+
func readWithTimeout(stream interface{}, timeout time.Duration, address string) (*proto.SignedGossipMessage, error) {
510+
incChan := make(chan *proto.SignedGossipMessage, 1)
511511
errChan := make(chan error, 1)
512512
go func() {
513513
if srvStr, isServerStr := stream.(proto.Gossip_GossipStreamServer); isServerStr {
@@ -541,7 +541,7 @@ func readWithTimeout(stream interface{}, timeout time.Duration, address string)
541541
}
542542
}
543543

544-
func (c *commImpl) createConnectionMsg(pkiID common.PKIidType, hash []byte, cert api.PeerIdentityType, signer proto.Signer) *proto.GossipMessage {
544+
func (c *commImpl) createConnectionMsg(pkiID common.PKIidType, hash []byte, cert api.PeerIdentityType, signer proto.Signer) *proto.SignedGossipMessage {
545545
m := &proto.GossipMessage{
546546
Tag: proto.GossipMessage_EMPTY,
547547
Nonce: 0,
@@ -553,8 +553,11 @@ func (c *commImpl) createConnectionMsg(pkiID common.PKIidType, hash []byte, cert
553553
},
554554
},
555555
}
556-
m.Sign(signer)
557-
return m
556+
sMsg := &proto.SignedGossipMessage{
557+
GossipMessage: m,
558+
}
559+
sMsg.Sign(signer)
560+
return sMsg
558561
}
559562

560563
type stream interface {

gossip/comm/comm_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func handshaker(endpoint string, comm Comm, t *testing.T, sigMutator func([]byte
149149
msg2Send := createGossipMsg()
150150
nonce := uint64(rand.Int())
151151
msg2Send.Nonce = nonce
152-
go stream.Send(msg2Send.NoopSign())
152+
go stream.Send(msg2Send.Envelope)
153153
return acceptChan
154154
}
155155

@@ -355,7 +355,7 @@ func TestResponses(t *testing.T) {
355355
for m := range inChan {
356356
reply := createGossipMsg()
357357
reply.Nonce = m.GetGossipMessage().Nonce + 1
358-
m.Respond(reply)
358+
m.Respond(reply.GossipMessage)
359359
}
360360
}()
361361
expectedNOnce := uint64(msg.Nonce + 1)
@@ -515,14 +515,14 @@ func TestPresumedDead(t *testing.T) {
515515
}
516516
}
517517

518-
func createGossipMsg() *proto.GossipMessage {
519-
return &proto.GossipMessage{
518+
func createGossipMsg() *proto.SignedGossipMessage {
519+
return (&proto.GossipMessage{
520520
Tag: proto.GossipMessage_EMPTY,
521521
Nonce: uint64(rand.Int()),
522522
Content: &proto.GossipMessage_DataMsg{
523523
DataMsg: &proto.DataMessage{},
524524
},
525-
}
525+
}).NoopSign()
526526
}
527527

528528
func remotePeer(port int) *RemotePeer {

gossip/comm/conn.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"google.golang.org/grpc"
2929
)
3030

31-
type handler func(*proto.GossipMessage)
31+
type handler func(message *proto.SignedGossipMessage)
3232

3333
type connFactory interface {
3434
createConnection(endpoint string, pkiID common.PKIidType) (*connection, error)
@@ -239,7 +239,7 @@ func (conn *connection) toDie() bool {
239239
return atomic.LoadInt32(&(conn.stopFlag)) == int32(1)
240240
}
241241

242-
func (conn *connection) send(msg *proto.GossipMessage, onErr func(error)) {
242+
func (conn *connection) send(msg *proto.SignedGossipMessage, onErr func(error)) {
243243
conn.Lock()
244244
defer conn.Unlock()
245245

@@ -248,10 +248,6 @@ func (conn *connection) send(msg *proto.GossipMessage, onErr func(error)) {
248248
return
249249
}
250250

251-
if msg.Envelope == nil {
252-
msg.NoopSign()
253-
}
254-
255251
m := &msgSending{
256252
envelope: msg.Envelope,
257253
onErr: onErr,
@@ -262,7 +258,7 @@ func (conn *connection) send(msg *proto.GossipMessage, onErr func(error)) {
262258

263259
func (conn *connection) serviceConnection() error {
264260
errChan := make(chan error, 1)
265-
msgChan := make(chan *proto.GossipMessage, util.GetIntOrDefault("peer.gossip.recvBuffSize", defRecvBuffSize))
261+
msgChan := make(chan *proto.SignedGossipMessage, util.GetIntOrDefault("peer.gossip.recvBuffSize", defRecvBuffSize))
266262
defer close(msgChan)
267263

268264
// Call stream.Recv() asynchronously in readFromStream(),
@@ -312,7 +308,7 @@ func (conn *connection) writeToStream() {
312308
}
313309
}
314310

315-
func (conn *connection) readFromStream(errChan chan error, msgChan chan *proto.GossipMessage) {
311+
func (conn *connection) readFromStream(errChan chan error, msgChan chan *proto.SignedGossipMessage) {
316312
defer func() {
317313
recover()
318314
}() // msgChan might be closed

gossip/comm/mock/mock_comm.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (packet *packetMock) Respond(msg *proto.GossipMessage) {
8787
packet.src.socket <- &packetMock{
8888
src: packet.dst,
8989
dst: packet.src,
90-
msg: msg,
90+
msg: msg.NoopSign(),
9191
}
9292
}
9393

@@ -98,8 +98,8 @@ func (packet *packetMock) GetSourceEnvelope() *proto.Envelope {
9898
}
9999

100100
// GetGossipMessage returns the underlying GossipMessage
101-
func (packet *packetMock) GetGossipMessage() *proto.GossipMessage {
102-
return packet.msg.(*proto.GossipMessage)
101+
func (packet *packetMock) GetGossipMessage() *proto.SignedGossipMessage {
102+
return packet.msg.(*proto.SignedGossipMessage)
103103
}
104104

105105
// GetPKIID returns the PKI-ID of the remote peer
@@ -141,7 +141,7 @@ func (mock *commMock) GetPKIid() common.PKIidType {
141141
}
142142

143143
// Send sends a message to remote peers
144-
func (mock *commMock) Send(msg *proto.GossipMessage, peers ...*comm.RemotePeer) {
144+
func (mock *commMock) Send(msg *proto.SignedGossipMessage, peers ...*comm.RemotePeer) {
145145
for _, peer := range peers {
146146
logger.Debug("Sending message to peer ", peer.Endpoint, "from ", mock.id)
147147
mock.members[peer.Endpoint].socket <- &packetMock{

gossip/comm/mock/mock_comm_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func TestMockComm(t *testing.T) {
4444
comm2 := NewCommMock(second.endpoint, members)
4545
defer comm2.Stop()
4646

47-
comm2.Send(&proto.GossipMessage{
47+
comm2.Send((&proto.GossipMessage{
4848
Content: &proto.GossipMessage_StateRequest{&proto.RemoteStateRequest{
4949
SeqNums: []uint64{1, 2, 3},
5050
}},
51-
}, &comm.RemotePeer{"first", common.PKIidType("first")})
51+
}).NoopSign(), &comm.RemotePeer{"first", common.PKIidType("first")})
5252

5353
msg := <-msgCh
5454

@@ -71,12 +71,12 @@ func TestMockComm_PingPong(t *testing.T) {
7171
rcvChA := peerA.Accept(all)
7272
rcvChB := peerB.Accept(all)
7373

74-
peerA.Send(&proto.GossipMessage{
74+
peerA.Send((&proto.GossipMessage{
7575
Content: &proto.GossipMessage_DataMsg{
7676
&proto.DataMessage{
7777
&proto.Payload{1, "", []byte("Ping")},
7878
}},
79-
}, &comm.RemotePeer{"peerB", common.PKIidType("peerB")})
79+
}).NoopSign(), &comm.RemotePeer{"peerB", common.PKIidType("peerB")})
8080

8181
msg := <-rcvChB
8282
dataMsg := msg.GetGossipMessage().GetDataMsg()

gossip/comm/msg.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
// ReceivedMessageImpl is an implementation of ReceivedMessage
2727
type ReceivedMessageImpl struct {
28-
*proto.GossipMessage
28+
*proto.SignedGossipMessage
2929
lock sync.Locker
3030
conn *connection
3131
}
@@ -38,12 +38,12 @@ func (m *ReceivedMessageImpl) GetSourceEnvelope() *proto.Envelope {
3838

3939
// Respond sends a msg to the source that sent the ReceivedMessageImpl
4040
func (m *ReceivedMessageImpl) Respond(msg *proto.GossipMessage) {
41-
m.conn.send(msg, func(e error) {})
41+
m.conn.send(msg.NoopSign(), func(e error) {})
4242
}
4343

4444
// GetGossipMessage returns the inner GossipMessage
45-
func (m *ReceivedMessageImpl) GetGossipMessage() *proto.GossipMessage {
46-
return m.GossipMessage
45+
func (m *ReceivedMessageImpl) GetGossipMessage() *proto.SignedGossipMessage {
46+
return m.SignedGossipMessage
4747
}
4848

4949
// GetPKIID returns the PKI-ID of the remote peer

gossip/discovery/discovery.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
// CryptoService is an interface that the discovery expects to be implemented and passed on creation
2525
type CryptoService interface {
2626
// ValidateAliveMsg validates that an Alive message is authentic
27-
ValidateAliveMsg(*proto.GossipMessage) bool
27+
ValidateAliveMsg(message *proto.SignedGossipMessage) bool
2828

2929
// SignMessage signs a message
3030
SignMessage(m *proto.GossipMessage, internalEndpoint string) *proto.Envelope
@@ -33,17 +33,17 @@ type CryptoService interface {
3333
// CommService is an interface that the discovery expects to be implemented and passed on creation
3434
type CommService interface {
3535
// Gossip gossips a message
36-
Gossip(msg *proto.GossipMessage)
36+
Gossip(msg *proto.SignedGossipMessage)
3737

3838
// SendToPeer sends to a given peer a message.
3939
// The nonce can be anything since the communication module handles the nonce itself
40-
SendToPeer(peer *NetworkMember, msg *proto.GossipMessage)
40+
SendToPeer(peer *NetworkMember, msg *proto.SignedGossipMessage)
4141

4242
// Ping probes a remote peer and returns if it's responsive or not
4343
Ping(peer *NetworkMember) bool
4444

4545
// Accept returns a read-only channel for membership messages sent from remote peers
46-
Accept() <-chan *proto.GossipMessage
46+
Accept() <-chan *proto.SignedGossipMessage
4747

4848
// PresumedDead returns a read-only channel for peers that are presumed to be dead
4949
PresumedDead() <-chan common.PKIidType

0 commit comments

Comments
 (0)