Sunday, September 21, 2014

Setting up a Ruby Bitcoin Machine, part 2

Now we are going to install Ruby 1.9.3 and bitcoin-ruby plus required libraries.

1. First Ruby: go to the Ubuntu Software Center, and type 'ruby' in the search box. Select and install the standard 1.9.3 Ruby package. This installs the basic Ruby environment.

2. You're going to need a few extras to get bitcoin-ruby to run.

$ sudo apt-get install ruby1.9.1-dev
$ sudo apt-get install build-essential g++
$ sudo gem install eventmachine
$ sudo apt-get install openssl
$ sudo apt-get install sqlite libsqlite3-dev
$ sudo gem install sqlite3
$ sudo gem install sequel
$ sudo gem install em-dns
$ sudo apt-get install git 

3. And now bitcoin-ruby itself:

$ sudo git clone https://github.com/lian/bitcoin-ruby.git; cd bitcoin-ruby
$ ruby bin/bitcoin_node

The last command should execute without any errors, which shows that the system is working. Stop it with Ctrl+C.

The final step is to load all the blockchain data into an sqlite3 database, with the following command:

$ ruby bin/bitcoin_node --storage sequel::sqlite://bitcoin.db --import ~/.bitcoin/blocks --skip-validation

This reads the bitcoin blockchain from your bitcoin client directory, and parses it before loading relevant information into a bitcoin.db database in your bitcoin-ruby directory.

If you get an error about opening the database like SQLite3::CantOpenException: unable to open database file (Sequel::DatabaseConnectionError) try running the previous command with sudo in front.

Once this has been running for a bit you can check, for example, that the balance.rb program works:

$ ruby examples/balance.rb 12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX

This should return:

INFO  storage: opened sequel store sqlite://bitcoin.db
Balance: 50.00000000

It's the first address used in the first (genesis) block of the blockchain. As these coins can never be redeemed it will always have a non-zero balance.

Now that we've shown that it all works, it's time to install the Ruby gem for bitcoin-ruby so the various methods and objects can be called from anywhere:

$ sudo gem build bitcoin-ruby.gemspec && sudo gem install bitcoin-ruby-0.0.5.gem

Right, with that all working, you can start writing Ruby programs to do stuff with bitcoin.

No comments:

Post a Comment