2016-06-16



Bitcoin & Blockchain

Bitcoin is a digital asset and a payment system invented by Satoshi Nakamoto, who published the invention in 2008 and released it as open-source software in 2009. It is also called as Cryptocurrency or Decentralized Digital Currency. The system is peer-to-peer and transactions take place between users directly, without any intermediary. These transactions are verified by network nodes and recorded in a public distributed ledger called the Blockchain.

Bitcoins are created as a reward for payment processing work in which users offer their computing power to verify and record payments into Blockchain. This activity is called mining and miners are rewarded with transaction fees and newly created Bitcoins. Apart from being obtained by mining, Bitcoins can be exchanged for other currencies, products, and services.

Bitcoin uses peer-to-peer technology to operate with no central authority or banks; managing transactions and the issuing of bitcoins is carried out collectively by the network. Bitcoin is open-source; its design is public, nobody owns or controls Bitcoin and everyone can take part. Through many of its unique properties, Bitcoin allows exciting uses that could not be covered by any previous payment system.

Ownership of Bitcoins implies that a user can spend bitcoins associated with a specific address. To do so, a payer must digitally sign the transaction using the corresponding private key. Without knowledge of the private key, the transaction cannot be signed and bitcoins cannot be spent. The network verifies the signature using the public key.

If the private key is lost, the Bitcoin network will not recognize any other evidence of ownership; the coins are then unusable, and thus effectively lost.

The unit of account of the Bitcoin system is Bitcoin. Small amounts of Bitcoin used as alternative units are:

millibitcoin (mBTC) = 0.001 Bitcoin (One thousandth of bitcoin)

microbitcoin (µBTC) = 0.000001 Bitcoin (One millionth of bitcoin, also referred as bit)

satoshi = 0.00000001 Bitcoin (Named in homage to Bitcoin’s creator, a satoshi is the smallest amount within Bitcoin , one hundred millionth of a bitcoin).



A Blockchain is just a record, a ledger of all bitcoin transactions that has ever taken place. It is similar to a ledger that a bank would maintain to record all transactions of their customers. However, that’s where the similarity ends. In a bank, the ledger is controlled by the bank itself. Only the bank can see the transactions. The bank has its own security and access system to secure the ledger and to enter transactions.

In the blockchain, a copy of the ledger file is shared between thousands of participants globally, also called miners. Even you can become a miner by simply downloading the open source bitcoin software. New bitcoin transactions are added in the blockchain by a consensus of a majority of the miners, explained below. People do mining because they receive new created bitcoins in return for their efforts. Once a transaction is entered in the blockchain, it can never be erased or modified.

The successful miner finding the new block is rewarded with newly created bitcoins and transaction fees, the reward amounted to 25 newly created bitcoins per block added to the block chain. To claim the reward, a special transaction called a coinbase is included with the processed payments. All bitcoins in existence have been created in such coinbase transactions. The bitcoin protocol specifies that the reward for adding a block will be halved every 210000 blocks (approximately every four years). Eventually, the reward will decrease to zero, and the limit of 21 million bitcoins will be reached; the record keeping will then be rewarded by transaction fees solely. Starting 10-Jul-16 the reward is going to be halved from 25 bitcoins to 12.5 bitcoins per block.

Paying a transaction fee is optional, but may speed up confirmation of the transaction, as miners can choose transactions to process and will prioritize those which pay higher fees. Fees are based on the storage size of the transaction generated, which in turn is dependent on the number of inputs used to create the transaction. Furthermore, priority is given to older unspent inputs.

Querying the Bitcoin blockchain with R

Bitcoin and the way it generates “trustless trust” is one of the hottest topics when it comes to technological innovations right now. The way Bitcoin transactions always backtrace the whole transaction list since the first discovered block (the Genesis block) does not only work for finance, but also related fields like corporate equity. Newer areas of application are emerging as Bitcoin is gaining ground and popularity.

Hence Bitcoin and especially its components the Blockchain and various Sidechains should also be among the most exciting fields for data science and visualization.

So to get started with R and the Blockchain, here’s a few lines of code. We can use, using package “Rbitcoin” by Jan Gorecki.

Here’s our first example, querying the Kraken exchange (Bitcoin exchange founded in San Francisco in 2011):

library(Rbitcoin)

## Loading required package: data.table

## You are currently using Rbitcoin 0.9.2, be aware of the changes coming in the next releases (0.9.3 – github, 0.9.4 – cran). Do not auto update Rbitcoin to 0.9.3 (or later) without testing. For details see github.com/jangorecki/Rbitcoin. This message will be removed in 0.9.5 (or later).

wait <- antiddos(market = ‘kraken’, antispam_interval = 5, verbose = 1)

market.api.process(‘kraken’,c(‘BTC’,’EUR’),’ticker’)

##   market base quote           timestamp market_timestamp last     vwap

## 1: kraken BTC   EUR 2015-01-02 13:12:03             <NA> 263.2 262.9169

##     volume   ask   bid

## 1: 458.3401 263.38 263.22

The function antiddos makes sure that you’re not overusing the Bitcoin API. A reasonable query interval should be one query every 10s.

Here’s a second example that gives you a time-series of the latest exchange values:

trades <- market.api.process(‘kraken’,c(‘BTC’,’EUR’),’trades’)

Rbitcoin.plot(trades, col=’blue’)

The last two examples all were based on aggregated values. But the Blockchain API allows to read every single transaction in the history of Bitcoin. Here’s a slightly longer code example on how to query historical transactions for one address and then mapping the connections between all addresses in this strand of the Blockchain. The red dot is the address we were looking at (so you can change the value to one of your own Bitcoin addresses):

wallet <- blockchain.api.process(’15Mb2QcgF3XDMeVn6M7oCG6CQLw4mkedDi’)

seed <- ‘1NfRMkhm5vjizzqkp2Qb28N7geRQCa4XqC’

genesis <- ‘1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa’

singleaddress <- blockchain.api.query(method = ‘Single Address’, bitcoin_address = seed, limit=100)

txs <- singleaddress$txs

bc <- data.frame()

for (t in txs) {

hash <- t$hash

for (inputs in t$inputs) {

from <- inputs$prev_out$addr

for (out in t$out) {

to <- out$addr

va <- out$value

bc <- rbind(bc, data.frame(from=from,to=to,value=va, stringsAsFactors=F))

}

}

}

After downloading and transforming the blockchain data, we’re now aggregating the resulting transaction table on address level:

library(plyr)

btc <- ddply(bc, c(“from”, “to”), summarize, value=sum(value))

Finally, we’re using igraph to calculate and draw the resulting network of transactions between addresses:

library(igraph)

btc.net <- graph.data.frame(btc, directed=T)

V(btc.net)$color <- “blue”

V(btc.net)$color[unlist(V(btc.net)$name) == seed] <- “red”

nodes <- unlist(V(btc.net)$name)

E(btc.net)$width <- log(E(btc.net)$value)/10

plot.igraph(btc.net, vertex.size=5, edge.arrow.size=0.1, vertex.label=NA, main=paste (“BTC transaction network forn”, seed))

References:

http://beautifuldata.net/2015/01/querying-the-bitcoin-blockchain-with-r/

https://www.bitcoin.com/you-need-to-know

https://en.wikipedia.org/wiki/Bitcoin

https://bitcoin.org/en/

https://www.zebpay.com/blockchain-simple-explanation/

www.coindesk.com/features/data-analysis/

http://www.economist.com/news/special-report/21650295-or-it-next-big-thing

The post Bitcoin and Block Chain appeared first on Mathminers Analytics Magazine.

Show more