Skip to content

Commit b9f89e7

Browse files
FAB-1846 Storing election config in gossip service
Parameters for leader election service are part of gossip configuration Those parameters stored gossip service, for future leader election creation Change-Id: Ic3ee090fe451070666a66a9212dfb70e8504075a Signed-off-by: Gennady Laventman <[email protected]>
1 parent c341fe5 commit b9f89e7

File tree

6 files changed

+132
-64
lines changed

6 files changed

+132
-64
lines changed

gossip/gossip/gossip_impl.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ type gossipServiceImpl struct {
7474
}
7575

7676
// NewGossipService creates a gossip instance attached to a gRPC server
77-
func NewGossipService(conf *Config, s *grpc.Server, secAdvisor api.SecurityAdvisor, mcs api.MessageCryptoService, selfIdentity api.PeerIdentityType, dialOpts ...grpc.DialOption) Gossip {
77+
func NewGossipService(conf *Config, s *grpc.Server, secAdvisor api.SecurityAdvisor, mcs api.MessageCryptoService, idMapper identity.Mapper, selfIdentity api.PeerIdentityType, dialOpts ...grpc.DialOption) Gossip {
7878
var c comm.Comm
7979
var err error
80-
idMapper := identity.NewIdentityMapper(mcs)
80+
8181
lgr := util.GetLogger(util.LoggingGossipModule, conf.ID)
8282
if s == nil {
8383
c, err = createCommWithServer(conf.BindPort, idMapper, selfIdentity)
@@ -168,8 +168,8 @@ func createCommWithoutServer(s *grpc.Server, cert *tls.Certificate, idStore iden
168168
}
169169

170170
// NewGossipServiceWithServer creates a new gossip instance with a gRPC server
171-
func NewGossipServiceWithServer(conf *Config, secAdvisor api.SecurityAdvisor, mcs api.MessageCryptoService, identity api.PeerIdentityType) Gossip {
172-
return NewGossipService(conf, nil, secAdvisor, mcs, identity)
171+
func NewGossipServiceWithServer(conf *Config, secAdvisor api.SecurityAdvisor, mcs api.MessageCryptoService, mapper identity.Mapper, identity api.PeerIdentityType) Gossip {
172+
return NewGossipService(conf, nil, secAdvisor, mcs, mapper, identity)
173173
}
174174

175175
func createCommWithServer(port int, idStore identity.Mapper, identity api.PeerIdentityType) (comm.Comm, error) {

gossip/gossip/gossip_test.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/hyperledger/fabric/gossip/common"
3232
"github.com/hyperledger/fabric/gossip/discovery"
3333
"github.com/hyperledger/fabric/gossip/gossip/algo"
34+
"github.com/hyperledger/fabric/gossip/identity"
3435
"github.com/hyperledger/fabric/gossip/util"
3536
proto "github.com/hyperledger/fabric/protos/gossip"
3637
"github.com/stretchr/testify/assert"
@@ -169,7 +170,11 @@ func newGossipInstance(portPrefix int, id int, maxMsgCount int, boot ...int) Gos
169170
PublishStateInfoInterval: time.Duration(1) * time.Second,
170171
RequestStateInfoInterval: time.Duration(1) * time.Second,
171172
}
172-
g := NewGossipServiceWithServer(conf, &orgCryptoService{}, &naiveCryptoService{}, api.PeerIdentityType(conf.InternalEndpoint))
173+
cryptoService := &naiveCryptoService{}
174+
idMapper := identity.NewIdentityMapper(cryptoService)
175+
176+
g := NewGossipServiceWithServer(conf, &orgCryptoService{}, cryptoService, idMapper, api.PeerIdentityType(conf.InternalEndpoint))
177+
173178
return g
174179
}
175180

@@ -192,7 +197,11 @@ func newGossipInstanceWithOnlyPull(portPrefix int, id int, maxMsgCount int, boot
192197
PublishStateInfoInterval: time.Duration(1) * time.Second,
193198
RequestStateInfoInterval: time.Duration(1) * time.Second,
194199
}
195-
g := NewGossipServiceWithServer(conf, &orgCryptoService{}, &naiveCryptoService{}, api.PeerIdentityType(conf.InternalEndpoint))
200+
201+
cryptoService := &naiveCryptoService{}
202+
idMapper := identity.NewIdentityMapper(cryptoService)
203+
204+
g := NewGossipServiceWithServer(conf, &orgCryptoService{}, cryptoService, idMapper, api.PeerIdentityType(conf.InternalEndpoint))
196205
return g
197206
}
198207

gossip/integration/integration.go

+6-49
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323
"time"
2424

2525
"github.com/hyperledger/fabric/gossip/api"
26-
"github.com/hyperledger/fabric/gossip/common"
2726
"github.com/hyperledger/fabric/gossip/gossip"
28-
"github.com/hyperledger/fabric/peer/gossip/mcs"
29-
"github.com/hyperledger/fabric/peer/gossip/sa"
27+
"github.com/hyperledger/fabric/gossip/identity"
3028
"github.com/spf13/viper"
3129
"google.golang.org/grpc"
3230
)
3331

32+
// This file is used to bootstrap a gossip instance and/or leader election service instance
33+
3434
func getIntOrDefault(key string, defVal int) int {
3535
if viper.GetInt(key) == 0 {
3636
return defVal
@@ -83,55 +83,12 @@ func newConfig(selfEndpoint string, externalEndpoint string, bootPeers ...string
8383
}
8484

8585
// NewGossipComponent creates a gossip component that attaches itself to the given gRPC server
86-
func NewGossipComponent(identity []byte, endpoint string, s *grpc.Server, dialOpts []grpc.DialOption, bootPeers ...string) gossip.Gossip {
87-
if overrideEndpoint := viper.GetString("peer.gossip.endpoint"); overrideEndpoint != "" {
88-
endpoint = overrideEndpoint
89-
}
86+
func NewGossipComponent(peerIdentity []byte, endpoint string, s *grpc.Server, secAdv api.SecurityAdvisor, cryptSvc api.MessageCryptoService, idMapper identity.Mapper, dialOpts []grpc.DialOption, bootPeers ...string) gossip.Gossip {
9087

9188
externalEndpoint := viper.GetString("peer.gossip.externalEndpoint")
9289

9390
conf := newConfig(endpoint, externalEndpoint, bootPeers...)
94-
cryptSvc := mcs.NewMessageCryptoService()
95-
secAdv := sa.NewSecurityAdvisor()
96-
97-
if viper.GetBool("peer.gossip.ignoreSecurity") {
98-
sec := &secImpl{[]byte(endpoint)}
99-
cryptSvc = sec
100-
secAdv = sec
101-
identity = []byte(endpoint)
102-
}
103-
104-
return gossip.NewGossipService(conf, s, secAdv, cryptSvc, identity, dialOpts...)
105-
}
106-
107-
type secImpl struct {
108-
identity []byte
109-
}
110-
111-
func (*secImpl) OrgByPeerIdentity(api.PeerIdentityType) api.OrgIdentityType {
112-
return api.OrgIdentityType("DEFAULT")
113-
}
114-
115-
func (s *secImpl) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType {
116-
return common.PKIidType(peerIdentity)
117-
}
118-
119-
func (s *secImpl) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error {
120-
return nil
121-
}
122-
123-
func (s *secImpl) Sign(msg []byte) ([]byte, error) {
124-
return msg, nil
125-
}
126-
127-
func (s *secImpl) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error {
128-
return nil
129-
}
130-
131-
func (s *secImpl) VerifyByChannel(chainID common.ChainID, peerIdentity api.PeerIdentityType, signature, message []byte) error {
132-
return nil
133-
}
91+
gossipInstance := gossip.NewGossipService(conf, s, secAdv, cryptSvc, idMapper, peerIdentity, dialOpts...)
13492

135-
func (s *secImpl) ValidateIdentity(peerIdentity api.PeerIdentityType) error {
136-
return nil
93+
return gossipInstance
13794
}

gossip/integration/integration_test.go

+46-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323
"testing"
2424
"time"
2525

26+
"github.com/hyperledger/fabric/gossip/api"
27+
"github.com/hyperledger/fabric/gossip/common"
28+
"github.com/hyperledger/fabric/gossip/identity"
2629
"github.com/hyperledger/fabric/msp/mgmt"
2730
"github.com/spf13/viper"
2831
"google.golang.org/grpc"
@@ -44,11 +47,16 @@ func TestNewGossipCryptoService(t *testing.T) {
4447
endpoint3 := "localhost:5613"
4548

4649
mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../msp/sampleconfig")
47-
identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
50+
peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
4851

49-
g1 := NewGossipComponent(identity, endpoint1, s1, []grpc.DialOption{grpc.WithInsecure()})
50-
g2 := NewGossipComponent(identity, endpoint2, s2, []grpc.DialOption{grpc.WithInsecure()}, endpoint1)
51-
g3 := NewGossipComponent(identity, endpoint3, s3, []grpc.DialOption{grpc.WithInsecure()}, endpoint1)
52+
cryptSvc := &cryptoService{}
53+
secAdv := &secAdviser{}
54+
55+
idMapper := identity.NewIdentityMapper(cryptSvc)
56+
57+
g1 := NewGossipComponent(peerIdentity, endpoint1, s1, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()})
58+
g2 := NewGossipComponent(peerIdentity, endpoint2, s2, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()}, endpoint1)
59+
g3 := NewGossipComponent(peerIdentity, endpoint3, s3, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()}, endpoint1)
5260
go s1.Serve(ll1)
5361
go s2.Serve(ll2)
5462
go s3.Serve(ll3)
@@ -71,3 +79,37 @@ func setupTestEnv() {
7179
panic(fmt.Errorf("Fatal error config file: %s \n", err))
7280
}
7381
}
82+
83+
type secAdviser struct {
84+
}
85+
86+
func (sa *secAdviser) OrgByPeerIdentity(api.PeerIdentityType) api.OrgIdentityType {
87+
return api.OrgIdentityType("DEFAULT")
88+
}
89+
90+
type cryptoService struct {
91+
}
92+
93+
func (s *cryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType {
94+
return common.PKIidType(peerIdentity)
95+
}
96+
97+
func (s *cryptoService) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error {
98+
return nil
99+
}
100+
101+
func (s *cryptoService) Sign(msg []byte) ([]byte, error) {
102+
return msg, nil
103+
}
104+
105+
func (s *cryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error {
106+
return nil
107+
}
108+
109+
func (s *cryptoService) VerifyByChannel(chainID common.ChainID, peerIdentity api.PeerIdentityType, signature, message []byte) error {
110+
return nil
111+
}
112+
113+
func (s *cryptoService) ValidateIdentity(peerIdentity api.PeerIdentityType) error {
114+
return nil
115+
}

gossip/service/gossip_service.go

+60-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ import (
2525
"github.com/hyperledger/fabric/gossip/api"
2626
gossipCommon "github.com/hyperledger/fabric/gossip/common"
2727
"github.com/hyperledger/fabric/gossip/gossip"
28+
"github.com/hyperledger/fabric/gossip/identity"
2829
"github.com/hyperledger/fabric/gossip/integration"
2930
"github.com/hyperledger/fabric/gossip/state"
3031
"github.com/hyperledger/fabric/gossip/util"
32+
"github.com/hyperledger/fabric/peer/gossip/mcs"
33+
"github.com/hyperledger/fabric/peer/gossip/sa"
3134
"github.com/hyperledger/fabric/protos/common"
3235
proto "github.com/hyperledger/fabric/protos/gossip"
36+
"github.com/spf13/viper"
3337
"google.golang.org/grpc"
3438
)
3539

@@ -74,6 +78,8 @@ type gossipServiceImpl struct {
7478
deliveryService deliverclient.DeliverService
7579
deliveryFactory DeliveryServiceFactory
7680
lock sync.RWMutex
81+
msgCrypto identity.Mapper
82+
peerIdentity []byte
7783
}
7884

7985
// This is an implementation of api.JoinChannelMessage.
@@ -93,13 +99,13 @@ func (jcm *joinChannelMessage) AnchorPeers() []api.AnchorPeer {
9399
var logger = util.GetLogger(util.LoggingServiceModule, "")
94100

95101
// InitGossipService initialize gossip service
96-
func InitGossipService(identity []byte, endpoint string, s *grpc.Server, bootPeers ...string) {
97-
InitGossipServiceCustomDeliveryFactory(identity, endpoint, s, &deliveryFactoryImpl{}, bootPeers...)
102+
func InitGossipService(peerIdentity []byte, endpoint string, s *grpc.Server, bootPeers ...string) {
103+
InitGossipServiceCustomDeliveryFactory(peerIdentity, endpoint, s, &deliveryFactoryImpl{}, bootPeers...)
98104
}
99105

100106
// InitGossipService initialize gossip service with customize delivery factory
101107
// implementation, might be useful for testing and mocking purposes
102-
func InitGossipServiceCustomDeliveryFactory(identity []byte, endpoint string, s *grpc.Server, factory DeliveryServiceFactory, bootPeers ...string) {
108+
func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string, s *grpc.Server, factory DeliveryServiceFactory, bootPeers ...string) {
103109
once.Do(func() {
104110
logger.Info("Initialize gossip with endpoint", endpoint, "and bootstrap set", bootPeers)
105111
dialOpts := []grpc.DialOption{}
@@ -109,11 +115,29 @@ func InitGossipServiceCustomDeliveryFactory(identity []byte, endpoint string, s
109115
dialOpts = append(dialOpts, grpc.WithInsecure())
110116
}
111117

112-
gossip := integration.NewGossipComponent(identity, endpoint, s, dialOpts, bootPeers...)
118+
cryptSvc := mcs.NewMessageCryptoService()
119+
secAdv := sa.NewSecurityAdvisor()
120+
121+
if overrideEndpoint := viper.GetString("peer.gossip.endpoint"); overrideEndpoint != "" {
122+
endpoint = overrideEndpoint
123+
}
124+
125+
if viper.GetBool("peer.gossip.ignoreSecurity") {
126+
sec := &secImpl{[]byte(endpoint)}
127+
cryptSvc = sec
128+
secAdv = sec
129+
peerIdentity = []byte(endpoint)
130+
}
131+
132+
idMapper := identity.NewIdentityMapper(cryptSvc)
133+
134+
gossip := integration.NewGossipComponent(peerIdentity, endpoint, s, secAdv, cryptSvc, idMapper, dialOpts, bootPeers...)
113135
gossipServiceInstance = &gossipServiceImpl{
114136
gossipSvc: gossip,
115137
chains: make(map[string]state.GossipStateProvider),
116138
deliveryFactory: factory,
139+
msgCrypto: idMapper,
140+
peerIdentity: peerIdentity,
117141
}
118142
})
119143
}
@@ -196,3 +220,35 @@ func (g *gossipServiceImpl) Stop() {
196220
g.deliveryService.Stop()
197221
}
198222
}
223+
224+
type secImpl struct {
225+
identity []byte
226+
}
227+
228+
func (*secImpl) OrgByPeerIdentity(api.PeerIdentityType) api.OrgIdentityType {
229+
return api.OrgIdentityType("DEFAULT")
230+
}
231+
232+
func (s *secImpl) GetPKIidOfCert(peerIdentity api.PeerIdentityType) gossipCommon.PKIidType {
233+
return gossipCommon.PKIidType(peerIdentity)
234+
}
235+
236+
func (s *secImpl) VerifyBlock(chainID gossipCommon.ChainID, signedBlock api.SignedBlock) error {
237+
return nil
238+
}
239+
240+
func (s *secImpl) Sign(msg []byte) ([]byte, error) {
241+
return msg, nil
242+
}
243+
244+
func (s *secImpl) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error {
245+
return nil
246+
}
247+
248+
func (s *secImpl) VerifyByChannel(chainID gossipCommon.ChainID, peerIdentity api.PeerIdentityType, signature, message []byte) error {
249+
return nil
250+
}
251+
252+
func (s *secImpl) ValidateIdentity(peerIdentity api.PeerIdentityType) error {
253+
return nil
254+
}

gossip/state/state_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/hyperledger/fabric/gossip/comm"
3535
"github.com/hyperledger/fabric/gossip/common"
3636
"github.com/hyperledger/fabric/gossip/gossip"
37+
"github.com/hyperledger/fabric/gossip/identity"
3738
gossipUtil "github.com/hyperledger/fabric/gossip/util"
3839
pcomm "github.com/hyperledger/fabric/protos/common"
3940
proto "github.com/hyperledger/fabric/protos/gossip"
@@ -167,7 +168,10 @@ func newGossipConfig(id int, maxMsgCount int, boot ...int) *gossip.Config {
167168

168169
// Create gossip instance
169170
func newGossipInstance(config *gossip.Config) gossip.Gossip {
170-
return gossip.NewGossipServiceWithServer(config, &orgCryptoService{}, &naiveCryptoService{}, []byte(config.InternalEndpoint))
171+
cryptoService := &naiveCryptoService{}
172+
idMapper := identity.NewIdentityMapper(cryptoService)
173+
174+
return gossip.NewGossipServiceWithServer(config, &orgCryptoService{}, cryptoService, idMapper, []byte(config.InternalEndpoint))
171175
}
172176

173177
// Create new instance of KVLedger to be used for testing

0 commit comments

Comments
 (0)