Translate

Sunday, January 31, 2021

Bitcoin and JUnit

 Source: https://dzone.com/articles/bitcoin-and-junit

Bitcoin and JUnit

Here's a solid guide to help you test your Bitcoin-centric projects with JUnit. See what it takes to set up and use a proper test environment.

· Java Zone · Tutorial

Bitcoin and blockchain are hot topics today. Many related projects are already out there and much more are being developed right now. If you are a developer in this area, then you know how important and tricky it is to have bulletproof testing in place. In this article, I will briefly describe the options for testing Bitcoin and then go into more detail of the choice that you can run easily and offline. Please note that this article is focusing purely on Bitcoin.

Testing Options

There are three possible modes or stages for testing Bitcoin applications.

  1. Regtest mode
  2. Test on the testnet
  3. Test on the mainnet

Regtest mode is what I would recommend starting with. It allows you to run the node on your local computer just as a sandbox, completely isolated from the internet. And it has one very useful feature. You are fully in control of the block generation, and new blocks can be 'mined' just by calling a simple command. This removes the need to wait for blocks to be mined. Also, you have an unlimited number of coins to play with because all the mining awards go to the account on your local node.

Testnet, as the name suggests, is a place that behaves almost the same as the real Bitcoin network. It is a fully functional network. This includes real mining, necessary waiting time, and a need to account for the activity of other people. The differences are that coins don't have any value, anyone can get a small number of free test coins, and the whole network gets nuked from time to time. It's a good next step after having a working system on the regtest.

Finally, mainnet is the network where the real transactions are happening. Bugs can become very expensive here, so it is better to leave this for the final test, after being confident that everything is working as planned.

I believe that during application development, you should walk through all three stages. The rest of this article will show how to connect regtest mode with JUnit. First, let's just get a little bit familiar with bitcoind, a program that implements the Bitcoin protocol and can act as a node.

Start with software installation. I prefer Bitcoin Unlimited. Download the latest version of the Official Bitcoin (BTC) Release for your platform and unzip/install it. Open the console and navigate to the folder with your binaries. A local node in the regtest mode can be started with the following command.


There are many parameters you can put into the command, and there is a reference guide here. The important ones are those that specify regtest mode, data directories, and open JSON-RPC ports for clients.

The next step is to open a second console, then navigate to the same directory, and use JSON-RPC client to perform actions. The following sequence generates blocks, sends coins to the specified address, and generates next set of blocks to simulate transaction progress in the chain.





If you have made it up to here, then you should be able to test your application and restart your chain from the beginning (by deleting the data directory) whenever you want. For the moment, though, that's all done manually. The next chapter will show you how to automate this.

An example project is available on the original post. It's a Java Maven project that contains a single unit test and that can be invoked from the command line just by mvn test. Bitcoin binaries for Windows are included. If you are using different platform, then please download and replace the binaries.

Coding is pretty straightforward and can be summarized in the following points:

  • Clean up the data directory and start the bitcoind process inside the test setup.
  • The tested client connects to the node during the test case as needed.
  • New blocks are generated on demand (there is a method for that).
  • The bitcoind process is stopped during the test teardown.

Note: Depending on your environment, you might need to deal with two issues — permissions and firewall.

Here is how you start the new bitcoind process.


The variable bcProcess is defined in the test class and is used in a teardown method to close the process. The 5-second thread sleep is trickier. processBuilder.start() returns immediately when the process is started. Unfortunately, the bitcoind process is not initialized at that point, so the connection would fail.

Next, how to stop the process.


That's the whole "magic". The rest of the code just cleans up the working directory before the test and invokes the actual test. For more details, please look at the source code.

Summary

As you can see, the Java code to run and interact with Bitcoin nodes is easy and it works. You can write test cases that run relatively fast and have a guaranteed environment. The portion that I don't really like is the dependency on the native program — especially while imagining handling multiple chains on the multiple systems. Then the number of necessary binaries can grow significantly. I have a couple of ways to resolve that in mind, but please let me know if you have an elegant solution for that.

