<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sarah Whelan on Medium]]></title>
        <description><![CDATA[Stories by Sarah Whelan on Medium]]></description>
        <link>https://medium.com/@sarwhelan11?source=rss-840519dedaf8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HuTl8ZSpVb1F73bwsi8erg.jpeg</url>
            <title>Stories by Sarah Whelan on Medium</title>
            <link>https://medium.com/@sarwhelan11?source=rss-840519dedaf8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 15:29:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sarwhelan11/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Launching your Hyperledger Fabric Network using Fabric SDK Go]]></title>
            <link>https://medium.com/@sarwhelan11/launching-your-hyperledger-fabric-network-using-fabric-sdk-go-f086982ee62c?source=rss-840519dedaf8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f086982ee62c</guid>
            <category><![CDATA[private-blockchain]]></category>
            <category><![CDATA[blockchain-development]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[hyperledger-fabric]]></category>
            <dc:creator><![CDATA[Sarah Whelan]]></dc:creator>
            <pubDate>Fri, 21 Jun 2019 21:27:17 GMT</pubDate>
            <atom:updated>2019-06-24T18:38:38.684Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>DISCLAIMER: This tutorial was written in Summer 2018 using fabric-sdk-go <a href="https://github.com/hyperledger/fabric-sdk-go/releases/tag/v1.0.0-alpha4">v1.0.0-alpha4</a>! Hyperledger Fabric moves QUICKLY so this may be a bit out of date to the current version — but the core concepts and ideas should be relatively the same. Hope this helps you in your development journey 👩🏼‍💻!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*81IowkXk3FxKI2YXtD-tMA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@chuttersnap?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">chuttersnap</a> on <a href="https://unsplash.com/search/photos/blockchain?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Using <a href="https://github.com/hyperledger/fabric-sdk-go/blob/v1.0.0-alpha4/pkg/client/resmgmt/resmgmt.go">fabric-sdk-go’s </a><a href="https://github.com/hyperledger/fabric-sdk-go/blob/v1.0.0-alpha4/pkg/client/resmgmt/resmgmt.go">resmgmt package</a>, developers have the ability to incorporate network setup as an intrinsic function of their project, as well as make the setup process <em>reusable</em> and <em>modular</em>. By writing wrappers for the core functionality that resmgmt provides, you can create your own fully functioning package to handle every create, join, install, and instantiate request that you can dream of. No need to use the CLI!</p><p>This tutorial assumes you’re already familiar with the Hyperledger Fabric environment and with Docker — if you’re not, here is a great article on <a href="https://medium.com/coinmonks/demystifying-hyperledger-fabric-1-3-fabric-architecture-a2fdb587f6cb">Demystifying Hyperledger Fabric</a>, and <a href="https://docker-curriculum.com/">some Docker tutorials</a> 🐳</p><p>Loosely, the steps of this tutorial include: setting up a resource manager, creating a channel in your network, having peers join that channel, installing chaincode (on each peer), and instantiating chaincode (this happens on the channel — not on individual peers).</p><ul><li>Before we dive in, add a constraint to your Gopkg.toml file that specifies <a href="https://github.com/golang/dep">Dep</a> to vendor fabric-sdk-go version “master” (or something equivalent if using a different vendoring pkg) (read <a href="https://medium.freecodecamp.org/an-intro-to-dep-how-to-manage-your-golang-project-dependencies-7b07d84e7ba5">this</a> article if you’re new to vendoring pkgs!)</li></ul><p>If at any point you’re stuck and need some guidance, log onto the <a href="https://chat.hyperledger.org/home">Hyperledger Fabric Rocket Chat</a> for assistance. There’s a good chance someone has had the same issue(s) you’re having!</p><h4>Step 1: Define a fabric resource management package within your project directory</h4><p>This is where all functions, structs, and methods related to fabric resource management will be defined. We’ll call it fabmanager.</p><h4>Step 2: Define structs for channel configuration and chaincode configuration</h4><p>The ChannelConfig struct should include:</p><ul><li>the ChannelName</li><li>the Org that you want the context to be related to (e.g. the organization that this fabric manager represents)</li><li>the Path to the Docker file where the channel is defined wrt the org (the path wrt the container)</li><li>the ChaincodeConfig associated with this channel and with this context</li></ul><p>The ChaincodeConfig struct should include:</p><ul><li>the ChaincodeName</li><li>the Version of the chaincode</li><li>the Path to the chaincode folder (the path wrt the container)</li><li>the MSP name associated with the ChannelConfiguration.Org (e.g. if the org is “Org0” then the MSP should be “Org0MSP”)</li></ul><p>These structs are meant to give you an idea of the information required during the resource management process. You don’t necessarily need to persist it this way.</p><h4>Step 3: Define a fabric manager struct</h4><p>Creating an instance of a fabric manager is at the heart of simplistically making use of fabric-sdk-go’s resmgmt package.</p><p>The FabricManager struct should include:</p><ul><li>the ChannelConfiguration related to the context</li><li>the contextApi.ClientProvider (AKA <em>the</em> context)</li><li>the *resmgmt.Client (used to access the fabric-sdk-go resmgmt methods)</li></ul><p>These fields can all be private unless you wish to access them outside of the package.</p><h4>Step 4: Define a function that returns a fabric manager for a specified context</h4><p>Our primary goal is to make this package easy and straight-forward to use. This means that we don’t want to be passing in a ton of parameters every time we wish to manage some aspect of the fabric network, which is where having a dedicated fabric manager for a defined context comes in. <strong>By “context”, I mean a set of fabric channel configurations (including chaincode configurations) from the perspective of some user belonging to an organization</strong>. Basically, the fabric manager will execute resource management actions on behalf of the user/org and apply them on the channel/to the chaincode specified. We’ll implement methods in the upcoming steps that each instance of a FabricManager can access.</p><p>Your fabric manager initialization function should do the following:</p><ul><li>Accept an instance of the ChannelConfig struct</li></ul><pre>func InitFabricManager(chanConfig ChannelConfig) <br>     (FabricManager, error) { }</pre><ul><li>Initialize the context (this requires the config path and org from the ChannelConfig passed in)</li></ul><pre>// generate a ConfigProvider wrt the path where the YAML file exists // that defines the channel configuration<br>configProvider := config.FromFile(chanConfig.Path)</pre><pre>// use configProvider to generate an instance of *FabricSDK<br>sdk, err := fabsdk.New(configProvider)</pre><pre>// generate the contextApi.ClientProvider<br>clientContext := sdk.Context(fabsdk.WithUser(&quot;Admin&quot;),<br>                 fabsdk.WithOrg(chanConfig.Org))</pre><ul><li>Generate a resource management client using the context</li></ul><pre>resmgmtClient, err := resmgmt.New(clientContext)</pre><p>Now create and return an instance of the FabricManager struct using the ChannelConfig provided and the clientContext and resmgmtClient that you generated.</p><h4>Step 5: Define a method for channel creation</h4><p>The <a href="https://github.com/hyperledger/fabric-sdk-go/blob/v1.0.0-alpha4/pkg/client/resmgmt/resmgmt.go#L107">resmgmt.SaveChannelRequest</a> struct defines the parameters for a <a href="https://github.com/hyperledger/fabric-sdk-go/blob/v1.0.0-alpha4/pkg/client/resmgmt/resmgmt.go#L834">save channel request</a>: the channel ID, the path to the channel configuration transaction (this should be in your channel-artifacts), and a signing identity for the request.</p><p>Define the channel creation method as a public method, e.g.:</p><pre>func (fm FabricManager) CreateChannel() error { }</pre><p>To retrieve a signing identity, we’ll need to create a MSP client using the clientContext generated for this fabric manager:</p><pre>// generate a membership service provider client for the context<br>mspClient, err := msp.New(fm.clientContext)</pre><pre>// use the mspClient to get a signing identity for the admin user<br>adminId, err := mspClient.GetSigningIdentity(&quot;Admin&quot;)</pre><p>Next, structure and execute the request:</p><pre>// structure the SaveChannelRequest<br>saveChannelReq := resmgmt.SaveChannelRequest{<br>     ChannelID:         fm.channelConfig.Name,<br>     ChannelConfigPath: &quot;path/&quot; + fm.channelConfig.Name + &quot;.tx&quot;,<br>     SigningIdentities: []msp.SigningIdentity{adminId},<br>}</pre><pre>// execute the request<br>_, err := fm.resmgmtClient.SaveChannel(saveChannelReq,     <br>      resmgmt.WithRetry(retry.DefaultResMgmtOpts),<br>      resmgmt.WithOrdererEndpoint(&quot;OrdererEndptHere&quot;))</pre><h4>Step 6: Define a method for peers to join a channel</h4><pre>func (fm FabricManager) JoinChannel() error { }</pre><p>resmgmt.JoinChannel joins all peers belonging to the organization associated with the resource management client to a specified channel. For example, if the fm.resmgmtClient was generated with a clientContext organization being “Org0”, all peers belonging to Org0 would join the channel.</p><pre>err := fm.resmgmtClient.JoinChannel(fm.channelConfig.Name)</pre><h4>Step 7: Define a method to install chaincode</h4><p>In order for endorsing peers to accept transaction proposals (i.e. proposals to update the ledger) and generate transaction proposal responses, they need to independently execute chaincode. To do this, chaincode must be installed on the peer’s file system and instantiated on the channel that the peer belongs to. By default, installation will occur on all peers belonging to the organization’s MSP specified in the context used to generate fm.resmgmtClient.</p><pre>func (fm FabricManager) InstallChaincode() error { }</pre><p>To generate an instance of a <a href="https://github.com/hyperledger/fabric-sdk-go/blob/v1.0.0-alpha4/pkg/client/resmgmt/resmgmt.go#L52">resmgmt.InstallCCRequest</a>, the chaincode needs to be packaged. This requires supplying the gopath, which is likely &quot;&quot; within the peer containers (unless you’ve installed Go on them). gopackager.NewCCPackage supplies a default gopath if a blank string is provided. To work around this, set the gopath in your peer container(s) to a made up path, but ensure that the path to the chaincode files is relative to the gopath you set. For example, in the docker-compose file:</p><pre># set the gopath environment variable<br>- GOPATH=/opt/gopath</pre><pre># under &quot;volumes&quot;, map the host cc path to the container path wrt<br># the GOPATH set<br>- hostCCPath:/opt/gopath/restOfPathToCC</pre><p>Then in the InstallChaincode method:</p><pre>ccPackage, err := <br>     gopackager.NewCCPackage(fm.channelConfig.chaincodeConfig.Path,   <br>     os.Getenv(&quot;GOPATH&quot;))</pre><pre>installCCReq := resmgmt.InstallCCRequest{<br>     Name:    fm.channelConfig.chaincodeConfig.Name,<br>     Version: fm.channelConfig.chaincodeConfig.Version,<br>     Path:    fm.channelConfig.chaincodeConfig.Path,<br>     Package: ccPackage,<br>}</pre><pre>// execute the InstallCC request and gather peer responses<br>resps, err := fm.resmgmtClient.InstallCC(installCCReq)</pre><pre>// if installation is successful, peer response will be 200 OK<br>for _, resp := range resps {<br>     if resp.Status != http.StatusOK {<br>          // do something<br>     }<br>}</pre><p>Note: if you install the same chaincode more than once on the same peer(s), the response status code will be 0.</p><h4>Step 8: Define a method for chaincode instantiation</h4><p>An endorsement policy (EP) needs to be provided when instantiating chaincode onto a channel. An EP defines the set of peers that need to endorse (sign) a proposed ledger change (e.g. a transaction proposal response) before it can be accepted by the network (meaning that before the orderer orders the transaction into a block and distributes this to all peers in the channel, the endorsing peers must come to a consensus on the change’s outcome). Read more about the details of how endorsement policies work <a href="https://hyperledger-fabric.readthedocs.io/en/release-1.2/peers/peers.html#peers-and-orderers">here</a> and <a href="https://hyperledger-fabric.readthedocs.io/en/release-1.2/glossary.html#endorsement-policy">here</a>.</p><p>There are a few ways that you can deal with generating EPs. If you know that you’ll only be dealing with a relatively small number of chaincodes, you could create a switch statement and hard-code the EPs, e.g.:</p><pre>var ccPolicy *cb.SignaturePolicyEnvelope<br>switch fm.channelConfig.chaincodeConfig.Name {<br>     case chaincode-one:<br>          ep := []string{&quot;Org0MSP&quot;, &quot;Org1MSP&quot;}<br>          ccPolicy = cauthdsl.SignedByAnyMember(ep)<br>     case chaincode-two:<br>          ep := &quot;Org1MSP&quot;<br>          ccPolicy = cauthdsl.SignedByMspPeer(ep)<br>}</pre><p>chaincode-one requires any peer belonging to Org0MSP <em>or</em> Org1MSP to sign the transaction proposal response, whereas chaincode-two specifically requires a signature from a member of Org1MSP.</p><p>A more dynamic approach would be to provide an EP on instantiation of eachfabric manager. An EP struct could be defined as so:</p><pre>type EP struct {<br>     PolicyName string<br>     ManyPeers  []string<br>     SinglePeer string<br>}</pre><p>Note that ManyPeers and SinglePeer are mutually exclusive. If the policy name was “SignedByAnyMember” then len(ManyPeers) would be greater than one, and SinglePeer would be &quot;&quot;. You could then generate the EP by using a switch statement based on PolicyName. To explore possible EPs, <a href="https://github.com/hyperledger/fabric-sdk-go/blob/a906355f73d060d7bf95874a9e90dc17589edbb3/third_party/github.com/hyperledger/fabric/common/cauthdsl/cauthdsl_builder.go">view the </a><a href="https://github.com/hyperledger/fabric-sdk-go/blob/a906355f73d060d7bf95874a9e90dc17589edbb3/third_party/github.com/hyperledger/fabric/common/cauthdsl/cauthdsl_builder.go">cauthdsl package</a>.</p><p>Use whichever option you feel is more suitable to your use case(s).</p><p>Once an EP is generated, the resmgmt.InstantiateCCRequest can be structured and executed. You may find that timeouts occur during the request. To account for this, loop until it is successful:</p><pre>instCCReq := resmgmt.InstantiateCCRequest{<br>     Name:    fm.channelConfig.chaincodeConfig.Name,<br>     Version: fm.channelConfig.chaincodeConfig.Version,<br>     Path:    fm.channelConfig.chaincodeConfig.Path,.<br>     Policy:  ccPolicy,<br>}</pre><pre>var err error<br>for attempt := 0; attempt &lt;= 50; attempt++ {<br>     _, err = fm.resmgmtClient.InstantiateCC(fm.channelConfig.Name, <br>              instCCReq)<br>     if err == nil { break }<br>     // do something with err if you want<br>}</pre><p>It is important to realize that instantiation happens <em>on a channel</em> and <em>not on individual peers</em>. Any peers added to the channel in the future who have the chaincode installed will also be able to instantiate the chaincode. <strong>Peers spawn chaincode containers only when they need to.</strong></p><h4>Example Usage</h4><pre>import fabmanager</pre><pre>// instantiate a fabric manager for channelX and chaincodeX with the // context being Org0 </pre><pre>chanConfigX := fabmanager.ChannelConfig{<br>     Name:            &quot;channelX&quot;,<br>     Org:             &quot;Org0&quot;,<br>     Path:            pathToChannelDefinition.yaml,<br>     ChaincodeConfig: fabmanager.ChaincodeConfig{<br>          Name:       &quot;chaincodeX&quot;,<br>          Version:    &quot;0&quot;,<br>          Path:       pathToChaincodeFolder,<br>          MspMember:  &quot;Org0MSP&quot;<br>     }<br>}</pre><pre>fabMgrX, err := fabmanager.InitFabricManager(chanConfigX)</pre><pre>// create channel &quot;channelX&quot;<br>err = fabManagerX.CreateChannel()</pre><pre>// join peers belonging to Org0 to channelX<br>err = fabManagerX.JoinChannel()</pre><pre>// install chaincode &quot;chaincodeX&quot; onto peers of Org0<br>err = fabManagerX.InstallChaincode()</pre><pre>// instantiate chaincode &quot;chaincodeX&quot; onto &quot;channelX&quot;<br>// assuming that EP is assigned via chaincode name<br>err = fabManagerX.InstantiateChaincode()</pre><pre>// now let&#39;s have peers from another org join the channel and<br>// install the chaincode as wel</pre><pre>chanConfigXOrg1 := fabmanager.ChannelConfig{<br>     Name:            &quot;channelX&quot;,<br>     Org:             &quot;Org1&quot;,<br>     Path:            pathToChannelDefinition.yaml,<br>     ChaincodeConfig: fabmanager.ChaincodeConfig{<br>          Name:       &quot;chaincodeX&quot;,<br>          Version:    &quot;0&quot;,<br>          Path:       pathToChaincodeFolder,<br>          MspMember:  &quot;Org1MSP&quot;<br>     }<br>}</pre><pre>fabMgrXOrg1, err := fabmanager.InitFabricManager(chanConfigXOrg1)</pre><pre>// join peers belonging to Org1 to channelX<br>err = fabMgrXOrg1.JoinChannel()</pre><pre>// install chaincode &quot;chaincodeX&quot; onto peers of Org1<br>err = fabMgrXOrg1.InstallChaincode()</pre><h4>Extensions</h4><ul><li>If the channel to chaincode relationship is 1-to-many, you may consider implementing the ChannelConfig.ChaincodeConfig as an array of ChaincodeConfig instances</li><li>Implementing a method to <a href="https://github.com/hyperledger/fabric-sdk-go/blob/v1.0.0-alpha4/pkg/client/resmgmt/resmgmt.go#L504">UpgradeChaincode</a> would be very similar to that of InstallChaincode</li></ul><h4>Voila</h4><p>If you’re reading this far down…you probably accomplished something or at least <em>learned</em> something new! 👏🏼 You also probably noticed along the way that you had to make this tutorial fit <em>your</em> application. This isn’t a one-size-fits-all kind of thing — this is what worked for a particular project I was working on. It may be different in your case and making adjustments is okay (plus, you learn more when you can be adaptable!). Your key takeaways are hopefully along the lines of:</p><ul><li>Making things modular is actually hella useful.</li><li>Breaking a big task into small chunks is way more manageable.</li><li>Hyperledger Fabric is evolving and is fluid — development in general can be frustrating but it’s also really rewarding when things (finally 😀) go as planned.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f086982ee62c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>