<?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 Clyde D&#39;Cruz on Medium]]></title>
        <description><![CDATA[Stories by Clyde D&#39;Cruz on Medium]]></description>
        <link>https://medium.com/@clydedcruz?source=rss-e618c51f9377------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*FhoOimXQdPSTtAiKyEPL0Q.jpeg</url>
            <title>Stories by Clyde D&amp;#39;Cruz on Medium</title>
            <link>https://medium.com/@clydedcruz?source=rss-e618c51f9377------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 20:55:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@clydedcruz/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[What really is a crypto token?]]></title>
            <link>https://medium.com/coinmonks/what-really-is-a-crypto-token-57b02c8bff3e?source=rss-e618c51f9377------2</link>
            <guid isPermaLink="false">https://medium.com/p/57b02c8bff3e</guid>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[token]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Clyde D'Cruz]]></dc:creator>
            <pubDate>Thu, 31 Mar 2022 18:28:00 GMT</pubDate>
            <atom:updated>2022-05-16T17:58:55.464Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cE0k6cZRv3-kYpfxn7ymjA.jpeg" /><figcaption><a href="https://www.pexels.com/photo/round-gold-colored-ethereum-coin-1036637">Photo by Moose Photos</a></figcaption></figure><p>Even though the term token is widely used in computer science literature, tech blogs and news, its usage is quite inconsistent and confusing. The word “token” could be used for describing different things. It is often equated to mean a digital object that has similar properties to physical cash. This post aims to address this confusion and shed light on what “token” really means and what it doesn&#39;t.</p><p>The below graphic shows the difference between two ways of storing and transferring value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n4DcfFWkAF6SQFuGX9HoYA.png" /><figcaption>Source: <a href="https://www.bis.org/publ/qtrpdf/r_qt2003j.htm">The technology of retail central bank digital currency (BIS March 2020)</a></figcaption></figure><p>In the first case, the value is maintained on a centralized ledger against an identity. This would store information like “Alice owns 100 dollars and Bob owns 50”. Alice would have to approach a bank and produce some identification. If a cashier is able to verify her ID then they would execute the instruction to transfer money. The bank would then debit Alice’s account and credit that of the recipient.</p><p>In the second case, there is no central ledger that maintains the balances. Claims are honored only based on knowledge, such as a digital signature. The transfer is executed and verified using asymmetric key pairs and digital signatures.</p><p>A couple of important differences are apparent between these two approaches to transferring value.</p><ol><li>In the first model the focus is on determining the legitimacy of the <em>parties </em>transacting , whereas in the second model the focus is on determining the legitimacy of the <em>thing </em>being transferred.</li><li>In the second model, you don’t need a central party to verify transfers. You can execute transfers using something that you know e.g. a private key</li></ol><p>The first approach here is known as the “accounts model” approach and the second, the “token model”. This helps us arrive at a possible definition for what a “Token” may be.</p><blockquote>Tokens are digital assets that can be transferred from one party to another without the involvement of a centralized third party</blockquote><p>Lets see how this definition holds against some commonly used assets that we consider “tokens” and some that we don&#39;t.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*0QNaVM2zpxX5gskXqeEo6w.png" /></figure><p>Here physical cash which is considered a token can simply be transferred from one party to another, as long as we that the cash we receive is genuine. Bank deposits which do need a centralized third party to verify and execute the transfer are digital balances and cannot be considered tokens. Bitcoin and Ether which are native cryptographic tokens of the Bitcoin and Ethereum blockchains respectively do not need a centralized party for their transfer, neither do <a href="https://tether.to/">Tether</a> ERC-20 tokens and <a href="https://www.cryptokitties.co/">CryptoKitty</a> NFTs.</p><p>So far, our definition looks pretty solid. But lets take a closer look at how “tokens” in common blockchain platforms really work.</p><p><strong>Bitcoin </strong>Wallets store cryptographic keys used for signing transactions. The user’s balance is maintained using a system called <a href="https://river.com/learn/bitcoins-utxo-model/">UTXO</a> on a distributed, replicated ledger of public nodes. Transfers involve verifying that UTXOs are being spent only by a user who holds a specified public key.</p><p><strong>Ethereum </strong>Like in Bitcoin, wallets store cryptographic keys used for signing transactions, and balances are maintained against accounts on a distributed, replicated ledger of public nodes. Transactions are cryptographically signed instructions from accounts. An account will initiate a transaction to update the state of the Ethereum network (for e.g. debit Bob’s account and credit that of Alice) and contains a digital signature component generated when the sender’s private key signs the transaction. This is used to confirm that the sender has authorized this transaction.</p><h3>Wait a minute…</h3><p>The way “tokens” work in Bitcoin and Ethereum, sounds awfully close to how the transfer of bank deposits does. i.e. your balance is maintained by a server, (though here it is a replicated bunch of them ) and you need to provide a digital signature to prove to the authority that it is really you who is spending the funds. Two important observations come forth from taking a closer look how tokens are implemented in these blockchain platforms.</p><p><strong>The ledger holds the tokens </strong>Your crypto wallet only holds the cryptographic keys that correspond to, and which are used to prove your identity. The blockchain holds the balance that corresponds to your public key. Hence its not the wallet that holds the tokens, but the ledger (blockchain) itself. If all blockchain nodes are wiped clean (though that very unlikely) so is your balance of tokens that corresponds to your identity.</p><p><strong>You can’t transfer tokens without the ledger </strong>In both Bitcoin and Ethereum, the transaction to transfer “tokens” cannot happen without access to the “ledger” i.e. the blockchain. A request to transfer value must go to a blockchain node and be accepted by a sufficient number of nodes who need to agree on the overall state of the blockchain.</p><p><strong>Crypto tokens ≠ bearer tokens</strong></p><p>These observations make it evident that these tokens differ from physical tokens such as Cash. Unlike cash, these are are not “bearer tokens”. They can be transferred from one party to another without the involvement of a “centralized third party” but do however need the decentralized network of nodes to holds balances and verify transactions.</p><p>A closer fit to the “bearer token” model of digitally transferring value was achieved by David Chaum’s <a href="https://www.usenix.org/legacy/publications/library/proceedings/ec95/full_papers/camp.txt">ECash </a>system which allowed for an offline transfer between two transactors, but it required the merchant receiving an electronic token to quickly present that token to the electronic bank for verification, or to risk having a worthless token.</p><p>Why then did the community start to call these blockchain based asset balances “tokens”?</p><p>The following set of excerpts from the <a href="https://www.federalreserve.gov/econres/notes/feds-notes/tokens-and-accounts-in-the-context-of-digital-currencies-122320.htm">FEDS Note on Tokens and accounts in the context of digital currencies</a> reveals how we came to refer to smart contract based asset balances as tokens.</p><blockquote>Ethereum’s design allows for the creation of generalized computer programs known as smart contracts, which are executable code stored on the Ethereum blockchain.</blockquote><blockquote>An early use case for smart contracts was the programmatic definition of assets (or representations thereof) on a blockchain. The Ethereum community termed these assets “tokens”. The general idea was that a smart contract could define its own ledger for tracking user balances of a token (essentially a sub-ledger of Ethereum, specific to that particular smart contract) and allowing users to transact in the asset represented by that token.</blockquote><p>This is also apparent in the ERC-20 token standard which was the earliest Ethereum Improvement Proposal (EIP)to standardize the way fungible assets are represented on Ethereum. It is described as a “standard interface for tokens” that “allows for the implementation of a standard API for tokens within smart contracts.”. Following in Ethereum’s footsteps, similar token standards have been adopted by other platforms with support for smart contracts, and hence the word “token” to refer to assets tracked on a blockchain has caught on.</p><h3>Conclusion</h3><p>This post began with distinguishing between modes of digital asset transfers i.e. the accounts model and the tokens model. It then tested a generic definition for tokens against assets commonly referred to as tokens. It further explored how tokens work on Bitcoin and Ethereum to compare them with “bearer tokens” . To conclude it looks at the early attempts to standardize representation of assets in Ethereum, and thereby attempts to address the confusion that arises from the similarity of blockchain based tokens with the accounts model.</p><h3>References</h3><ol><li><a href="https://www.federalreserve.gov/econres/notes/feds-notes/tokens-and-accounts-in-the-context-of-digital-currencies-122320.htm">FEDS Notes — Tokens and accounts in the context of digital currencies</a></li><li><a href="https://www.stlouisfed.org/on-the-economy/2020/october/tokens-accounts-why-distinction-matters">Charles M Kahn — Tokens vs. Accounts: Why the Distinction Still Matters</a></li><li><a href="https://www.linkedin.com/pulse/its-time-abandon-token-vs-account-discussion-ezechiel-copic/">Ezechiel Copic — It’s Time to Abandon the “Token vs. Account” Discussion</a></li></ol><blockquote>Join Coinmonks<a href="https://t.me/coincodecap"> Telegram Channel</a> and<a href="https://www.youtube.com/c/coinmonks/videos"> Youtube Channel</a> learn about crypto trading and investing</blockquote><h3>Also, Read</h3><ul><li><a href="https://coincodecap.com/bookmap-review-2021-best-trading-software">Bookmap Review</a> |<a href="https://coincodecap.com/crypto-exchange-usa"> 5 Best Crypto Exchanges in the USA</a></li><li>The Best Crypto<a href="https://medium.com/coinmonks/hardware-wallets-dfa1211730c6"> Hardware wallet</a> |<a href="https://medium.com/coinmonks/bitbns-review-38256a07e161"> Bitbns Review</a></li><li><a href="https://coincodecap.com/crypto-exchange-in-singapore">10 Best Crypto Exchange in Singapore</a> |<a href="https://coincodecap.com/buy-axs-token"> Buy AXS</a></li><li><a href="https://coincodecap.com/red-dog-casino-review">Red Dog Casino Review</a> |<a href="https://coincodecap.com/swyftx-review"> Swyftx Review</a> |<a href="https://coincodecap.com/coingate-review"> CoinGate Review</a></li><li><a href="https://coincodecap.com/best-crypto-to-invest-in-india-in-2021">Best Crypto to Invest in India</a> |<a href="https://coincodecap.com/wazirx-p2p"> WazirX P2P</a> |<a href="https://coincodecap.com/hi-dollar-review"> Hi Dollar Review</a></li><li><a href="https://coincodecap.com/5-best-crypto-trading-bots-in-canada">Best Crypto Trading bots in Canada</a> |<a href="https://coincodecap.com/kucoin-review"> KuCoin Review</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57b02c8bff3e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/what-really-is-a-crypto-token-57b02c8bff3e">What really is a crypto token?</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[Corda backchain verification explained]]></title>
            <link>https://clydedcruz.medium.com/corda-backchain-verification-explained-6a11b2c405ba?source=rss-e618c51f9377------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a11b2c405ba</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[privacy]]></category>
            <category><![CDATA[corda]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Clyde D'Cruz]]></dc:creator>
            <pubDate>Mon, 28 Jun 2021 19:24:09 GMT</pubDate>
            <atom:updated>2021-06-30T05:41:46.012Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YIqJya6cd8os65-yrcoTmA.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@matej-117839?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Matej</strong></a> from <a href="https://www.pexels.com/photo/gray-and-black-chain-886601/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels"><strong>Pexels</strong></a></figcaption></figure><p>Corda does not need every node in the network to hold a copy of the entire ledger like how nodes of some blockchain technologies do. Nodes in Corda only need to agree on shared facts relevant to them. There is no single network-wide ledger. In the absence of a single global view of the ledger, nodes uses a process called “walking the chain” to verify the provenance of assets being consumed in a Corda transaction.</p><p>This blog explains how “backchains” are formed, how they figure in transaction verification, effects of long backchains and approaches to improving privacy and performance in the context of backchain verification.</p><h3>Backchain verification</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M6AUhB9-49TMPHvs-6BxJg.png" /><figcaption><a href="https://docs.corda.net/docs/corda-os/4.7/api-transactions.html#transaction-components">Transaction components</a></figcaption></figure><h4><strong>How transactions are linked</strong></h4><p>A transaction consists of states, commands, attachments and a time window. States can include zero or more input states, output states and <a href="https://docs.corda.net/docs/corda-os/4.8/key-concepts-states.html#reference-states">reference states</a>. Inputs are added to a transaction as a <a href="https://api.corda.net/api/corda-os/4.8/html/api/kotlin/corda/net.corda.core.contracts/-state-and-ref/index.html">StateAndRef</a><strong>,</strong> a class which combines the ContractState instance of an input with its <a href="https://api.corda.net/api/corda-os/4.8/html/api/kotlin/corda/net.corda.core.contracts/-state-ref/index.html">StateRef</a>. The StateRef here is of much importance. It contains the transaction hash of the past transaction where a particular input state was produced as an output and the output index at which it was produced.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G-xkPhbukeMg-PP2Olr1OA.png" /><figcaption>StateRefs form links from an input state to the transaction that generated it as an output</figcaption></figure><p>These two pieces of information from the StateRef form a link from an input state to the past transaction that produced the state as an output. These links form chains (commonly referred to as backchains) connecting each input back to an original issuance transaction. A Corda node can use these links to “walk the chain” and verify that each input was generated from a valid sequence of transactions.</p><h4><strong>Resolving and verifying transaction dependencies</strong></h4><p>A look at the <a href="https://github.com/corda/corda/blob/59d49b77f84fd0499950a86ccf3c10b7bd73f289/core/src/main/kotlin/net/corda/core/transactions/SignedTransaction.kt#L167-L187">code docs</a> for SignedTransaction class reveals that calling verify() on it does the following:</p><ul><li>Checks that the transaction’s signatures are valid, optionally checks if all required signatures are present.</li><li>Resolves inputs and attachments from the local storage</li><li>Performs full transaction verification, including running the contracts</li></ul><p>The verify() check is not only called on the incoming transaction, but also on past transactions that produced the states referenced in the current transaction. Since verify() resolves inputs and attachments from local storage, as a prior step it needs to fetch all missing transaction dependencies from the transaction proposer using the inbuilt <a href="https://docs.corda.net/docs/corda-os/4.6/api-flows.html#sendtransactionflowreceivetransactionflow">SendTransactionFlow and ReceiveTransactionFlow</a>.</p><p>The transaction proposer uses <em>SendTransactionFlow </em>to send the new transaction to the receiver and to process all subsequent requests for dependencies as the receiver walks the dependency chain.</p><p>On the receiver side the ReceiveTransactionFlow receives the initial transaction, calls <a href="https://github.com/corda/corda/blob/release/os%2F4.8/core/src/main/kotlin/net/corda/core/internal/ResolveTransactionsFlow.kt">ResolveTransactionFlow</a> to automatically resolve its dependencies and lastly calls <em>verify()</em> on the SignedTransaction instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*5y_lqoIjr1ui-5PuGW0j1g.png" /><figcaption><a href="https://github.com/corda/corda/blob/release/os%2F4.8/core/src/main/kotlin/net/corda/core/flows/ReceiveTransactionFlow.kt">ReceiveTransactionFlow.kt</a></figcaption></figure><h4><strong>Bulk fetch of transaction backchains</strong></h4><p>This Corda Enterprise feature <a href="https://www.corda.net/blog/performance-improvements-in-corda-enterprise-4-4-and-4-5/">introduced</a> in version 4.4, optimizes transaction backchain resolution through the use of bulk requests. The Corda node configuration includes a <a href="https://docs.corda.net/docs/corda-enterprise/4.8/operations/deployment/corda-configuration-fields.html#backchainfetchbatchsize">backchainFetchBatchSize</a> parameter that can be used to indicate the number of states (the default is 50) that should be fetched simultaneously during backchain resolution.</p><h3>Long transaction backchains</h3><p>An asset modeled as a Corda state may quite likely need to live through multiple transactions before it is no longer necessary. For eg. In a naïve implementation a tokenized cash unit once issued, could have a potentially infinite number of transfers before it is redeemed. A <a href="https://www.r3.com/blog/what-is-a-uxto/">UTXO</a> that is a result of a long sequence of transactions, would have a long transaction backchain. Long backchains are undesirable for two reasons as outlined by <a href="https://docs.corda.net/docs/corda-os/4.8/reissuing-states.html#overview">Corda’s docs</a>.</p><p><strong>Performance: </strong>Resolution along the chain slows down significantly. As the backchain grows, a node that was previously unaware of the transaction would have to sift through a large amount of transactions during verification, hence incurring much latency from the additional I/O and compute.</p><p><strong>Privacy: </strong>A node receiving a transaction gets the complete sequence of transactions that led to the current state of an asset. This can reveal information such as past owners of the asset, details of past exchanges/deals involving that asset etc. For example, consider the below pair of transactions. In the first transaction, E Corp buys a space ship part from R Corp for 8000 units of Star Coin. In the second transaction E Corp attempts to complete a deal for the same part with a different vendor. Here, X Corp could analyze the backchain of the Star Coin asset and find out that E Corp has previously purchased the same part for double the price offered in the current deal. This leak of past information could dampen E Corp’s chances of getting a good deal with X Corp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fSxNphsNs3pBqkle3pwh3w.png" /></figure><h4>Pruning long back-chains by asset re-issuance</h4><p>An issuance transaction requires no inputs — hence an asset’s backchain can be severed from its current state by having a trusted issuer re-issue an asset. In this regard, Corda has out-of-the-box <a href="https://docs.corda.net/docs/corda-os/4.8/reissuing-states.html">re-issuance flows</a> which can be used to re-issue states with guaranteed state replacement. The re-issuance mechanism makes use of <a href="https://docs.corda.net/docs/corda-os/4.8/tutorial-contract.html#defining-encumbrances">encumbrances</a> to ensure that the deletion of the old state(which has the backchain) and the issuance of the new state happens atomically.</p><h3>Improving performance and privacy</h3><p>While the Corda platform itself is designed to optimize transaction privacy and performance for most cases, a CorDapp’s designers need to carefully design its elements to meet the quality requirements of their use case. This section lists a number of features and future enhancements relevant to backchain verification, which could potentially be leveraged to improve transaction privacy and performance.</p><h4>Confidential identities</h4><p>Participants of a transaction can generate and exchange <a href="https://docs.corda.net/docs/corda-os/4.8/api-identity.html#confidential-identities">confidential identities</a> before constructing a transaction. Only the participants of that transaction would know the well known identity that owns a confidential identity. This means that resolution of the transaction backchain wont reveal which parties were involved in transactions that the verifying node wasn&#39;t a part of.</p><h4><strong>Using secure hardware to preserve backchain privacy</strong></h4><p><a href="https://en.wikipedia.org/wiki/Software_Guard_Extensions">Intel SGX</a> allows for creating private regions of memory called enclaves whose contents are protected and inaccessible to the host and any process outside the enclave. It is also possible to prove the integrity of the code running in the enclave to a remote client using a process called <a href="https://docs.conclave.net/enclaves.html#remote-attestation"><em>remote attestation</em></a>. A remote client can use a key agreement protocol to establish an encrypted communication channel with the enclave.</p><p>These capabilities could potentially be used by a sender to encrypt all transaction backchain data such that <em>only </em>a recipient’s enclave can decrypt it and use for verifying the backchain. Thus ensuring that ledger integrity is assured without leaking information from the transaction backchain to the recipient. Corda’s <a href="https://docs.corda.net/en/pdf/corda-technical-whitepaper.pdf">technical whitepaper</a> (p 61) and <a href="https://www.corda.net/blog/corda-and-sgx-a-privacy-update/">blog</a> has more details on this privacy enhancement.</p><h4><strong>Zero knowledge proofs</strong></h4><p>Zero knowledge proofs could be used in the future to prove the validity of a transaction to a counter-party without revealing the contents of the transaction. Most references to this in the Corda context mention that this privacy upgrade will happen once ZKPs are mature enough.</p><h4>Consensual states</h4><p>This is a relatively new pattern which was introduced in the last <a href="https://www.r3.com/cordacon/consensual-states-cordas-programming-model-is-evolving/">CordaCon </a>and is on the Corda 5 roadmap. It reasons that, in workflows where all of a Linear State’s participants are also signers, backchain verification, notarization and contract verification are not necessary. This programming model can potentially have significant performance and privacy benefits when applicable. This <a href="https://www.corda.net/blog/thoughts-on-a-powerful-addition-to-the-programming-model-for-corda-consensual-states/">blog</a> explains it in further detail.</p><h3>Wrapping up</h3><p>This post explained how StateRefs link current transaction inputs to older transaction output states thus forming a backchain which is resolved and verified along with the current transaction. It further discussed the effects of long transaction backchains on performance and privacy and lastly mentioned a number of features and future enhancements relevant to backchain verification that can be can be employed to improve transaction privacy and performance.</p><p>That&#39;s all for this post. Do leave your thoughts in the comments below and a 👏 if you found this useful. Stay tuned for more!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a11b2c405ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is crypto currency?]]></title>
            <link>https://clydedcruz.medium.com/is-crypto-currency-a4b80061476e?source=rss-e618c51f9377------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4b80061476e</guid>
            <category><![CDATA[stable-coin]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[money]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Clyde D'Cruz]]></dc:creator>
            <pubDate>Tue, 30 Mar 2021 19:48:55 GMT</pubDate>
            <atom:updated>2022-03-31T18:44:14.236Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eZez_f1nj0WX-VT_iAhHTg.jpeg" /><figcaption>A drawing of <em>Monetaria moneta</em> (synonym :<em>Cypraea moneta</em>) Linnaeus, 1758 — Niccolò Gualtieri, Public domain, via Wikimedia Commons</figcaption></figure><p>This article explores the question of when an asset can be called money, particularly in the context of cryptocurrencies. It further goes on to explain supply and demand and its role on the price and volatility of cryptocurrencies. To conclude it briefly touches upon a relatively new form of cryptocurrency that attempts to address the volatility problem.</p><p>Historically, money has taken various forms like <a href="https://www.nbbmuseum.be/en/2013/03/kakao.htm">cocoa beans</a>, <a href="https://en.wikipedia.org/wiki/Shell_money">cowrie shells</a>, metal coins, <a href="https://en.wikipedia.org/wiki/Card_money">playing cards</a> etc. With the Bitcoin experiment, the 21st century saw the emergence of a new form of money “cryptocurrency” that uses cryptography to control its creation and transactions, rather than a central authority. Bitcoin — the most popular cryptocurrency, provides various <a href="https://www.investopedia.com/ask/answers/100314/what-are-advantages-paying-bitcoin.asp">benefits</a> to its users. These include:</p><p><strong>User-autonomy:</strong> Users control how they spend their money without having to go through an intermediate authority like a bank or government</p><p><strong>Discretion: </strong>Since Bitcoin uses pseudonymous identities it is much harder to link a user’s transactions to their real world identity.</p><p><strong>Peer-to-Peer focus: </strong>Users are able to transact with any one in the world without having to get special approvals from any external authority or having to pay exchange fees.</p><p>Despite their numerous advantages, cryptocurrencies like Bitcoin and Ethereum still have trouble fitting in as “money”. By <a href="https://open.lib.umn.edu/principleseconomics/chapter/24-1-what-is-money/">definition</a>, an asset is considered as “money” if it can serve as a unit of account, a store of value and a medium of exchange.</p><h3>Elon Musk on Twitter: &quot;You can now buy a Tesla with Bitcoin / Twitter&quot;</h3><p>You can now buy a Tesla with Bitcoin</p><p>To be a medium of exchange people must be willing to accept the asset in exchange for goods and services. Elon Musk recently <a href="https://www.bbc.com/news/technology-56508568">announced </a>that Tesla now accepts Bitcoin as a mode of payment. This sort of news enhances Bitcoin’s credibility as a medium of exchange. A payment using Bitcoin is however costly in both time(needs about 60 minutes for a transaction to be considered irreversible) and money (transaction fees are <a href="https://ycharts.com/indicators/bitcoin_average_transaction_fee">12.84 USD/transaction</a> as of today). Its also difficult to use it as a medium of exchange if it’s value can quickly shift over time. For instance the Bitcoin price of a Tesla car could change day to day.</p><p>For a currency to serve as a unit of account, sellers should be able to reliably price their goods and services using that currency. Even though a lot of merchants accept Bitcoin as payment, hardly any price their products using its unit of measure i.e. BTC.</p><p>To be a store of value means that the asset holds value over time. Imagine you find an old forgotten ₹1000 note in the pages of a book. You would be delighted if it still has value. The stability of an asset’s value is also important. While Bitcoin’s value has grown dramatically over the years, it has also seen sudden <a href="https://www.cnbc.com/2017/11/30/bitcoin-plunges-20-percent-from-its-high.html">plunges</a>. This price volatility undermines Bitcoin’s ability as a store of value.</p><p>But what indeed causes the price of an asset to move up or down?</p><p><strong>Supply and demand</strong></p><p>Simply put, the <a href="https://www.investopedia.com/video/play/law-supply-and-demand/">law of supply and demand</a> states that when an item is scarce, but more people want it, its price will increase. Conversely if there is larger supply of an item than consumers warrant, its price will decrease. Supply and demand rise and fall until they achieve balance.</p><p>Supply and demand can be affected by numerous factors. Take for example the effects of the Covid-19 pandemic on the demand for hand sanitizer and Bitcoin, as represented in Randall Monroe’s 2280th issue of XKCD. The comic shows a conversation about Bitcoin and hand-sanitizer between Whitehat who lives in 2010 and Cueball who lives in 2020. White Hat is curious about life in 2020 and is particularly interested in how Bitcoin has caught on as a currency. But its apparent that Whitehat does not initially understand what Cueball was trying to convey because the former is oblivious to the supply and demand conditions of the future and their effect on the prices of these two commodities of interest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*nXJTD5EaQYwKsdDe0p6h8w.png" /><figcaption>2010 and 2020 - <a href="https://xkcd.com/2280/">XKCD #2280</a></figcaption></figure><p>At the time when the comic was released, COVID-19 had begun spreading around the world, causing scores of people to die and billions to panic. This increased the demand for hygiene products, including hand sanitizers, and therefore their price increased. It also triggered a panic on financial markets, including severe devaluation of the infamously volatile bitcoin.</p><p>Bitcoin, like gold has a limited supply. It’s supply increases at a fixed rate every 10 minutes until it reaches the limit of 21 million BTC. Because the total supply of Bitcoin is fixed by an algorithm, changes in the demand for it cause the price to fluctuate accordingly. This supply cannot expand or contract to compensate for the changes in demand. Could price of a crypto currency be stabilized if supply could be adjusted to match changes in demand?</p><p><strong>Stablecoins</strong></p><p>Stablecoins are cryptocurrencies that peg to a traditional currency (typically USD) or other assets. The price of a stablecoin is maintained relative to its peg by automatically allowing its users to expand and contract the money supply of the stablecoin. Stablecoins have gained traction in recent years as they attempt to offer the best of both worlds — the instant processing and security or privacy of payments of cryptocurrencies, and the volatility-free stable valuations of <a href="https://www.investopedia.com/terms/f/fiatmoney.asp">f</a>iat currencies. There are three types of stablecoins based on their stability mechanism:</p><p><strong>Fiat backed stablecoins: </strong>Stablecoins that are backed by reserves of a fiat currency such as the U.S Dollar. e.g. <a href="https://www.coinbase.com/usdc">USDC</a>, <a href="https://trueusd.com/">TrueUSD</a></p><p><strong>Crypto backed stablecoins: </strong>Stablecoins that are backed by a crypto currency or a basket of crypto currencies. E.g. MakerDao’s <a href="https://developer.makerdao.com/dai/1">Dai</a></p><p><strong>Algorithmic stablecoins: </strong>Stablecoins that do not hold any reserves but use a mechanism like that of central banks to regulate its value relative to the peg. E.g. <a href="https://frax.finance/">Frax</a>, <a href="https://www.ampleforth.org/">Ampleforth</a></p><p>In this post we had a look at what requirements an asset needs to meet to be considered as money, tried to understand the effects of supply and demand on cryptocurrency and scratched the surface of the stablecoins world. In future posts, I will dig deeper into stablecoins and explain how they work. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4b80061476e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Geth’s insecure unlock]]></title>
            <link>https://clydedcruz.medium.com/geths-insecure-unlock-c28b79dce923?source=rss-e618c51f9377------2</link>
            <guid isPermaLink="false">https://medium.com/p/c28b79dce923</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[ethereum-blockchain]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[geth]]></category>
            <dc:creator><![CDATA[Clyde D'Cruz]]></dc:creator>
            <pubDate>Fri, 09 Oct 2020 04:58:53 GMT</pubDate>
            <atom:updated>2021-01-22T04:56:08.635Z</atom:updated>
            <content:encoded><![CDATA[<p>The Ethereum ecosystem is rapidly changing. Being open source software, a lot of improvements can pour in every year. A side effect of this is that “tutorial” and “quickstart” blogs can possibly get outdated, and leave readers confused. I was one such reader. In this article I’m going to describe an interesting error I ran into while trying to unlock an account with geth and the subsequent learning that ensued.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pGpT2epd3Wo_-dkqfRW0tg.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@eye4dtail?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">George Becker</a> from <a href="https://www.pexels.com/photo/monochrome-photography-of-keys-792031/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><p>As per the tutorial I was following, I first started an Ethereum node using geth --ropsten --syncmode &quot;light&quot; --rpc --rpcaddr 127.0.0.1 . My intent was to start a “light” node on the <a href="https://docs.ethhub.io/using-ethereum/test-networks/">Ropsten testnet</a> and deploy a contract to it using <a href="https://www.trufflesuite.com/">Truffle</a>. I then ran geth attach \\.\pipe\geth.ipc to connect to the Geth console via the IPC end point and tried to unlock an account which I would use to deploy the contract. Here Geth errored out with this message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/1*1MXma-gK7WTz05UpeuiuiQ.png" /><figcaption><em>Error: account unlock with HTTP access is forbidden</em></figcaption></figure><p>At first this did not make complete sense because I was connected to the Ethereum node via IPC, not HTTP. <strong>So what is Geth complaining about?</strong></p><p>I took a step back and had a closer look at the geth command that I used for starting the node. geth --help (sounds like a pun :D) easily revealed explanations for each of the options I had used.</p><pre><strong>--rpc  </strong>          Enable the HTTP-RPC server (deprecated, use --http)<em><br></em><strong>--ropsten</strong>        Ropsten network: pre-configured proof-of-work test network<br><strong>--rpcaddr value</strong>  HTTP-RPC server listening interface (deprecated, use --http.addr) (default: &quot;localhost&quot;)</pre><p>As the help page says, the --rpc option enables the Ethereum node’s HTTP-RPC server. <strong>What is this HTTP-RPC server and what is it used for?</strong></p><p>This server is an HTTP endpoint that enables Javascript applications to talk to an Ethereum node using the <a href="http://www.jsonrpc.org/specification">JSON-RPC</a> protocol. We can use libraries like <a href="https://web3js.readthedocs.io/en/v1.3.0/">web3.js</a> and <a href="https://web3py.readthedocs.io/en/stable/">web3.py</a> to programatically interact with an Ethereum node over this protocol.</p><p>Back to our error — <strong>Why is account unlock forbidden when the HTTP server is enabled?</strong></p><p>If an Ethereum account is unlocked and the HTTP interface is exposed to the outside world (i.e. on the host’s public network interface) then it is possible for anyone to transfer funds from your account or sign a transaction using your account’s key. In 2018 there were <a href="https://www.zdnet.com/article/hackers-ramp-up-attacks-on-mining-rigs-before-ethereum-price-crashes-into-the-gutter">reports</a> of a massive campaign by attackers that scanned for internet-exposed Ethereum wallets and mining equipment. Also common, was to have <a href="https://github.com/ethereum/go-ethereum/issues/17013">accounts mysteriously emptied by bots</a> soon after they were unlocked.</p><p>To mitigate this potential vulnerability a <a href="https://github.com/ethereum/go-ethereum/pull/17037">change</a> was introduced in Geth to disallow unlocking accounts when the HTTP or websockets interface is enabled. This <a href="https://github.com/ethereum/go-ethereum/pull/17037#issuecomment-477950848">comment</a> in the pull request describes how Geth can be expected to behave at present. However it’s still possible to unlock accounts whilst exposing the HTTP or Websockets interfaces by explicitly providing the --allow-insecure-unlock option to geth. This must be used with great caution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*kNom7V4ZoejB7xMtyUdP8w.png" /></figure><p>Its worth noting that by default the geth node doesn’t start the HTTP and WebSocket servers. And even when they are explicitly started using the --http and --ws flags, not all functionality is provided over these interfaces for security reasons. These defaults can be overridden with the --rpcapi and --wsapi arguments, or with <a href="https://geth.ethereum.org/docs/rpc/ns-admin#admin_startrpc">admin.startRPC()</a> and <a href="https://geth.ethereum.org/docs/rpc/ns-admin#admin_startws">admin.startWS()</a>.</p><h3>Best practices when using accounts</h3><ul><li>Do not enable the RPC or WebSockets interfaces when doing operations on Geth that require an unlocked account. You can use the IPC endpoint for attaching to the interactive javascript console. For e.g. you can start a light node on the ropsten testnet using geth --ropsten --syncmode &quot;light&quot;. The node prints the IPC endoint address in the logs on startup. You can then attach to the IPC endpoint using geth attach path/to/ipc/geth.ipc</li><li>When using the CLI, it is recommended to do the account unlock interactively as opposed to specifying the password directly as part of the command. For e.g. The following command starts a node on the Ropsten testnet with the specified account unlocked. The HTTP-RPC and Websockets interfaces are not enabled.</li></ul><pre>geth --ropsten --syncmode &quot;light&quot; --unlock 7a67c39db201fbba9e9e0e...</pre><ul><li>Avoid entering your password in the Geth’s Javascript interactive console, and if you do, erase the $HOME/.ethereum/history file. If you execute personal.unlockAccount(eth.accounts[0], &quot;myethsecret&quot;, 5) you will be able to see your password printed in the <em>.history</em> file mentioned above. By default the directory in which your <em>.history </em>file is located is only readable by your own system user. But if your system user account is compromised, your password will be visible in your history file.</li><li>Consider using a wallet to interact with the Ethereum blockchain. For e.g. using <a href="https://metamask.io/">Metamask</a> you can securely manage your account, use your account to authenticate to websites and send transactions.</li></ul><h3>Conclusion</h3><p>In this post we tried to undertand why Geth disallows unlocking accounts when the HTTP or Websockets interface is enabled, potential risks in that scenario and best practices when working with accounts.</p><p>Thats all for now. Stay tuned for more! Please share your thoughts in the comments below and 👏for this post if you found it helpful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c28b79dce923" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A dive into SoftHSM]]></title>
            <link>https://clydedcruz.medium.com/a-dive-into-softhsm-e4be3e70c7bc?source=rss-e618c51f9377------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4be3e70c7bc</guid>
            <category><![CDATA[cryptography]]></category>
            <category><![CDATA[pkcs11]]></category>
            <category><![CDATA[hms]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[hardware-security-module]]></category>
            <dc:creator><![CDATA[Clyde D'Cruz]]></dc:creator>
            <pubDate>Mon, 27 Apr 2020 17:45:19 GMT</pubDate>
            <atom:updated>2020-04-29T04:08:41.525Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*H7jk3dckI43Lfn5DRbpzRw.png" /><figcaption><a href="https://en.wikipedia.org/wiki/Western_Electric">Western Electric</a> 229G cryptoprocessor. <a href="https://commons.wikimedia.org/wiki/File:WE_229G_die.JPG">Pauli Rautakorpi</a> / <a href="https://creativecommons.org/licenses/by/3.0">CC BY</a></figcaption></figure><p>A Hardware Security Module (HSM) is a physical device that attaches directly to a computer or server and is used to securely manage and perform operations on cryptographic keys. The purpose of these devices is, among others, to generate cryptographic keys and sign information without revealing private-key material to the outside world. These are hardened, intrusion-resistant devices that typically provide features like on-board key-generation, key storage and accelerated encryption. HSMs are commonly used in Public Key Cryptography (PKI) deployments to secure Certificate Authority keys.</p><p>This blog is about SoftHSM and its usage in Java. SoftHSM isn’t exactly an HSM per se, but a software implementation of a generic PKCS#11 device. This is being developed as part of <a href="https://www.opendnssec.org/">OpenDNSSEC</a> and allows you to explore PKCS#11 without having a real HSM.</p><p>To understand how SoftHSM is used in Java we need to get familiar with the layers involved in interacting with an HSM and related concepts.</p><h4>The PKCS#11 standard</h4><p>PKCS#11 specifies an Application Programming Interface(API) called <em>Cryptoki</em> for devices that hold cryptographic information and perform cryptographic functions. These devices are known as <em>Cryptographic Tokens</em>. PKCS#11 presents a common logical view of tokens to applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*E8fHWoNLQsENzhTNFZuRBg.png" /><figcaption>Layers used for interacting with an HSM from Java code</figcaption></figure><h4>Cryptography interfaces in Java</h4><p>Java defines a set of programming interfaces for performing cryptographic operations. These are collectively known as Java Cryptography Architecture (JCA) and Java Cryptography Extension (JCE). These interfaces are <em>provider-based</em>. This means that when performing a cryptographic operation in our application, the application talks to the interface, but the actual operation is performed in the configured provider which implements that interface. This architecture supports different types of provider implementations. Some may perform the operations in software; others, on a hardware token.</p><h4>The Sun PKCS#11 provider</h4><p>The <a href="https://docs.oracle.com/en/java/javase/11/security/pkcs11-reference-guide1.html#GUID-6DA72F34-6C6A-4F7D-ADBA-5811576A9331">Sun PKCS#11</a> provider does not implement cryptographic algorithms by itself, but acts as a bridge between the JCA, JCE APIs and the native PKCS#11 module. This native module must be in the form of a shared-object library (.so file on Solaris and Linux) or dynamic-link library (.dll on Windows) and is provided by the vendor of the HSM device.</p><h3>Using SoftHSM</h3><h4>Setup</h4><p>SoftHSM can be installed using already available binary packages, or from <a href="https://wiki.opendnssec.org/display/SoftHSMDOCS/SoftHSM+Documentation+v2">source</a>. For example, on a recent version of Ubuntu, a simple sudo apt-get install softhsm will suffice. As part of the installation, the necessary command line tools will be installed along with the shared object file libsofthsm2.so which is the native implementation of PKCS#11 for SoftHSM.</p><h4>Create a cryptograpic token</h4><p>Use the softhsm2-util command line utility to create a new token as follows</p><pre><em>softhsm2-util --init-token --slot 0 --label &quot;&lt;token name&gt;&quot;</em></pre><p>This prompts you for a <em>Security Officer PIN </em>(used to add/ remove tokens) and a <em>User PIN</em>(used for accessing the token from the application).</p><h4>Create a PKCS#11 configuration file</h4><p>The PKCS#11 configuration file must contain the location of the <em>.so</em> file and the slot number of the token.</p><pre><em>name = softhsm_pkcs11<br>library = /usr/lib64/libsofthsm2.so<br>slot = 0</em></pre><h4>Interacting with the token in Java</h4><p><strong>Configure JCA provider</strong></p><p>In this snippet, I’ve programmatically configured the JCA provider at runtime. You can also statically configure it in the Java security properties file $JAVA_HOME/lib/security/java.security</p><pre>Provider sunPkcsProvider = new SunPKCS11(pkcs11ConfigFilePath);<br>if (-1 == Security.addProvider(sunPkcsProvider)) {<br>    throw new RuntimeException(&quot;Could not configure SunPKCS11 provider&quot;);<br>}</pre><p><strong>Load keystore</strong></p><p>Operations that deal with keys in the token, require you to first login using your User PIN before the operations can proceed. You can supply the PIN in the password input parameter to the load method while accessing the token as a keystore via the java.security.KeyStore class.</p><pre>String userPinStr = &quot;&lt;user PIN specified while creating token&gt;&quot;<br>char[] userPin = userPinStr.toCharArray();<br>KeyStore keyStore = KeyStore.getInstance(&quot;PKCS11&quot;, sunPkcsProvider);<br>keyStore.load(null, userPin);</pre><p><strong>Save Private key to keystore</strong></p><p>Here I’m saving an RSA 2048 Private key to the keystore</p><pre>keyStore.setKeyEntry(<br>        <em>keyAlias</em>,               // Alias for this key <br>        privateKey,             // of type java.security.PrivateKey<br>        ksPasswd.toCharArray(), // keystore password to protect key<br>        caCertChain);           // the cert chain for its public key<br>keyStore.store(null);</pre><p><strong>Operations using stored key</strong></p><p>We can now use the stored key to perform cryptographic operations. In this case, I’m using the key to decrypt content that was encrypted using its corresponding public key.</p><pre>PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, ksPasswd.toCharArray());<br>Cipher decryptCipher = null;<br>decryptCipher = Cipher.getInstance(&quot;RSA&quot;);</pre><pre>decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);<br>byte[] encryptedBytes = decryptCipher.doFinal(dataBytes);<br>String plaintextString = new  String(encryptedBytes);</pre><p><strong>What if we print this private key on the console?</strong></p><p>The HSM will let you perform crypto operations using the stored private key, but won’t let you access the content of the key itself. Here is what we get if we try to print the key to the console via the Java code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*RdzVgWOzhK44EJrmNY1SLw.png" /><figcaption>Private key content when printed to console</figcaption></figure><h3>Concluding notes</h3><p>Being a software implementation of a generic HSM, SoftHSM allows us to explore the cryptoki standard and understand its usage even if we don’t have a real HSM. In Java application code, we can leverage JCA APIs to interact with the HSM via the Sun PKCS#11 provider, which acts as a bridge between JCA APIs and platform specific native PKCS#11 module.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4be3e70c7bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Batching database writes in Spring]]></title>
            <link>https://clydedcruz.medium.com/batching-database-writes-in-spring-479bee626fbf?source=rss-e618c51f9377------2</link>
            <guid isPermaLink="false">https://medium.com/p/479bee626fbf</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[batching]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[jpa]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Clyde D'Cruz]]></dc:creator>
            <pubDate>Tue, 24 Mar 2020 11:40:21 GMT</pubDate>
            <atom:updated>2020-03-25T06:47:26.929Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ACpL-6CxFdNfusrJCH7Kuw.png" /></figure><p>Not long ago I was working on improving the performance of a <a href="https://spring.io/projects/spring-boot">Spring Boot</a> application. A Spring Boot app that uses <a href="https://spring.io/projects/spring-data-jpa">Spring Data JPA</a>. Since we had a whole bunch of database writes happening it was necessary that we batch these database writes in order to reduce the communication overhead to the database server and thereby improve performance.</p><p>In this post I’m going to describe what I learnt and use an example to show how batching works under the hood when using Spring Data JPA.</p><p><strong>Approaches to data access in Spring</strong></p><p>There are multiple approaches to implementing a JDBC based persistence layer for a Spring application. <a href="https://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/jdbc.html">JDBC template</a> is the classic Spring JDBC approach and the most popular. This is the most “low level” approach and offers maximum flexibility. Spring Data JPA on the other hand provides an abstraction over the data access layer using <a href="https://projects.eclipse.org/projects/ee4j.jpa">JPA(Jakarta Persistence API)</a> and an underlying ORM (e.g. <a href="https://hibernate.org/orm/">Hibernate</a>). Though not as flexible as the former approach, it does reduce programmer effort and the amount of boilerplate needed to implement a data access layer. These are advantages that help when building a rapid prototype where the data model is likely to change often.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*qd1Mcg-8FDOJZ9WwkGq52w.png" /><figcaption><strong>Spring JDBC Template v/s Spring Data JPA:</strong> Comparison of work involved for implementing a persistence layer</figcaption></figure><p>In the rest of this article we will use an example to undertand how batching can be implemented when using Spring Data JPA and also see how it behaves in different scenarios. We’re going to be using <a href="https://kotlinlang.org/">Kotlin </a>for this example.</p><h4><strong>Create entity and repository</strong></h4><p>Define a simple <strong><em>Employee </em></strong>class for storing Employee objects and annotate it as a JPA entity. We are using our own identifier here as opposed to a generated one. This decision is of some significance for our batching experiment which will be apparent later</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29568cae367e911dca1d440339c17ac0/href">https://medium.com/media/29568cae367e911dca1d440339c17ac0/href</a></iframe><p>Next, create a simple repository for this entity by extending Spring JPA’s <a href="https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html">CrudRepository</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cc90c9b98b079c5bbd873dd6f7ae94f/href">https://medium.com/media/3cc90c9b98b079c5bbd873dd6f7ae94f/href</a></iframe><p>We can now autowire this repository class in our test to verify that the save() works as expected. I used the embedded <a href="https://www.h2database.com/html/main.html">H2 </a>database to be able to test this quickly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f26df7229e73c16a5a4a92c1f12f004/href">https://medium.com/media/4f26df7229e73c16a5a4a92c1f12f004/href</a></iframe><h4><strong>Introduce Batching</strong></h4><p>To perform multiple inserts in a single call to the database, the</p><p>CrudRepository provides the saveAll() function. A little digging through the docs will reveal that we also need to set a batch size for this to work.</p><pre>spring.jpa.properties.hibernate.jdbc.batch_size = 10</pre><p>To check if batching works, add an additional test case that inserts and updates a list of Employees in the database. When we run this, it seems to work without issues. But we still don’t have any indication of what it really does under the hood.</p><h4><strong>Improve logging</strong></h4><p>Add the following properties to your spring application. This will show some verbose information to help us determine if batching is working as expected.</p><pre>spring.jpa.show-sql = true<br>spring.jpa.properties.hibernate.generate_statistics = true</pre><p>We’re also going to use a <a href="http://ttddyy.github.io/datasource-proxy/docs/current/user-guide/">data-source-proxy</a> to print detailed logs of every query that gets executed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df32459934f979eed4e139edc0960f52/href">https://medium.com/media/df32459934f979eed4e139edc0960f52/href</a></iframe><h4><strong>Behavior of batch insert operations</strong></h4><p>Two inferences can be made from the detailed logs</p><ol><li>The inserts got batched</li><li>For every INSERT statement, there is also an additional SELECT <em>statement </em>being executed</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e82b7b27a22b657438b52dcc4ba215a2/href">https://medium.com/media/e82b7b27a22b657438b52dcc4ba215a2/href</a></iframe><p>I found clues to this behavior in Spring’s default <a href="https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.entity-persistence.state-detection-strategies">entity state detection strategies</a>.</p><blockquote>By default, Spring Data JDBC inspects the identifier property of the given entity. If the identifier property is null, then the entity is assumed to be new. Otherwise, it is assumed to not be new.</blockquote><p>In our code since our <strong><em>id </em></strong>field is set to non-null value before persistence, Spring concludes that we are trying to persist an existing object! And hence tries to read the existing value using a SELECT. To work with our manually assigned identifier we will have to go with the second listed approach. i.e. Using <a href="https://docs.spring.io/spring-data/data-commons/docs/current/api/org/springframework/data/domain/Persistable.html">Persistable</a></p><p>A <a href="https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.entity-persistence.saving-entites.strategies">common pattern</a> for working with manually assigned identifiers is to</p><ol><li>Use a common base class which has a transient flag defaulting to indicate a new instance.</li><li>Use JPA lifecycle callbacks <em>“@PrePersist”</em>and <em>“@PostLoad”</em> to flip that flag on persistence operations. The flag must get flipped to indicate an existing entity after a repository call to<strong> </strong>save()</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a49b36b9e5f482b285c916fd6aa6ad28/href">https://medium.com/media/a49b36b9e5f482b285c916fd6aa6ad28/href</a></iframe><p>With the updated entity class we no longer see the unnecessary SELECT queries.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39ed7a193c515a78e0b38f1c587232b0/href">https://medium.com/media/39ed7a193c515a78e0b38f1c587232b0/href</a></iframe><h4><strong>Batching UPDATE queries</strong></h4><p>Update queries do get batched, but like before we can see an addtional SELECT executed for each update query.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/202964cf12a539ff2e10a85044f93fd2/href">https://medium.com/media/202964cf12a539ff2e10a85044f93fd2/href</a></iframe><p><strong>Behaviour of Spring Data JPA batching when using custom HQL queries</strong></p><p>Add a custom query that will let us update the Employee entity WITHOUT the select that gets included when we use saveAll()<strong><em>. </em></strong>However this executes as individual statements and not as a batch.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9bff7b45da695fe8bcc5d49afdfb9252/href">https://medium.com/media/9bff7b45da695fe8bcc5d49afdfb9252/href</a></iframe><h3>Conclusion</h3><p>Spring Data JPA makes it simple to add JDBC to a Spring application and reduces boiler plate for implementing commonly used JDBC features. But the higher level of abstraction can make it less flexible compared to JDBC Template when impementing un-common scenarios. For use cases where performance is an important requirement, its necessary to understand how it works under the hood.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=479bee626fbf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>