Saturday, January 30, 2021

Blockchain: Why It’s So Much More Than Bitcoin

 Source https://dzone.com/articles/blockchain-why-its-so-much-more-than-bitcoin

 

Blockchain: Why It’s So Much More Than Bitcoin

The great promise of blockchain extends far beyond its role as a platform for the successful operation of Bitcoin and other cryptocurrencies. And yet, for some applications under consideration, technological challenges including performance and scalability will need to be addressed.

· Cloud Zone · News

There’s a new kid on the block of disruptive technology. Blockchain. A day doesn’t go by that you can’t find numerous articles about it. Topics range from how JP Morgan is making significant investments in exploring the technology, to the Republican GOP using blockchain to allow online voting in the recent Utah caucus.

It’s definitely a hot topic of discussion. At times, it sounds like blockchain is replacing Cloud in discussions as the technology panacea that will solve all problems. On the flip side, one of the first statements I invariably hear when blockchain enters the conversation with clients is, “We’re not interested in Bitcoin.” For many, the terms blockchain and Bitcoin are synonymous.

Blockchain ≠ Bitcoin!

In a recent weekend tweetchat (#CIOChat hosted by Myles Suer, a business strategist at Informatica), blockchain was the topic of discussion. One of the threads in the chat centered on the challenge caused by the blockchain/Bitcoin association. Ryan Fay, CIO at ACI Specialty Benefits said it best, “I can’t wait to start a conversation about blockchain and not spend an hour talking about Bitcoin.” This reflected the viewpoint of many in the chat.

The connection is understandable. If you do a Google search on blockchain, the top results inevitably pair the terms “blockchain” and “Bitcoin.” Blockchain technology originated in the establishment of Bitcoin. It enables digital currencies like Bitcoin to work.

A colleague of mine, Zach Slayton (VP of Digital Solutions at Collaborative Consulting) gave the analogy of fish and water. A fish (Bitcoin) needs water (blockchain) to survive. But water (blockchain) does not need the fish (Bitcoin). So while Bitcoin needs blockchain to work, blockchain doesn’t need Bitcoin to provide value.

(I’m not going to attempt to discuss the pros and cons of Bitcoin here. Those conversations can be almost as emotional as political discussions—and voluminous enough to fill books.)

What Exactly is Blockchain?

In simple terms, blockchain is a digital ledger. You can think of it as a spreadsheet. The blockchain ledger comprises a constantly growing list of transactions called “blocks”—all of which are sequentially connected. Each block has a link to the previous one in the list.

Once a block is in the chain it can’t be removed, so it becomes part of a permanent database containing all the transactions that have occurred since its inception.

One of the more interesting features about blockchain technology is that there’s no central authority or single source of the ledger. Which means it exists on every node that’s associated with it. Yes, every node has its own complete copy of the blockchain. As new blocks are added, they’re also received by every node that participates. Distributed consensus and trust ensures the integrity of the system.

Instead of Bitcoin, Think in Terms of Assets

If you look at blockchain through the lens of Bitcoin (or for that matter, cryptocurrency in general), it can provide a very limited view of blockchain’s business value and usefulness. If instead, we think of it in relation to assets of all kinds, we see a landscape of nearly boundless potential.

In her book, “Blockchain: Blueprint for a New Economy,” author Melanie Swan describes three categories of blockchain:

  • Blockchain 1.0 – Currencies. This includes currency transfers, remittances, and digital payment systems. This is the area most of us are familiar with as it is the territory of Bitcoin and other cryptocurrencies.
  • Blockchain 2.0 – Contracts. This extends blockchain into financial and marketplace applications. Assets include items such as bonds, stocks, loans, titles, and anything that has an implied agreement or contract.
  • Blockchain 3.0 – Organizing Activity. This takes blockchain out of the financial space and into areas such as education, government, health, and art. In these areas, asset types may be physical, digital, or human in nature.

In the second two categories, blockchain has far more potential than Bitcoin. And these applications are starting to garner significant interest throughout various industries.

Blockchain 2.0 – Contracts

The potential benefits (and disruption) presented by blockchain in the financial services industry has not gone unnoticed. As I mentioned at the beginning, JP Morgan is making significant investments in exploring blockchain. And many in the financial services industry are starting to take a serious look at blockchain technology and its potential impact.

  • Oliver Bussmann, CIO of UBS says that blockchain technology could “pare transaction processing time from days to minutes.”
  • Visa, Citi, and Nasdaq have invested $30M in the blockchain startup chain.com.
  • DTCC (Depository Trust & Clearhouse Corp.), recently held a symposium on blockchain titled, “Embracing the Disruption.” (DTCC acts as the trusted third-party clearing house for settling a majority of the securities transactions in the United States. Adoption of blockchain in the securities arena could cause a huge disruptive impact to their business model.)
  • In a recent article in Fortune Magazine, Christopher Giancarlo, a commissioner of the Commodity Futures Trading Commission expressed the view that blockchain could have helped save Lehman Bros. He said, “If an accurate [blockchain] record of all of Lehman’s transactions had been available in 2008, then Lehman’s prudential regulators could have used data-mining tools, smart contracts, and other analytical applications to recognize anomalies. Regulators could have reacted sooner to Lehman’s deteriorating credit-worthiness.”

It is worth noting that Bitcoin has no place in any of those discussions. This is purely about leveraging the technological benefits blockchain may provide to solve real-world business issues in the financial services sector.

Blockchain 3.0 – Organized Activity

Outside of the financial arena , blockchain could be used in some very out-of-the-box scenarios that one might not consider at first. As noted earlier, the use of blockchain to enable online voting during Utah’s recent GOP primary is just one example.

This application of the technology clearly demonstrates the concept of distributed ownership. When I create my “vote asset” and it’s placed on the chain, I don’t own the chain but I do own my asset, which is my vote—and also a block in the chain. So every voter owns a block in the chain. These blocks become permanent records of each individual’s assets and are immutably validated by consensus on the chain.

With that concept in mind, it’s exciting to think about blockchain’s potential in sectors and industries like government, education, and healthcare—just to name a few.

  • In Estonia, the government is using blockchain to secure over 1 million electronic health records (EHR). It’s important to remember that patients own their individual EHR assets, the security and integrity of which are maintained within the blockchain. So rather than calling and relying on providers for record transferals, patients can transfer records themselves through the blockchain.
  • The United Kingdom’s chief scientific advisor recently published a report entitled “Distributed Ledger Technology—Beyond Blockchain.” In it, he states that the technology “can revolutionize services, both in government and the private sector.”
  • During the weekend #CIOChat tweetchat I mentioned earlier, the topic of blockchain’s potentially disruptive impact on education produced a lengthy chat that continued well beyond the weekend. Digital strategy advisor Stephen diFilipo, (Digital Strategy Advisor) Peter Salvitti (CTO, Boston College), and Joanna Young (CIO, Michigan State University) all raised points of disruption and impact including:
    • Proof of learning and achievements
      • Credentials validation
      • Transcript verification
    • The concept of micro-learning and micro-credentials
  • And in the world of the Internet of Things (IoT), IBM and Samsung jointly developed a proof of concept demonstrating a decentralized IoT telemetry system powered by blockchain technology.

We Have Miles to Go and Promises to Keep

The great promise of blockchain extends far beyond its role as a platform for the successful operation of Bitcoin and other cryptocurrencies. And yet, for some applications under consideration, technological challenges including performance and scalability will need to be addressed.

As with any technology, blockchain is a tool, not a destination. Ultimately, as technologists, our job is to help the business achieve its goals—and reach its destination—by leveraging tools that provide business value.

I count having blockchain in our toolbox as a net positive. Because as I’ve illustrated above, it’s so much more than Bitcoin. And I’m confident that as we progress forward, that association will slowly fade away.