Skip to content

Commit af69416

Browse files
sykesmmastersingh24
authored andcommitted
Relocate raft config specific helpers to raft tests
Move AddConsenter, RemoveConsenter, ConsenterAdder, ConsenterRemover, and UpdateEtcdRaftMetadata out of the nwo package and into the etcdraft configuration tests. FAB-17049 # done Change-Id: I2a8eeb5ec67c57ae963fc0520ded512acf755a36 Signed-off-by: Matthew Sykes <[email protected]>
1 parent ce7aca8 commit af69416

File tree

2 files changed

+125
-122
lines changed

2 files changed

+125
-122
lines changed

integration/nwo/configblock.go

+8-107
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
77
package nwo
88

99
import (
10-
"bytes"
1110
"io/ioutil"
1211
"os"
1312
"path/filepath"
@@ -16,7 +15,6 @@ import (
1615
"github.com/golang/protobuf/proto"
1716
"github.com/hyperledger/fabric-protos-go/common"
1817
protosorderer "github.com/hyperledger/fabric-protos-go/orderer"
19-
ectdraft_protos "github.com/hyperledger/fabric-protos-go/orderer/etcdraft"
2018
"github.com/hyperledger/fabric/integration/nwo/commands"
2119
"github.com/hyperledger/fabric/internal/configtxlator/update"
2220
"github.com/hyperledger/fabric/protoutil"
@@ -25,7 +23,7 @@ import (
2523
"github.com/onsi/gomega/gexec"
2624
)
2725

28-
// GetConfigBlock retrieves the current config block for a channel
26+
// GetConfigBlock retrieves the current config block for a channel.
2927
func GetConfigBlock(n *Network, peer *Peer, orderer *Orderer, channel string) *common.Block {
3028
tempDir, err := ioutil.TempDir(n.RootDir, "getConfigBlock")
3129
Expect(err).NotTo(HaveOccurred())
@@ -49,7 +47,7 @@ func GetConfigBlock(n *Network, peer *Peer, orderer *Orderer, channel string) *c
4947
return configBlock
5048
}
5149

52-
// GetConfig retrieves the last config of the given channel
50+
// GetConfig retrieves the last config of the given channel.
5351
func GetConfig(n *Network, peer *Peer, orderer *Orderer, channel string) *common.Config {
5452
configBlock := GetConfigBlock(n, peer, orderer, channel)
5553
// unmarshal the envelope bytes
@@ -197,8 +195,8 @@ func FetchConfigBlock(n *Network, peer *Peer, orderer *Orderer, channel string,
197195
Eventually(fetch, n.EventuallyTimeout).Should(Equal(0))
198196
}
199197

200-
// UpdateOrdererConfig computes, signs, and submits a configuration update which requires orderers signature and waits
201-
// for the update to complete.
198+
// UpdateOrdererConfig computes, signs, and submits a configuration update
199+
// which requires orderers signature and waits for the update to complete.
202200
func UpdateOrdererConfig(n *Network, orderer *Orderer, channel string, current, updated *common.Config, submitter *Peer, additionalSigners ...*Orderer) {
203201
tempDir, err := ioutil.TempDir(n.RootDir, "updateConfig")
204202
Expect(err).NotTo(HaveOccurred())
@@ -240,7 +238,7 @@ func UpdateOrdererConfigSession(n *Network, orderer *Orderer, channel string, cu
240238

241239
ComputeUpdateOrdererConfig(updateFile, n, channel, current, updated, submitter, additionalSigners...)
242240

243-
//session should not return with a zero exit code nor with a success response
241+
// session should not return with a zero exit code nor with a success response
244242
sess, err := n.OrdererAdminSession(orderer, submitter, commands.ChannelUpdate{
245243
ChannelID: channel,
246244
Orderer: n.OrdererAddress(orderer, ListenPort),
@@ -289,93 +287,11 @@ func UnmarshalBlockFromFile(blockFile string) *common.Block {
289287
return block
290288
}
291289

292-
// AddConsenter adds a new consenter to the given channel
293-
func AddConsenter(n *Network, peer *Peer, orderer *Orderer, channel string, consenter ectdraft_protos.Consenter) {
294-
UpdateEtcdRaftMetadata(n, peer, orderer, channel, func(metadata *ectdraft_protos.ConfigMetadata) {
295-
metadata.Consenters = append(metadata.Consenters, &consenter)
296-
})
297-
}
298-
299-
// RemoveConsenter removes a consenter with the given certificate in PEM format from the given channel
300-
func RemoveConsenter(n *Network, peer *Peer, orderer *Orderer, channel string, certificate []byte) {
301-
UpdateEtcdRaftMetadata(n, peer, orderer, channel, func(metadata *ectdraft_protos.ConfigMetadata) {
302-
var newConsenters []*ectdraft_protos.Consenter
303-
for _, consenter := range metadata.Consenters {
304-
if bytes.Equal(consenter.ClientTlsCert, certificate) || bytes.Equal(consenter.ServerTlsCert, certificate) {
305-
continue
306-
}
307-
newConsenters = append(newConsenters, consenter)
308-
}
309-
310-
metadata.Consenters = newConsenters
311-
})
312-
}
313-
314-
// ConsenterRemover constructs configs that can be used by `UpdateOrdererConfig` to remove a consenter
315-
func ConsenterRemover(n *Network, peer *Peer, orderer *Orderer, channel string, certificate []byte) (current, updated *common.Config) {
316-
config := GetConfig(n, peer, orderer, channel)
317-
updatedConfig := proto.Clone(config).(*common.Config)
318-
319-
consensusTypeConfigValue := updatedConfig.ChannelGroup.Groups["Orderer"].Values["ConsensusType"]
320-
consensusTypeValue := &protosorderer.ConsensusType{}
321-
err := proto.Unmarshal(consensusTypeConfigValue.Value, consensusTypeValue)
322-
Expect(err).NotTo(HaveOccurred())
323-
324-
metadata := &ectdraft_protos.ConfigMetadata{}
325-
err = proto.Unmarshal(consensusTypeValue.Metadata, metadata)
326-
Expect(err).NotTo(HaveOccurred())
327-
328-
var newConsenters []*ectdraft_protos.Consenter
329-
for _, consenter := range metadata.Consenters {
330-
if bytes.Equal(consenter.ClientTlsCert, certificate) || bytes.Equal(consenter.ServerTlsCert, certificate) {
331-
continue
332-
}
333-
newConsenters = append(newConsenters, consenter)
334-
}
335-
336-
metadata.Consenters = newConsenters
337-
consensusTypeValue.Metadata, err = proto.Marshal(metadata)
338-
Expect(err).NotTo(HaveOccurred())
339-
340-
updatedConfig.ChannelGroup.Groups["Orderer"].Values["ConsensusType"] = &common.ConfigValue{
341-
ModPolicy: "Admins",
342-
Value: protoutil.MarshalOrPanic(consensusTypeValue),
343-
}
344-
345-
return config, updatedConfig
346-
}
347-
348-
// ConsenterAdder constructs configs that can be used by `UpdateOrdererConfig` to add a consenter
349-
func ConsenterAdder(n *Network, peer *Peer, orderer *Orderer, channel string, consenter ectdraft_protos.Consenter) (current, updated *common.Config) {
350-
config := GetConfig(n, peer, orderer, channel)
351-
updatedConfig := proto.Clone(config).(*common.Config)
352-
353-
consensusTypeConfigValue := updatedConfig.ChannelGroup.Groups["Orderer"].Values["ConsensusType"]
354-
consensusTypeValue := &protosorderer.ConsensusType{}
355-
err := proto.Unmarshal(consensusTypeConfigValue.Value, consensusTypeValue)
356-
Expect(err).NotTo(HaveOccurred())
357-
358-
metadata := &ectdraft_protos.ConfigMetadata{}
359-
err = proto.Unmarshal(consensusTypeValue.Metadata, metadata)
360-
Expect(err).NotTo(HaveOccurred())
361-
362-
metadata.Consenters = append(metadata.Consenters, &consenter)
363-
364-
consensusTypeValue.Metadata, err = proto.Marshal(metadata)
365-
Expect(err).NotTo(HaveOccurred())
366-
367-
updatedConfig.ChannelGroup.Groups["Orderer"].Values["ConsensusType"] = &common.ConfigValue{
368-
ModPolicy: "Admins",
369-
Value: protoutil.MarshalOrPanic(consensusTypeValue),
370-
}
371-
372-
return config, updatedConfig
373-
}
374-
375-
// ConsensusMetadataMutator receives ConsensusType.Metadata and mutates it
290+
// ConsensusMetadataMutator receives ConsensusType.Metadata and mutates it.
376291
type ConsensusMetadataMutator func([]byte) []byte
377292

378-
// UpdateConsensusMetadata executes a config update that updates the consensus metadata according to the given ConsensusMetadataMutator
293+
// UpdateConsensusMetadata executes a config update that updates the consensus
294+
// metadata according to the given ConsensusMetadataMutator.
379295
func UpdateConsensusMetadata(network *Network, peer *Peer, orderer *Orderer, channel string, mutateMetadata ConsensusMetadataMutator) {
380296
config := GetConfig(network, peer, orderer, channel)
381297
updatedConfig := proto.Clone(config).(*common.Config)
@@ -394,18 +310,3 @@ func UpdateConsensusMetadata(network *Network, peer *Peer, orderer *Orderer, cha
394310

395311
UpdateOrdererConfig(network, orderer, channel, config, updatedConfig, peer, orderer)
396312
}
397-
398-
// UpdateEtcdRaftMetadata executes a config update that updates the etcdraft metadata according to the given function f
399-
func UpdateEtcdRaftMetadata(network *Network, peer *Peer, orderer *Orderer, channel string, f func(md *ectdraft_protos.ConfigMetadata)) {
400-
UpdateConsensusMetadata(network, peer, orderer, channel, func(originalMetadata []byte) []byte {
401-
metadata := &ectdraft_protos.ConfigMetadata{}
402-
err := proto.Unmarshal(originalMetadata, metadata)
403-
Expect(err).NotTo(HaveOccurred())
404-
405-
f(metadata)
406-
407-
newMetadata, err := proto.Marshal(metadata)
408-
Expect(err).NotTo(HaveOccurred())
409-
return newMetadata
410-
})
411-
}

0 commit comments

Comments
 (0)