Skip to content

Commit

Permalink
break import cycle to prevent code duplication
Browse files Browse the repository at this point in the history
Signed-off-by: May Rosenbaum <mayro1595@gmail.com>
  • Loading branch information
MayRosenbaum committed Jan 21, 2024
1 parent 7f404af commit a78393f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 118 deletions.
62 changes: 3 additions & 59 deletions orderer/common/msgprocessor/maintenancefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ package msgprocessor

import (
"bytes"
"time"

"github.com/SmartBFT-Go/consensus/pkg/types"
"github.com/hyperledger/fabric/orderer/consensus/smartbft/util"

"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/orderer"
Expand Down Expand Up @@ -141,7 +141,7 @@ func (mf *MaintenanceFilter) inspect(configEnvelope *cb.ConfigEnvelope, ordererC
return errors.Wrap(err, "failed to unmarshal BFT metadata configuration")
}

_, err := validateBFTMetadataOptions(1, updatedMetadata)
_, err := util.ConfigFromMetadataOptions(1, updatedMetadata)
if err != nil {
return errors.New("invalid BFT metadata configuration")
}
Expand Down Expand Up @@ -210,62 +210,6 @@ func (mf *MaintenanceFilter) ensureConsensusTypeChangeOnly(configEnvelope *cb.Co
return nil
}

func validateBFTMetadataOptions(selfID uint64, options *smartbft.Options) (types.Configuration, error) {
var err error

config := types.DefaultConfig
config.SelfID = selfID

if options == nil {
return config, errors.New("config metadata options field is nil")
}

config.RequestBatchMaxCount = options.RequestBatchMaxCount
config.RequestBatchMaxBytes = options.RequestBatchMaxBytes
if config.RequestBatchMaxInterval, err = time.ParseDuration(options.RequestBatchMaxInterval); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestBatchMaxInterval")
}
config.IncomingMessageBufferSize = options.IncomingMessageBufferSize
config.RequestPoolSize = options.RequestPoolSize
if config.RequestForwardTimeout, err = time.ParseDuration(options.RequestForwardTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestForwardTimeout")
}
if config.RequestComplainTimeout, err = time.ParseDuration(options.RequestComplainTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestComplainTimeout")
}
if config.RequestAutoRemoveTimeout, err = time.ParseDuration(options.RequestAutoRemoveTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestAutoRemoveTimeout")
}
if config.ViewChangeResendInterval, err = time.ParseDuration(options.ViewChangeResendInterval); err != nil {
return config, errors.Wrap(err, "bad config metadata option ViewChangeResendInterval")
}
if config.ViewChangeTimeout, err = time.ParseDuration(options.ViewChangeTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option ViewChangeTimeout")
}
if config.LeaderHeartbeatTimeout, err = time.ParseDuration(options.LeaderHeartbeatTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option LeaderHeartbeatTimeout")
}
config.LeaderHeartbeatCount = options.LeaderHeartbeatCount
if config.CollectTimeout, err = time.ParseDuration(options.CollectTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option CollectTimeout")
}
config.SyncOnStart = options.SyncOnStart
config.SpeedUpViewChange = options.SpeedUpViewChange

config.LeaderRotation = false
config.DecisionsPerLeader = 0

if err = config.Validate(); err != nil {
return config, errors.Wrap(err, "config validation failed")
}

if options.RequestMaxBytes == 0 {
config.RequestMaxBytes = config.RequestBatchMaxBytes
}

return config, nil
}

