Playing with Blockchain

… using Ruby!

Today we’ll be playing with some Blockchain. Let’s first look at one very simple example of Bitcoin exchange.

bitcoin_diagram

This drafty drawing shows the broadcast and verification process that happens in the block. Now let’s define what block is in the Blockchain methodology.

What is block?

Transaction data is permanently recorded in files called blocks. They can be thought of as the individual pages of a city recorder’s recordbook (where changes to title to real estate are recorded) or a stock transaction ledger. Blocks are organized into a linear sequence over time (also known as the block chain). New transactions are constantly being processes by miners into new blocks which are added to the end of the chain and can never be changed or removed once accepted by the network (although some software will remove orphaned blocks).

Source: https://en.bitcoin.it/wiki/Block

Now to the fun part - let’s implement small code that checks latest block count:

# Use the blockchain gem library (install via 'gem install blockchain')
require 'blockchain'
# Create a new explorer with the default source of https://blockchain.info/
explorer = Blockchain::BlockExplorer.new
# Use the explorer to grab the latest block height
explorer.get_latest_block.height

That’s something publicly available for example via the following API:

https://blockexplorer.com/api/status?q=getBlockCount

Now you probably know about the hashes that people are trying to figure out via mining. Let’s look at some of those:

require 'blockchain'
explorer = Blockchain::BlockExplorer.new
@hashes = []
explorer.get_blocks.each {|b| @hashes << b.hash}

Here’s what we got:

irb > puts @hashes
000000000000000000a0b8b90d1692c92598a361ebfecbda410ac94f801e5c6b
000000000000000000c76e31baa382c431b97030d003de9e7f235f07abbf3f42
0000000000000000007dd568d2a3f7424c69155464cfb1cc99c8f4c2d17469c8
000000000000000000b24e6301b78cd7c19473ef7aa0a80097978440e9918ac7
0000000000000000004610810b608a2b62ee58e2f3c330a337c5f530b6a23abd
000000000000000000058501e776d06bbc7c5996b78a529021314e9f268f856a
0000000000000000004dea95f8da793f812684967255aa27d3a75e6a44fb283e
000000000000000000b9bdf239b06bf51bad922c73ea005e0303cf58644e2069
000000000000000000729c3f0d310ed246370b0ab829f9d79251240da8764b86
000000000000000000a3a476360dee98c7e48fa4d2b5efbbe5ccf33522cc8130
000000000000000000ae87d6d3ee8ef861ae92a35bd154ecbe9aed7dd2db1f9a
0000000000000000002bb90189b4c5a19089d21ca19d4a5ef44b09463905f599
000000000000000000cefd9cce93e15ba0b78f6d897880a0e84d6fc3b0d9c8ef
00000000000000000054eb7c2e27ac1f39a9099b93638846847a746a9f351f0c
00000000000000000099b3ffc59dbe3aa2df6ebfd2517d2e7d93d70aa11db109
0000000000000000005a86aeda7a2f8e4b0d315a1044afdf40b4ec34ea7d2a08
000000000000000000818dd1fb90ab5f300d3570de651d396e6f00e6541d74d3
0000000000000000009f229fd4bdd548c4f187a74d64ee132db27c9e5cb8a5fd
0000000000000000006d9806a2a65f495b9ab3ecbbc574466963856ad165699a
000000000000000000201afdebf389738aa7f30a75938b8c4e9f748805662f76
0000000000000000003d61d6ef2bd0a7db79f8173afb7fcb4705a3ab7a00087d
0000000000000000007ed18c6287cf75290ba3148f471a86004ff8f056913f8a
0000000000000000009f78a7ce268815db800de50b6afcd279b646528474017d
000000000000000000a05e90e2eb9ca9a63978bfd28ccf7b9cf69181332065e6
00000000000000000096cfd1e819a40676080212112d3515cef0a8e1b8888cd1
0000000000000000002bc7d5f1df0a164edda9d043f4fdfdea4277f7fd52d8af
000000000000000000b6ee89ad37aa6105574f6bd87bc1040ebc25205979b277
00000000000000000086a04c95cddd0b2650c7910a594f3873c16c92bb17d20d
000000000000000000303cf8044cba5532c9066a80c3aac4614dc3ed8fcc220f
000000000000000000c327d275c180ab532cd04717725a8345059a3a854c676f
0000000000000000007c86e72209b63042732a257d0b2b222128183b861d44dc
0000000000000000005ef65111941de1d8b694d54d021f33e33c24733b2f4754
000000000000000000cf3620d570d08d1799a1cafbbfae512fdba2124665eca0
0000000000000000003cfc941c7efceec6d65ec984d8400fa945b03dac2f2c2b
0000000000000000006fe30ccc57fa87a259bc70ee7fc69f571b344ac5027ed7
00000000000000000049d11d37ca23866857607d7d097a3f35caa1827d4cd9f5
00000000000000000079e6c59b441cb5057f1fd592f4b1e85fa67c003fb12fd9
00000000000000000014646c32b253867749f8f5b1743383efb28dbca6a4390e
00000000000000000028afcfebb1efc9700a0f8eca0d28ff7a4adbe5624a13c3
000000000000000000a162d90f08a0393135aa4c4afaa8ec2d03786caf694894
000000000000000000a55633ef842b9d9b599ed4e9fea2d5ed986b2bcbd48b08
000000000000000000c24222a4e0425ed0e213ee9217fa943eeea412b24d795e
0000000000000000001a310c18da081b77581386f649e0b6208c782f37fab1fb
0000000000000000007c622388a243e7aeca334cece259602f96b87771a3469f
000000000000000000776103302a025d489e20769c01d16cd4c60280234e8f50
0000000000000000005ad90cfd37b73a49ba094df383610217b9d670a5ac5a1f
0000000000000000006381713ff1c2b54dd05a59c18bb7b726b766c624a9ece2
00000000000000000063156b517142cda65d88dae4a9462c48f405650409b17c
00000000000000000087eb6812ea487ae7d2391050dd9f14b02ef1a32a1cb8cc
00000000000000000012ce63b4e9ace17a6438af0ebfecf6e1fd1ca9981392a1
00000000000000000016cd6df1d25852400bf63a6c071a8eaa111ac8e736d1c7
0000000000000000007b1d8173e11d1d819a09169c2f2b20ef47d2727a945992
000000000000000000ba7dc461deb9adbe4852fbbdbc020d2a40841e18647a9e
 => nil 
irb > 

What is a hash? Back to the wiki pages.

What is a hash?

A hash algorithm turns an arbitrarily-large amount of data into a fixed-length hash. The same hash will always result from the same data, but modifying the data by even one bit will completely change the hash. Like all computer data, hashes are large numbers, and are usually written as hexadecimal. BitCoin uses the SHA-256 hash algorithm to generate verifiably “random” numbers in a way that requires a predictable amount of CPU effort. Generating a SHA-256 hash with a value less than the current target solves a block and wins you some coins.

Source: https://en.bitcoin.it/wiki/Hash

You can play with the blockchain library in a number of ways. At the moment I see the following methods available for use:

  • :get_block
  • :get_tx
  • :get_block_height
  • :get_address
  • :get_unspent_outputs
  • :get_latest_block
  • :get_unconfirmed_tx
  • :get_blocks –> we already saw this in action
  • :get
  • :create_wallet
  • :get_ticker
  • :to_btc
  • :pushtx

Try them out and play with them yourself. It’s a lot of fun since you’re getting the live data everyone involved with the coins is poking with :)

Categories: ,

Updated: