Skip to content

Commit

Permalink
FAB-1846 Storing election config in gossip service
Browse files Browse the repository at this point in the history
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 <gennady@il.ibm.com>
  • Loading branch information
gennadylaventman committed Feb 12, 2017
1 parent c341fe5 commit b9f89e7
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 64 deletions.
8 changes: 4 additions & 4 deletions gossip/gossip/gossip_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions gossip/gossip/gossip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
55 changes: 6 additions & 49 deletions gossip/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
50 changes: 46 additions & 4 deletions gossip/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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
}
64 changes: 60 additions & 4 deletions gossip/service/gossip_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.
Expand All @@ -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{}
Expand All @@ -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,
}
})
}
Expand Down Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion gossip/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b9f89e7

Please sign in to comment.