func validateBFTConsenterMapping(currentOrdererConfig channelconfig.Orderer, nextOrdererConfig channelconfig.Orderer) error {
// extract raft consenters from consensusTypeValue.metadata
raftMetadata := &etcdraft.ConfigMetadata{}
Expand Down
2 changes: 1 addition & 1 deletion orderer/consensus/smartbft/consenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (c *Consenter) HandleChain(support consensus.ConsenterSupport, metadata *cb
c.Logger.Panicf("Failed initializing block puller")
}

config, err := configFromMetadataOptions((uint64)(selfID), configOptions)
config, err := ConfigFromMetadataOptions((uint64)(selfID), configOptions)

Check failure on line 191 in orderer/consensus/smartbft/consenter.go

View workflow job for this annotation

GitHub Actions / Basic Checks

undefined: ConfigFromMetadataOptions

Check failure on line 191 in orderer/consensus/smartbft/consenter.go

View workflow job for this annotation

GitHub Actions / Basic Checks

undefined: ConfigFromMetadataOptions
if err != nil {
return nil, errors.Wrap(err, "failed parsing smartbft configuration")
}
Expand Down
60 changes: 2 additions & 58 deletions orderer/consensus/smartbft/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"encoding/pem"
"fmt"
"sort"
"time"

"github.com/SmartBFT-Go/consensus/pkg/types"
"github.com/SmartBFT-Go/consensus/smartbftprotos"
Expand All @@ -30,6 +29,7 @@ import (
"github.com/hyperledger/fabric/orderer/common/localconfig"
"github.com/hyperledger/fabric/orderer/consensus"
"github.com/hyperledger/fabric/orderer/consensus/etcdraft"
"github.com/hyperledger/fabric/orderer/consensus/smartbft/util"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -119,7 +119,7 @@ func configBlockToBFTConfig(selfID uint64, block *cb.Block, bccsp bccsp.BCCSP) (
return types.Configuration{}, err
}

return configFromMetadataOptions(selfID, consensusConfigOptions)
return util.ConfigFromMetadataOptions(selfID, consensusConfigOptions)
}

//go:generate counterfeiter -o mocks/mock_blockpuller.go . BlockPuller
Expand Down Expand Up @@ -189,62 +189,6 @@ func getViewMetadataFromBlock(block *cb.Block) (*smartbftprotos.ViewMetadata, er
return &viewMetadata, nil
}

func configFromMetadataOptions(selfID uint64, options *smartbft.Options) (types.Configuration, error) {
var err error

config := types.DefaultConfig
config.SelfID = selfID

if options == nil {
return config, errors.New("config metadata options field is nil")
}

config.RequestBatchMaxCount = options.RequestBatchMaxCount
config.RequestBatchMaxBytes = options.RequestBatchMaxBytes
if config.RequestBatchMaxInterval, err = time.ParseDuration(options.RequestBatchMaxInterval); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestBatchMaxInterval")
}
config.IncomingMessageBufferSize = options.IncomingMessageBufferSize
config.RequestPoolSize = options.RequestPoolSize
if config.RequestForwardTimeout, err = time.ParseDuration(options.RequestForwardTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestForwardTimeout")
}
if config.RequestComplainTimeout, err = time.ParseDuration(options.RequestComplainTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestComplainTimeout")
}
if config.RequestAutoRemoveTimeout, err = time.ParseDuration(options.RequestAutoRemoveTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestAutoRemoveTimeout")
}
if config.ViewChangeResendInterval, err = time.ParseDuration(options.ViewChangeResendInterval); err != nil {
return config, errors.Wrap(err, "bad config metadata option ViewChangeResendInterval")
}
if config.ViewChangeTimeout, err = time.ParseDuration(options.ViewChangeTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option ViewChangeTimeout")
}
if config.LeaderHeartbeatTimeout, err = time.ParseDuration(options.LeaderHeartbeatTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option LeaderHeartbeatTimeout")
}
config.LeaderHeartbeatCount = options.LeaderHeartbeatCount
if config.CollectTimeout, err = time.ParseDuration(options.CollectTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option CollectTimeout")
}
config.SyncOnStart = options.SyncOnStart
config.SpeedUpViewChange = options.SpeedUpViewChange

config.LeaderRotation = false
config.DecisionsPerLeader = 0

if err = config.Validate(); err != nil {
return config, errors.Wrap(err, "config validation failed")
}

if options.RequestMaxBytes == 0 {
config.RequestMaxBytes = config.RequestBatchMaxBytes
}

return config, nil
}

type request struct {
sigHdr *cb.SignatureHeader
envelope *cb.Envelope
Expand Down
71 changes: 71 additions & 0 deletions orderer/consensus/smartbft/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package util

import (
"time"

"github.com/SmartBFT-Go/consensus/pkg/types"
"github.com/hyperledger/fabric-protos-go/orderer/smartbft"
"github.com/pkg/errors"
)

func ConfigFromMetadataOptions(selfID uint64, options *smartbft.Options) (types.Configuration, error) {
var err error

config := types.DefaultConfig
config.SelfID = selfID

if options == nil {
return config, errors.New("config metadata options field is nil")
}

config.RequestBatchMaxCount = options.RequestBatchMaxCount
config.RequestBatchMaxBytes = options.RequestBatchMaxBytes
if config.RequestBatchMaxInterval, err = time.ParseDuration(options.RequestBatchMaxInterval); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestBatchMaxInterval")
}
config.IncomingMessageBufferSize = options.IncomingMessageBufferSize
config.RequestPoolSize = options.RequestPoolSize
if config.RequestForwardTimeout, err = time.ParseDuration(options.RequestForwardTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestForwardTimeout")
}
if config.RequestComplainTimeout, err = time.ParseDuration(options.RequestComplainTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestComplainTimeout")
}
if config.RequestAutoRemoveTimeout, err = time.ParseDuration(options.RequestAutoRemoveTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option RequestAutoRemoveTimeout")
}
if config.ViewChangeResendInterval, err = time.ParseDuration(options.ViewChangeResendInterval); err != nil {
return config, errors.Wrap(err, "bad config metadata option ViewChangeResendInterval")
}
if config.ViewChangeTimeout, err = time.ParseDuration(options.ViewChangeTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option ViewChangeTimeout")
}
if config.LeaderHeartbeatTimeout, err = time.ParseDuration(options.LeaderHeartbeatTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option LeaderHeartbeatTimeout")
}
config.LeaderHeartbeatCount = options.LeaderHeartbeatCount
if config.CollectTimeout, err = time.ParseDuration(options.CollectTimeout); err != nil {
return config, errors.Wrap(err, "bad config metadata option CollectTimeout")
}
config.SyncOnStart = options.SyncOnStart
config.SpeedUpViewChange = options.SpeedUpViewChange

config.LeaderRotation = false
config.DecisionsPerLeader = 0

if err = config.Validate(); err != nil {
return config, errors.Wrap(err, "config validation failed")
}

if options.RequestMaxBytes == 0 {
config.RequestMaxBytes = config.RequestBatchMaxBytes
}

return config, nil
}

0 comments on commit a78393f

Please sign in to comment.