1 Star 1 Fork 0

Hyperledger Fabric 国密 / fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gossip_service.go 19.96 KB
一键复制 编辑 原始数据 按行查看 历史
Jtyoui 提交于 2021-07-22 15:59 . 国密
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package service
import (
"fmt"
"sync"
"gitee.com/hyperledger-fabric-gm/fabric/common/metrics"
"gitee.com/hyperledger-fabric-gm/fabric/core/comm"
"gitee.com/hyperledger-fabric-gm/fabric/core/committer"
"gitee.com/hyperledger-fabric-gm/fabric/core/committer/txvalidator"
"gitee.com/hyperledger-fabric-gm/fabric/core/common/privdata"
deliverclient "gitee.com/hyperledger-fabric-gm/fabric/core/deliverservice"
"gitee.com/hyperledger-fabric-gm/fabric/core/deliverservice/blocksprovider"
"gitee.com/hyperledger-fabric-gm/fabric/gossip/api"
gossipCommon "gitee.com/hyperledger-fabric-gm/fabric/gossip/common"
"gitee.com/hyperledger-fabric-gm/fabric/gossip/election"
"gitee.com/hyperledger-fabric-gm/fabric/gossip/gossip"
"gitee.com/hyperledger-fabric-gm/fabric/gossip/integration"
gossipMetrics "gitee.com/hyperledger-fabric-gm/fabric/gossip/metrics"
privdata2 "gitee.com/hyperledger-fabric-gm/fabric/gossip/privdata"
"gitee.com/hyperledger-fabric-gm/fabric/gossip/state"
"gitee.com/hyperledger-fabric-gm/fabric/gossip/util"
"gitee.com/hyperledger-fabric-gm/fabric/protos/common"
gproto "gitee.com/hyperledger-fabric-gm/fabric/protos/gossip"
"gitee.com/hyperledger-fabric-gm/fabric/protos/transientstore"
"github.com/pkg/errors"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
var (
gossipServiceInstance *gossipServiceImpl
once sync.Once
)
type gossipSvc gossip.Gossip
// GossipService encapsulates gossip and state capabilities into single interface
type GossipService interface {
gossip.Gossip
// DistributePrivateData distributes private data to the peers in the collections
// according to policies induced by the PolicyStore and PolicyParser
DistributePrivateData(chainID string, txID string, privateData *transientstore.TxPvtReadWriteSetWithConfigInfo, blkHt uint64) error
// NewConfigEventer creates a ConfigProcessor which the channelconfig.BundleSource can ultimately route config updates to
NewConfigEventer() ConfigProcessor
// InitializeChannel allocates the state provider and should be invoked once per channel per execution
InitializeChannel(chainID string, oac OrdererAddressConfig, support Support)
// AddPayload appends message payload to for given chain
AddPayload(chainID string, payload *gproto.Payload) error
}
// DeliveryServiceFactory factory to create and initialize delivery service instance
type DeliveryServiceFactory interface {
// Returns an instance of delivery client
Service(g GossipService, oac OrdererAddressConfig, msc api.MessageCryptoService) (deliverclient.DeliverService, error)
}
type deliveryFactoryImpl struct {
isStaticLeader bool
}
// Returns an instance of delivery client
func (d *deliveryFactoryImpl) Service(g GossipService, ec OrdererAddressConfig, mcs api.MessageCryptoService) (deliverclient.DeliverService, error) {
return deliverclient.NewDeliverService(&deliverclient.Config{
IsStaticLeader: d.isStaticLeader,
CryptoSvc: mcs,
Gossip: g,
ConnFactory: deliverclient.DefaultConnectionFactory,
ABCFactory: deliverclient.DefaultABCFactory,
}, deliverclient.ConnectionCriteria{
OrdererEndpointsByOrg: ec.AddressesByOrg,
Organizations: ec.Organizations,
OrdererEndpoints: ec.Addresses,
OrdererEndpointOverrides: ec.AddressOverrides,
})
}
// OrdererAddressConfig defines the addresses of the ordering service nodes
type OrdererAddressConfig struct {
Addresses []string
AddressesByOrg map[string][]string
Organizations []string
AddressOverrides map[string]*comm.OrdererEndpoint
}
type privateHandler struct {
support Support
coordinator privdata2.Coordinator
distributor privdata2.PvtDataDistributor
reconciler privdata2.PvtDataReconciler
}
func (p privateHandler) close() {
p.coordinator.Close()
p.reconciler.Stop()
}
type gossipServiceImpl struct {
gossipSvc
privateHandlers map[string]privateHandler
chains map[string]state.GossipStateProvider
leaderElection map[string]election.LeaderElectionService
deliveryService map[string]deliverclient.DeliverService
deliveryFactory DeliveryServiceFactory
lock sync.RWMutex
mcs api.MessageCryptoService
peerIdentity []byte
secAdv api.SecurityAdvisor
metrics *gossipMetrics.GossipMetrics
}
// This is an implementation of api.JoinChannelMessage.
type joinChannelMessage struct {
seqNum uint64
members2AnchorPeers map[string][]api.AnchorPeer
}
func (jcm *joinChannelMessage) SequenceNumber() uint64 {
return jcm.seqNum
}
// Members returns the organizations of the channel
func (jcm *joinChannelMessage) Members() []api.OrgIdentityType {
members := make([]api.OrgIdentityType, len(jcm.members2AnchorPeers))
i := 0
for org := range jcm.members2AnchorPeers {
members[i] = api.OrgIdentityType(org)
i++
}
return members
}
// AnchorPeersOf returns the anchor peers of the given organization
func (jcm *joinChannelMessage) AnchorPeersOf(org api.OrgIdentityType) []api.AnchorPeer {
return jcm.members2AnchorPeers[string(org)]
}
var logger = util.GetLogger(util.ServiceLogger, "")
// InitGossipService initialize gossip service
func InitGossipService(
peerIdentity []byte,
metricsProvider metrics.Provider,
endpoint string, s *grpc.Server,
certs *gossipCommon.TLSCertificates,
mcs api.MessageCryptoService,
secAdv api.SecurityAdvisor,
secureDialOpts api.PeerSecureDialOpts,
orgLeader bool,
bootPeers ...string,
) error {
// TODO: Remove this.
// TODO: This is a temporary work-around to make the gossip leader election module load its logger at startup
// TODO: in order for the flogging package to register this logger in time so it can set the log levels as requested in the config
util.GetLogger(util.ElectionLogger, "")
return InitGossipServiceCustomDeliveryFactory(
peerIdentity,
metricsProvider,
endpoint,
s,
certs,
&deliveryFactoryImpl{isStaticLeader: orgLeader},
mcs,
secAdv,
secureDialOpts,
bootPeers...,
)
}
// InitGossipServiceCustomDeliveryFactory initialize gossip service with customize delivery factory
// implementation, might be useful for testing and mocking purposes
func InitGossipServiceCustomDeliveryFactory(
peerIdentity []byte,
metricsProvider metrics.Provider,
endpoint string,
s *grpc.Server,
certs *gossipCommon.TLSCertificates,
factory DeliveryServiceFactory,
mcs api.MessageCryptoService,
secAdv api.SecurityAdvisor,
secureDialOpts api.PeerSecureDialOpts,
bootPeers ...string,
) error {
var err error
var gossip gossip.Gossip
once.Do(func() {
if overrideEndpoint := viper.GetString("peer.gossip.endpoint"); overrideEndpoint != "" {
endpoint = overrideEndpoint
}
logger.Info("Initialize gossip with endpoint", endpoint, "and bootstrap set", bootPeers)
gossipMetrics := gossipMetrics.NewGossipMetrics(metricsProvider)
gossip, err = integration.NewGossipComponent(peerIdentity, endpoint, s, secAdv,
mcs, secureDialOpts, certs, gossipMetrics, bootPeers...)
gossipServiceInstance = &gossipServiceImpl{
mcs: mcs,
gossipSvc: gossip,
privateHandlers: make(map[string]privateHandler),
chains: make(map[string]state.GossipStateProvider),
leaderElection: make(map[string]election.LeaderElectionService),
deliveryService: make(map[string]deliverclient.DeliverService),
deliveryFactory: factory,
peerIdentity: peerIdentity,
secAdv: secAdv,
metrics: gossipMetrics,
}
})
return errors.WithStack(err)
}
// GetGossipService returns an instance of gossip service
func GetGossipService() GossipService {
return gossipServiceInstance
}
// DistributePrivateData distribute private read write set inside the channel based on the collections policies
func (g *gossipServiceImpl) DistributePrivateData(chainID string, txID string, privData *transientstore.TxPvtReadWriteSetWithConfigInfo, blkHt uint64) error {
g.lock.RLock()
handler, exists := g.privateHandlers[chainID]
g.lock.RUnlock()
if !exists {
return errors.Errorf("No private data handler for %s", chainID)
}
if err := handler.distributor.Distribute(txID, privData, blkHt); err != nil {
err := errors.WithMessage(err, fmt.Sprint("failed to distribute private collection, txID ", txID, ", channel %s", chainID))
logger.Error(err)
return err
}
if err := handler.coordinator.StorePvtData(txID, privData, blkHt); err != nil {
logger.Error("Failed to store private data into transient store, txID",
txID, "channel", chainID, "due to", err)
return err
}
return nil
}
// NewConfigEventer creates a ConfigProcessor which the channelconfig.BundleSource can ultimately route config updates to
func (g *gossipServiceImpl) NewConfigEventer() ConfigProcessor {
return newConfigEventer(g)
}
// Support aggregates functionality of several
// interfaces required by gossip service
type Support struct {
Validator txvalidator.Validator
Committer committer.Committer
Store privdata2.TransientStore
Cs privdata.CollectionStore
IdDeserializeFactory privdata2.IdentityDeserializerFactory
CapabilityProvider privdata2.CapabilityProvider
}
// DataStoreSupport aggregates interfaces capable
// of handling either incoming blocks or private data
type DataStoreSupport struct {
committer.Committer
privdata2.TransientStore
}
// InitializeChannel allocates the state provider and should be invoked once per channel per execution
func (g *gossipServiceImpl) InitializeChannel(chainID string, oac OrdererAddressConfig, support Support) {
g.lock.Lock()
defer g.lock.Unlock()
// Initialize new state provider for given committer
logger.Debug("Creating state provider for chainID", chainID)
servicesAdapter := &state.ServicesMediator{GossipAdapter: g, MCSAdapter: g.mcs}
// Embed transient store and committer APIs to fulfill
// DataStore interface to capture ability of retrieving
// private data
storeSupport := &DataStoreSupport{
TransientStore: support.Store,
Committer: support.Committer,
}
// Initialize private data fetcher
dataRetriever := privdata2.NewDataRetriever(storeSupport)
collectionAccessFactory := privdata2.NewCollectionAccessFactory(support.IdDeserializeFactory)
fetcher := privdata2.NewPuller(g.metrics.PrivdataMetrics, support.Cs, g.gossipSvc, dataRetriever,
collectionAccessFactory, chainID, privdata2.GetBtlPullMargin())
coordinatorConfig := privdata2.CoordinatorConfig{
TransientBlockRetention: privdata2.GetTransientBlockRetention(),
PullRetryThreshold: viper.GetDuration("peer.gossip.pvtData.pullRetryThreshold"),
SkipPullingInvalidTransactions: viper.GetBool("peer.gossip.pvtData.skipPullingInvalidTransactionsDuringCommit"),
}
selfSignedData := g.createSelfSignedData()
mspID := string(g.secAdv.OrgByPeerIdentity(selfSignedData.Identity))
coordinator := privdata2.NewCoordinator(mspID, privdata2.Support{
ChainID: chainID,
CollectionStore: support.Cs,
Validator: support.Validator,
TransientStore: support.Store,
Committer: support.Committer,
Fetcher: fetcher,
CapabilityProvider: support.CapabilityProvider,
}, selfSignedData, g.metrics.PrivdataMetrics, coordinatorConfig)
reconcilerConfig := privdata2.GetReconcilerConfig()
var reconciler privdata2.PvtDataReconciler
if reconcilerConfig.IsEnabled {
reconciler = privdata2.NewReconciler(chainID, g.metrics.PrivdataMetrics,
support.Committer, fetcher, reconcilerConfig)
} else {
reconciler = &privdata2.NoOpReconciler{}
}
pushAckTimeout := viper.GetDuration("peer.gossip.pvtData.pushAckTimeout")
g.privateHandlers[chainID] = privateHandler{
support: support,
coordinator: coordinator,
distributor: privdata2.NewDistributor(chainID, g, collectionAccessFactory, g.metrics.PrivdataMetrics, pushAckTimeout),
reconciler: reconciler,
}
g.privateHandlers[chainID].reconciler.Start()
g.chains[chainID] = state.NewGossipStateProvider(chainID, servicesAdapter, coordinator,
g.metrics.StateMetrics, getStateConfiguration())
if g.deliveryService[chainID] == nil {
var err error
g.deliveryService[chainID], err = g.deliveryFactory.Service(g, oac, g.mcs)
if err != nil {
logger.Warningf("Cannot create delivery client, due to %+v", errors.WithStack(err))
}
}
// Delivery service might be nil only if it was not able to get connected
// to the ordering service
if g.deliveryService[chainID] != nil {
// Parameters:
// - peer.gossip.useLeaderElection
// - peer.gossip.orgLeader
//
// are mutual exclusive, setting both to true is not defined, hence
// peer will panic and terminate
leaderElection := viper.GetBool("peer.gossip.useLeaderElection")
isStaticOrgLeader := viper.GetBool("peer.gossip.orgLeader")
if leaderElection && isStaticOrgLeader {
logger.Panic("Setting both orgLeader and useLeaderElection to true isn't supported, aborting execution")
}
if leaderElection {
logger.Debug("Delivery uses dynamic leader election mechanism, channel", chainID)
g.leaderElection[chainID] = g.newLeaderElectionComponent(chainID, g.onStatusChangeFactory(chainID,
support.Committer), g.metrics.ElectionMetrics)
} else if isStaticOrgLeader {
logger.Debug("This peer is configured to connect to ordering service for blocks delivery, channel", chainID)
g.deliveryService[chainID].StartDeliverForChannel(chainID, support.Committer, func() {})
} else {
logger.Debug("This peer is not configured to connect to ordering service for blocks delivery, channel", chainID)
}
} else {
logger.Warning("Delivery client is down won't be able to pull blocks for chain", chainID)
}
}
func (g *gossipServiceImpl) createSelfSignedData() common.SignedData {
msg := make([]byte, 32)
sig, err := g.mcs.Sign(msg)
if err != nil {
logger.Panicf("Failed creating self signed data because message signing failed: %v", err)
}
return common.SignedData{
Data: msg,
Signature: sig,
Identity: g.peerIdentity,
}
}
// updateAnchors constructs a joinChannelMessage and sends it to the gossipSvc
func (g *gossipServiceImpl) updateAnchors(config Config) {
myOrg := string(g.secAdv.OrgByPeerIdentity(api.PeerIdentityType(g.peerIdentity)))
if !g.amIinChannel(myOrg, config) {
logger.Error("Tried joining channel", config.ChainID(), "but our org(", myOrg, "), isn't "+
"among the orgs of the channel:", orgListFromConfig(config), ", aborting.")
return
}
jcm := &joinChannelMessage{seqNum: config.Sequence(), members2AnchorPeers: map[string][]api.AnchorPeer{}}
for _, appOrg := range config.ApplicationOrgs() {
logger.Debug(appOrg.MSPID(), "anchor peers:", appOrg.AnchorPeers())
jcm.members2AnchorPeers[appOrg.MSPID()] = []api.AnchorPeer{}
for _, ap := range appOrg.AnchorPeers() {
anchorPeer := api.AnchorPeer{
Host: ap.Host,
Port: int(ap.Port),
}
jcm.members2AnchorPeers[appOrg.MSPID()] = append(jcm.members2AnchorPeers[appOrg.MSPID()], anchorPeer)
}
}
// Initialize new state provider for given committer
logger.Debug("Creating state provider for chainID", config.ChainID())
g.JoinChan(jcm, gossipCommon.ChainID(config.ChainID()))
}
func (g *gossipServiceImpl) updateEndpoints(chainID string, criteria deliverclient.ConnectionCriteria) {
if ds, ok := g.deliveryService[chainID]; ok {
logger.Debugf("Updating endpoints for chainID", chainID)
if err := ds.UpdateEndpoints(chainID, criteria); err != nil {
// The only reason to fail is because of absence of block provider
// for given channel id, hence printing a warning will be enough
logger.Warningf("Failed to update ordering service endpoints, due to %s", err)
}
}
}
// AddPayload appends message payload to for given chain
func (g *gossipServiceImpl) AddPayload(chainID string, payload *gproto.Payload) error {
g.lock.RLock()
defer g.lock.RUnlock()
return g.chains[chainID].AddPayload(payload)
}
// Stop stops the gossip component
func (g *gossipServiceImpl) Stop() {
g.lock.Lock()
defer g.lock.Unlock()
for chainID := range g.chains {
logger.Info("Stopping chain", chainID)
if le, exists := g.leaderElection[chainID]; exists {
logger.Infof("Stopping leader election for %s", chainID)
le.Stop()
}
g.chains[chainID].Stop()
g.privateHandlers[chainID].close()
if g.deliveryService[chainID] != nil {
g.deliveryService[chainID].Stop()
}
}
g.gossipSvc.Stop()
}
func (g *gossipServiceImpl) newLeaderElectionComponent(chainID string, callback func(bool),
electionMetrics *gossipMetrics.ElectionMetrics) election.LeaderElectionService {
PKIid := g.mcs.GetPKIidOfCert(g.peerIdentity)
adapter := election.NewAdapter(g, PKIid, gossipCommon.ChainID(chainID), electionMetrics)
config := election.ElectionConfig{
StartupGracePeriod: util.GetDurationOrDefault("peer.gossip.election.startupGracePeriod", election.DefStartupGracePeriod),
MembershipSampleInterval: util.GetDurationOrDefault("peer.gossip.election.membershipSampleInterval", election.DefMembershipSampleInterval),
LeaderAliveThreshold: util.GetDurationOrDefault("peer.gossip.election.leaderAliveThreshold", election.DefLeaderAliveThreshold),
LeaderElectionDuration: util.GetDurationOrDefault("peer.gossip.election.leaderElectionDuration", election.DefLeaderElectionDuration),
}
return election.NewLeaderElectionService(adapter, string(PKIid), callback, config)
}
func (g *gossipServiceImpl) amIinChannel(myOrg string, config Config) bool {
for _, orgName := range orgListFromConfig(config) {
if orgName == myOrg {
return true
}
}
return false
}
func (g *gossipServiceImpl) onStatusChangeFactory(chainID string, committer blocksprovider.LedgerInfo) func(bool) {
return func(isLeader bool) {
if isLeader {
yield := func() {
g.lock.RLock()
le := g.leaderElection[chainID]
g.lock.RUnlock()
le.Yield()
}
logger.Info("Elected as a leader, starting delivery service for channel", chainID)
if err := g.deliveryService[chainID].StartDeliverForChannel(chainID, committer, yield); err != nil {
logger.Errorf("Delivery service is not able to start blocks delivery for chain, due to %+v", errors.WithStack(err))
}
} else {
logger.Info("Renounced leadership, stopping delivery service for channel", chainID)
if err := g.deliveryService[chainID].StopDeliverForChannel(chainID); err != nil {
logger.Errorf("Delivery service is not able to stop blocks delivery for chain, due to %+v", errors.WithStack(err))
}
}
}
}
func orgListFromConfig(config Config) []string {
var orgList []string
for _, appOrg := range config.ApplicationOrgs() {
orgList = append(orgList, appOrg.MSPID())
}
return orgList
}
func getStateConfiguration() *state.Configuration {
config := &state.Configuration{
AntiEntropyInterval: state.DefAntiEntropyInterval,
AntiEntropyStateResponseTimeout: state.DefAntiEntropyStateResponseTimeout,
AntiEntropyBatchSize: state.DefAntiEntropyBatchSize,
MaxBlockDistance: state.DefMaxBlockDistance,
AntiEntropyMaxRetries: state.DefAntiEntropyMaxRetries,
ChannelBufferSize: state.DefChannelBufferSize,
EnableStateTransfer: true,
BlockingMode: state.Blocking,
}
if viper.IsSet("peer.gossip.state.checkInterval") {
config.AntiEntropyInterval = viper.GetDuration("peer.gossip.state.checkInterval")
}
if viper.IsSet("peer.gossip.state.responseTimeout") {
config.AntiEntropyStateResponseTimeout = viper.GetDuration("peer.gossip.state.responseTimeout")
}
if viper.IsSet("peer.gossip.state.batchSize") {
config.AntiEntropyBatchSize = uint64(viper.GetInt("peer.gossip.state.batchSize"))
}
if viper.IsSet("peer.gossip.state.blockBufferSize") {
config.MaxBlockDistance = viper.GetInt("peer.gossip.state.blockBufferSize")
}
if viper.IsSet("peer.gossip.state.maxRetries") {
config.AntiEntropyMaxRetries = viper.GetInt("peer.gossip.state.maxRetries")
}
if viper.IsSet("peer.gossip.state.channelSize") {
config.ChannelBufferSize = viper.GetInt("peer.gossip.state.channelSize")
}
if viper.IsSet("peer.gossip.state.enabled") {
config.EnableStateTransfer = viper.GetBool("peer.gossip.state.enabled")
}
if viper.GetBool("peer.gossip.nonBlockingCommitMode") {
config.BlockingMode = state.NonBlocking
}
return config
}
Go
1
https://gitee.com/hyperledger-fabric-gm/fabric.git
git@gitee.com:hyperledger-fabric-gm/fabric.git
hyperledger-fabric-gm
fabric
fabric
v1.4.9

搜索帮助

53164aa7 5694891 3bd8fe86 5694891