Implementation of Junit Test Cases for Bitcoin

Posted By : Himanshu Kumar | 20-Aug-2019

Overview:- 

Bitcoin and blockchain are hotly debated issues today. Many related tasks are as of now out there and substantially more are being grown at present. On the off chance that you are an engineer here, at that point you know how significant and precarious it is to have impenetrable testing set up. In this article, I will quickly portray the choices for testing Bitcoin and after that expound on the decision that you can run effectively and disconnected. It would be ideal if you note that this article is concentrating simply on Bitcoin.

 

Testing Options:- 

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

 

  • Regtest mode
  • Test on the testnet
  • Test on the mainnet

 

Regtest mode is the thing that I would suggest beginning with. It enables you to run the hub on your nearby PC similarly as a sandbox, totally detached from the web. What's more, it has one helpful component. You are completely responsible for the square age, and new squares can be 'mined' just by calling a basic direction. This expels the need to trust that squares will be mined. Likewise, you have a boundless number of coins to play with because all the mining grants go to the record on your neighborhood hub. 

 

Testnet, as the name proposes, is a spot that carries on nearly equivalent to the genuine Bitcoin organize. It is a completely utilitarian system. This incorporates genuine mining, essential holding uptime, and a need to represent the action of other individuals. The distinctions are that coins don't have any worth, anybody can get a few free test coins, and the entire system gets nuked now and again. It's a decent following stage after having a working framework on the regtest. 

 

At long last, mainnet is where the genuine exchanges are going on. Bugs can turn out to be over the top expensive here, so it is smarter to leave this for the last test, in the wake of being certain that everything is filling in as arranged. 

 

I accept that during application advancement, you should stroll through each of the three phases. The remainder of this article will tell the best way to interface regtest mode with JUnit. To begin with allows simply get somewhat acquainted with bitcoind, a program that executes the Bitcoin convention and can go about as a hub. 

 

Begin with programming establishment. I lean toward Bitcoin Unlimited. Download the most recent rendition of the Official Bitcoin (BTC) Release for your stage and unfasten/introduce it. Open the support and explore to the organizer with your doubles. A nearby hub in the regtest mode can be begun with the accompanying order.

 

bitcoind -regtest -txindex=1 -server -rpcallowip=127.0.0.1 -rpcport=18332 -rpcuser=user -rpcpassword=Password1

 

There are numerous parameters you can put into the direction, and there is a reference manage here. The significant ones are those that indicate regtest mode, information indexes, and open JSON-RPC ports for customers. 

 

The subsequent stage is to open a subsequent comfort, at that point explore to a similar index, and use JSON-RPC customers to perform activities. The accompanying grouping creates squares, sends coins to the predetermined location, and produces the next arrangement of squares to reproduce exchange advance in the chain.

 

 

bitcoin-cli -regtest -rpcport=18332 -rpcuser=user -rpcpassword=Password1 generate 101

 

 

bitcoin-cli -regtest -rpcport=18332 -rpcuser=user -rpcpassword=Password1 getbalance

 

 

bitcoin-cli -regtest -rpcport=18332 -rpcuser=user -rpcpassword=Password1 sendtoaddress put_your_address_here 10

 

 

bitcoin-cli -regtest -rpcport=18332 -rpcuser=user -rpcpassword=Password1 generate 7

 

On the off chance that you have made it up to here, at that point you ought to have the option to test your application and restart your chain from the earliest starting point (by erasing the information registry) at whatever point you need. For the occasion, however, that is altogether done physically. The following part will tell you the best way to robotize this. 

A model venture is accessible on the first post. It's a Java Maven venture that contains a solitary unit test and that can be conjured from the order line just by mvn test. Bitcoin pairs for Windows are incorporated. If you are utilizing a distinctive stage, at that point please download and supplant the pairs. 

Coding is truly clear and can be abridged in the accompanying focuses:-

 

  • Tidy up the information index and begin the bitcoind procedure inside the test arrangement.
  • The tried customer interfaces with the hub during the experiment as required.
  • New squares are created on interest (there is a technique for that).
  • The bitcoin procedure is quit during the test teardown.

 

Note:- Contingent upon your condition, you may need to manage two issues — consents and firewall. 

Here is how you begin the new bitcoin process.

 

 

 

ProcessBuilder processBuilder = new ProcessBuilder(BIN_DIR_PATH + "/bitcoind.exe", "-regtest",
        "-datadir=" + dataDir.getAbsolutePath(), "-txindex=1", "-server",
        "-rpcallowip=127.0.0.1", "-rpcport=18332", "-rpcuser=user", "-rpcpassword=Password1");
processBuilder.directory(new File("src/test/resources/bitcoinUnlimited-1.0.3/bin"));
try {
    bcProcess = processBuilder.start();
    Thread.sleep(5000);
} catch (IOException e) {
    throw new RuntimeException("error during process start", e);
} catch (InterruptedException e) {
    throw new RuntimeException(e);
}

 

The variable bcProcess is characterized in the test class and is utilized in a teardown strategy to close the procedure. The 5-second string rest is trickier. processBuilder.start() returns promptly when the procedure is begun. Tragically, the bitcoin procedure isn't introduced by then, so the association would fall flat.

 

Next, how to stop the procedure.

 

 

 try {
            bcProcess.getInputStream().close();
            bcProcess.getErrorStream().close();
            bcProcess.getOutputStream().close();
            bcProcess.destroy();
            bcProcess = null;
            Thread.sleep(1000);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

 

That is the entire "enchantment". The remainder of the code just tidies up the working registry before the test and summons the genuine test.

 

Summary:- 

As should be obvious, the Java code to run and connect with Bitcoin hubs is simple and it works. You can compose experiments that run generally quick and have an ensured situation. The bit that I don't generally like is the reliance on the local program — particularly while envisioning taking care of various chains on the numerous frameworks. At that point, the number of vital parallels can develop fundamentally.

 

 

 

 

About Author

Author Image
Himanshu Kumar

Himanshu Kumar is a seasoned Backend Developer with extensive experience in the industry. He has a deep understanding and hands-on expertise in a range of technologies, including Core Java, Spring-Boot, Hibernate, Apache Kafka messaging queue, and blockchain development with Ethereum, Tron, and smart contract development. He is also well-versed in relational databases like MySQL and PostgreSQL. His proficiency in API implementation, web services, development testing, and deployment has contributed to the successful delivery of various client projects, such as Wethio Exchange, Hedgex Exchange, Envoychain payment gateway based on Stellar assets, and more. With a creative mind and excellent analytical skills, Himanshu enjoys staying up to date with the latest technologies and exploring new ideas to bring value to his work.

Request for Proposal

Name is required

Comment is required

Sending message..