Skip to content

Commit

Permalink
FAB-16715 Wire orderer endpoint overrides via config
Browse files Browse the repository at this point in the history
Add peer.deliverclient.addressOverides to config
and wire into createChain.  This is not ideal
but the viper call is isolated in a config function.

Added top-level field to sampleconfig buy commented
out any to/from mappings.

Change-Id: I2f6287db37492b2cdd2fb3d889ed834332bfc7c0
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Sep 30, 2019
1 parent 0953c69 commit 456e9ad
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 22 deletions.
22 changes: 22 additions & 0 deletions core/peer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,25 @@ func GetClientCertificate() (tls.Certificate, error) {
}
return cert, nil
}

type addressOverride struct {
From string `mapstructure:"from"`
To string `mapstructure:"to"`
}

func GetOrdererAddressOverrides() (map[string]string, error) {
var overrides []addressOverride
err := viper.UnmarshalKey("peer.deliveryclient.addressOverrides", &overrides)
if err != nil {
return nil, err
}

var overrideMap map[string]string
if len(overrides) > 0 {
overrideMap = make(map[string]string)
for _, override := range overrides {
overrideMap[override.From] = override.To
}
}
return overrideMap, nil
}
28 changes: 28 additions & 0 deletions core/peer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SPDX-License-Identifier: Apache-2.0
package peer

import (
"bytes"
"crypto/tls"
"fmt"
"net"
Expand Down Expand Up @@ -318,3 +319,30 @@ func TestGetClientCertificate(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expected, cert)
}

func TestGetOrdererAddressOverrides(t *testing.T) {
conf := `
peer:
deliveryclient:
addressOverrides:
- from: myaddress
to: youraddress
- from: myaddress2
to: youraddress2`

viper.SetConfigType("yaml")
err := viper.ReadConfig(bytes.NewBuffer([]byte(conf)))
if err != nil {
t.Fatalf("Failed to read test config: %s", err)
}

expected := map[string]string{
"myaddress": "youraddress",
"myaddress2": "youraddress2",
}
overrides, err := GetOrdererAddressOverrides()
if err != nil {
t.Fatalf("Failed to get overrides: %s", err)
}
assert.Equal(t, expected, overrides)
}
22 changes: 18 additions & 4 deletions core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,15 @@ func getCurrConfigBlockFromLedger(ledger ledger.PeerLedger) (*common.Block, erro
}

// createChain creates a new chain object and insert it into the chains
func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block, ccp ccprovider.ChaincodeProvider, sccp sysccprovider.SystemChaincodeProvider, pm txvalidator.PluginMapper) error {
func createChain(
cid string,
ledger ledger.PeerLedger,
cb *common.Block,
ccp ccprovider.ChaincodeProvider,
sccp sysccprovider.SystemChaincodeProvider,
pm txvalidator.PluginMapper,
) error {

chanConf, err := retrievePersistedChannelConfig(ledger)
if err != nil {
return err
Expand Down Expand Up @@ -458,6 +466,11 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block, ccp ccp
return errors.New("no ordering service endpoint provided in configuration block")
}

ordererAddressOverrides, err := GetOrdererAddressOverrides()
if err != nil {
return errors.Errorf("failed to get override addresses: %s", err)
}

// TODO: does someone need to call Close() on the transientStoreFactory at shutdown of the peer?
store, err := TransientStoreFactory.OpenStore(bundle.ConfigtxValidator().ChainID())
if err != nil {
Expand All @@ -470,9 +483,10 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block, ccp ccp
simpleCollectionStore := privdata.NewSimpleCollectionStore(csStoreSupport)

oac := service.OrdererAddressConfig{
Addresses: ordererAddresses,
AddressesByOrg: ordererAddressesByOrg,
Organizations: ordererOrganizations,
Addresses: ordererAddresses,
AddressesByOrg: ordererAddressesByOrg,
Organizations: ordererOrganizations,
AddressOverrides: ordererAddressOverrides,
}
service.GetGossipService().InitializeChannel(bundle.ConfigtxValidator().ChainID(), oac, service.Support{
Validator: validator,
Expand Down
57 changes: 43 additions & 14 deletions gossip/service/gossip_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ func (*deliveryFactoryImpl) Service(g GossipService, ec OrdererAddressConfig, mc
ConnFactory: deliverclient.DefaultConnectionFactory,
ABCFactory: deliverclient.DefaultABCFactory,
}, deliverclient.ConnectionCriteria{
OrdererEndpointsByOrg: ec.AddressesByOrg,
Organizations: ec.Organizations,
OrdererEndpoints: ec.Addresses,
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
Addresses []string
AddressesByOrg map[string][]string
Organizations []string
AddressOverrides map[string]string
}

type privateHandler struct {
Expand Down Expand Up @@ -139,22 +141,49 @@ func (jcm *joinChannelMessage) AnchorPeersOf(org api.OrgIdentityType) []api.Anch
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, bootPeers ...string) error {
func InitGossipService(
peerIdentity []byte,
metricsProvider metrics.Provider,
endpoint string, s *grpc.Server,
certs *gossipCommon.TLSCertificates,
mcs api.MessageCryptoService,
secAdv api.SecurityAdvisor,
secureDialOpts api.PeerSecureDialOpts,
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{},
mcs, secAdv, secureDialOpts, bootPeers...)
return InitGossipServiceCustomDeliveryFactory(
peerIdentity,
metricsProvider,
endpoint,
s,
certs,
&deliveryFactoryImpl{},
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 {
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() {
Expand Down
17 changes: 13 additions & 4 deletions sampleconfig/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,25 @@ peer:

# Delivery service related config
deliveryclient:
# It sets the total time the delivery service may spend in reconnection
# attempts until its retry logic gives up and returns an error
# The total time to spend retrying connections to ordering nodes
# before giving up and returning an error.
reconnectTotalTimeThreshold: 3600s

# It sets the delivery service <-> ordering service node connection timeout
# The connection timeout when connecting to ordering service nodes.
connTimeout: 3s

# It sets the delivery service maximal delay between consecutive retries
# The maximum delay between consecutive connection retry attempts to
# ordering nodes.
reConnectBackoffThreshold: 3600s

# A list of orderer endpoint addresses which should be overridden
# when found in channel configurations.
addressOverrides:
# - from:
# to:
# - from:
# to:

# Type for the local MSP - by default it's of type bccsp
localMspType: bccsp

Expand Down

0 comments on commit 456e9ad

Please sign in to comment.