<?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 Sam LAM on Medium]]></title>
        <description><![CDATA[Stories by Sam LAM on Medium]]></description>
        <link>https://medium.com/@reasdom?source=rss-922f4352103d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DSwWLvVb15pyd0A9Z-yjAg@2x.jpeg</url>
            <title>Stories by Sam LAM on Medium</title>
            <link>https://medium.com/@reasdom?source=rss-922f4352103d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 04:00:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@reasdom/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[A Brief Discussion on the Existence of Matter from the Perspective of Early Buddhism and the…]]></title>
            <link>https://medium.com/@reasdom/a-brief-discussion-on-the-existence-of-matter-from-the-perspective-of-early-buddhism-and-the-7d18c4c71baa?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d18c4c71baa</guid>
            <category><![CDATA[philosophy]]></category>
            <category><![CDATA[buddhist]]></category>
            <category><![CDATA[buddhism]]></category>
            <category><![CDATA[buddha]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Thu, 10 Apr 2025 07:44:48 GMT</pubDate>
            <atom:updated>2025-04-10T07:44:48.851Z</atom:updated>
            <content:encoded><![CDATA[<h3>Does a cup exist by itself?</h3><h3>A Brief Discussion on the Existence of Matter from the Perspective of Early Buddhism and the Perspective of Modern Physics</h3><p><strong>Introduction</strong></p><p>“The world is unstable; It is swept away.” (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, p. 141</a>)<a href="#_ftn1">[1]</a> In daily experience, “matter” to mundane people exists stably and independently. For instance, when one sees a cup, one differentiates it from the other objects in the scene, and it seems concrete and unchanged, like a mountain. Nevertheless, does the cup exist like that? This question seems stupid and obvious, but under a thoughtful analysis, might we have another unexpected response? Might we have an insight? More importantly, is the understanding of how matter exists related to our suffering? Mundane people often experience suffering due to their attachment and clinging to matter. For example, one loves a new headset (“my headset”) very much and worries about losing it all the time.</p><p>This short essay is to discuss the existence of matter from the perspective of Early Buddhism and the perspective of Modern Physics (specifically, the Theory of Relativity), and see if there is an insight on how matter exists. Buddhism is not only about the description (of the reality or truth) but also about the prescription (of the suffering) (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, p. 6</a>). Hence, the essay takes one more step forward: how is the understanding of the existence of matter related to the cessation of suffering in Early Buddhism?</p><p><strong>Definition</strong></p><p>According to Cambridge Dictionary, the definition of “matter” is “physical substance”. In other words, matter is established atomically. In Classical Physics, “matter” can be defined by intrinsic or primary qualities, such as size, shape, and mass (<a href="#_ENREF_1">Institute, 2013, 00:07:35</a>). In Early Buddhism, “matter” is the four modes of primary material behavior or the four great elements, including solidity (earth-element), liquidity (water-element), temperature (fire-element), and motion (wind-element) (<a href="#_ENREF_2">Somaratne, 2023A, p. 10</a>). Interestingly, in Modern Physics, scientists began reflecting on whether intrinsic qualities could define matter objectively (<a href="#_ENREF_1">Institute, 2013, 00:30:30</a>). It is similar to the question “Does the cup exist like that (stable, independent, concrete, and unchanged)?” raised in the introduction section. We started to question and challenge the past understanding of the physical world.</p><p><strong>Comparing Between the Existence of Matter from the Early Buddhism Perspective (Name-and-matter and Consciousness) and That from the Modern Physics Perspective (the Theory of Relativity)</strong></p><p><em>Early Buddhism Perspective</em></p><p>In Early Buddhism, matter exists for one as sights, sounds, smells, tastes, touches, and mental images (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, pp. 70–71</a>). Also, this existence is involved with consciousness (<a href="#_ENREF_2">Somaratne, 2023A, p. 29</a>) and must be associated with name together, which includes feeling, perception, and mental (subjective) configuration (<a href="#_ENREF_2">Somaratne, 2023A, pp. 6, 19</a>). For example, when one sees a cup, one uses the internal eye-base and eye organ to perceive the sight of the cup (<a href="#_ENREF_2">Somaratne, 2023A, pp. 10, 32</a>). Also, there must be name associated with the cup, such as our feelings for the cup (pleasant, painful, or neutral), perception of the cup, and how the cup exists from our point of view (e.g., beautiful cup, my cup). It is called name-and-matter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*eVRN1T819Wef2pZGV0U65A.png" /><figcaption>(<a href="#_ENREF_2">Somaratne, 2023A, p. 6</a>)</figcaption></figure><p>Finally, there is an observer. One comprehends the sensory object or phenomenon via the eye-consciousness (<a href="#_ENREF_2">Somaratne, 2023A, pp. 30, 32</a>). It is like a forest and its trees. We see a “forest” that is constructed by many trees. “Forest” does not exist by itself, but with feeling, perception, and mental (subjective) configuration to the trees as a whole, “forest” (just a name or concept) seems to exist on its own concretely. Hence, the existence of matter depends on the consciousness and name (name-and-matter). In other words, matter cannot be said to exist by itself, and it demonstrates itself as a sensory phenomenon (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, p. 151</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*1vwiJqU2E2Upw2uXoVNqmQ.png" /></figure><p><em>Modern Physics Perspective (the Theory of Relativity)</em></p><p>In the Theory of Relativity, there is a well-known thought experiment called “Pole and Barn”, which suggests “the failure of classical realism” under Modern Physics (<a href="#_ENREF_1">Institute, 2013, 00:18:15</a>). The thought experiment is to put a pole inside a barn and close the doors from 2 sides of the barn. Yet, the challenge is that the pole is longer than the barn. How should we accomplish this experiment? (<a href="#_ENREF_1">Institute, 2013, 00:19:55</a>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*og48whO7tixwXAKY_rC8VA.png" /><figcaption>(<a href="#_ENREF_1">Institute, 2013, 00:19:55</a>)</figcaption></figure><p>Thought Experiment (1): According to the Theory of Relativity, when the pole is moving very fast (say, 0.7 of light speed), it becomes shorter when it is seen from the barn. So, we can put the pole inside the barn and close the doors simultaneously. (<a href="#_ENREF_1">Institute, 2013, 00:22:30</a>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*W1BQ9M6P9VKv7hQ-TQC6iQ.png" /><figcaption>(<a href="#_ENREF_1">Institute, 2013, 00:22:30</a>)</figcaption></figure><p>Thought Experiment (2): Next, if we switch the viewpoint from the barn to the pole. What if the barn moves very fast this time? The barn becomes shorter, and the pole cannot fit in the barn definitely. Anyway, experiment (1) and (2) looks the same. (<a href="#_ENREF_1">Institute, 2013, 00:24:50</a>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*DI7XNl2vc3LpaaIPgS9GYQ.png" /><figcaption>(<a href="#_ENREF_1">Institute, 2013, 00:24:50</a>)</figcaption></figure><p>Now, the experiment (1) and (2) achieves different outcomes. Therefore, can we put the pole inside the barn? Scientists who experiment (1) think it is possible, and those who experiment (2) say that it is not possible. Albert Einstein answers that both are true because the result depends on the context or reference frame. That is, there is no single and absolute truth. (<a href="#_ENREF_1">Institute, 2013, 00:27:00</a>) Comparatively, in Classical Physics, intrinsic qualities such as size (e.g., length) define matter. And these qualities should be purely objective and observer-independent. By the way, in Modern Physics, these qualities like size depend on the context and reference frame. In other words, the intrinsic qualities of matter depend on the observer’s perspective (subjectivity). (<a href="#_ENREF_1">Institute, 2013, 00:30:30</a>)</p><p><em>Early Buddhism Perspective and Modern Physics (the Theory of Relativity) Perspective</em></p><p>There is a similarity between the Early Buddhism perspective and Modern Physics (the Theory of Relativity) perspective on the existence of matter: Matter cannot be said to exist by itself objectively. The existence of matter depends on the subject or observer. In Early Buddhism, that subject is the consciousness (<a href="#_ENREF_2">Somaratne, 2023A, pp. 30–32</a>). And the consciousness depends on sense-bases and name-and-matter (<a href="#_ENREF_2">Somaratne, 2023A, pp. 14, 16</a>). Without name, there is no matter; Without matter, there is no name (<a href="#_ENREF_2">Somaratne, 2023A, p. 44</a>). Even the “four great elements” that are so fundamental are cease-able with the cessation of consciousness and name (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, pp. 153–154</a>). In Modern Physics, that subject is the context or reference frame. Even the “intrinsic qualities” that are so foundational are observer-dependent (<a href="#_ENREF_1">Institute, 2013, 00:30:30</a>). With this knowledge, when one sees a cup, along with its size, shape, color, earth-element, water-element, fire-element, and wind-element, does it still seem concrete and unchanged? Both perspectives challenge the common understanding of the things around us.</p><p><strong>What Next? The Understanding of Existence of Matter and Cessation of Suffering in Early Buddhism</strong></p><p>Buddhism is not only about the reality description but also about the solution to suffering (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, p. 6</a>). Does the understanding of how matter exists lead to the cessation of suffering in Early Buddhism? “Body” and external objects are all matters. When one, the consciousness, perceives the body and external objects through the internal sense-bases and sense organs, one regards these matters as “my body” and “objects in my world” (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, p. 144</a>). Also, these matters are always associated with name (name-and-matter) (<a href="#_ENREF_2">Somaratne, 2023A, p. 19</a>). To a mundane person, both name-and-matter and consciousness are with clinging that is the consideration of “this is mine”, “this am I”, and “this is my self” (<a href="#_ENREF_2">Somaratne, 2023A, p. 48</a>). In this way, “my self” is constructed and assumed by the whole situation in these sensory experiences (<a href="#_ENREF_3">Somaratne, 2023B, pp. 5, 31</a>). With this belief of self, one projects the world with subjective configurations and suffers from craving, clinging, birth, aging, and death (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, pp. 133–134</a>)<a href="#_ftn1">[2]</a>.</p><p>The understanding of the existence of matter is that matter cannot be said to exist on its own (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, p. 151</a>). We attach and cling to “my body” and “objects in my world”, and we experience suffering. How can the clinging, “my body”, “objects in my world”, and even “the four great elements in my world” come to an end? Their footing, the consciousness, should cease first. In the end, to uproot the consciousness, one should end the subjective configurations and ignorance. Then, the whole mass of suffering ceases. It is the insight into the existence of matter, and it leads to the cessation of suffering. (<a href="#_ENREF_2">Somaratne, 2023A, p. 35</a>)</p><p><strong>Conclusion</strong></p><p>In summary, this short essay first shows the meaning of “matter” from daily usage according to the Cambridge Dictionary, its scientific interpretation in Classical Physics, and that in Early Buddhism. Next, it points out that in both Modern Physics and Early Buddhism, we challenge the past understanding of the matter. Subsequently, it compares the existence of matter from the Early Buddhism perspective and Modern Physics perspective, and there is a similarity: the existence of matter depends on the subject or observer.</p><p>Last but not least, the essay discusses how the understanding of the existence of matter can be related to the cessation of suffering, since the value of Buddhism is not only description but also prescription (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, p. 6</a>). From the insight that “matter cannot be said to exist by itself” (<a href="#_ENREF_4">Somaratne &amp; SpringerLink, 2021, p. 151</a>), we can investigate the cessation of consciousness (and the cessation of subjective configurations and ignorance) and finally, the removal of suffering. (<a href="#_ENREF_2">Somaratne, 2023A, p. 35</a>)</p><p><a href="#_ftnref1">[1]</a> It is reference from “Arahat Raṭṭhapāla — (M.82&gt;M.II.68)”</p><p><a href="#_ftnref1">[2]</a> It is reference from “Andhakāra-sutta (S.56.46&gt;S.V.454–5)”.</p><p><strong>Reference</strong></p><p>Institute, M. L. (2013). <em>Day2 pm — Mind and Life XXVI: Mind, Brain and Matter</em> [Video file]. <a href="https://www.youtube.com/watch?v=JzKKuPmk95g&amp;list=PLOafJ4rP1PHwafTGL23zXK29knJsXMbMg&amp;index=4">https://www.youtube.com/watch?v=JzKKuPmk95g&amp;list=PLOafJ4rP1PHwafTGL23zXK29knJsXMbMg&amp;index=4</a></p><p>Somaratne, G. A. (2023A). Sensory experience and the Five Clinging-Aggregates. In <em>BSTC6079 Early Buddhism</em>. Class Lecture 6 at The University of Hong Kong (HKU), 16 Oct 2023</p><p>Somaratne, G. A. (2023B). Not-self. In <em>BSTC6079 Early Buddhism</em>. Class Lecture 7 at The University of Hong Kong (HKU), 30 Oct 2023</p><p>Somaratne, G. A., &amp; SpringerLink. (2021). <em>The Buddha’s Teaching : A Buddhistic Analysis</em> (1st 2021. ed.). Springer Singapore : Imprint: Palgrave Macmillan.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d18c4c71baa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Danger of State History” — Hyperledger Fabric]]></title>
            <link>https://medium.com/@reasdom/danger-of-state-history-hyperledger-fabric-16766513bffe?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/16766513bffe</guid>
            <category><![CDATA[hyperledger-fabric]]></category>
            <category><![CDATA[blockchain-development]]></category>
            <category><![CDATA[hyperledger]]></category>
            <category><![CDATA[chaincode]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Sat, 18 May 2019 06:34:24 GMT</pubDate>
            <atom:updated>2019-05-18T06:34:24.066Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hyperledger Fabric — “Danger of State History”</h3><p>Reminder to people who used <strong><em>GetHistoryForKey</em></strong> in HLF Chaincode in v1.3.0, v1.2.1, v1.4.0, v1.1.2, v1.4.1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z2REkO4RIp73p4wESu_0IA.png" /><figcaption><a href="https://www.hyperledger.org/blog/2018/11/28/conducting-data-with-concerto-and-hyperledger-fabric">Hyperledger Fabric Image</a></figcaption></figure><h4>What is <strong><em>GetHistoryForKey</em></strong></h4><p><strong><em>GetHistoryForKey </em></strong>is a Chaincode function to obtain <strong><em>blockchain state histories</em></strong>. The process is to get <strong><em>indexes</em></strong> in history DB, then find state histories <strong><em>from the ledger</em></strong> using those indexes.</p><p>But, recently, I found that the <strong><em>GetHistoryForKey</em></strong> function in HLF Chaincode behaves strangely — some histories of data seem <strong><em>lost</em></strong>!</p><blockquote>Is it because of bugs in Chaincode stub, or history DB, or Fabric code? What happens?</blockquote><p>After investigation and searching, I found that there is a new Jira: <a href="https://jira.hyperledger.org/browse/FAB-15450?jql=project%20%3D%20FAB%20AND%20issuetype%20%3D%20Bug%20AND%20text%20~%20history">https://jira.hyperledger.org/browse/FAB-15450?jql=project%20%3D%20FAB%20AND%20issuetype%20%3D%20Bug%20AND%20text%20~%20history</a></p><p>Let me quote the problem from Jira:</p><blockquote>[…] at multiples of 256, the encoded block number in the history db key will contain nil bytes, and therefore these history keys will be filtered out from the history results. […]</blockquote><h4>So…</h4><p>In short, <strong><em>histories data are still here! Just we cannot get some of them successfully…</em></strong> (those cannot be gotten are transactions in blocks with block number that is multiple of 256, e.g. block 256, 512)</p><h4>What to do before it is fixed</h4><p>Before this issue is fixed, these are possible solutions:</p><ol><li>Code a block reader, store indexes in another database, then get blocks and histories via Fabric SDK</li><li>State histories are temporarily stored in another database</li><li>Re-implement Chaincode to avoid <strong><em>GetHistoryForKey</em></strong></li></ol><p>Note that each possible solution has <strong><em>multiple ways to implement</em></strong> as well. Let me give you a simple and naive example of (3): just storing state history using another state.</p><p>Later, when the bug is fixed, you can upgrade Fabric.</p><p>[Update] From Jira, I saw:</p><blockquote>In v2.0, we can rebuild history db to a new format that protects against these edge cases.</blockquote><p><strong>Thanks! If you like my stories, </strong><a href="https://medium.com/@reasdom"><strong>please follow me</strong></a><strong> for new updates!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16766513bffe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hyperledger Fabric — The Taste of Raft]]></title>
            <link>https://medium.com/coinmonks/hyperledger-fabric-the-taste-of-raft-4f9f0df20b5e?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f9f0df20b5e</guid>
            <category><![CDATA[consensus]]></category>
            <category><![CDATA[blockchain-development]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[hyperledger]]></category>
            <category><![CDATA[hyperledger-fabric]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Thu, 18 Apr 2019 06:26:23 GMT</pubDate>
            <atom:updated>2020-08-26T13:37:09.991Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hyperledger Fabric — The Taste of Raft</h3><h4>Let’s try the newly added consensus — Raft, in Fabric</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z2REkO4RIp73p4wESu_0IA.png" /><figcaption><a href="https://www.hyperledger.org/blog/2018/11/28/conducting-data-with-concerto-and-hyperledger-fabric">Hyperledger Fabric Image</a></figcaption></figure><h4>Introduction</h4><p>Since Fabric <strong><em>1.4.1</em></strong>, Fabric supports a new consensus called <strong><em>Raft</em></strong>, rather than just Kafka and Solo. This short article is to “taste” Raft in Fabric’s Build-Your-First-Network tutorial.</p><blockquote>Later, you will see that I shutdown some orderers in which each of them is a Raft leader. it is to show you that orderers using Raft can:</blockquote><blockquote><strong>Re-elect leader </strong>under crash of leader node <strong>automatically</strong></blockquote><blockquote><a href="https://coincodecap.com">Discover and review best Blockchain softwares</a></blockquote><h4>Setting Up the Environment</h4><p>We use Fabric’s <a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html">Build-Your-First-Network</a>, which contains:</p><ul><li>2 organizations</li><li>5 orderers</li><li>2 peers in each org, with CouchDB</li><li>A command line interface to control peers and orderers</li><li>A channel (mychannel)</li><li>A simple chaincode by Fabric</li></ul><p>First of all, prepare the prerequisites by <strong><em>following instructions below</em></strong>:</p><p><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html">https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html</a></p><p><strong><em>After</em></strong> prerequisites are satisfied, open your terminal, locate to a place you like, e.g. Desktop.</p><p>Then type the following command:</p><pre>curl <strong>-</strong>sSL http:<strong>//</strong>bit<strong>.</strong>ly<strong>/</strong>2ysbOFE <strong>|</strong> bash <strong>-</strong>s</pre><p>This command is to download Fabric docker images and pull a directory <strong><em>fabric-samples</em></strong> which contains the tutorial files.</p><p>Now follow the commands <strong><em>one by one</em></strong> below:</p><pre>cd fabric<strong>-</strong>samples<strong>/</strong>first<strong>-</strong>network</pre><pre><strong>../</strong>bin<strong>/</strong>cryptogen generate <strong>--</strong>config<strong>=./</strong>crypto<strong>-</strong>config<strong>.</strong>yaml</pre><pre>export FABRIC_CFG_PATH=$PWD</pre><pre><strong>../</strong>bin<strong>/</strong>configtxgen <strong>-</strong>profile SampleMultiNodeEtcdRaft <strong>-</strong>channelID byfn<strong>-</strong>sys<strong>-</strong>channel <strong>-</strong>outputBlock <strong>./</strong>channel<strong>-</strong>artifacts<strong>/</strong>genesis<strong>.</strong>block</pre><pre>export CHANNEL_NAME=mychannel</pre><pre>../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME</pre><pre>../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP</pre><pre>../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP</pre><pre>docker<strong>-</strong>compose <strong>-</strong>f docker<strong>-</strong>compose<strong>-</strong>cli<strong>.</strong>yaml <strong>-</strong>f docker-compose-couch.yaml <strong>-</strong>f docker-compose-etcdraft2.yaml up <strong>-</strong>d</pre><pre>docker exec <strong>-</strong>it cli bash</pre><pre>CORE_PEER_MSPCONFIGPATH<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>users<strong>/</strong>Admin@org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>msp<br>CORE_PEER_ADDRESS<strong>=</strong>peer0<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com:7051<br>CORE_PEER_LOCALMSPID<strong>=</strong>&quot;Org1MSP&quot;<br>CORE_PEER_TLS_ROOTCERT_FILE<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>peers<strong>/</strong>peer0<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>tls<strong>/</strong>ca<strong>.</strong>crt</pre><pre>export CHANNEL_NAME=mychannel</pre><pre>peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem</pre><pre>peer channel join <strong>-</strong>b mychannel<strong>.</strong>block</pre><pre>CORE_PEER_MSPCONFIGPATH<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org2<strong>.</strong>example<strong>.</strong>com<strong>/</strong>users<strong>/</strong>Admin@org2<strong>.</strong>example<strong>.</strong>com<strong>/</strong>msp CORE_PEER_ADDRESS<strong>=</strong>peer0<strong>.</strong>org2<strong>.</strong>example<strong>.</strong>com:9051 CORE_PEER_LOCALMSPID<strong>=</strong>&quot;Org2MSP&quot; CORE_PEER_TLS_ROOTCERT_FILE<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org2<strong>.</strong>example<strong>.</strong>com<strong>/</strong>peers<strong>/</strong>peer0<strong>.</strong>org2<strong>.</strong>example<strong>.</strong>com<strong>/</strong>tls<strong>/</strong>ca<strong>.</strong>crt </pre><pre>peer channel join <strong>-</strong>b mychannel<strong>.</strong>block</pre><pre>CORE_PEER_MSPCONFIGPATH<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>users<strong>/</strong>Admin@org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>msp<br>CORE_PEER_ADDRESS<strong>=</strong>peer0<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com:7051<br>CORE_PEER_LOCALMSPID<strong>=</strong>&quot;Org1MSP&quot;<br>CORE_PEER_TLS_ROOTCERT_FILE<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>peers<strong>/</strong>peer0<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>tls<strong>/</strong>ca<strong>.</strong>crt</pre><pre>peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem</pre><pre>CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:9051 CORE_PEER_LOCALMSPID=&quot;Org2MSP&quot; CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt </pre><pre>peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem</pre><pre>peer chaincode install <strong>-</strong>n mycc <strong>-</strong>v 1.0 <strong>-</strong>l java <strong>-</strong>p <strong>/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>chaincode<strong>/</strong>chaincode_example02<strong>/</strong>java<strong>/</strong></pre><pre>peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -l java -v 1.0 -c &#39;{&quot;Args&quot;:[&quot;init&quot;,&quot;a&quot;, &quot;100&quot;, &quot;b&quot;,&quot;200&quot;]}&#39; -P &quot;AND (&#39;Org1MSP.peer&#39;,&#39;Org2MSP.peer&#39;)&quot;</pre><pre>CORE_PEER_MSPCONFIGPATH<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>users<strong>/</strong>Admin@org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>msp<br>CORE_PEER_ADDRESS<strong>=</strong>peer0<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com:7051<br>CORE_PEER_LOCALMSPID<strong>=</strong>&quot;Org1MSP&quot;<br>CORE_PEER_TLS_ROOTCERT_FILE<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>peers<strong>/</strong>peer0<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>tls<strong>/</strong>ca<strong>.</strong>crt</pre><pre>peer chaincode install <strong>-</strong>n mycc <strong>-</strong>v 1.0 <strong>-</strong>l java <strong>-</strong>p <strong>/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>chaincode<strong>/</strong>chaincode_example02<strong>/</strong>java<strong>/</strong></pre><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;query&quot;,&quot;a&quot;]}&#39;</pre><pre>peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c &#39;{&quot;Args&quot;:[&quot;invoke&quot;,&quot;a&quot;,&quot;b&quot;,&quot;10&quot;]}&#39;</pre><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;query&quot;,&quot;a&quot;]}&#39;</pre><pre>CORE_PEER_MSPCONFIGPATH<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org2<strong>.</strong>example<strong>.</strong>com<strong>/</strong>users<strong>/</strong>Admin@org2<strong>.</strong>example<strong>.</strong>com<strong>/</strong>msp<br>CORE_PEER_ADDRESS<strong>=</strong>peer1<strong>.</strong>org2<strong>.</strong>example<strong>.</strong>com:10051<br>CORE_PEER_LOCALMSPID<strong>=</strong>&quot;Org2MSP&quot;<br>CORE_PEER_TLS_ROOTCERT_FILE<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org2<strong>.</strong>example<strong>.</strong>com<strong>/</strong>peers<strong>/</strong>peer1<strong>.</strong>org2<strong>.</strong>example<strong>.</strong>com<strong>/</strong>tls<strong>/</strong>ca<strong>.</strong>crt</pre><pre>peer chaincode install <strong>-</strong>n mycc <strong>-</strong>v 1.0 <strong>-</strong>l java <strong>-</strong>p <strong>/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>chaincode<strong>/</strong>chaincode_example02<strong>/</strong>java<strong>/</strong></pre><pre>peer channel join <strong>-</strong>b mychannel<strong>.</strong>block</pre><pre>CORE_PEER_MSPCONFIGPATH<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>users<strong>/</strong>Admin@org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>msp<br>CORE_PEER_ADDRESS<strong>=</strong>peer1<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com:8051<br>CORE_PEER_LOCALMSPID<strong>=</strong>&quot;Org1MSP&quot;<br>CORE_PEER_TLS_ROOTCERT_FILE<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>peers<strong>/</strong>peer1<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>tls<strong>/</strong>ca<strong>.</strong>crt</pre><pre>peer chaincode install <strong>-</strong>n mycc <strong>-</strong>v 1.0 <strong>-</strong>l java <strong>-</strong>p <strong>/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>chaincode<strong>/</strong>chaincode_example02<strong>/</strong>java<strong>/</strong></pre><pre>peer channel join <strong>-</strong>b mychannel<strong>.</strong>block</pre><pre>CORE_PEER_MSPCONFIGPATH<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>users<strong>/</strong>Admin@org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>msp<br>CORE_PEER_ADDRESS<strong>=</strong>peer0<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com:7051<br>CORE_PEER_LOCALMSPID<strong>=</strong>&quot;Org1MSP&quot;<br>CORE_PEER_TLS_ROOTCERT_FILE<strong>=/</strong>opt<strong>/</strong>gopath<strong>/</strong>src<strong>/</strong>github<strong>.</strong>com<strong>/</strong>hyperledger<strong>/</strong>fabric<strong>/</strong>peer<strong>/</strong>crypto<strong>/</strong>peerOrganizations<strong>/</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>peers<strong>/</strong>peer0<strong>.</strong>org1<strong>.</strong>example<strong>.</strong>com<strong>/</strong>tls<strong>/</strong>ca<strong>.</strong>crt</pre><blockquote>In fact, if you think too many commands to type above, you can just type a single command (next time…):</blockquote><blockquote><strong>./byfn.sh up -o etcdraft -l java -s couchdb</strong></blockquote><blockquote><strong>***Please don’t type the single command here, if you had finished that long list of commands already…</strong></blockquote><h4>List Existing Nodes</h4><p>Once the environment is setup, we take a look what nodes are running in the network.</p><pre>exit</pre><pre>docker ps -a</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4cp6RKCrr9txeaveUAiw0w.png" /><figcaption>List of running containers</figcaption></figure><p>We can see that we have orderer.example.com, orderer2.example.com,… (read the <strong><em>NAMES</em></strong> column, a bit small, sorry)</p><p>In total, we have 5 orderers thus 5 nodes in Raft.</p><h4>Killing a Leader!</h4><p>Next, I will shutdown a Raft leader, then see what will happen in the Raft network!</p><p>Before killing, let’s <strong><em>find out which orderer is the current Raft leader</em></strong>.</p><pre>docker logs orderer3.example.com</pre><p><strong><em>In my case (your case might be different! But the checking process is the same)</em></strong>, I observed the following logs:</p><pre>[orderer.consensus.etcdraft] start -&gt; INFO 03b Starting raft node as part of a new channel channel=mychannel <strong>node=3</strong></pre><pre>[...]</pre><pre>[orderer.consensus.etcdraft] serveRequest -&gt; INFO 05d <strong>Raft leader changed: 0 -&gt; 3 </strong>channel=mychannel node=3</pre><blockquote>If you cannot see similar logs like above, no worry, try <strong>“docker logs orderer3.example.com”</strong> command few more times or just try it later (e.g. 10–20 seconds later).</blockquote><p>So, orderer3 is the Raft node 3, and the current leader is node 3. That is, orderer3 is the current Raft leader <strong><em>in my case</em></strong>!</p><p>Let’s kill it:</p><pre>docker stop orderer3.example.com</pre><p>Then, observe logs from another orderer:</p><pre>docker logs orderer<strong>2</strong>.example.com</pre><p><strong><em>In my case</em></strong>, I observed:</p><pre>[orderer.consensus.etcdraft] serveRequest -&gt; INFO 05a <strong>Raft leader changed: 0 -&gt; 1 </strong>channel=mychannel node=2</pre><p>Now node 1 (it should be orderer.example.com) is the new Raft leader, which is re-elected automatically.</p><p>But to prove that the network is still working properly, let’s perform some transactions:</p><pre>docker exec -it cli bash</pre><pre>export CHANNEL_NAME=mychannel</pre><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;query&quot;,&quot;a&quot;]}&#39;</pre><pre>// then you should get 90, or whatever number </pre><pre>peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c &#39;{&quot;Args&quot;:[&quot;invoke&quot;,&quot;a&quot;,&quot;b&quot;,&quot;10&quot;]}&#39;</pre><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;query&quot;,&quot;a&quot;]}&#39;</pre><pre>// then you should get 80, <br>// or a number different from the query<br>// result above, thus the network is working!</pre><h4>Killing a Leader Again!</h4><p>Kill a leader again to see what will happen. Remember that our current leader is node 1, that is orderer.example.com.</p><pre>exit</pre><pre>docker stop orderer.example.com</pre><pre>// now observe logs from another orderer</pre><pre>docker logs orderer4.example.com</pre><pre>[orderer.consensus.etcdraft] serveRequest -&gt; INFO 087 Raft leader changed: <strong>0 -&gt; 4</strong> channel=mychannel node=4</pre><p><strong><em>In my case</em></strong>, the new re-elected leader is node 4 (it should orderer4.example.com) now.</p><h4>Killing a Leader Again and Again!</h4><pre>docker stop orderer4.example.com</pre><pre>// now observe logs from another orderer</pre><pre>docker logs orderer2.example.com</pre><p>Oh? It seems that this time, no new leader is elected (no new election logs appear), why?</p><h4>What’s Wrong?</h4><p>In Raft, in order for the network to function properly, the majority of orderers needs to be held. In our case, we have 5 orderers, and I have killed 3 of them, the majority rule is broken!</p><h4>Fix the Raft</h4><p>Let turn on a stopped orderer, thus to fix the majority rule:</p><pre>docker start orderer4.example.com</pre><pre>docker logs orderer4.example.com</pre><pre>// you will observe log like this:</pre><pre>[orderer.consensus.etcdraft] serveRequest -&gt; INFO 0ab <strong>Raft leader changed: 0 -&gt; 2 </strong>channel=mychannel node=4</pre><p>The re-election process is normal again, once the majority rule is fulfilled.</p><p>Delete Orderer Data</p><p>We now try delete orderer data, then see whether data can be recovered from existing orderer.</p><pre>docker start orderer4.example.com</pre><pre>docker logs orderer4.example.com</pre><pre>// you will observe log like this:</pre><pre>[orderer.consensus.etcdraft] serveRequest -&gt; INFO 0ab <strong>Raft leader changed: 0 -&gt; 2 </strong>channel=mychannel node=4</pre><h4>Conclusion</h4><p>In this short article, in Fabric’s Raft, we see that orderers can:</p><ul><li>Re-elect leader under crash of leader node automatically, <strong><em>given that the majority of orderers holds</em></strong></li></ul><p><strong>Thanks! If you like my stories, </strong><a href="https://medium.com/@reasdom"><strong>please follow me</strong></a><strong> for new updates!</strong></p><h4>Reference</h4><p><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html">Building Your First Network - hyperledger-fabricdocs master documentation</a></p><figure><a href="https://cryptofi.co"><img alt="" src="https://cdn-images-1.medium.com/max/407/1*HrFLDXz92JavTZXjhmvU7g.png" /></a></figure><blockquote><a href="https://coincodecap.com/?utm_source=coinmonks">Get Best Software Deals Directly In Your Inbox</a></blockquote><figure><a href="https://coincodecap.com/?utm_source=coinmonks"><img alt="" src="https://cdn-images-1.medium.com/max/584/0*OJ-qb5G6i863msBB.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f9f0df20b5e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/hyperledger-fabric-the-taste-of-raft-4f9f0df20b5e">Hyperledger Fabric — The Taste of Raft</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Web Development Tutorial — Simple Draggable Table]]></title>
            <link>https://medium.com/@reasdom/web-development-tutorial-simple-draggable-table-625745d776e2?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/625745d776e2</guid>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Sat, 13 Apr 2019 12:08:16 GMT</pubDate>
            <atom:updated>2019-04-14T01:37:00.967Z</atom:updated>
            <content:encoded><![CDATA[<h3>Web Development Tutorial — Simple Draggable Table</h3><h4>Vue.js + JQuery</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*e3muCEsRJ6nD7bVw.png" /><figcaption>Vue.js logo</figcaption></figure><h4>Introduction</h4><p>This is a short web development tutorial to show you <strong><em>how to create a draggable table using Vue + JQuery</em></strong>, such that users can change the order of rows in a table by dragging and dropping.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XuQ5wrd2IPm1wcMD2SUKOw.gif" /><figcaption>The finished web</figcaption></figure><p>Code files used in this tutorial: <a href="https://github.com/reasdom/Draggable">https://github.com/reasdom/Draggable</a></p><h4>1. Preparation of Files</h4><h4>1.1 Create a folder “Draggable” in desktop</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/73/1*WaHwSjw3x3L7fKgkHee6-A.png" /></figure><h4>1.2 Create two files inside the folder created above</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*XCm466FmTrtpPmjxuDkIWQ.png" /></figure><p><strong><em>main.js</em></strong> will contain JavaScript and <strong><em>runner.html </em></strong>will contain the web UI.</p><h4>1.3 Use your IDE/Editor to start editing the two files above</h4><p>Personally, I use <a href="https://code.visualstudio.com/">Visual Studio Code</a>, you can use whatever Editor you like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1019/1*774lvQjCb3H3xyAD0wqNig.png" /><figcaption>Visual Studio Code</figcaption></figure><h4>2. Import all libraries we need in runner.html</h4><p>We will use the following libraries in<strong><em> runner.html</em></strong>:</p><ol><li><a href="https://getbootstrap.com/">Bootstrap</a></li><li><a href="https://jquery.com/">JQuery</a></li><li><a href="https://jqueryui.com/">JQuery UI</a></li><li><a href="https://vuejs.org/">Vue.js</a></li></ol><blockquote>And we will also import our <strong>main.js</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1bd215e639a5f08d0f7fa7b161f58f0/href">https://medium.com/media/c1bd215e639a5f08d0f7fa7b161f58f0/href</a></iframe><h4>3. Create our table and UI in runner.html</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vC7W7jXktlIJ1LDes9RLxw.png" /><figcaption>What the web looks like after step 3</figcaption></figure><p>Let’s code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97d2432561d26a31d1e5fe529ba7e458/href">https://medium.com/media/97d2432561d26a31d1e5fe529ba7e458/href</a></iframe><blockquote><strong>v-for</strong> is a functionality supported by Vue.js library, it is to construct table rows based on an array (named “items” in our case) that contains some items.</blockquote><blockquote>“items” will be defined in <strong>main.js </strong>later.</blockquote><h4>4. Make the table draggable in <em>main.js</em></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L5GUVHnqc5DCWKkmmHYNwg.gif" /><figcaption>Effect after step 4</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b686dcf73bba93fb272e5d209764534/href">https://medium.com/media/5b686dcf73bba93fb272e5d209764534/href</a></iframe><h4>5. Prepare our Vue object</h4><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*UUT_nVEslWhJ4se5u1fy8g.gif" /><figcaption>Effect after step 5</figcaption></figure><p>A Vue object is basically an object to hold our data, e.g. items, and some functions, e.g. update the new order of items (rows) in the table.</p><p>We will create a Vue object that holds:</p><ol><li>Data: a list of items</li><li>Functions (methods): adding a new item</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c3cdd24af746f2861d241b64112f606/href">https://medium.com/media/2c3cdd24af746f2861d241b64112f606/href</a></iframe><p>Remarks:</p><ol><li>In <strong><em>data</em></strong>, there is a list of <strong><em>items</em></strong>, each item contains an <strong><em>order</em></strong> and <strong><em>name</em></strong></li><li>In <strong><em>methods</em></strong> (functions), there is a function <strong><em>addItem</em></strong>, it is to add a new item to the list of <strong><em>items</em></strong>.</li><li><strong><em>Vue.set(…)</em></strong> function is used to insert a item to the list of <strong><em>items</em></strong></li></ol><h4>5.1 The item adding logic</h4><p>According to <strong><em>main.js</em></strong>, the list of <strong><em>items</em></strong> initially contains only one item (with <strong><em>order</em></strong> “0” and <strong><em>name</em></strong> “Item 0&quot;), let’s call it <strong><em>item0</em></strong>.</p><p>We will represent an item in this way: itemX { “order”, “name” }, for example, <strong><em>item0</em></strong> is represented by <strong><em>item0 {“0”, “Item 0”}</em></strong>.</p><p>Now, this is our list of <strong><em>items</em></strong>:</p><blockquote>[ item0 {“0&quot;, “Item 0&quot;} ]</blockquote><p>What if we add an item — <strong><em>item1</em></strong>?</p><p>Then hopefully the list will be like this:</p><blockquote>[ item0 {“0”, “Item 0”}, item1 {“1”, “Item 1”} ]</blockquote><p>So, how do we decide the next item’s order? <strong><em>One possible way is the length of the list before adding the new item</em></strong>. Let’s try:</p><p>Our current list of <strong><em>items</em></strong>:</p><blockquote>[ item0 {“0”, “Item 0”}, item1 {“1”, “Item 1”} ]</blockquote><p>Now, add a new item —<strong><em> item2</em></strong>, with an order the <strong><em>length of the list</em></strong>, that is <strong><em>2</em></strong>:</p><blockquote>[ item0 {“0”, “Item 0”}, item1 {“1”, “Item 1”}, item2 {“2”, “Item 2”} ]</blockquote><p>That’s way in line 16, we get the length of the list of <strong><em>items</em></strong>, then use that length value as the <strong><em>order</em></strong> of the new item.</p><p>Let’s see what our web looks like now:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UUT_nVEslWhJ4se5u1fy8g.gif" /><figcaption>Effect after step 5</figcaption></figure><h4>6. Final step: update the order value when drag and drop a row</h4><p>Currently, after step 5, we can already drag and drop a row in the table to change the sorting:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NWVBCnmEK_pQtihkcPx7oA.gif" /></figure><p>But note that the <strong><em>order </em></strong>value is not updated after sorting. In this final step, we are going to make that value updated when a user drags and drops a row.</p><p>That’s what we want (note that the <strong><em>order </em></strong>column is updated when the items sorting is changed):</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*XuQ5wrd2IPm1wcMD2SUKOw.gif" /><figcaption>Expected final web</figcaption></figure><p>Logically, we will need to:</p><ol><li>Capture the event when a user drags and drops a row. In other words, we want to be notified at <strong><em>the moment when a user drops a row</em></strong>. We can achieve that by defining an <strong><em>update </em></strong>function inside <strong><em>sortable</em></strong> (read the code below)</li><li>After we are notified, we should tell our Vue object <strong><em>“please update the order of items based on the current sorting of the table UI”</em></strong></li><li>Thus, inside the Vue object’s <strong><em>methods</em></strong>, there is an <strong><em>updateOrders </em></strong>function to do so</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4aff77e17b7405a573f3d0d12716d1bc/href">https://medium.com/media/4aff77e17b7405a573f3d0d12716d1bc/href</a></iframe><h4>6.1 The orders update logic</h4><p>The <strong><em>updateOrders </em></strong>function is the most complicated one!</p><p>Before proceeding, we have to be clear about that — we hold a data — list of <strong><em>items </em></strong>in the Vue object, and in <strong><em>runner.html</em></strong>, our table UI shows those items based on that list. This is just <strong><em>one-way relationship</em></strong> (data-to-UI).</p><blockquote>Thus, if we update the data — list of items in the Vue object, the table UI changes accordingly and automatically. <strong>But if we change the items’ order in the table UI, the list of items in the Vue object is not updated accordingly</strong>.</blockquote><ol><li>We first get the current order of items in the table UI (line 28), note that this is just the order reflecting the table UI, not the order of our real data — list of items in the Vue object</li><li>Then, we tell JQuery UI to cancel the sorting, that is, after a user drags and drops thus changing the sorting of items in the table UI, we tell JQuery <strong><em>“please don’t sort the table UI for me, change back to the order before dragging and dropping” </em></strong>(line 29)</li><li>Next, based on the order we got in (1), that is the order reflecting the table UI, but not our data, we update each item’s order in the list of items and the list’s sorting accordingly (line 30 – 39)</li><li>Once we change contents in the list of items in the Vue object (in line 39), the table UI will update automatically, since we define <strong><em>v-for to bind the list of items to the table UI</em></strong>.</li></ol><blockquote>Note that we had cancelled the table UI update by JQuery UI in (2) and instead, we let Vue.js to do it automatically (3) &amp; (4).</blockquote><blockquote>Why do we need to <strong>cancel </strong>JQuery UI’s sorting and let Vue.js to do so for the table UI? Becasue if both of them sort the table UI, the result might be wrong (two libraries are in conflict if they sort the table UI together).</blockquote><p>Finally, you should have the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XuQ5wrd2IPm1wcMD2SUKOw.gif" /><figcaption>Finally…</figcaption></figure><p>Code files used in this tutorial: <a href="https://github.com/reasdom/Draggable">https://github.com/reasdom/Draggable</a></p><p><strong>Thanks! If you like my stories, </strong><a href="https://medium.com/@reasdom"><strong>please follow me</strong></a><strong> for new updates!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=625745d776e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[CouchDB] Hyperledger Fabric Account-based Wallet Java Chaincode]]></title>
            <link>https://medium.com/coinmonks/couchdb-hyperledger-fabric-account-based-wallet-java-chaincode-82166c165266?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/82166c165266</guid>
            <category><![CDATA[hyperledger-fabric]]></category>
            <category><![CDATA[blockchain-development]]></category>
            <category><![CDATA[hyperledger]]></category>
            <category><![CDATA[chaincode]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Thu, 21 Mar 2019 04:04:05 GMT</pubDate>
            <atom:updated>2020-08-26T14:01:04.464Z</atom:updated>
            <content:encoded><![CDATA[<h4>CouchDB Version — Account-based Wallet Model in A Transaction System</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z2REkO4RIp73p4wESu_0IA.png" /><figcaption><a href="https://www.hyperledger.org/blog/2018/11/28/conducting-data-with-concerto-and-hyperledger-fabric">Hyperledger Fabric Image</a></figcaption></figure><p>This is a tutorial for Hyperledger Fabric <strong><em>Java</em></strong> <strong><em>Chaincode with CouchDB</em></strong>.</p><p>Basically, this tutorial is the <strong><em>CouchDB version </em></strong>of my previous story — <a href="https://medium.com/coinmonks/hyperledger-fabric-account-based-wallet-java-chaincode-8cbf80a6fb82">Hyperledger Fabric Account-based Wallet Java Chaincode</a>, but with some new functions to demonstrate what the CouchDB could provide us — <strong><em>Rich Query</em></strong>, that enables us to <strong><em>query our blockchain data in a more flexible way</em></strong>, such as a query of “all wallets with balance greater than $10”. While in LevelDB, data is simply stored as a key-value pair.</p><blockquote>Note that, in general, LevelDB has a better performance than CouchDB’s, while CouchDB could provide us Rich Query. The option depends on the use case.</blockquote><h4>Before We Start</h4><p>We will use Hyperledger Fabric “Build Your First Network (BYFN)” as our testing environment (v1.4 network)</p><blockquote>In case you don’t know how to start a “BYFN” network, I will also provide the steps, no worry. But If you want to learn how “BYFN” works, you could refer to:</blockquote><blockquote>(1) <a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html">Hyperledger Fabric official “BYFN” tutorial</a></blockquote><blockquote>(2) <a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-1-2d3b32213529">My previous story about “BYFN”</a></blockquote><h4>What Account-based Wallet Model Is Basically</h4><p>Account-based wallet model is one of the implementations of the <strong><em>transaction system</em></strong>, in which, generally speaking, <strong><em>there are wallets and each wallet contains some values or tokens that could be used to transfer</em></strong>. Of course, there are lots of variations on the details of the implementations, such as the token exchange and multi-tokens features.</p><h4>Features</h4><p>In the Java Chaincode, we will implement:</p><ol><li>User wallet creation</li><li>User wallet query with a walletId</li><li>Wallet token transfer (i.e. to send money or tokens to from one wallet another wallet)</li><li><strong><em>User wallet query with a condition that the token amount needs to be greater than a certain value (new feature in this CouchDB version tutorial!)</em></strong></li></ol><h4>The IDE — Tool for Development of Java Chaincode</h4><p>We use <a href="https://www.jetbrains.com/idea/download/#section=mac"><strong><em>IntelliJ IDEA Community</em></strong></a>. Also, you need to have <a href="https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html"><strong><em>JDK</em></strong></a>. Please install them.</p><p>Of course, if you have your own Java IDE, you could also use that. But in this tutorial, I use IntelliJ.</p><h4>Step 1 — Preparation for Development</h4><p>Let’s open IntelliJ.</p><p><strong><em>Create New Project</em></strong> in IntelliJ:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*BeK_U3DaLdND6tg3dlkz2Q.png" /></figure><p>Choose <strong><em>Gradle</em></strong> on the left, then Next:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C5RaZu6XsNLL4kF_Mfvr4A.png" /></figure><p>Type your <strong><em>GroupId</em></strong> and <strong><em>ArtifactId</em></strong>. In my case, I use <strong><em>“java_account_model_cc”</em></strong> for both. Then Next:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IHYV97VEABixSrahqGrHiw.png" /></figure><p>Then, just next. Now, you should configure your own <strong><em>project location</em></strong>. In my case, I use <strong><em>“~/Desktop/java_account_model_cc”</em></strong>. Click <strong><em>Finish</em></strong>.</p><h4>1.1 settings.gradle</h4><p>In the project files on the left, you should have <strong><em>“settings.gradle”</em></strong>. Let’s double click to open it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*idCn-3hO7VCuxwkria1YlQ.png" /><figcaption>Project files on the left panel</figcaption></figure><p>Type the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3444468dee949ab819e7425c2270f84/href">https://medium.com/media/a3444468dee949ab819e7425c2270f84/href</a></iframe><h4>1.2 build.gradle</h4><p>In the project files panel on the left, you should have <strong><em>“build.gradle”</em></strong>. Let’s double click to open it.</p><p>Then, type the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0179182b4584d27fab51399beeb19fd6/href">https://medium.com/media/0179182b4584d27fab51399beeb19fd6/href</a></iframe><p>*If there is a <strong><em>“Gradle project needs to be imported”</em></strong> popup on the bottom right in your IntelliJ, let’s choose <strong><em>“Import Changes”</em></strong>.</p><h4>1.3 The Chaincode File</h4><p>In the project files panel on the left, under <strong><em>src &gt; main &gt; java</em></strong>, right click on it and choose <strong><em>New &gt; Java Class</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cw5t6UGDlc2oFAVFwpacsA.png" /></figure><p>Type <strong><em>“org.hyperledger.fabric.chaincode.AccountBasedChaincode”</em></strong> for the <strong><em>Name</em></strong> field:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*afNlb8jtwSzNSQ1J3rA14g.png" /><figcaption>org.hyperledger.fabric.chaincode.AccountBasedChaincode</figcaption></figure><p>Then, you should have the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*4dqv_ou4Kx3oXVoBfFaOWA.png" /></figure><p><strong><em>AccountBasedChaincode</em></strong>(.java) above is the place we write Chaincode in Java.</p><h4>Step 2 — Analysis of the Requirement</h4><p>Before we start coding, let’s organize what we need to code.</p><p>As said in the “Features” section, in the Java Chaincode, we will implement:</p><ol><li>User wallet creation</li><li>User wallet query with a walletId</li><li>Wallet token transfer (i.e. to send money to from one wallet another wallet)</li><li><strong><em>User wallet query with a condition that the token amount needs to be greater than a certain value</em></strong></li></ol><p>From this simple requirement above, we need to have the following classes:</p><ol><li>Wallet Class</li><li>The Chaincode class — holding the Chaincode</li></ol><p>And our Chaincode should provide these functions:</p><ol><li>Create a wallet</li><li>Transfer tokens from one wallet to another</li><li>Get (query) a wallet with a walletId</li><li><strong><em>Get (query) some wallets with a condition that the token amount needs to be greater than a certain value</em></strong></li><li>Init function — called when the Chaincode is <strong><em>instantiated</em></strong></li><li>Invoke function — called when a user wants to call the function (1), (2), (3), or<strong><em> (4)</em></strong></li></ol><blockquote>The <strong>Init</strong> function must be implemented in Chaincode and will be called automatically whenever we <strong>instantiate<em> </em></strong>or<strong><em> </em>upgrade<em> </em></strong>the Chaincode. Usually, it is used to initialize some data in the Blockchain.</blockquote><blockquote>The <strong>Invoke</strong> function is used to receive <strong>all</strong> user function calls, then based on the call, it invokes (calls) the corresponding function — (1), (2), (3), or <strong>(4)</strong>. It is like a router — routing incoming requests to a different path.</blockquote><h4>Step 3 — The Wallet Class</h4><p>Now, we create and code the Wallet class.</p><p>Create a new package under <strong><em>“org.hyperledger.fabric.chaincode”:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m1d9U__dRWFOttVm7rdCSg.png" /></figure><p>Call the package <strong><em>Models</em></strong>, then OK:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*wit7083YVAh3NBkkaGLc7w.png" /><figcaption>Package “Models”</figcaption></figure><p>Now you should have the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*qTlLZ34iSi7TJVG-gyj6_A.png" /></figure><p>Under the <strong><em>Models</em></strong> package, create a new Java class and name it <strong><em>Wallet</em></strong>. (I don’t show detailed steps this time, let’s try yourself)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dRGRjQiWn8d3k-dqYLs-pg.png" /><figcaption>Creating <strong><em>Wallet </em></strong><em>class under </em><strong>Models</strong> package</figcaption></figure><p>Now we code for the <strong><em>Wallet</em></strong> class, let’s type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b2ae64812f88ea1b03c790234bed921/href">https://medium.com/media/4b2ae64812f88ea1b03c790234bed921/href</a></iframe><p>The wallet has a <strong><em>wallet id</em></strong> to identify a certain wallet and a <strong><em>token amount</em></strong> to specify how many tokens the wallet owns.</p><blockquote>Note that in the production case, the Wallet class should be (far) more complicated. For example, you probably use “BigDecimal” data type instead of “Double” for tokenAmount. Also, in our case, we support only one token type (i.e. only one currency) in the whole transaction system.</blockquote><h4>Step 4 — The Chaincode Class</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0719cbab9874815acad1a50215f62f7/href">https://medium.com/media/a0719cbab9874815acad1a50215f62f7/href</a></iframe><blockquote><strong>Note that there are two new functions in this tutorial, compared to the previous one — query() and getWalletsWithTokenAmountGreaterThan(). I have put some comments above them.</strong></blockquote><blockquote>Note that in the production case, the codes should be different from mine. These codes are in demo or tutorial purpose mainly.</blockquote><h4>Step 5 — Running the Chaincode in BYFN</h4><h4>5.1 Install Prerequisites</h4><p>We use Hyperledger Fabric v1.4 in this tutorial.</p><p>First of all, you could install prerequisites by following the official instructions:</p><ol><li><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html#prerequisites">Install prerequisites</a></li><li><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/install.html">Install samples, programs, and docker images from Hyperledger Fabric</a></li></ol><h4>5.2 Chaincode Preparation</h4><p>Then, let’s switch to this directory (*supposed that you finish the prerequisite part, you should have all needed files and directories):</p><pre>cd fabric-samples/chaincode/chaincode_example02/<br>mv java java_01<br>mkdir java</pre><p>Now, copy the following <strong><em>highlighted</em></strong> files in your project directory to <strong><em>”fabric-samples/chaincode/chaincode_example02/java/”</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*rTQG3Dw879v25ofUobCqww.png" /><figcaption>Copy the highlighted files to <strong><em>”fabric-samples/chaincode/chaincode_example02/java/”</em></strong></figcaption></figure><h4>5.3 Bring Up the Network</h4><pre>cd ../..<strong>/</strong>first<strong>-</strong>network<br>./byfn.sh up -l java <strong>-s couchdb</strong></pre><blockquote><strong>Note that this time, there is a “-s couchdb” to enable CouchDB.</strong></blockquote><blockquote>After running this script, you might need to wait for a moment…</blockquote><blockquote><strong>*** If you see the following error (instead of other errors), that’s OKAY, just proceed to the next steps ***</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/367e1b445e8ba3d2cd43164e06b8cb9f/href">https://medium.com/media/367e1b445e8ba3d2cd43164e06b8cb9f/href</a></iframe><blockquote>Also, remember to start Docker, before you run the command above.</blockquote><blockquote>*In case you make something wrongly, you can run the following commands to turn down the network, and bring up again:</blockquote><blockquote>./byfn.sh down<br>./byfn.sh up -l java -s couchdb</blockquote><p>Now, we test whether our Chaincode works.</p><h4>5.4 Access the Cli</h4><p>There is a cli Docker container created automatically, it is a command line interface to <strong><em>control the nodes</em></strong>.</p><p>Let’s get access to the cli:</p><pre>docker exec -it cli bash</pre><p>Then, set up <strong><em>environment variables</em></strong> used by some programs:</p><pre>export CHANNEL_NAME=mychannel<br>export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp<br>export CORE_PEER_ADDRESS=peer0.org1.example.com:7051<br>export CORE_PEER_LOCALMSPID=&quot;Org1MSP&quot;<br>export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt</pre><blockquote>You could just copy all of them, then paste to your terminal and press “enter”.</blockquote><blockquote>These environment variables are used to let some Hyperledger Fabric programs know that we need to use <strong>peer0.org1.example.com:7051</strong> to invoke Chaincode functions.</blockquote><p>Now, we <strong><em>create two wallets</em></strong> with wallet IDs — <strong><em>“tom” </em></strong>and <strong><em>“sam”</em></strong>:</p><pre>peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c &#39;{&quot;Args&quot;:[&quot;createWallet&quot;,&quot;tom&quot;,&quot;<strong>100</strong>&quot;]}&#39;</pre><pre>peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c &#39;{&quot;Args&quot;:[&quot;createWallet&quot;,&quot;sam&quot;,&quot;<strong>200</strong>&quot;]}&#39;</pre><blockquote>Note that Tom has $100 and Sam has $200.</blockquote><blockquote>Update: Note that we have two <em>--peerAddresses</em> in order to send to 2 Peers to endorse the transaction, since the default endorsement policy in Build Your First Network (BYFN) is an <strong>AND </strong>policy, which requires both organizations, Org1 and Org2, to sign each transaction. For more explainations, please refer to <a href="https://medium.com/@reasdom/hello-tommaso-7d10c5f48c9f">here</a>.</blockquote><p>You should see something like this in the terminal after running each command above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d31a3b7bb517386efb71f24c70327d3/href">https://medium.com/media/3d31a3b7bb517386efb71f24c70327d3/href</a></iframe><p>Now, we <strong><em>get two wallets </em></strong>created above to verify whether they are existing in the Blockchain:</p><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWallet&quot;,&quot;tom&quot;]}&#39;</pre><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWallet&quot;,&quot;sam&quot;]}&#39;</pre><p>You should see something like this in the terminal after running each command above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b1e03415c7f6183cd105ceabcbaa019/href">https://medium.com/media/4b1e03415c7f6183cd105ceabcbaa019/href</a></iframe><p>Above, we can see that the two wallets created previously can be queried. Also, Tom has <strong><em>100 tokens</em></strong> and Sam has <strong><em>200 tokens</em></strong>.</p><p>Next, we make a <strong><em>transfer</em></strong> transaction — let’s transfer <strong><em>50 tokens from “tom” wallet to “sam” wallet</em></strong>:</p><pre>peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c &#39;{&quot;Args&quot;:[&quot;transfer&quot;,&quot;tom&quot;,&quot;sam&quot;,&quot;<strong>50</strong>&quot;]}&#39;</pre><p>Finally, let’s verify two wallets again:</p><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWallet&quot;,&quot;tom&quot;]}&#39;</pre><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWallet&quot;,&quot;sam&quot;]}&#39;</pre><p>Can you have the following result?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d54b45f7470ff17a1d405cefbb3e997c/href">https://medium.com/media/d54b45f7470ff17a1d405cefbb3e997c/href</a></iframe><p>Note that now <strong><em>Tom’s wallet remains 50 tokens and Sam’s wallet has 250 tokens</em></strong>, the <strong><em>transaction is done </em></strong>and written into the Blockchain ledger.</p><h4>Test the New Function for CouchDB</h4><p>Now we try our new functions for this CouchDB version tutorial.</p><p>Remember that:</p><ul><li>Tom now has $50</li><li>Sam now has $250</li></ul><p>We will try to query all wallets with a token amount:</p><ol><li>&gt; $40</li><li>&gt; 200</li></ol><p>In (1), we should be able to obtain both Tom’s and Sam’s wallets.</p><p>In (2), we should be able to obtain Sam’s wallet only.</p><p>Now, let’s query:</p><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWalletsWithTokenAmountGreaterThan&quot;,&quot;40&quot;]}&#39;</pre><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWalletsWithTokenAmountGreaterThan&quot;,&quot;200&quot;]}&#39;</pre><p>Can you have the following result?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbd948c10fb08bfa81fb43db03274a80/href">https://medium.com/media/cbd948c10fb08bfa81fb43db03274a80/href</a></iframe><h4>Step 6 — Cleaning Up</h4><pre>exit<br>./byfn.sh down</pre><p>Then, delete directory <strong><em>“fabric-samples/chaincode/chaincode_example02/java”</em></strong> and rename directory <strong><em>”java_01”</em></strong> back to <strong><em>”java”</em></strong>.</p><p><strong>Thanks! If you like my stories, </strong><a href="https://medium.com/@reasdom"><strong>please follow me</strong></a><strong> for new updates!</strong></p><h4>Reference</h4><ul><li><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html">Building Your First Network - hyperledger-fabricdocs master documentation</a></li></ul><blockquote><a href="https://coincodecap.com/?utm_source=coinmonks">Get Best Software Deals Directly In Your Inbox</a></blockquote><figure><a href="https://coincodecap.com/?utm_source=coinmonks"><img alt="" src="https://cdn-images-1.medium.com/max/584/0*OJ-qb5G6i863msBB.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82166c165266" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/couchdb-hyperledger-fabric-account-based-wallet-java-chaincode-82166c165266">[CouchDB] Hyperledger Fabric Account-based Wallet Java Chaincode</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hyperledger Fabric Transaction Flow]]></title>
            <link>https://medium.com/coinmonks/hyperledger-fabric-transaction-flow-43a6b7e39c6e?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/43a6b7e39c6e</guid>
            <category><![CDATA[hyperledger]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[blockchain-development]]></category>
            <category><![CDATA[blockchain-application]]></category>
            <category><![CDATA[hyperledger-fabric]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Fri, 22 Feb 2019 08:54:57 GMT</pubDate>
            <atom:updated>2020-08-26T14:22:56.557Z</atom:updated>
            <content:encoded><![CDATA[<h4>From nothing to a block, what is the big picture?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*z2REkO4RIp73p4wESu_0IA.png" /><figcaption><a href="https://www.hyperledger.org/blog/2018/11/28/conducting-data-with-concerto-and-hyperledger-fabric">Hyperledger Fabric Image</a></figcaption></figure><p>This is a short story about <strong><em>Hyperledger Fabric Transaction Flow</em></strong>. By knowing the transaction flow, it might help us debug and design our Hyperledger Fabric application.</p><h4>What is the Transaction Flow?</h4><ul><li>Starts with an API request from the client — I want to write some data to the Blockchain</li><li>Ends with an API response to the client — the block is committed in the Blockchain for your data</li></ul><blockquote>Then, what do happen in the middle? Let’s open the black box.</blockquote><h4>The Black Box — There is No Magic</h4><p>The following is a common transaction flow in Hyperledger Fabric.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*YskFcmxXS13F1YfJ-vck8Q.png" /><figcaption>The (One Possible) Transaction Flow</figcaption></figure><p>Assume that there is no error:</p><ol><li>A Client, such as an iOS app, sends an API request to a Backend server with Hyperledger Fabric SDK — I want to write some data to the Blockchain</li><li>The SDK sends a <strong><em>transaction proposal</em></strong> to the Peer(s) for the endorsement</li><li>The Peer(s) <strong><em>endorses</em></strong> (signs) the transaction proposal and returns it to the SDK (, also, the Peer performs Chaincode / Smart Contract execution at this point, before endorsing the transaction proposal)</li><li>The SDK sends a <strong><em>transaction</em></strong> to the Orderer (&amp; Kafka) for the transaction ordering service</li><li>The Orderer orders the transactions, forms block and broadcasts it to the Peers</li><li>The Peer(s) notifies the SDK — the block is committed in the Blockchain for your data (, also, the Peer performs transactions validation and block commitment at this point, before notifying the SDK)</li><li>The SDK (the Backend server) sends an API response to a client — the block is committed in the Blockchain for your data</li></ol><blockquote>It seems that SDK (the Backend server) is quite busy, in terms of the number of arrows. But don’t forget that Peer’s endorsement (signing), Chaincode execution/simulation, and transactions validation are high-load jobs as well.</blockquote><p>So, that’s the big picture. Next, I plan to write a short story about <strong><em>Corda transaction flow </em></strong>and some stories about design practices for the Chaincode or Smart Contract development.</p><blockquote>Corda is another Permissioned Blockchain framework like Hyperledger Fabric.</blockquote><p><strong>Thanks! If you like my stories, </strong><a href="https://medium.com/@reasdom"><strong>please follow me</strong></a><strong> for new updates!</strong></p><h4>More About Hyperledger Fabric</h4><p>To learn more about Hyperledger Fabric, you might read some of my stories:</p><ol><li><a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-1-2d3b32213529">Hyperledger Fabric “Building Your First Network” Tutorial — Part 1</a></li><li><a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-2-267fc5d36118">Hyperledger Fabric “Building Your First Network” Tutorial — Part 2</a></li><li><a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-3-fde6022d8739">Hyperledger Fabric “Building Your First Network” Tutorial — Part 3</a></li><li><a href="https://medium.com/coinmonks/hyperledger-fabric-account-based-wallet-java-chaincode-8cbf80a6fb82">Hyperledger Fabric Account-based Wallet Java Chaincode</a></li><li><a href="https://medium.com/coinmonks/hyperledger-fabric-updating-channel-configs-45082a5dc9b2">Hyperledger Fabric — Updating Channel Configs</a></li><li><a href="https://medium.com/coinmonks/hyperledger-fabric-damn-implicitmetapolicy-44cc9cc5c472">Hyperledger Fabric Damn ImplicitMetaPolicy</a></li></ol><p>I will keep posting new stories about Blockchain.</p><blockquote><a href="https://coincodecap.com/?utm_source=coinmonks">Get Best Software Deals Directly In Your Inbox</a></blockquote><figure><a href="https://coincodecap.com/?utm_source=coinmonks"><img alt="" src="https://cdn-images-1.medium.com/max/584/0*OJ-qb5G6i863msBB.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43a6b7e39c6e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/hyperledger-fabric-transaction-flow-43a6b7e39c6e">Hyperledger Fabric Transaction Flow</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Damn AVL Tree!]]></title>
            <link>https://medium.com/@reasdom/ddamn-avl-tree-3ee45c7ef047?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ee45c7ef047</guid>
            <category><![CDATA[binary-search-tree]]></category>
            <category><![CDATA[avl-tree]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[binary-tree]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Fri, 15 Feb 2019 01:48:50 GMT</pubDate>
            <atom:updated>2019-02-15T11:01:51.117Z</atom:updated>
            <content:encoded><![CDATA[<p>Talk about <strong><em>Algorithm</em></strong> — <strong>A</strong>delson-<strong>V</strong>elsky and <strong>L</strong>andis Tree’s Self-balancing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XbN-eBstJQ-sV-IxuZFuBg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/GbY8Xg5iTOA?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Marina Khrapova</a> on <a href="https://unsplash.com/search/photos/apple-tree?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><blockquote><a href="https://en.wikipedia.org/wiki/AVL_tree">AVL Tree</a> is self-balancing Binary Search Tree (BST).</blockquote><h4>Apple Tree</h4><p>An apple tree is a tree in which there are some apples. An apple a day keeps the doctor away.</p><p>Joking apart. <strong><em>Tree</em></strong> is a data structure to store data in a <strong><em>hierarchical</em></strong> manner (so there are some levels) and it contains a collection of nodes with <strong><em>no node cycle</em></strong>.</p><p>Tree data structure is like an inverted tree in daily life. The root is the top node!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vA_boYKQ9Z6RjlaWmdECmA.png" /></figure><h4>Why Tree?</h4><p><strong><em>Tree</em></strong> is a data structure that is manipulated by some algorithms efficiently in some cases, such as AVL Sorting.</p><h4>Credit</h4><p>In this story, I mainly take reference from previous learning from <a href="http://www.cse.ust.hk/~desmond/">Dr. Desmond Yau-chat Tsoi</a>, who is currently a lecturer in the Department of Computer Science and Engineering, The Hong Kong University of Science and Technology (HKUST). In fact, he was my teacher in some courses before and is one of the best teachers in my opinion.</p><p>Another main source is the <a href="https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/index.htm">Introduction to Algorithms</a> course (6.006, Fall 2011) in MIT OpenCourseWare. Thanks to MIT, for those free and high-quality online courses!</p><h4>Binary Tree</h4><p>A Binary Tree is a <strong><em>Tree</em></strong> in which <strong><em>every</em></strong> node in the tree can have <strong><em>at most</em></strong> two children (nodes).</p><p>An example of a Binary Tree is:</p><pre>      8<br>  4      5<br>2   3  9   7</pre><blockquote>Does it match the definition of Binary Tree? Let’s check it yourself.</blockquote><h4>Binary Search Tree (BST)</h4><p>A Binary Search Tree (BST) is a <strong><em>Binary Tree</em></strong> that satisfies that given <strong><em>any</em></strong> node x, all nodes’ value in the left subtree ≤ x, while all nodes’ value in the right subtree ≥ x.</p><p>An example of a <strong><em>balanced</em></strong> (at least you can feel it, can you feel it at the moment?) Binary Search Tree (BST) is:</p><pre>      5<br>  3       8<br>2   4   7   9</pre><blockquote>See? Given a root node x (5), all nodes’ value in the left subtree ≤ 5, while all nodes’ value in the right subtree ≥ 5. One more, given a child node y (3), all nodes’s value in the left subtree ≤ 3, while all nodes’ value in the right subtree ≥ 3. This rule applies to any node in this tree if this is a binary search tree (BST).</blockquote><p>An example of an <strong><em>unbalanced</em></strong> (at least you can feel it, can you feel it at the moment?) Binary Search Tree (BST) is:</p><pre>5<br>  6<br>    7<br>      8</pre><h4>Why Balanced Or U<strong><em>nbalanced Matters?</em></strong></h4><p>For example, some algorithms manipulating the balanced tree are <strong><em>more efficient</em></strong> than the same algorithms manipulating the unbalanced tree.</p><blockquote>We need a balanced life!</blockquote><h4>Height</h4><p>It is the length of the longest path from the node to a leaf.</p><p>For example, the height of the root node (5) is 3. One more, the height of the child node (6) is 2.</p><pre>5<br>  6<br>    7<br>      8</pre><h4>AVL Tree’s “Balanced” Rule</h4><p>“Balanced” rule in AVL Tree is that the height of the two subtrees of <strong><em>any</em></strong> node differs by <strong><em>at most</em></strong> one. (More details later, no worry!)</p><h4>AVL Tree’s Self-balancing</h4><p>A function of an AVL Tree such that it can <strong><em>check</em></strong> whether itself is balanced and in the case of being unbalanced, the tree can <strong><em>fix</em></strong> itself to be balanced again.</p><h4>AVL Tree</h4><p>An AVL tree is a self-balancing <strong><em>Binary Search Tree</em></strong> where the height of the two subtrees of <strong><em>any</em></strong> node differs by <strong><em>at most</em></strong> one.</p><blockquote><strong>[Optional To Read]</strong> Also, each node stores a height value, which refers the height of the node that will be used for balancing check.</blockquote><h4>Node Insertion For AVL Tree</h4><p>Insert a node following the <strong><em>rule</em></strong> in <strong><em>Binary Search Tree</em></strong>.</p><blockquote>The rule: given <strong>any</strong> node x, all nodes’ value in the left subtree ≤ x, while all nodes’ value in the right subtree ≥ x</blockquote><p>For example, given an AVL Tree (or Binary Search Tree)</p><pre>      5<br>  3       8</pre><p>Now, we insert a node with value 9:</p><pre>      5<br>  3       8<br>              9</pre><h4>What Happened?</h4><ol><li>Starting from the root node (5), compare 5 and 9</li><li>9 ≥ 5, then node (9) should be put to the right subtree (of node (5))</li><li>The “root node” of the right subtree above is node (8), compare 8 and 9</li><li>9 ≥ 8, then node (9) should be put to the right subtree (of node (8))</li><li>Node (9) reaches the leaf location, then node (9) should be put to that location</li></ol><blockquote>Note that the rule in Binary Search Tree is maintained, even after adding something into the tree.</blockquote><h4>Node Insertion That Makes An AVL Tree Unbalanced</h4><p>Sometimes, the node insertion will result in an unbalanced AVL Tree.</p><p>For example,</p><pre>      5<br>  3       8<br>              9</pre><p>Now, we insert a node with value 10.</p><pre>      5<br>  3       8<br>              9<br>                 10</pre><h4>Now, This Is An Unbalanced AVL Tree — Why?</h4><blockquote>An AVL tree is […] where the height of the two subtrees of <strong><em>any</em></strong> node differs by <strong><em>at most</em></strong> one.</blockquote><p>For example, let’s look at node (8), the height of its left subtree is <strong><em>-1</em></strong> (since left subtree <strong><em>doesn’t exist</em></strong> in this case), but the height of its right subtree is 1, and ABS(-1 – 1) = 2, which means that “the height of the two subtrees of <strong><em>any</em></strong> node differs by <strong><em>at most</em></strong> one (1)” is <strong><em>not</em></strong> satisfied.</p><p>One more, let’s look at node (5), the height of its left subtree is 0, but the height of its right subtree is 2, and ABS(0 – 2) = 2, which means that “the height of the two subtrees of <strong><em>any</em></strong> node differs by <strong><em>at most</em></strong> one (1)” is <strong><em>not</em></strong> satisfied.</p><blockquote>If your life is unbalanced, first of all, you have to notice it, then, you are going to fix it.</blockquote><h4>Fixing “Unbalanced” Case In AVL Tree</h4><p>For example, given an unbalanced AVL Tree:</p><pre>      5<br>  3       8<br>              9<br>                 10</pre><p>How self-balancing in AVL Tree works?</p><h4>Step 1</h4><p>Find the <strong><em>lowest</em></strong> node that violates the “balanced” rule of AVL Tree.</p><blockquote>The rule: the height of the two subtrees of <strong>any</strong> node differs by <strong>at most</strong> one.</blockquote><p>In the example unbalanced AVL Tree above, we can observe that node (5) and node (8) are the two nodes that violate the “balanced” rule of AVL Tree. But, we have to select one of them, which? The <strong><em>lowest </em></strong>one — node (8).</p><p>And from now on, we call node (8) a <strong><em>sad node</em></strong>. It is not simply sad, but extremely sad :( :( :( :(…</p><blockquote>Why are node (5) and node (8) the two nodes that violate the “balanced” rule of AVL Tree? Please refer to section “Now, This Is An Unbalanced AVL Tree — Why?”, that is the previous section.</blockquote><h4>Step 2</h4><p>Decide whether the <strong><em>sad</em></strong> node (8) is <strong><em>right-heavy or left-heavy.</em></strong></p><pre>      5<br>  3       <strong>8 &lt;-- SAD :(</strong><br>              9<br>                 10</pre><p>Keep using the same example unbalanced AVL Tree, let’s guess, is the <strong><em>sad</em></strong> node (8) <strong><em>right-heavy (R) or left-heavy (L)</em></strong>?</p><p>Obviously, left-heavy! No, no, no, it should be <strong><em>right-heavy (R)</em></strong>!</p><p>When your left-hand holds nothing and right-hand holds a 10kg object, is it <strong><em>right-heavy (R) or left-heavy (L)</em></strong>?</p><p>So, the final decision: the sad node (8) is right-heavy. Let’s mark it <strong><em>“R”</em></strong>. (In case of left-heavy, we mark it <strong><em>“L”</em></strong>)</p><h4>Step 3</h4><ol><li>If step 2 marks <strong><em>“R”</em></strong>, we check whether the <strong><em>sad node’s (8) right child</em></strong> is right-heavy or balanced. If so, mark it “R”. Else, mark it “L”.</li><li>Or, If step 2 marks <strong><em>“L”</em></strong>, we check whether the <strong><em>sad node’s (8) left child</em></strong> is left-heavy or balanced. If so, mark it “L”. Else, mark it “R”.</li></ol><p>Keep using the same example unbalanced AVL Tree:</p><pre>      5<br>  3       8 &lt;-- SAD :(<br>              <strong>9</strong> <strong>&lt;-- SAD&#39;s <em>r-child</em> </strong><br>                 10</pre><p>In our case, we marked <strong><em>“R” </em></strong>in step 2, so we check whether the <strong><em>sad node’s (8) right child</em></strong> (9) is right-heavy or balanced.</p><p>It follows that the sad <em>node’s (8) right child </em>(9) is <strong><em>right-heavy</em></strong>.</p><p>Then, we mark it <strong><em>“R”</em></strong>.</p><blockquote>***So, now, consider the marks for both step 2 and step 3 — we have “RR” or “Right-Right” case.</blockquote><blockquote>*In other words, given any unbalanced AVL Tree, we might have “LL”, “LR”, “RR”, or “RL” after step 2 and 3.<strong> In our example, we meet the “RR” case</strong>.</blockquote><h4>Step 4</h4><ol><li>If “RR”, perform a <strong><em>left rotation</em></strong> on the<strong><em> sad node</em></strong></li><li>If “RL”, perform a <strong><em>right rotation</em></strong> on the<strong><em> sad node’s right child</em></strong>, then perform <strong><em>left rotation</em></strong> on the<strong><em> sad node</em></strong></li><li>If “LL”, perform a <strong><em>right rotation</em></strong> on the<strong><em> sad node</em></strong></li><li>If “LR”, perform a <strong><em>left rotation</em></strong> on the<strong><em> sad node’s left child</em></strong>, then perform <strong><em>right rotation</em></strong> on the<strong><em> sad node</em></strong></li></ol><p><strong><em>No worry, let’s focus on our case first — 1. “RR”.</em></strong></p><p>Our case is <strong><em>“RR”</em></strong>, so we perform a <strong><em>left rotation</em></strong> on the<strong><em> sad node</em></strong>.</p><pre>      5<br>  3       8 &lt;-- rotate left here<br>              9 <br>                 10</pre><pre>      5<br> 3          9<br>        8      10</pre><p>Now, the AVL Tree is fixed. And no rule is broken (for both BST rule and AVL “balanced” rule).</p><h4>What About Other Cases? — “LL”, “LR”, and “RL”</h4><p>Just follow this:</p><ol><li>If “RR”, perform a <strong><em>left rotation</em></strong> on the<strong><em> sad node</em></strong></li><li>If “RL”, perform a <strong><em>right rotation</em></strong> on the<strong><em> sad node’s right child</em></strong>, then perform <strong><em>left rotation</em></strong> on the<strong><em> sad node</em></strong></li><li>If “LL”, perform a <strong><em>right rotation</em></strong> on the<strong><em> sad node</em></strong></li><li>If “LR”, perform a <strong><em>left rotation</em></strong> on the<strong><em> sad node’s left child</em></strong>, then perform <strong><em>right rotation</em></strong> on the<strong><em> sad node</em></strong></li></ol><p>Now, let’s look at the rotation ideas in graphic form:</p><h4>“RR” Case</h4><ul><li>Perform a <strong><em>left rotation</em></strong> on the<strong><em> sad node </em></strong>(node “a” in the image)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VZufKZZU7dmKp-ZLwaz8-Q.png" /><figcaption>Thanks to Dr. Desmond Yau-chat Tsoi</figcaption></figure><h4>“LL” Case</h4><ul><li>Perform a <strong><em>right rotation</em></strong> on the<strong><em> sad node </em></strong>(node “b” in the image)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*10NGdknZJtkL2H8s5jpTfQ.png" /><figcaption>Thanks to Dr. Desmond Yau-chat Tsoi</figcaption></figure><h4>“RL” Case</h4><ol><li>Perform a <strong><em>right rotation</em></strong> on the<strong><em> sad node’s right child</em></strong> (node “c” in the image)</li><li>Perform <strong><em>left rotation</em></strong> on the<strong><em> sad node </em></strong>(node “a” in the image)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Z-GfvPtKSBh4ZwQrft6sQ.png" /><figcaption>Thanks to Dr. Desmond Yau-chat Tsoi</figcaption></figure><h4>“LR” Case</h4><ol><li>Perform a <strong><em>left rotation</em></strong> on the<strong><em> sad node’s left child </em></strong>(node “a” in the image)</li><li>Perform <strong><em>right rotation</em></strong> on the<strong><em> sad node </em></strong>(node “c” in the image)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bb1sC-QIb2nH_yrMHk8X4A.png" /><figcaption>Thanks to Dr. Desmond Yau-chat Tsoi</figcaption></figure><h4>[Optional To Read] Why Do “RL” And “LR” Cases Need Double Rotations?</h4><p>Before answering the question, first, we have to know that <strong><em>“LL” and “RR” cases are easy cases</em></strong> that we know how to <strong><em>solve easily and directly by a single rotation</em></strong>.</p><p>Then, <strong><em>“RL” and “LR” cases are more complicated cases</em></strong> that we don’t know how to solve it directly…</p><p>So, we try to make “RL” and “LR” cases <strong><em>down to</em></strong> the cases that we are familiar with — “RR” and “LL”.</p><p>In other words, the answer to the question:</p><blockquote>It is because the first rotation is to make “RL” and “LR” cases down to “RR” and “LL”, then the second rotation is to solve or fix the “unbalanced” AVL Tree easily and directly.</blockquote><p><strong>Thanks! If you like my stories, </strong><a href="https://medium.com/@reasdom"><strong>please follow me</strong></a><strong> for new updates!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ee45c7ef047" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hyperledger Fabric Account-based Wallet Java Chaincode]]></title>
            <link>https://medium.com/coinmonks/hyperledger-fabric-account-based-wallet-java-chaincode-8cbf80a6fb82?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cbf80a6fb82</guid>
            <category><![CDATA[blockchain-development]]></category>
            <category><![CDATA[chaincode]]></category>
            <category><![CDATA[hyperledger]]></category>
            <category><![CDATA[hyperledger-fabric]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Sat, 09 Feb 2019 17:34:04 GMT</pubDate>
            <atom:updated>2020-08-26T14:31:25.786Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learning by Example — Account-based Wallet Model in A Transaction System</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z2REkO4RIp73p4wESu_0IA.png" /><figcaption><a href="https://www.hyperledger.org/blog/2018/11/28/conducting-data-with-concerto-and-hyperledger-fabric">Hyperledger Fabric Image</a></figcaption></figure><p>This is a tutorial for Hyperledger Fabric <strong><em>Java</em></strong> <strong><em>Chaincode</em></strong>. There are Go and NodeJS Chaincodes as well and we might talk about them in the future. Also, I adopt the account-based wallet model as an example, and we will use Hyperledger Fabric <a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html">“Build Your First Network (BYFN)”</a> as our testing environment (v1.4 network).</p><blockquote>In case you don’t know how to start a “BYFN” network, I will also provide the steps, no worry. But If you want to learn how “BYFN” works, you could refer to:</blockquote><blockquote>(1) <a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html">Hyperledger Fabric official “BYFN” tutorial</a></blockquote><blockquote>(2) <a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-1-2d3b32213529">My previous story about “BYFN”</a></blockquote><h4>What Account-based Wallet Model Is Basically</h4><p>Account-based wallet model is one of the implementations of the <strong><em>transaction system</em></strong>, in which, generally speaking, <strong><em>there are wallets and each wallet contains some values or tokens that could be used to transfer</em></strong>. Of course, there are lots of variations on the details of the implementations, such as the token exchange and multi-tokens features.</p><h4>Features</h4><p>In the Java Chaincode, we will implement:</p><ol><li>User wallet creation</li><li>User wallet query</li><li>Wallet token transfer (i.e. to send money or tokens to from one wallet another wallet)</li></ol><blockquote><strong>[Optional] </strong>There are two database options in Hyperledger Fabric to hold the ledger <strong>world state</strong> — LevelDB and CouchDB, in this tutorial, I will use LevelDB for simplicity.</blockquote><blockquote><strong>[Optional] </strong>Above,<strong> world state</strong> (or global state) is <strong>not</strong> the ledger itself and is separated from the ledger, though it is <strong>derived and could be recovered anytime from the ledger</strong>. Also, world state is stored in the database to represent the ledger, such that those nodes don’t need to always search the ledger for data retrieval and could retrieve data (states) more quickly from the database.</blockquote><blockquote><strong>[Optional]</strong> In my load test and some papers about Hyperledger Fabric performance, LevelDB’s performance is better than CouchDB’s. But CouchDB can support rich query that could be used to build a more complex query, compared with the simple key-value query in LevelDB.</blockquote><h4>The IDE — Tool for Development of Java Chaincode</h4><p>We use <a href="https://www.jetbrains.com/idea/download/#section=mac"><strong><em>IntelliJ IDEA Community</em></strong></a>. Also, you need to have <a href="https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html"><strong><em>JDK</em></strong></a>. Please install them.</p><p>Of course, if you have your own Java IDE, you could also use that. But in this tutorial, I use IntelliJ.</p><h4>Step 1 — Preparation for Development</h4><p>Let’s open IntelliJ.</p><p><strong><em>Create New Project</em></strong> in IntelliJ:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*BeK_U3DaLdND6tg3dlkz2Q.png" /></figure><p>Choose <strong><em>Gradle</em></strong> on the left, then Next:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C5RaZu6XsNLL4kF_Mfvr4A.png" /></figure><p>Type your <strong><em>GroupId</em></strong> and <strong><em>ArtifactId</em></strong>. In my case, I use <strong><em>“java_account_model_cc”</em></strong> for both. Then Next:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IHYV97VEABixSrahqGrHiw.png" /></figure><p>Then, just next. Now, you should configure your own <strong><em>project location</em></strong>. In my case, I use <strong><em>“~/Desktop/java_account_model_cc”</em></strong>. Click <strong><em>Finish</em></strong>.</p><h4>1.1 settings.gradle</h4><p>In the project files on the left, you should have <strong><em>“settings.gradle”</em></strong>. Let’s double click to open it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*idCn-3hO7VCuxwkria1YlQ.png" /><figcaption>Project files on the left panel</figcaption></figure><p>Type the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3444468dee949ab819e7425c2270f84/href">https://medium.com/media/a3444468dee949ab819e7425c2270f84/href</a></iframe><h4>1.2 build.gradle</h4><p>In the project files panel on the left, you should have <strong><em>“build.gradle”</em></strong>. Let’s double click to open it.</p><p>Then, type the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0179182b4584d27fab51399beeb19fd6/href">https://medium.com/media/0179182b4584d27fab51399beeb19fd6/href</a></iframe><p>*If there is a <strong><em>“Gradle project needs to be imported”</em></strong> popup on the bottom right in your IntelliJ, let’s choose <strong><em>“Import Changes”</em></strong>.</p><h4>1.3 The Chaincode File</h4><p>In the project files panel on the left, under <strong><em>src &gt; main &gt; java</em></strong>, right click on it and choose <strong><em>New &gt; Java Class</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cw5t6UGDlc2oFAVFwpacsA.png" /></figure><p>Type <strong><em>“org.hyperledger.fabric.chaincode.AccountBasedChaincode”</em></strong> for the <strong><em>Name</em></strong> field:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*afNlb8jtwSzNSQ1J3rA14g.png" /><figcaption>org.hyperledger.fabric.chaincode.AccountBasedChaincode</figcaption></figure><p>Then, you should have the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*4dqv_ou4Kx3oXVoBfFaOWA.png" /></figure><p><strong><em>AccountBasedChaincode</em></strong>(.java) above is the place we write Chaincode in Java.</p><h4>Step 2 — Analysis of the Requirement</h4><p>Before we start coding, let’s organize what we need to code.</p><p>As said in the “Features” section, in the Java Chaincode, we will implement:</p><ol><li>User wallet creation</li><li>User wallet query</li><li>Wallet token transfer (i.e. to send money to from one wallet another wallet)</li></ol><p>From this simple requirement above, we need to have the following classes:</p><ol><li>Wallet Class</li><li>The Chaincode class — holding the Chaincode</li></ol><p>And our Chaincode should provide these functions:</p><ol><li>Create a wallet</li><li>Transfer tokens from one wallet to another</li><li>Get (query) a wallet</li><li>Init function — called when the Chaincode is <strong><em>instantiated</em></strong></li><li>Invoke function — called when a user wants to call the function (1), (2), or (3)</li></ol><blockquote>The <strong>Init</strong> function must be implemented in Chaincode and will be called automatically whenever we <strong>instantiate<em> </em></strong>or<strong><em> </em>upgrade<em> </em></strong>the Chaincode. Usually, it is used to initialize some data in the Blockchain.</blockquote><blockquote>The <strong>Invoke</strong> function is used to receive <strong>all</strong> user function calls, then based on the call, it invokes (calls) the corresponding function — (1), (2), or (3). It is like a router — routing incoming requests to a different path.</blockquote><h4>Step 3 — The Wallet Class</h4><p>Now, we create and code the Wallet class.</p><p>Create a new package under <strong><em>“org.hyperledger.fabric.chaincode”:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m1d9U__dRWFOttVm7rdCSg.png" /></figure><p>Call the package <strong><em>Models</em></strong>, then OK:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*wit7083YVAh3NBkkaGLc7w.png" /><figcaption>Package “Models”</figcaption></figure><p>Now you should have the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*qTlLZ34iSi7TJVG-gyj6_A.png" /></figure><p>Under the <strong><em>Models</em></strong> package, create a new Java class and name it <strong><em>Wallet</em></strong>. (I don’t show detailed steps this time, let’s try yourself)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dRGRjQiWn8d3k-dqYLs-pg.png" /><figcaption>Creating <strong><em>Wallet </em></strong><em>class under </em><strong>Models</strong> package</figcaption></figure><p>Now we code for the <strong><em>Wallet</em></strong> class, let’s type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b2ae64812f88ea1b03c790234bed921/href">https://medium.com/media/4b2ae64812f88ea1b03c790234bed921/href</a></iframe><p>The wallet has a <strong><em>wallet id</em></strong> to identify a certain wallet and a <strong><em>token amount</em></strong> to specify how many tokens the wallet owns.</p><blockquote>Note that in the production case, the Wallet class should be (far) more complicated. For example, you probably use “BigDecimal” data type instead of “Double” for tokenAmount. Also, in our case, we support only one token type (i.e. only one currency) in the whole transaction system.</blockquote><h4>Step 4— The Chaincode Class</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe829b23eaac2e4e08299425c5719679/href">https://medium.com/media/fe829b23eaac2e4e08299425c5719679/href</a></iframe><blockquote>Note that in the production case, the codes should be different from mine. These codes are in demo or tutorial purpose mainly.</blockquote><h4>Step 5 — Running the Chaincode in BYFN</h4><h4>5.1 Install Prerequisites</h4><p>We use Hyperledger Fabric v1.4 in this tutorial.</p><p>First of all, you could install prerequisites by following the official instructions:</p><ol><li><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/prereqs.html#prerequisites">Install prerequisites</a></li><li><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/install.html">Install samples, programs, and docker images from Hyperledger Fabric</a></li></ol><h4>5.2 Chaincode Preparation</h4><p>Then, let’s switch to this directory (*supposed that you finish the prerequisite part, you should have all needed files and directories):</p><pre>cd fabric-samples/chaincode/chaincode_example02/<br>mv java java_01<br>mkdir java</pre><p>Now, copy the following <strong><em>highlighted</em></strong> files in your project directory to <strong><em>”fabric-samples/chaincode/chaincode_example02/java/”</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*rTQG3Dw879v25ofUobCqww.png" /><figcaption>Copy the highlighted files to <strong><em>”fabric-samples/chaincode/chaincode_example02/java/”</em></strong></figcaption></figure><h4>5.3 Bring Up the Network</h4><pre>cd ../..<strong>/</strong>first<strong>-</strong>network<br>./byfn.sh up -l java</pre><blockquote>After running this script, you might need to wait for a moment…</blockquote><blockquote><strong>*** If you see the following error (instead of other errors), that’s OKAY, just proceed to the next steps ***</strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/367e1b445e8ba3d2cd43164e06b8cb9f/href">https://medium.com/media/367e1b445e8ba3d2cd43164e06b8cb9f/href</a></iframe><blockquote>Also, remember to start Docker, before you run the command above.</blockquote><blockquote>*In case you make something wrongly, you can run the following commands to turn down the network, and bring up again:</blockquote><blockquote>./byfn.sh down<br>./byfn.sh up -l java</blockquote><p>Now, we test whether our Chaincode works.</p><h4>5.4 Access the Cli</h4><p>There is a cli Docker container created automatically, it is a command line interface to <strong><em>control the nodes</em></strong>.</p><p>Let’s get access to the cli:</p><pre>docker exec -it cli bash</pre><p>Then, set up <strong><em>environment variables</em></strong> used by some programs:</p><pre>export CHANNEL_NAME=mychannel<br>export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp<br>export CORE_PEER_ADDRESS=peer0.org1.example.com:7051<br>export CORE_PEER_LOCALMSPID=&quot;Org1MSP&quot;<br>export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt</pre><blockquote>You could just copy all of them, then paste to your terminal and press “enter”.</blockquote><blockquote>These environment variables are used to let some Hyperledger Fabric programs know that we need to use <strong>peer0.org1.example.com:7051</strong> to invoke Chaincode functions.</blockquote><p>Now, we <strong><em>create two wallets</em></strong> with wallet IDs —<strong><em> “tom” </em></strong>and <strong><em>“sam”</em></strong>:</p><pre>peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c &#39;{&quot;Args&quot;:[&quot;createWallet&quot;,&quot;tom&quot;,&quot;100&quot;]}&#39;</pre><pre>peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c &#39;{&quot;Args&quot;:[&quot;createWallet&quot;,&quot;sam&quot;,&quot;100&quot;]}&#39;</pre><blockquote>Update: Note that we have two --peerAddresses in order to send to 2 Peers to endorse the transaction, since the default endorsement policy in Build Your First Network (BYFN) is an <strong>AND </strong>policy, which requires both organizations, Org1 and Org2, to sign each transaction. For more explainations, please refer to <a href="https://medium.com/@reasdom/hello-tommaso-7d10c5f48c9f">here</a>.</blockquote><p>You should see something like this in the terminal after running each command above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d31a3b7bb517386efb71f24c70327d3/href">https://medium.com/media/3d31a3b7bb517386efb71f24c70327d3/href</a></iframe><blockquote>Update: As <a href="https://medium.com/u/cbc527cd27aa">Tommaso Martin</a> suggested, --peerAddresses can support array for its argument as well. Instead of the <em>--peerAddresses </em>usage in the Invoke commands above (using <em>--peerAddresses</em> two times for two Peers), you <strong>may </strong>consider (using <em>--peerAddresses </em>one time and passing it an array of two Peers):</blockquote><pre>[...] --peerAddresses peer0.org1.example.com:7051 peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt [...]</pre><p>Now, we <strong><em>get two wallets </em></strong>created above to verify whether they are existing in the Blockchain:</p><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWallet&quot;,&quot;tom&quot;]}&#39;</pre><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWallet&quot;,&quot;sam&quot;]}&#39;</pre><p>You should see something like this in the terminal after running each command above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/757a8371c3a69a20a278155c1a2b9dea/href">https://medium.com/media/757a8371c3a69a20a278155c1a2b9dea/href</a></iframe><p>Above, we can see that the two wallets created previously can be queried. And both of them have <strong><em>100 tokens</em></strong>.</p><p>Next, we make a <strong><em>transfer</em></strong> transaction — let’s transfer <strong><em>10 tokens from “tom” wallet to “sam” wallet</em></strong>:</p><pre>peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c &#39;{&quot;Args&quot;:[&quot;transfer&quot;,&quot;tom&quot;,&quot;sam&quot;,&quot;10&quot;]}&#39;</pre><p>Finally, let’s verify two wallets again:</p><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWallet&quot;,&quot;tom&quot;]}&#39;</pre><pre>peer chaincode query -C $CHANNEL_NAME -n mycc -c &#39;{&quot;Args&quot;:[&quot;getWallet&quot;,&quot;sam&quot;]}&#39;</pre><p>Can you have the following result?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a5248e5be2008ac976880660f15aca3/href">https://medium.com/media/3a5248e5be2008ac976880660f15aca3/href</a></iframe><p>Note that now <strong><em>Tom’s wallet remains 90 tokens and Sam’s wallet has 110 tokens</em></strong>, the <strong><em>transaction is done </em></strong>and written into the Blockchain ledger.</p><h4>Step 6— Cleaning Up</h4><pre>exit<br>./byfn.sh down</pre><p>Then, delete directory <strong><em>“fabric-samples/chaincode/chaincode_example02/java”</em></strong> and rename directory <strong><em>”java_01”</em></strong> back to <strong><em>”java”</em></strong>.</p><p><strong>Thanks! If you like my stories, </strong><a href="https://medium.com/@reasdom"><strong>please follow me</strong></a><strong> for new updates!</strong></p><h4>Reference</h4><ul><li><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html">Building Your First Network - hyperledger-fabricdocs master documentation</a></li><li><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/chaincode4ade.html">Chaincode for Developers - hyperledger-fabricdocs master documentation</a></li></ul><figure><a href="https://cryptofi.co"><img alt="" src="https://cdn-images-1.medium.com/max/407/1*HrFLDXz92JavTZXjhmvU7g.png" /></a></figure><blockquote><a href="https://coincodecap.com/?utm_source=coinmonks">Get Best Software Deals Directly In Your Inbox</a></blockquote><figure><a href="https://coincodecap.com/?utm_source=coinmonks"><img alt="" src="https://cdn-images-1.medium.com/max/584/0*OJ-qb5G6i863msBB.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cbf80a6fb82" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/hyperledger-fabric-account-based-wallet-java-chaincode-8cbf80a6fb82">Hyperledger Fabric Account-based Wallet Java Chaincode</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hyperledger Fabric “Building Your First Network” Tutorial — Part 3]]></title>
            <link>https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-3-fde6022d8739?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/fde6022d8739</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[hyperledger]]></category>
            <category><![CDATA[blockchain-development]]></category>
            <category><![CDATA[hyperledger-fabic]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Wed, 06 Feb 2019 06:26:22 GMT</pubDate>
            <atom:updated>2019-02-12T12:14:13.476Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hyperledger Fabric “Building Your First Network” Tutorial — Part 3</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ct_H7XYmPIC1xqgy3X4KsQ.png" /><figcaption><a href="https://www.hyperledger.org/projects/fabric">Hyperledger Fabric Logo</a></figcaption></figure><p>In part <a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-1-2d3b32213529">1</a> and <a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-2-267fc5d36118">2</a>, I had introduced files in BYFN. In this part 3, I am going to continue practical steps!</p><h4>3. Bring Up the Network</h4><h4>3.1 Switch to BYFN Directory</h4><pre>cd fabric<strong>-</strong>samples<strong>/</strong>first<strong>-</strong>network</pre><h4>3.2 Bring Up the Network</h4><pre>./byfn.sh up</pre><blockquote>After running this script, you might need to wait for a moment…</blockquote><blockquote><strong>Also, remember to start Docker, before you run the command above.</strong></blockquote><blockquote>*In case you make something wrongly, you can run the following commands to turn down the network, and bring up again (and be careful):</blockquote><blockquote>./byfn.sh down<br>./byfn.sh up</blockquote><p>Remember what it does?</p><ol><li>Generate certificates and keys based on <strong><em>crypto-config.yaml</em></strong></li><li>Generate channel artifacts based on <strong><em>configtx.yaml</em></strong>, outputs will be stored in <strong><em>channel-artifacts/</em></strong></li><li>Bring up the (virtual) Fabric network based on <strong><em>docker-compose-cli.yaml</em></strong></li><li>Create Hyperledger Fabric channel, <strong><em>mychannel</em></strong></li><li>Join Peer nodes into the channel</li><li>Install the Smart Contract (Chaincode) in Peer nodes</li><li>Instantiate the Smart Contract (Chaincode) in one of the Peer nodes</li></ol><blockquote>After the <em>byfn.sh</em> is finished, you should also find that there are files generated in <strong>channel-artifacts/</strong>.</blockquote><blockquote>And If you execute command “docker ps”, you should see that there are some running containers, which are those nodes in the Blockchain network.</blockquote><h4>4. Verification</h4><p>How could we verify that the network is up?</p><h4>4.1 Access the Cli</h4><p>There is a cli Docker container created automatically, it is a command line interface to <strong><em>control the nodes</em></strong>.</p><p>Let’s get access to the cli:</p><pre>docker exec -it cli bash</pre><p>Then, set up <strong><em>environment variables</em></strong> used by some programs:</p><pre>export CHANNEL_NAME=mychannel<br>export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp<br>export CORE_PEER_ADDRESS=peer0.org1.example.com:7051<br>export CORE_PEER_LOCALMSPID=&quot;Org1MSP&quot;<br>export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt</pre><blockquote>You could just copy all of them, then paste to your terminal and press “enter”.</blockquote><p>To verify, we will make a transaction. Let’s check a’s account balance:</p><pre>peer chaincode query -C mychannel -n mycc -c &#39;{&quot;Args&quot;:[&quot;query&quot;,&quot;a&quot;]}&#39;</pre><p>You should see <strong><em>90</em></strong>. Now we <strong><em>transfer $10 from a to b</em></strong>:</p><pre>peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c &#39;{&quot;Args&quot;:[&quot;invoke&quot;,&quot;a&quot;,&quot;b&quot;,&quot;10&quot;]}&#39;</pre><p>Then, we check a’s account balance again:</p><pre>peer chaincode query -C mychannel -n mycc -c &#39;{&quot;Args&quot;:[&quot;query&quot;,&quot;a&quot;]}&#39;</pre><p>You should see <strong><em>80</em></strong> (90 - 10 = 80). The Blockchain network is working!</p><p>Now exit the cil and turn down the network:</p><pre>exit<br>./byfn.sh down</pre><p>Later, to develop Smart Contract (Chaincode), we could use this BYFN to test the functionalities. And we could take advantage of such a virtual Docker network — turn up and down easily.</p><p><strong>Thanks! If you like my stories, </strong><a href="https://medium.com/@reasdom"><strong>please follow me</strong></a><strong> for new updates!</strong></p><h4>Reference</h4><p><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html">Building Your First Network - hyperledger-fabricdocs master documentation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fde6022d8739" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hyperledger Fabric “Building Your First Network” Tutorial — Part 2]]></title>
            <link>https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-2-267fc5d36118?source=rss-922f4352103d------2</link>
            <guid isPermaLink="false">https://medium.com/p/267fc5d36118</guid>
            <category><![CDATA[blockchain-development]]></category>
            <category><![CDATA[hyperledger-fabric]]></category>
            <category><![CDATA[hyperledger]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Sam LAM]]></dc:creator>
            <pubDate>Wed, 06 Feb 2019 05:57:27 GMT</pubDate>
            <atom:updated>2019-02-12T12:13:04.607Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hyperledger Fabric “Building Your First Network” Tutorial — Part 2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ct_H7XYmPIC1xqgy3X4KsQ.png" /><figcaption><a href="https://www.hyperledger.org/projects/fabric">Hyperledger Fabric Logo</a></figcaption></figure><p>In <a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-1-2d3b32213529">part 1</a> of this tutorial set, I was introducing the files in BYFN. In part 2, I am going to introduce the remaining files.</p><h4>Recap the File List</h4><p>Let’s recap our list of files:</p><ol><li>docker-compose-cli.yaml (<a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-1-2d3b32213529">Part 1</a>)</li><li><strong><em>base/docker-compose-base.yaml (Part 2)</em></strong></li><li><strong><em>base/peer-base.yaml (Part 2)</em></strong></li><li><strong><em>channel-artifacts/ (Part 2)</em></strong></li><li><strong><em>crypto-config.yaml (Part 2)</em></strong></li><li><strong><em>configtx.yaml (Part 2)</em></strong></li><li><strong><em>byfn.sh (Part 2)</em></strong></li><li><strong><em>scripts/script.sh (Part 2)</em></strong></li><li><strong><em>scripts/utils.sh (Part 2)</em></strong></li></ol><p>We will finish all the remaining files in this part.</p><h4>2.2 base/docker-compose-base.yaml</h4><p>In <a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-1-2d3b32213529">part 1</a>, we have introduced<strong><em>docker-compose-cli.yaml,</em></strong> which is to define our (virtual) Fabric network. We review a part of docker-compose-cli.yaml:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/226fc8de86612dc6ccef3d789e713c2b/href">https://medium.com/media/226fc8de86612dc6ccef3d789e713c2b/href</a></iframe><p>Notice that there is an <strong><em>extends</em></strong> keyword. As discussed in <a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-1-2d3b32213529">part 1</a>, it is to <strong><em>re-use </em></strong>another <strong><em>base</em></strong> Docker compose file. The purpose of this is to make things <strong><em>cleaner</em></strong> — by grouping the shared configurations in one base file, such that other Docker compose files can <strong><em>extend</em></strong> it. It is about good practice — <strong><em>reusability</em></strong>, but this is not mandatory.</p><p>Then, the <strong><em>file</em></strong> keyword in line 8 specifies which base file to extend, and <strong><em>service</em></strong> keyword specifies which item (service) in the base file to extend.</p><p>Now, we look at <strong><em>base/docker-compose-base.yaml</em></strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/799a9aa7fd924c2b61233ea1e08e5e0d/href">https://medium.com/media/799a9aa7fd924c2b61233ea1e08e5e0d/href</a></iframe><p>The base file is similar to <strong><em>docker-compose-cli.yaml</em></strong>.</p><p>Lines 3 to 28 define a template node called <strong><em>orderer.example.com</em></strong>, and <strong><em>docker-compose-cli.yaml</em></strong> extends it and add some extra configurations based on this template.</p><p>Line 5 specifies which image this node uses. In our case, the node uses an image from Hyperledger official — <strong><em>hyperledger/fabric-orderer</em></strong>. That <strong><em>$IMAGE_TAG</em></strong> is the image version, such as <strong><em>1.4</em></strong>.</p><p>Other keywords and configurations are similar to <strong><em>docker-compose-cli.yaml</em></strong>. The point of having a base file is just to group common configurations to a base file to make them reusable.</p><p>Notice that in line 32, there is also an <strong><em>extends</em></strong> keyword, that is, again, to extend or reuse another base file,<strong><em>base/peer-base.yaml</em></strong>, which we will quickly take a look.</p><h4>2.3 base/peer-base.yaml</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2cadd392fe0cc17692b8d1b569bf703/href">https://medium.com/media/f2cadd392fe0cc17692b8d1b569bf703/href</a></iframe><p>The syntax of this file is more or less the same as <strong><em>docker-compose-cli.yaml</em></strong> and <strong><em>base/peer-base.yaml</em></strong>. Again, why do we need these base files? Because of reusability, though it is not mandatory.</p><h4>2.4 channel-artifacts/</h4><p>Notice that this is an empty directory currently. This directory <strong><em>will</em></strong> be used to store some configuration transactions and a genesis block. More details later.</p><h4>2.5 crypto-config.yaml</h4><p>This is a file to <strong><em>define certifications and keys to be generated and used in the network</em></strong>.</p><p>In Hyperledger Fabric, we have a set of certifications and keys for users and nodes. For example, Peer needs to have a set of certificates and keys to perform endorsement, prove itself as a member in the Blockchain network, do signing, etc.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46e647d6de3c83e787a2d2458f11c068/href">https://medium.com/media/46e647d6de3c83e787a2d2458f11c068/href</a></iframe><p>Line 4 <strong><em>OrdererOrgs</em></strong> is defining certificates and keys generation for organization related to Orderer nodes.</p><p>Line 22 <strong><em>Template &gt; Count </em></strong>specifies how many nodes in Org1 (organization 1), while 29 <strong><em>Users &gt; Count</em></strong> specifies how many users in Org1.</p><h4>2.6 configtx.yaml</h4><p>This is a file to <strong><em>define the configurations of the Blockchain network</em></strong>, though later they could be updated.</p><p>This is a long file, so I don’t embed into the blog post. You could refer to it <a href="https://gist.github.com/reasdom/2f20afe7fd9254b2681c9b5bd47d1879">here</a>.</p><p>Let’s look at some parts of this file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e084ee74e2d95ef7b3297fee48a7d1fb/href">https://medium.com/media/e084ee74e2d95ef7b3297fee48a7d1fb/href</a></iframe><p>Here, we are defining an organization — Org1 in the Blockchain network, along with some policies.</p><p>Line 15 specifies <strong><em>Anchor Peer</em></strong> of Org1, that is a special role of Peer node — to act as a <strong><em>representative</em></strong> to talk with other organizations.</p><p>Line 4 specifies the location of org1 <strong><em>certifications and keys</em></strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e81e77b435b906ae8eaa48dd2368efb6/href">https://medium.com/media/e81e77b435b906ae8eaa48dd2368efb6/href</a></iframe><p>Here, we are defining configurations related to how to form a block for the Blockchain, such as <strong><em>MaxMessageCount</em></strong>, that is the number of transactions in each block.</p><p>So, overall, there are three organizations, OrdererOrg, Org1, and Org2. You may refer to <a href="https://gist.github.com/reasdom/2f20afe7fd9254b2681c9b5bd47d1879">here</a> for more details of <em>configtx.yaml</em>.</p><h4><strong><em>2.7 byfn.sh</em></strong></h4><p><a href="https://github.com/hyperledger/fabric-samples/blob/release-1.4/first-network/byfn.sh">This is a convenient script file</a> by Hyperledger Fabric to start the network.</p><p>We use it this way to bring up the network (we will run together later):</p><pre>./byfn.sh up</pre><p>Basically, it does the followings:</p><ol><li>Generate certificates and keys based on <strong><em>crypto-config.yaml</em></strong></li><li>Generate channel artifacts based on <strong><em>configtx.yaml</em></strong>, outputs will be stored in <strong><em>channel-artifacts/</em></strong></li><li>Bring up the (virtual) Fabric network based on <strong><em>docker-compose-cli.yaml</em></strong></li><li>Create Hyperledger Fabric channel, <strong><em>mychannel</em></strong></li><li>Join Peer nodes into the channel</li><li>Install the Smart Contract (Chaincode) in Peer nodes</li><li>Instantiate the Smart Contract (Chaincode) in one of the Peer nodes</li></ol><blockquote><strong>Channel</strong> in Hyperledger Fabric is a private group in a Blockchain network. And each Blockchain network could contain multiple Channels, where each Channel is independent to other Channels, has its own ledger, and contains (multiple) organisation(s).</blockquote><h4><strong><em>2.8 scripts/script.sh &amp; scripts/utils.sh</em></strong></h4><ul><li><em>scripts/script.sh</em> is a script <strong><em>used by </em></strong><strong><em>byfn.sh for steps 4 to 7</em></strong> above.</li><li><em>scripts/utils.sh</em> is a script <strong><em>used by </em></strong><strong><em>scripts/script.sh</em></strong>. It contains some common utility functions, such as installing Smart Contract (Chaincode)</li></ul><p>Starting from the next part, we will continue some practical steps.</p><p><strong>Thanks! If you like my stories, </strong><a href="https://medium.com/@reasdom"><strong>please follow me</strong></a><strong> for new updates!</strong></p><p>Next: <a href="https://medium.com/@reasdom/hyperledger-fabric-building-your-first-network-tutorial-part-3-fde6022d8739">Part3</a></p><h4>Reference</h4><p><a href="https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html">Building Your First Network - hyperledger-fabricdocs master documentation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=267fc5d36118" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>