Build your own blockchain ter R

Build your own blockchain in R

Everybody is freaking out about the rise of the Bitcoin and the potential of the Blockchain technologies. The advent of cryptocurrencies, spel switching use cases, disruption of established business models by disintermediation, etc..

By the time I’m writing this article, there are more than 1300 crypto-currencies listed te coinmarketcap.. And a loterijlot more coming with the next ICOs (Internet Coin Suggesting).

Most certainly, the main enabler of Bitcoin and of many other currencies (albeit not all of them) is the Blockchain technology.

Albeit the original paper from Satoshi explains very well the ground idea behind this technology, nothing like creating your own blockchain to fully understand how it works, its limitations and potential improvements (aka “If you can code it, you certainly understand it”).

Ter this postbode I’d like to share a gentle coding exercise te R (#rstats). Why R? Just because it’s my dearest language… I wouldn’t choose R for a productive, full-fledge block chain implementation, but again, this is not the purpose of this postbode. This is just a learning exercise hacked fairly quickly without any aspiration of everzwijn running this code ter a productive environment, and should be understood spil such.

For convenience, I stored the code te a git repository, for others to improve, re-use, attempt, etc.

Very first things very first: what is what ter a Blockchain

A blockchain is an immutable chain of sequential records or blocks that are “chained” together using hashes. Blocks can be understood spil containers, typically of transactions, but it can be extended to documents, etc. Wij can think of a blockchain spil a database where fresh gegevens is stored ter blocks, that are added to an immutable chain based on all other existing blocks.

Blockchain is often referred spil a digital ledger of transactions performed te a cryptocurrency of choice (Bitcoin or whatever). A transaction requires a sender address, a recipient address and a given amount and needs to be assigned to a block.

The json below shows a typical block with a particular index, an oprecht indicating the proof, a hashed representation of the previous block, which permits for consistency check (more about proof and hashing zometeen) and a timestamp.

Once wij have understood the concept behind the blockchain, let’s build one and make it work

Wij are going to need three different files:

  • The class definition verkeersopstopping, where wij create the Blockchain class with its components and methods.
  • The API definition opstopping, where wij instantiate the class, register a knot and expose the blockchain methods spil GET and Postbode calls for the peers to interact with.
  • The plumber launch verkeersopstopping, to commence the server and expose the API methods

Building a Blockchain

To represent the Blockchain, wij need a list of blocks (the chain), a list of currentTransactions (waiting to be mined) and a list of mining knots.

Our Blockchain class implements a method to register a fresh transaction addTransaction. Transactions are appended to the list of currentTransactions until a fresh block is created, which takes care of the freshly added transactions that have not bot added to any block yet.

The creation of a fresh block takes place after calling nextBlock. To maintain the consistency, a fresh block can only be added knowing the hashed value of the previous one spil part of a Proof of Work proces (PoW). Basically, it is just finding a number that pleases a condition. This number should be effortless enough to be verified by anyone te the chain, but difficult enough so that a brute force attack to find it wouldn’t be feasible (too long, too expensive).

Ter our example, proofOfWork relies on finding a number called proof such that if wij append this proof to the previous proof and wij hash it, the last Two characters of the resulting string are exactly two zeroes. The way it works ter the real Bitcoin chain is fairly different, but based on the same concept.

Te Bitcoin, the Proof of Work algorithm is called Hashcash. Solving this problem requires computational resources (the longer the strings and the more characters to be found within it, the higher the complexity), and miners are rewarded by receiving a coin—in a transaction.

The Blockchain class provides a method to check the consistency overheen all blocks validChain, which iterates overheen the chain checking if both each block is decently linked to the previous one and whether the PoW is preserved.

The method registerNode adds the URL of a mining knot to the central knots register.

Ultimately, spil wij need to think of a Blockchain spil a distributed system, there is a chance that one knot has a different chain to another knot. The handleConflicts resolves this conflict by proclaiming the longest chain the zindelijk one… the one that all knots take.

Defining the API Methods

After defining the Blockchain class, wij need to create an example of it running on a knot. Very first wij generate a valid identifier for the knot (using Universally Unique IDentifier). Then, wij add the genesis block or very first block te the chain, using some default parameters (previousHash=1 and proof=100).

Everything else takes place when users invoke the “transaction/new” method to create fresh transactions and miners call the “/mine” function to trigger the creation of fresh blocks according to the PoW schema and process the freshly created transactions.

Bijzonder from thesis core methods, wij also enable the registration of fresh knots “/knots/register”, the overeenstemming method to treat potential conflicts “/knots/resolve”, the retrieval “/chain” and html display of the chain “/chain/show”.

To host thesis methods, wij use rplumber, which is a wonderful package to expose R functions spil Surplus and RESTFULL services. It’s indeed versatile and effortless to use (but single-threaded, which requires more advanced setups).

Speciaal from the aforementioned methods, wij define a filterzakje to loom all requests with the server (logger).

Wij stored the code above ter a opstopping called “blockchain-node-server.R” to “plumb” it with the script below. Very first of all, wij define a custom-built Serializer to treat a json boxing punt, spil shown here.

Rplumber implements the swagger UI, which can be reached under http://127.0.0.1:8000/__swagger__/. The picture below shows our methods spil wij announced them te the blockchain-node-server.R script:

Interacting with our Blockchain

Once wij have our Blockchain API running te the url wij specified te the plumb directive, wij can attempt it with a elementary client:

To attempt it locally, I opened Two instances of RStudio: the very first one for the server, where rplumber executes the server part, and the 2nd one for the client, from where I fired all the sample requests from the previous script.

You can check the status of the chain ter the browser, spil shown ter the picture below

But you can also interact with the chain programmatically from the client:

Credits

This coding has bot inspired by different articles and tutorials (mostly te Python) demonstrating how to build a blockchain from scrape. For example, the ones from Gerald Nash, Eric Alcaide and

Special mention to the one created by Daniel van Flymen, very inspiring and effortless to go after.

Related movie: VICTORINOX SWISSCARD NAILCARE REVIEW


Leave a Reply

Your email address will not be published. Required fields are marked *