github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/pkg/peer/orderers/connection_test.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package orderers_test
     8  
     9  import (
    10  	"bytes"
    11  	"io/ioutil"
    12  	"sort"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  
    17  	"github.com/hechain20/hechain/common/flogging"
    18  	"github.com/hechain20/hechain/internal/pkg/peer/orderers"
    19  )
    20  
    21  type comparableEndpoint struct {
    22  	Address   string
    23  	RootCerts [][]byte
    24  }
    25  
    26  // stripEndpoints makes a comparable version of the endpoints specified.  This
    27  // is necessary because the endpoint contains a channel which is not
    28  // comparable.
    29  func stripEndpoints(endpoints []*orderers.Endpoint) []comparableEndpoint {
    30  	endpointsWithChannelStripped := make([]comparableEndpoint, len(endpoints))
    31  	for i, endpoint := range endpoints {
    32  		certs := endpoint.RootCerts
    33  		sort.Slice(certs, func(i, j int) bool {
    34  			return bytes.Compare(certs[i], certs[j]) >= 0
    35  		})
    36  		endpointsWithChannelStripped[i].Address = endpoint.Address
    37  		endpointsWithChannelStripped[i].RootCerts = certs
    38  	}
    39  	return endpointsWithChannelStripped
    40  }
    41  
    42  var _ = Describe("Connection", func() {
    43  	var (
    44  		cert1 []byte
    45  		cert2 []byte
    46  		cert3 []byte
    47  
    48  		org1 orderers.OrdererOrg
    49  		org2 orderers.OrdererOrg
    50  
    51  		org1Certs     [][]byte
    52  		org2Certs     [][]byte
    53  		overrideCerts [][]byte
    54  
    55  		cs *orderers.ConnectionSource
    56  
    57  		endpoints []*orderers.Endpoint
    58  	)
    59  
    60  	BeforeEach(func() {
    61  		var err error
    62  		cert1, err = ioutil.ReadFile("testdata/tlsca.example.com-cert.pem")
    63  		Expect(err).NotTo(HaveOccurred())
    64  
    65  		cert2, err = ioutil.ReadFile("testdata/tlsca.org1.example.com-cert.pem")
    66  		Expect(err).NotTo(HaveOccurred())
    67  
    68  		cert3, err = ioutil.ReadFile("testdata/tlsca.org2.example.com-cert.pem")
    69  		Expect(err).NotTo(HaveOccurred())
    70  
    71  		org1 = orderers.OrdererOrg{
    72  			Addresses: []string{"org1-address1", "org1-address2"},
    73  			RootCerts: [][]byte{cert1, cert2},
    74  		}
    75  
    76  		org2 = orderers.OrdererOrg{
    77  			Addresses: []string{"org2-address1", "org2-address2"},
    78  			RootCerts: [][]byte{cert3},
    79  		}
    80  
    81  		org1Certs = [][]byte{cert1, cert2}
    82  		org2Certs = [][]byte{cert3}
    83  		overrideCerts = [][]byte{cert2}
    84  
    85  		cs = orderers.NewConnectionSource(flogging.MustGetLogger("peer.orderers"), map[string]*orderers.Endpoint{
    86  			"override-address": {
    87  				Address:   "re-mapped-address",
    88  				RootCerts: overrideCerts,
    89  			},
    90  		})
    91  		cs.Update(nil, map[string]orderers.OrdererOrg{
    92  			"org1": org1,
    93  			"org2": org2,
    94  		})
    95  
    96  		endpoints = cs.Endpoints()
    97  	})
    98  
    99  	It("holds endpoints for all of the defined orderers", func() {
   100  		Expect(stripEndpoints(endpoints)).To(ConsistOf(
   101  			stripEndpoints([]*orderers.Endpoint{
   102  				{
   103  					Address:   "org1-address1",
   104  					RootCerts: org1Certs,
   105  				},
   106  				{
   107  					Address:   "org1-address2",
   108  					RootCerts: org1Certs,
   109  				},
   110  				{
   111  					Address:   "org2-address1",
   112  					RootCerts: org2Certs,
   113  				},
   114  				{
   115  					Address:   "org2-address2",
   116  					RootCerts: org2Certs,
   117  				},
   118  			}),
   119  		))
   120  	})
   121  
   122  	It("does not mark any of the endpoints as refreshed", func() {
   123  		for _, endpoint := range endpoints {
   124  			Expect(endpoint.Refreshed).NotTo(BeClosed())
   125  		}
   126  	})
   127  
   128  	When("an update does not modify the endpoint set", func() {
   129  		BeforeEach(func() {
   130  			cs.Update(nil, map[string]orderers.OrdererOrg{
   131  				"org1": org1,
   132  				"org2": org2,
   133  			})
   134  		})
   135  
   136  		It("does not update the endpoints", func() {
   137  			newEndpoints := cs.Endpoints()
   138  			Expect(newEndpoints).To(Equal(endpoints))
   139  		})
   140  
   141  		It("does not close any of the refresh channels", func() {
   142  			for _, endpoint := range endpoints {
   143  				Expect(endpoint.Refreshed).NotTo(BeClosed())
   144  			}
   145  		})
   146  	})
   147  
   148  	When("an update change's an org's TLS CA", func() {
   149  		BeforeEach(func() {
   150  			org1.RootCerts = [][]byte{cert1}
   151  
   152  			cs.Update(nil, map[string]orderers.OrdererOrg{
   153  				"org1": org1,
   154  				"org2": org2,
   155  			})
   156  		})
   157  
   158  		It("creates a new set of orderer endpoints", func() {
   159  			newOrg1Certs := [][]byte{cert1}
   160  
   161  			newEndpoints := cs.Endpoints()
   162  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   163  				stripEndpoints([]*orderers.Endpoint{
   164  					{
   165  						Address:   "org1-address1",
   166  						RootCerts: newOrg1Certs,
   167  					},
   168  					{
   169  						Address:   "org1-address2",
   170  						RootCerts: newOrg1Certs,
   171  					},
   172  					{
   173  						Address:   "org2-address1",
   174  						RootCerts: org2Certs,
   175  					},
   176  					{
   177  						Address:   "org2-address2",
   178  						RootCerts: org2Certs,
   179  					},
   180  				}),
   181  			))
   182  		})
   183  
   184  		It("closes the refresh channel for all of the old endpoints", func() {
   185  			for _, endpoint := range endpoints {
   186  				Expect(endpoint.Refreshed).To(BeClosed())
   187  			}
   188  		})
   189  	})
   190  
   191  	When("an update change's an org's endpoint addresses", func() {
   192  		BeforeEach(func() {
   193  			org1.Addresses = []string{"org1-address1", "org1-address3"}
   194  			cs.Update(nil, map[string]orderers.OrdererOrg{
   195  				"org1": org1,
   196  				"org2": org2,
   197  			})
   198  		})
   199  
   200  		It("creates a new set of orderer endpoints", func() {
   201  			newEndpoints := cs.Endpoints()
   202  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   203  				stripEndpoints([]*orderers.Endpoint{
   204  					{
   205  						Address:   "org1-address1",
   206  						RootCerts: org1Certs,
   207  					},
   208  					{
   209  						Address:   "org1-address3",
   210  						RootCerts: org1Certs,
   211  					},
   212  					{
   213  						Address:   "org2-address1",
   214  						RootCerts: org2Certs,
   215  					},
   216  					{
   217  						Address:   "org2-address2",
   218  						RootCerts: org2Certs,
   219  					},
   220  				}),
   221  			))
   222  		})
   223  
   224  		It("closes the refresh channel for all of the old endpoints", func() {
   225  			for _, endpoint := range endpoints {
   226  				Expect(endpoint.Refreshed).To(BeClosed())
   227  			}
   228  		})
   229  	})
   230  
   231  	When("an update references an overridden org endpoint address", func() {
   232  		BeforeEach(func() {
   233  			cs.Update(nil, map[string]orderers.OrdererOrg{
   234  				"org1": {
   235  					Addresses: []string{"org1-address1", "override-address"},
   236  					RootCerts: [][]byte{cert1, cert2},
   237  				},
   238  			})
   239  		})
   240  
   241  		It("creates a new set of orderer endpoints with overrides", func() {
   242  			newEndpoints := cs.Endpoints()
   243  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   244  				stripEndpoints([]*orderers.Endpoint{
   245  					{
   246  						Address:   "org1-address1",
   247  						RootCerts: org1Certs,
   248  					},
   249  					{
   250  						Address:   "re-mapped-address",
   251  						RootCerts: overrideCerts,
   252  					},
   253  				}),
   254  			))
   255  		})
   256  	})
   257  
   258  	When("an update removes an ordering organization", func() {
   259  		BeforeEach(func() {
   260  			cs.Update(nil, map[string]orderers.OrdererOrg{
   261  				"org2": org2,
   262  			})
   263  		})
   264  
   265  		It("creates a new set of orderer endpoints", func() {
   266  			newEndpoints := cs.Endpoints()
   267  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   268  				stripEndpoints([]*orderers.Endpoint{
   269  					{
   270  						Address:   "org2-address1",
   271  						RootCerts: org2Certs,
   272  					},
   273  					{
   274  						Address:   "org2-address2",
   275  						RootCerts: org2Certs,
   276  					},
   277  				}),
   278  			))
   279  		})
   280  
   281  		It("closes the refresh channel for all of the old endpoints", func() {
   282  			for _, endpoint := range endpoints {
   283  				Expect(endpoint.Refreshed).To(BeClosed())
   284  			}
   285  		})
   286  
   287  		When("the org is added back", func() {
   288  			BeforeEach(func() {
   289  				cs.Update(nil, map[string]orderers.OrdererOrg{
   290  					"org1": org1,
   291  					"org2": org2,
   292  				})
   293  			})
   294  
   295  			It("returns to the set of orderer endpoints", func() {
   296  				newEndpoints := cs.Endpoints()
   297  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   298  					stripEndpoints([]*orderers.Endpoint{
   299  						{
   300  							Address:   "org1-address1",
   301  							RootCerts: org1Certs,
   302  						},
   303  						{
   304  							Address:   "org1-address2",
   305  							RootCerts: org1Certs,
   306  						},
   307  						{
   308  							Address:   "org2-address1",
   309  							RootCerts: org2Certs,
   310  						},
   311  						{
   312  							Address:   "org2-address2",
   313  							RootCerts: org2Certs,
   314  						},
   315  					}),
   316  				))
   317  			})
   318  		})
   319  	})
   320  
   321  	When("an update modifies the global endpoints but does does not affect the org endpoints", func() {
   322  		BeforeEach(func() {
   323  			cs.Update(nil, map[string]orderers.OrdererOrg{
   324  				"org1": org1,
   325  				"org2": org2,
   326  			})
   327  		})
   328  
   329  		It("does not update the endpoints", func() {
   330  			newEndpoints := cs.Endpoints()
   331  			Expect(newEndpoints).To(Equal(endpoints))
   332  		})
   333  
   334  		It("does not close any of the refresh channels", func() {
   335  			for _, endpoint := range endpoints {
   336  				Expect(endpoint.Refreshed).NotTo(BeClosed())
   337  			}
   338  		})
   339  	})
   340  
   341  	When("the configuration does not contain orderer org endpoints", func() {
   342  		var globalCerts [][]byte
   343  
   344  		BeforeEach(func() {
   345  			org1.Addresses = nil
   346  			org2.Addresses = nil
   347  
   348  			globalCerts = [][]byte{cert1, cert2, cert3}
   349  
   350  			cs.Update([]string{"global-addr1", "global-addr2"}, map[string]orderers.OrdererOrg{
   351  				"org1": org1,
   352  				"org2": org2,
   353  			})
   354  		})
   355  
   356  		It("creates endpoints for the global addrs", func() {
   357  			newEndpoints := cs.Endpoints()
   358  			Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   359  				stripEndpoints([]*orderers.Endpoint{
   360  					{
   361  						Address:   "global-addr1",
   362  						RootCerts: globalCerts,
   363  					},
   364  					{
   365  						Address:   "global-addr2",
   366  						RootCerts: globalCerts,
   367  					},
   368  				}),
   369  			))
   370  		})
   371  
   372  		It("closes the refresh channel for all of the old endpoints", func() {
   373  			for _, endpoint := range endpoints {
   374  				Expect(endpoint.Refreshed).To(BeClosed())
   375  			}
   376  		})
   377  
   378  		When("the global list of addresses grows", func() {
   379  			BeforeEach(func() {
   380  				cs.Update([]string{"global-addr1", "global-addr2", "global-addr3"}, map[string]orderers.OrdererOrg{
   381  					"org1": org1,
   382  					"org2": org2,
   383  				})
   384  			})
   385  
   386  			It("creates endpoints for the global addrs", func() {
   387  				newEndpoints := cs.Endpoints()
   388  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   389  					stripEndpoints([]*orderers.Endpoint{
   390  						{
   391  							Address:   "global-addr1",
   392  							RootCerts: globalCerts,
   393  						},
   394  						{
   395  							Address:   "global-addr2",
   396  							RootCerts: globalCerts,
   397  						},
   398  						{
   399  							Address:   "global-addr3",
   400  							RootCerts: globalCerts,
   401  						},
   402  					}),
   403  				))
   404  			})
   405  
   406  			It("closes the refresh channel for all of the old endpoints", func() {
   407  				for _, endpoint := range endpoints {
   408  					Expect(endpoint.Refreshed).To(BeClosed())
   409  				}
   410  			})
   411  		})
   412  
   413  		When("the global set of addresses shrinks", func() {
   414  			BeforeEach(func() {
   415  				cs.Update([]string{"global-addr1"}, map[string]orderers.OrdererOrg{
   416  					"org1": org1,
   417  					"org2": org2,
   418  				})
   419  			})
   420  
   421  			It("creates endpoints for the global addrs", func() {
   422  				newEndpoints := cs.Endpoints()
   423  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   424  					stripEndpoints([]*orderers.Endpoint{
   425  						{
   426  							Address:   "global-addr1",
   427  							RootCerts: globalCerts,
   428  						},
   429  					}),
   430  				))
   431  			})
   432  
   433  			It("closes the refresh channel for all of the old endpoints", func() {
   434  				for _, endpoint := range endpoints {
   435  					Expect(endpoint.Refreshed).To(BeClosed())
   436  				}
   437  			})
   438  		})
   439  
   440  		When("the global set of addresses is modified", func() {
   441  			BeforeEach(func() {
   442  				cs.Update([]string{"global-addr1", "global-addr3"}, map[string]orderers.OrdererOrg{
   443  					"org1": org1,
   444  					"org2": org2,
   445  				})
   446  			})
   447  
   448  			It("creates endpoints for the global addrs", func() {
   449  				newEndpoints := cs.Endpoints()
   450  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   451  					stripEndpoints([]*orderers.Endpoint{
   452  						{
   453  							Address:   "global-addr1",
   454  							RootCerts: globalCerts,
   455  						},
   456  						{
   457  							Address:   "global-addr3",
   458  							RootCerts: globalCerts,
   459  						},
   460  					}),
   461  				))
   462  			})
   463  
   464  			It("closes the refresh channel for all of the old endpoints", func() {
   465  				for _, endpoint := range endpoints {
   466  					Expect(endpoint.Refreshed).To(BeClosed())
   467  				}
   468  			})
   469  		})
   470  
   471  		When("an update to the global addrs references an overridden org endpoint address", func() {
   472  			BeforeEach(func() {
   473  				cs.Update([]string{"global-addr1", "override-address"}, map[string]orderers.OrdererOrg{
   474  					"org1": org1,
   475  					"org2": org2,
   476  				},
   477  				)
   478  			})
   479  
   480  			It("creates a new set of orderer endpoints with overrides", func() {
   481  				newEndpoints := cs.Endpoints()
   482  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   483  					stripEndpoints([]*orderers.Endpoint{
   484  						{
   485  							Address:   "global-addr1",
   486  							RootCerts: globalCerts,
   487  						},
   488  						{
   489  							Address:   "re-mapped-address",
   490  							RootCerts: overrideCerts,
   491  						},
   492  					}),
   493  				))
   494  			})
   495  		})
   496  
   497  		When("an orderer org adds an endpoint", func() {
   498  			BeforeEach(func() {
   499  				org1.Addresses = []string{"new-org1-address"}
   500  				cs.Update([]string{"global-addr1", "global-addr2"}, map[string]orderers.OrdererOrg{
   501  					"org1": org1,
   502  					"org2": org2,
   503  				})
   504  			})
   505  
   506  			It("removes the global endpoints and uses only the org level ones", func() {
   507  				newEndpoints := cs.Endpoints()
   508  				Expect(stripEndpoints(newEndpoints)).To(ConsistOf(
   509  					stripEndpoints([]*orderers.Endpoint{
   510  						{
   511  							Address:   "new-org1-address",
   512  							RootCerts: org1Certs,
   513  						},
   514  					}),
   515  				))
   516  			})
   517  
   518  			It("closes the refresh channel for all of the old endpoints", func() {
   519  				for _, endpoint := range endpoints {
   520  					Expect(endpoint.Refreshed).To(BeClosed())
   521  				}
   522  			})
   523  		})
   524  	})
   525  })