How To connect to Hyperledger Fabric network’s Peer Smart Code from External Service

To connect to Blockchain network’s Peer(e.g. peer0.org2.example.com:7051) where chain code deployed, we usually use below three packages :

1. “github.com/hyperledger/fabric-sdk-go/pkg/fabsdk”

returns an instance of FabricSDK for given configuration file

var Sdk *fabsdk.FabricSDK

https://github.com/hyperledger/fabric-sdk-go/blob/cb4ad138c14fd98039c56ab6301e665254a930bc/doc.go

2. “github.com/hyperledger/fabric-sdk-go/pkg/client/channel”

Get the Channel Client Context by passing the reference of the instance of Fabric SDK crated from previous step

var Client *channel.Client

Refer for Query and Execute

https://github.com/hyperledger/fabric-sdk-go/blob/master/pkg/client/channel/chclient.go

// If calling Query
response, err := instance.Client.Query(channel.Request{ChaincodeID: request.ChaincodeId, Fcn: request.Fn, Args: argsAsBytes})
if err != nil {
utils.Info(“Failed to query cc: %s”, err)
}

// If calling Invoke
response, err := instance.Client.Execute(channel.Request{ChaincodeID: request.ChaincodeId, Fcn: request.Fn, Args: argsAsBytes})
if err != nil {
utils.Info(“Failed to invoke cc: %s\n”, err)
}

3. “github.com/hyperledger/fabric-sdk-go/pkg/client/ledger”

If needed ledger Client then go to this package and then pass Channel Context.

var LedgerClient *ledger.Client

References : (How the combination of 1,2,3 have been used to access the deployed chain code in network)

func queryCC(t *testing.T, client *channel.Client, targetEndpoints …string) []byte {
response, err := client.Query(channel.Request{ChaincodeID: ccID, Fcn: “invoke”, Args: integration.ExampleCCDefaultQueryArgs()},
channel.WithRetry(retry.DefaultChannelOpts),
channel.WithTargetEndpoints(targetEndpoints…),
)
if err != nil {
t.Fatalf(“Failed to query funds: %s”, err)
}
return response.Payload
}

https://github.com/hyperledger/fabric-sdk-go/blob/b2585b784ebff1cdb1642cf04ac743bf32547784/test/integration/e2e/end_to_end.go

https://github.com/hyperledger/fabric-sdk-go/blob/b2585b784ebff1cdb1642cf04ac743bf32547784/test/integration/e2e/end_to_end.go

https://github.com/hyperledger/fabric-sdk-go/blob/master/test/integration/pkg/client/ledger/ledger_queries_test.go

https://github.com/hyperledger/fabric-sdk-go/blob/master/pkg/client/ledger/ledger.go

https://github.com/hyperledger/fabric-sdk-go/blob/master/test/integration/pkg/client/ledger/channel_ledger_test.go

https://github.com/hyperledger/fabric-sdk-go/blob/dcb2645139205ca2f39550ad8c4fc159cb0f2082/test/integration/pkg/client/ledger/channel_ledger_test.go

Also refer :

https://www.skcript.com/svr/setting-hyperledger-fabric-network-multiple-chaincodes-channels/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s