From b9f89e7d46c2874d179ee536245febb4ac8b4a22 Mon Sep 17 00:00:00 2001 From: Gennady Laventman Date: Wed, 1 Feb 2017 17:37:33 +0200 Subject: [PATCH] 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 --- gossip/gossip/gossip_impl.go | 8 ++-- gossip/gossip/gossip_test.go | 13 +++++- gossip/integration/integration.go | 55 +++------------------- gossip/integration/integration_test.go | 50 ++++++++++++++++++-- gossip/service/gossip_service.go | 64 ++++++++++++++++++++++++-- gossip/state/state_test.go | 6 ++- 6 files changed, 132 insertions(+), 64 deletions(-) diff --git a/gossip/gossip/gossip_impl.go b/gossip/gossip/gossip_impl.go index a3567631010..dffba09caa7 100644 --- a/gossip/gossip/gossip_impl.go +++ b/gossip/gossip/gossip_impl.go @@ -74,10 +74,10 @@ type gossipServiceImpl struct { } // NewGossipService creates a gossip instance attached to a gRPC server -func NewGossipService(conf *Config, s *grpc.Server, secAdvisor api.SecurityAdvisor, mcs api.MessageCryptoService, selfIdentity api.PeerIdentityType, dialOpts ...grpc.DialOption) Gossip { +func NewGossipService(conf *Config, s *grpc.Server, secAdvisor api.SecurityAdvisor, mcs api.MessageCryptoService, idMapper identity.Mapper, selfIdentity api.PeerIdentityType, dialOpts ...grpc.DialOption) Gossip { var c comm.Comm var err error - idMapper := identity.NewIdentityMapper(mcs) + lgr := util.GetLogger(util.LoggingGossipModule, conf.ID) if s == nil { c, err = createCommWithServer(conf.BindPort, idMapper, selfIdentity) @@ -168,8 +168,8 @@ func createCommWithoutServer(s *grpc.Server, cert *tls.Certificate, idStore iden } // NewGossipServiceWithServer creates a new gossip instance with a gRPC server -func NewGossipServiceWithServer(conf *Config, secAdvisor api.SecurityAdvisor, mcs api.MessageCryptoService, identity api.PeerIdentityType) Gossip { - return NewGossipService(conf, nil, secAdvisor, mcs, identity) +func NewGossipServiceWithServer(conf *Config, secAdvisor api.SecurityAdvisor, mcs api.MessageCryptoService, mapper identity.Mapper, identity api.PeerIdentityType) Gossip { + return NewGossipService(conf, nil, secAdvisor, mcs, mapper, identity) } func createCommWithServer(port int, idStore identity.Mapper, identity api.PeerIdentityType) (comm.Comm, error) { diff --git a/gossip/gossip/gossip_test.go b/gossip/gossip/gossip_test.go index 26dc2dec4af..c5cb23fa154 100644 --- a/gossip/gossip/gossip_test.go +++ b/gossip/gossip/gossip_test.go @@ -31,6 +31,7 @@ import ( "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/discovery" "github.com/hyperledger/fabric/gossip/gossip/algo" + "github.com/hyperledger/fabric/gossip/identity" "github.com/hyperledger/fabric/gossip/util" proto "github.com/hyperledger/fabric/protos/gossip" "github.com/stretchr/testify/assert" @@ -169,7 +170,11 @@ func newGossipInstance(portPrefix int, id int, maxMsgCount int, boot ...int) Gos PublishStateInfoInterval: time.Duration(1) * time.Second, RequestStateInfoInterval: time.Duration(1) * time.Second, } - g := NewGossipServiceWithServer(conf, &orgCryptoService{}, &naiveCryptoService{}, api.PeerIdentityType(conf.InternalEndpoint)) + cryptoService := &naiveCryptoService{} + idMapper := identity.NewIdentityMapper(cryptoService) + + g := NewGossipServiceWithServer(conf, &orgCryptoService{}, cryptoService, idMapper, api.PeerIdentityType(conf.InternalEndpoint)) + return g } @@ -192,7 +197,11 @@ func newGossipInstanceWithOnlyPull(portPrefix int, id int, maxMsgCount int, boot PublishStateInfoInterval: time.Duration(1) * time.Second, RequestStateInfoInterval: time.Duration(1) * time.Second, } - g := NewGossipServiceWithServer(conf, &orgCryptoService{}, &naiveCryptoService{}, api.PeerIdentityType(conf.InternalEndpoint)) + + cryptoService := &naiveCryptoService{} + idMapper := identity.NewIdentityMapper(cryptoService) + + g := NewGossipServiceWithServer(conf, &orgCryptoService{}, cryptoService, idMapper, api.PeerIdentityType(conf.InternalEndpoint)) return g } diff --git a/gossip/integration/integration.go b/gossip/integration/integration.go index f067ecbd1de..0f3567f2450 100644 --- a/gossip/integration/integration.go +++ b/gossip/integration/integration.go @@ -23,14 +23,14 @@ import ( "time" "github.com/hyperledger/fabric/gossip/api" - "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/gossip" - "github.com/hyperledger/fabric/peer/gossip/mcs" - "github.com/hyperledger/fabric/peer/gossip/sa" + "github.com/hyperledger/fabric/gossip/identity" "github.com/spf13/viper" "google.golang.org/grpc" ) +// This file is used to bootstrap a gossip instance and/or leader election service instance + func getIntOrDefault(key string, defVal int) int { if viper.GetInt(key) == 0 { return defVal @@ -83,55 +83,12 @@ func newConfig(selfEndpoint string, externalEndpoint string, bootPeers ...string } // NewGossipComponent creates a gossip component that attaches itself to the given gRPC server -func NewGossipComponent(identity []byte, endpoint string, s *grpc.Server, dialOpts []grpc.DialOption, bootPeers ...string) gossip.Gossip { - if overrideEndpoint := viper.GetString("peer.gossip.endpoint"); overrideEndpoint != "" { - endpoint = overrideEndpoint - } +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 { externalEndpoint := viper.GetString("peer.gossip.externalEndpoint") conf := newConfig(endpoint, externalEndpoint, bootPeers...) - cryptSvc := mcs.NewMessageCryptoService() - secAdv := sa.NewSecurityAdvisor() - - if viper.GetBool("peer.gossip.ignoreSecurity") { - sec := &secImpl{[]byte(endpoint)} - cryptSvc = sec - secAdv = sec - identity = []byte(endpoint) - } - - return gossip.NewGossipService(conf, s, secAdv, cryptSvc, identity, dialOpts...) -} - -type secImpl struct { - identity []byte -} - -func (*secImpl) OrgByPeerIdentity(api.PeerIdentityType) api.OrgIdentityType { - return api.OrgIdentityType("DEFAULT") -} - -func (s *secImpl) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType { - return common.PKIidType(peerIdentity) -} - -func (s *secImpl) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error { - return nil -} - -func (s *secImpl) Sign(msg []byte) ([]byte, error) { - return msg, nil -} - -func (s *secImpl) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error { - return nil -} - -func (s *secImpl) VerifyByChannel(chainID common.ChainID, peerIdentity api.PeerIdentityType, signature, message []byte) error { - return nil -} + gossipInstance := gossip.NewGossipService(conf, s, secAdv, cryptSvc, idMapper, peerIdentity, dialOpts...) -func (s *secImpl) ValidateIdentity(peerIdentity api.PeerIdentityType) error { - return nil + return gossipInstance } diff --git a/gossip/integration/integration_test.go b/gossip/integration/integration_test.go index 5245e47b837..31e69a27bb6 100644 --- a/gossip/integration/integration_test.go +++ b/gossip/integration/integration_test.go @@ -23,6 +23,9 @@ import ( "testing" "time" + "github.com/hyperledger/fabric/gossip/api" + "github.com/hyperledger/fabric/gossip/common" + "github.com/hyperledger/fabric/gossip/identity" "github.com/hyperledger/fabric/msp/mgmt" "github.com/spf13/viper" "google.golang.org/grpc" @@ -44,11 +47,16 @@ func TestNewGossipCryptoService(t *testing.T) { endpoint3 := "localhost:5613" mgmt.LoadFakeSetupWithLocalMspAndTestChainMsp("../../msp/sampleconfig") - identity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize() + peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize() - g1 := NewGossipComponent(identity, endpoint1, s1, []grpc.DialOption{grpc.WithInsecure()}) - g2 := NewGossipComponent(identity, endpoint2, s2, []grpc.DialOption{grpc.WithInsecure()}, endpoint1) - g3 := NewGossipComponent(identity, endpoint3, s3, []grpc.DialOption{grpc.WithInsecure()}, endpoint1) + cryptSvc := &cryptoService{} + secAdv := &secAdviser{} + + idMapper := identity.NewIdentityMapper(cryptSvc) + + g1 := NewGossipComponent(peerIdentity, endpoint1, s1, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()}) + g2 := NewGossipComponent(peerIdentity, endpoint2, s2, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()}, endpoint1) + g3 := NewGossipComponent(peerIdentity, endpoint3, s3, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()}, endpoint1) go s1.Serve(ll1) go s2.Serve(ll2) go s3.Serve(ll3) @@ -71,3 +79,37 @@ func setupTestEnv() { panic(fmt.Errorf("Fatal error config file: %s \n", err)) } } + +type secAdviser struct { +} + +func (sa *secAdviser) OrgByPeerIdentity(api.PeerIdentityType) api.OrgIdentityType { + return api.OrgIdentityType("DEFAULT") +} + +type cryptoService struct { +} + +func (s *cryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType { + return common.PKIidType(peerIdentity) +} + +func (s *cryptoService) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error { + return nil +} + +func (s *cryptoService) Sign(msg []byte) ([]byte, error) { + return msg, nil +} + +func (s *cryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error { + return nil +} + +func (s *cryptoService) VerifyByChannel(chainID common.ChainID, peerIdentity api.PeerIdentityType, signature, message []byte) error { + return nil +} + +func (s *cryptoService) ValidateIdentity(peerIdentity api.PeerIdentityType) error { + return nil +} diff --git a/gossip/service/gossip_service.go b/gossip/service/gossip_service.go index 1ce9e72a8bc..6523e6a78b2 100644 --- a/gossip/service/gossip_service.go +++ b/gossip/service/gossip_service.go @@ -25,11 +25,15 @@ import ( "github.com/hyperledger/fabric/gossip/api" gossipCommon "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/gossip" + "github.com/hyperledger/fabric/gossip/identity" "github.com/hyperledger/fabric/gossip/integration" "github.com/hyperledger/fabric/gossip/state" "github.com/hyperledger/fabric/gossip/util" + "github.com/hyperledger/fabric/peer/gossip/mcs" + "github.com/hyperledger/fabric/peer/gossip/sa" "github.com/hyperledger/fabric/protos/common" proto "github.com/hyperledger/fabric/protos/gossip" + "github.com/spf13/viper" "google.golang.org/grpc" ) @@ -74,6 +78,8 @@ type gossipServiceImpl struct { deliveryService deliverclient.DeliverService deliveryFactory DeliveryServiceFactory lock sync.RWMutex + msgCrypto identity.Mapper + peerIdentity []byte } // This is an implementation of api.JoinChannelMessage. @@ -93,13 +99,13 @@ func (jcm *joinChannelMessage) AnchorPeers() []api.AnchorPeer { var logger = util.GetLogger(util.LoggingServiceModule, "") // InitGossipService initialize gossip service -func InitGossipService(identity []byte, endpoint string, s *grpc.Server, bootPeers ...string) { - InitGossipServiceCustomDeliveryFactory(identity, endpoint, s, &deliveryFactoryImpl{}, bootPeers...) +func InitGossipService(peerIdentity []byte, endpoint string, s *grpc.Server, bootPeers ...string) { + InitGossipServiceCustomDeliveryFactory(peerIdentity, endpoint, s, &deliveryFactoryImpl{}, bootPeers...) } // InitGossipService initialize gossip service with customize delivery factory // implementation, might be useful for testing and mocking purposes -func InitGossipServiceCustomDeliveryFactory(identity []byte, endpoint string, s *grpc.Server, factory DeliveryServiceFactory, bootPeers ...string) { +func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string, s *grpc.Server, factory DeliveryServiceFactory, bootPeers ...string) { once.Do(func() { logger.Info("Initialize gossip with endpoint", endpoint, "and bootstrap set", bootPeers) dialOpts := []grpc.DialOption{} @@ -109,11 +115,29 @@ func InitGossipServiceCustomDeliveryFactory(identity []byte, endpoint string, s dialOpts = append(dialOpts, grpc.WithInsecure()) } - gossip := integration.NewGossipComponent(identity, endpoint, s, dialOpts, bootPeers...) + cryptSvc := mcs.NewMessageCryptoService() + secAdv := sa.NewSecurityAdvisor() + + if overrideEndpoint := viper.GetString("peer.gossip.endpoint"); overrideEndpoint != "" { + endpoint = overrideEndpoint + } + + if viper.GetBool("peer.gossip.ignoreSecurity") { + sec := &secImpl{[]byte(endpoint)} + cryptSvc = sec + secAdv = sec + peerIdentity = []byte(endpoint) + } + + idMapper := identity.NewIdentityMapper(cryptSvc) + + gossip := integration.NewGossipComponent(peerIdentity, endpoint, s, secAdv, cryptSvc, idMapper, dialOpts, bootPeers...) gossipServiceInstance = &gossipServiceImpl{ gossipSvc: gossip, chains: make(map[string]state.GossipStateProvider), deliveryFactory: factory, + msgCrypto: idMapper, + peerIdentity: peerIdentity, } }) } @@ -196,3 +220,35 @@ func (g *gossipServiceImpl) Stop() { g.deliveryService.Stop() } } + +type secImpl struct { + identity []byte +} + +func (*secImpl) OrgByPeerIdentity(api.PeerIdentityType) api.OrgIdentityType { + return api.OrgIdentityType("DEFAULT") +} + +func (s *secImpl) GetPKIidOfCert(peerIdentity api.PeerIdentityType) gossipCommon.PKIidType { + return gossipCommon.PKIidType(peerIdentity) +} + +func (s *secImpl) VerifyBlock(chainID gossipCommon.ChainID, signedBlock api.SignedBlock) error { + return nil +} + +func (s *secImpl) Sign(msg []byte) ([]byte, error) { + return msg, nil +} + +func (s *secImpl) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error { + return nil +} + +func (s *secImpl) VerifyByChannel(chainID gossipCommon.ChainID, peerIdentity api.PeerIdentityType, signature, message []byte) error { + return nil +} + +func (s *secImpl) ValidateIdentity(peerIdentity api.PeerIdentityType) error { + return nil +} diff --git a/gossip/state/state_test.go b/gossip/state/state_test.go index dd3ca8ba435..8acb318db6a 100644 --- a/gossip/state/state_test.go +++ b/gossip/state/state_test.go @@ -34,6 +34,7 @@ import ( "github.com/hyperledger/fabric/gossip/comm" "github.com/hyperledger/fabric/gossip/common" "github.com/hyperledger/fabric/gossip/gossip" + "github.com/hyperledger/fabric/gossip/identity" gossipUtil "github.com/hyperledger/fabric/gossip/util" pcomm "github.com/hyperledger/fabric/protos/common" proto "github.com/hyperledger/fabric/protos/gossip" @@ -167,7 +168,10 @@ func newGossipConfig(id int, maxMsgCount int, boot ...int) *gossip.Config { // Create gossip instance func newGossipInstance(config *gossip.Config) gossip.Gossip { - return gossip.NewGossipServiceWithServer(config, &orgCryptoService{}, &naiveCryptoService{}, []byte(config.InternalEndpoint)) + cryptoService := &naiveCryptoService{} + idMapper := identity.NewIdentityMapper(cryptoService) + + return gossip.NewGossipServiceWithServer(config, &orgCryptoService{}, cryptoService, idMapper, []byte(config.InternalEndpoint)) } // Create new instance of KVLedger to be used for testing