Unverified Commit 4f802d51 authored by yacovm's avatar yacovm Committed by Artem Barger
Browse files

[FAB-13331] Refactor metadata updates in nwo



This change set refactors metadata updates by making them
use a function that dictates how to handle consensus metadata.

Change-Id: I3aa68e4b268a24887e4cba891e02ebce1a2ec65d
Signed-off-by: default avataryacovm <yacovm@il.ibm.com>
parent 567981aa
......@@ -26,10 +26,7 @@ import (
"github.com/hyperledger/fabric/core/aclmgmt/resources"
"github.com/hyperledger/fabric/integration/nwo"
"github.com/hyperledger/fabric/integration/nwo/commands"
"github.com/hyperledger/fabric/protos/common"
protosorderer "github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric/protos/orderer/etcdraft"
"github.com/hyperledger/fabric/protos/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
......@@ -270,32 +267,20 @@ var _ = Describe("EndToEnd", func() {
nwo.DeployChaincode(network, "testchannel", orderer, chaincode)
RunQueryInvokeQuery(network, orderer, peer, "testchannel")
config := nwo.GetConfig(network, peer, orderer, channel)
updatedConfig := proto.Clone(config).(*common.Config)
consensusTypeConfigValue := updatedConfig.ChannelGroup.Groups["Orderer"].Values["ConsensusType"]
consensusTypeValue := &protosorderer.ConsensusType{}
err := proto.Unmarshal(consensusTypeConfigValue.Value, consensusTypeValue)
Expect(err).NotTo(HaveOccurred())
metadata := &etcdraft.Metadata{}
err = proto.Unmarshal(consensusTypeValue.Metadata, metadata)
Expect(err).NotTo(HaveOccurred())
// update max in flight messages
metadata.Options.MaxInflightMsgs = 1000
metadata.Options.MaxSizePerMsg = 512
nwo.UpdateConsensusMetadata(network, peer, orderer, channel, func(originalMetadata []byte) []byte {
metadata := &etcdraft.Metadata{}
err := proto.Unmarshal(originalMetadata, metadata)
Expect(err).NotTo(HaveOccurred())
// write metadata back
consensusTypeValue.Metadata, err = proto.Marshal(metadata)
Expect(err).NotTo(HaveOccurred())
// update max in flight messages
metadata.Options.MaxInflightMsgs = 1000
metadata.Options.MaxSizePerMsg = 512
updatedConfig.ChannelGroup.Groups["Orderer"].Values["ConsensusType"] = &common.ConfigValue{
ModPolicy: "Admins",
Value: utils.MarshalOrPanic(consensusTypeValue),
}
nwo.UpdateOrdererConfig(network, orderer, channel, config, updatedConfig, peer, orderer)
// write metadata back
newMetadata, err := proto.Marshal(metadata)
Expect(err).NotTo(HaveOccurred())
return newMetadata
})
})
})
......
......@@ -11,10 +11,14 @@ import (
"os"
"path/filepath"
"bytes"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/tools/configtxlator/update"
"github.com/hyperledger/fabric/integration/nwo/commands"
"github.com/hyperledger/fabric/protos/common"
protosorderer "github.com/hyperledger/fabric/protos/orderer"
ectdraft_protos "github.com/hyperledger/fabric/protos/orderer/etcdraft"
"github.com/hyperledger/fabric/protos/utils"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
......@@ -202,3 +206,62 @@ func UnmarshalBlockFromFile(blockFile string) *common.Block {
return block
}
// AddConsenter adds a new consenter to the given channel
func AddConsenter(n *Network, peer *Peer, orderer *Orderer, channel string, consenter ectdraft_protos.Consenter) {
UpdateConsensusMetadata(n, peer, orderer, channel, func(originalMetadata []byte) []byte {
metadata := &ectdraft_protos.Metadata{}
err := proto.Unmarshal(originalMetadata, metadata)
Expect(err).NotTo(HaveOccurred())
metadata.Consenters = append(metadata.Consenters, &consenter)
newMetadata, err := proto.Marshal(metadata)
Expect(err).NotTo(HaveOccurred())
return newMetadata
})
}
// RemoveConsenter removes a consenter with the given certificate in PEM format from the given channel
func RemoveConsenter(n *Network, peer *Peer, orderer *Orderer, channel string, certificate []byte) {
UpdateConsensusMetadata(n, peer, orderer, channel, func(originalMetadata []byte) []byte {
metadata := &ectdraft_protos.Metadata{}
err := proto.Unmarshal(originalMetadata, metadata)
Expect(err).NotTo(HaveOccurred())
var newConsenters []*ectdraft_protos.Consenter
for _, consenter := range metadata.Consenters {
if bytes.Equal(consenter.ClientTlsCert, certificate) || bytes.Equal(consenter.ServerTlsCert, certificate) {
continue
}
newConsenters = append(newConsenters, consenter)
}
metadata.Consenters = newConsenters
newMetadata, err := proto.Marshal(metadata)
Expect(err).NotTo(HaveOccurred())
return newMetadata
})
}
// ConsensusMetadataMutator receives ConsensusType.Metadata and mutates it
type ConsensusMetadataMutator func([]byte) []byte
// UpdateConsensusMetadata executes a config update that updates the consensus metadata according to the given ConsensusMetadataMutator
func UpdateConsensusMetadata(network *Network, peer *Peer, orderer *Orderer, channel string, mutateMetadata ConsensusMetadataMutator) {
config := GetConfig(network, peer, orderer, channel)
updatedConfig := proto.Clone(config).(*common.Config)
consensusTypeConfigValue := updatedConfig.ChannelGroup.Groups["Orderer"].Values["ConsensusType"]
consensusTypeValue := &protosorderer.ConsensusType{}
err := proto.Unmarshal(consensusTypeConfigValue.Value, consensusTypeValue)
Expect(err).NotTo(HaveOccurred())
consensusTypeValue.Metadata = mutateMetadata(consensusTypeValue.Metadata)
updatedConfig.ChannelGroup.Groups["Orderer"].Values["ConsensusType"] = &common.ConfigValue{
ModPolicy: "Admins",
Value: utils.MarshalOrPanic(consensusTypeValue),
}
UpdateOrdererConfig(network, orderer, channel, config, updatedConfig, peer, orderer)
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment