Skip to content

Commit 0c5aaea

Browse files
committed
address review comments
Signed-off-by: May.Buzaglo <May.Buzaglo@ibm.com>
1 parent 6311754 commit 0c5aaea

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,12 @@ func (config *Configuration) BuildNewConfiguration(block *common.Block) (*Config
806806

807807
consensusMetadata, err := ReadConsensusMetadataFromConfigBlock(block)
808808
if err != nil {
809-
return nil, errors.Errorf("failed applying new config, failed to read consensusMetadata from last config block, block number is %d", block.Header.Number)
809+
return nil, errors.Wrapf(err, "failed applying new config, failed to read consensusMetadata from last config block, block number is %d", block.Header.Number)
810810
}
811811

812812
err = proto.Unmarshal(consensusMetadata, newConfig.SharedConfig)
813813
if err != nil {
814-
return nil, errors.Errorf("failed applying new config, failed to unmarshal consensusMetadata to a shared configuration")
814+
return nil, errors.Wrapf(err, "failed applying new config, failed to unmarshal consensusMetadata to a shared configuration")
815815
}
816816
return newConfig, nil
817817
}

config/reconfig.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,42 @@ type NodeConfig interface {
2222
GetTlsCert() []byte
2323
}
2424

25-
type NodeConfigWthSign interface {
25+
type NodeConfigWithSign interface {
2626
NodeConfig
2727
GetSignCert() []byte
2828
}
2929

30-
// IsPartyEvicted checks if a party exists in the configuration and returns true if it is evicted or not.
31-
func IsPartyEvicted(partyID types.PartyID, newConfig *Configuration) bool {
32-
newSharedPartyConfig := FindParty(partyID, newConfig)
33-
return newSharedPartyConfig == nil
30+
// IsPartyEvicted returns true if the given party does not appear in the provided configuration.
31+
func IsPartyEvicted(partyID types.PartyID, newConfig *Configuration) (bool, error) {
32+
newSharedPartyConfig, err := FindParty(partyID, newConfig)
33+
if err != nil {
34+
return false, err
35+
}
36+
return newSharedPartyConfig == nil, nil
3437
}
3538

3639
// FindParty returns the PartyConfig associated with the given partyID from the shared configuration.
37-
// It returns nil if the party is not found.
38-
func FindParty(partyID types.PartyID, config *Configuration) *config_protos.PartyConfig {
40+
// It returns nil if the party is not found and returns error if the provided configuration is nil or incomplete.
41+
func FindParty(partyID types.PartyID, config *Configuration) (*config_protos.PartyConfig, error) {
42+
if config == nil {
43+
return nil, errors.New("the provided configuration is nil")
44+
}
45+
if config.SharedConfig == nil {
46+
return nil, errors.New("the provided configuration has nil shared config")
47+
}
3948
for _, party := range config.SharedConfig.PartiesConfig {
4049
if types.PartyID(party.PartyID) == partyID {
41-
return party
50+
return party, nil
4251
}
4352
}
44-
return nil
53+
return nil, nil
4554
}
4655

47-
// IsNodeConfigChangeRestartRequired reports whether a restart is required due to changed between current configuration and new configuration.
48-
// A restart is required if any of the following fields differ:
56+
// IsNodeConfigChangeRestartRequired reports whether a restart is required due to configuration updates.
57+
// A restart is required if any of the following parts were updated:
4958
// - host or port
5059
// - TLS certificate
51-
// - sign certificate (if both configs implement NodeConfigWithSignCert)
60+
// - sign certificate (this is checked if both configs implement NodeConfigWithSign)
5261
//
5362
// Both arguments must represent the same node type and be non-nil.
5463
func IsNodeConfigChangeRestartRequired(currentConfig, newConfig NodeConfig) (bool, error) {
@@ -60,18 +69,19 @@ func IsNodeConfigChangeRestartRequired(currentConfig, newConfig NodeConfig) (boo
6069
return false, errors.New("new config is nil")
6170
}
6271

72+
extendedCurrConfig, currOK := currentConfig.(NodeConfigWithSign)
73+
extendedNewConfig, newOK := newConfig.(NodeConfigWithSign)
74+
if currOK != newOK {
75+
return false, errors.New("type mismatch: current node config and new node config are not from the same type")
76+
}
77+
6378
currAddr := net.JoinHostPort(currentConfig.GetHost(), strconv.Itoa(int(currentConfig.GetPort())))
6479
newAddr := net.JoinHostPort(newConfig.GetHost(), strconv.Itoa(int(newConfig.GetPort())))
6580

6681
if currAddr != newAddr || !bytes.Equal(currentConfig.GetTlsCert(), newConfig.GetTlsCert()) {
6782
return true, nil
6883
}
6984

70-
extendedCurrConfig, currOK := currentConfig.(NodeConfigWthSign)
71-
extendedNewConfig, newOK := newConfig.(NodeConfigWthSign)
72-
if currOK != newOK {
73-
return false, errors.New("type mismatch: current node config and new node config are not from the same type")
74-
}
7585
if currOK && !bytes.Equal(extendedCurrConfig.GetSignCert(), extendedNewConfig.GetSignCert()) {
7686
return true, nil
7787
}

config/reconfig_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ Copyright IBM Corp. All Rights Reserved.
44
SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
package config
7+
package config_test
88

99
import (
1010
"testing"
1111

12+
"github.com/hyperledger/fabric-x-orderer/config"
13+
1214
"github.com/hyperledger/fabric-x-orderer/common/types"
1315
"github.com/hyperledger/fabric-x-orderer/config/protos"
1416
"github.com/stretchr/testify/require"
@@ -19,16 +21,20 @@ func TestIsPartyEvicted(t *testing.T) {
1921
partyConfig := &protos.PartyConfig{
2022
PartyID: 2,
2123
}
22-
config := &Configuration{
24+
conf := &config.Configuration{
2325
SharedConfig: &protos.SharedConfig{
2426
PartiesConfig: []*protos.PartyConfig{partyConfig},
2527
MaxPartyID: 1,
2628
},
2729
}
2830

29-
require.True(t, IsPartyEvicted(partyID, config))
31+
isPartyEvicted, err := config.IsPartyEvicted(partyID, conf)
32+
require.NoError(t, err)
33+
require.True(t, isPartyEvicted)
3034
partyID = types.PartyID(2)
31-
require.False(t, IsPartyEvicted(partyID, config))
35+
isPartyEvicted, err = config.IsPartyEvicted(partyID, conf)
36+
require.NoError(t, err)
37+
require.False(t, isPartyEvicted)
3238
}
3339

3440
func TestIsNodeConfigChangeRestartRequired(t *testing.T) {
@@ -45,20 +51,20 @@ func TestIsNodeConfigChangeRestartRequired(t *testing.T) {
4551
TlsCert: []byte("cert"),
4652
}
4753

48-
isRestartRequired, err := IsNodeConfigChangeRestartRequired(currRouterConfig, newRouterConfig)
54+
isRestartRequired, err := config.IsNodeConfigChangeRestartRequired(currRouterConfig, newRouterConfig)
4955
require.NoError(t, err)
5056
require.False(t, isRestartRequired)
5157

5258
newRouterConfig.Port = 5070
5359

54-
isRestartRequired, err = IsNodeConfigChangeRestartRequired(currRouterConfig, newRouterConfig)
60+
isRestartRequired, err = config.IsNodeConfigChangeRestartRequired(currRouterConfig, newRouterConfig)
5561
require.NoError(t, err)
5662
require.True(t, isRestartRequired)
5763

5864
newRouterConfig.Port = 5060
5965
newRouterConfig.TlsCert = []byte("TLSCert")
6066

61-
isRestartRequired, err = IsNodeConfigChangeRestartRequired(currRouterConfig, newRouterConfig)
67+
isRestartRequired, err = config.IsNodeConfigChangeRestartRequired(currRouterConfig, newRouterConfig)
6268
require.NoError(t, err)
6369
require.True(t, isRestartRequired)
6470

@@ -79,12 +85,12 @@ func TestIsNodeConfigChangeRestartRequired(t *testing.T) {
7985
TlsCert: []byte("TLSCert"),
8086
}
8187

82-
isRestartRequired, err = IsNodeConfigChangeRestartRequired(currBatcherConfig, newBatcherConfig)
88+
isRestartRequired, err = config.IsNodeConfigChangeRestartRequired(currBatcherConfig, newBatcherConfig)
8389
require.NoError(t, err)
8490
require.False(t, isRestartRequired)
8591

8692
newBatcherConfig.SignCert = []byte("NewSignCert")
87-
isRestartRequired, err = IsNodeConfigChangeRestartRequired(currBatcherConfig, newBatcherConfig)
93+
isRestartRequired, err = config.IsNodeConfigChangeRestartRequired(currBatcherConfig, newBatcherConfig)
8894
require.NoError(t, err)
8995
require.True(t, isRestartRequired)
9096
}

0 commit comments

Comments
 (0)