Skip to content

Commit

Permalink
Refactor orderer block fetching with retry logic and update inspect c…
Browse files Browse the repository at this point in the history
…ommand to use ledger client

- Replaced manual retry logic in `fetchOrdererChannelBlock` and `fetchConfigBlock` methods with a retry option from the Fabric SDK, improving code readability and maintainability.
- Updated the `inspectChannelCmd` to utilize the new ledger client for querying the configuration block, enhancing the command's functionality and performance.
- Removed deprecated code related to resource management client in the inspect command.

These changes streamline the process of fetching orderer blocks and improve the overall robustness of the channel inspection functionality.

Signed-off-by: David VIEJO <[email protected]>
  • Loading branch information
dviejokfs committed Jan 14, 2025
1 parent 44dd66f commit 44d7402
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
34 changes: 15 additions & 19 deletions controllers/mainchannel/mainchannel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
cb "github.com/hyperledger/fabric-protos-go/common"
sb "github.com/hyperledger/fabric-protos-go/orderer/smartbft"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
fab2 "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
Expand Down Expand Up @@ -397,18 +398,12 @@ func (r *FabricMainChannelReconciler) joinInternalOrderers(ctx context.Context,
func (r *FabricMainChannelReconciler) fetchOrdererChannelBlock(resClient *resmgmt.Client, fabricMainChannel *hlfv1alpha1.FabricMainChannel, resmgmtOptions []resmgmt.RequestOption) (*common.Block, error) {
var ordererChannelBlock *common.Block
var err error
attemptsLeft := 5
for {
ordererChannelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...)
if err == nil || attemptsLeft == 0 {
break
}
if err != nil {
attemptsLeft--
}
log.Infof("Failed to get block %v, attempts left %d", err, attemptsLeft)
time.Sleep(1500 * time.Millisecond)
}
resmgmtOptions = append(resmgmtOptions, resmgmt.WithRetry(retry.Opts{
Attempts: 5,
InitialBackoff: 1000 * time.Millisecond,
MaxBackoff: 10 * time.Second,
}))
ordererChannelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...)
if err != nil {
return nil, errors.Wrapf(err, "failed to get block from channel %s", fabricMainChannel.Spec.Name)
}
Expand Down Expand Up @@ -500,14 +495,15 @@ func (r *FabricMainChannelReconciler) setupResmgmtOptions(fabricMainChannel *hlf
func (r *FabricMainChannelReconciler) fetchConfigBlock(resClient *resmgmt.Client, fabricMainChannel *hlfv1alpha1.FabricMainChannel, resmgmtOptions []resmgmt.RequestOption) ([]byte, error) {
var channelBlock *cb.Block
var err error
resmgmtOptions = append(resmgmtOptions, resmgmt.WithRetry(retry.Opts{
Attempts: 5,
InitialBackoff: 1000 * time.Millisecond,
MaxBackoff: 10 * time.Second,
}))

for i := 0; i < 5; i++ {
channelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...)
if err == nil {
break
}
log.Warnf("Attempt %d failed to query config block from orderer: %v retrying in 1 second", i+1, err)
time.Sleep(1 * time.Second)
channelBlock, err = resClient.QueryConfigBlockFromOrderer(fabricMainChannel.Spec.Name, resmgmtOptions...)
if err != nil {
return nil, errors.Wrapf(err, "failed to query config block from orderer %s", fabricMainChannel.Spec.Name)
}

if err != nil {
Expand Down
40 changes: 23 additions & 17 deletions kubectl-hlf/cmd/channel/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package channel
import (
"bytes"
"fmt"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"io"
"io/ioutil"

"github.com/hyperledger/fabric-config/protolator"
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric/common/tools/protolator"
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
"github.com/spf13/cobra"
"io"
)

type inspectChannelCmd struct {
Expand Down Expand Up @@ -42,26 +43,31 @@ func (c *inspectChannelCmd) run(out io.Writer) error {
if err != nil {
return err
}
org1AdminClientContext := sdk.Context(
fabsdk.WithUser(c.userName),
fabsdk.WithOrg(mspID),
)
resClient, err := resmgmt.New(org1AdminClientContext)
// org1AdminClientContext := sdk.Context(
// fabsdk.WithUser(c.userName),
// fabsdk.WithOrg(mspID),
// )
// resClient, err := resmgmt.New(org1AdminClientContext)
// if err != nil {
// return err
// }
chContext := sdk.ChannelContext(c.channelName, fabsdk.WithUser(c.userName), fabsdk.WithOrg(mspID))
ledgerClient, err := ledger.New(chContext)
if err != nil {
return err
}
block, err := resClient.QueryConfigBlockFromOrderer(
c.channelName,
)
if err != nil {
return err
}
cmnConfig, err := resource.ExtractConfigFromBlock(block)
block, err := ledgerClient.QueryConfigBlock()
if err != nil {
return err
}
ioutil.WriteFile("block.json", []byte(block.String()), 0644)

// cmnConfig, err := resource.ExtractConfigFromBlock(block)
// if err != nil {
// return err
// }
var buf bytes.Buffer
err = protolator.DeepMarshalJSON(&buf, cmnConfig)
err = protolator.DeepMarshalJSON(&buf, block)
if err != nil {
return err
}
Expand Down

0 comments on commit 44d7402

Please sign in to comment.