Setup testnet for bitcoin on Mac OSX

    #Bitcoin

    1. Install Bitcoin-Qt Client

    1) Download Mac version from the download page of official website.

    2) Double click the dmg file and drag into /Application folder.

    2. Configuration file

    Before you start it, you need to configure some options such as testnet, rpcuser, rpcpassword.

    1) Create the data content directory for Bitcoin if it doesn’t exist.

    mkdir -p ~/Library/Application\ Support/Bitcoin
    

    2) Add bitcoin.conf in the data content directory. Here is a sample bitcoin configuration file:

    testnet=1
    server=1
    rpcuser=YOUR_RPC_USER
    rpcpassword=YOUR_RPC_PASSWORD
    rpctimeout=30
    rpcport=8332
    

    For more available options in bitcoin.conf, check it here

    3. Run Bitcoin-Qt

    To start Bitcoin-Qt, you only need to click the application icon in /Applications. If you have enabled testnet option in bitcoin.conf, the application icon should be green instead of normal orange color. When Bitcoin-Qt is running, you can see a testnet3 folder in ~/Library/Application\ Support/Bitcoin, it is the same structure as its parent directory(for the real bitcoin network):

    ├── blocks
    ├── chainstate
    ├── database
    ├── db.log
    ├── debug.log
    ├── peers.dat
    └── wallet.dat
    

    For the details of this structure, check it here.

    All transaction logs can be seen in debug.log. When you received bitcoins, you will get something like this:

    2014-04-20 03:11:05 receive version message: /Satoshi:0.9.1/: version 70002, blocks=225797, us=221.232.86.145:18535, them=23.239.17.57:18333, peer=23.239.17.57:18333
    2014-04-20 03:11:05 Added time data, samples 13, offset -1 (+0 minutes)
    2014-04-20 03:11:05 nTimeOffset = +0  (+0 minutes)
    2014-04-20 03:11:48 AddToWallet eb204331b354e0977e337b24fb0b145c9935776bf985ab2ae283742a7f3839b7  new
    

    4. Retrieve testnet bitcoins

    1) To retrieve testnet bitcoins, you need to get a receiving address first with Bitcoin-Qt.

    2) Go to a testnet faucet, enter the receiving address and bitcoin amount, then summit the form.

    3) After the transaction is finished, you can check it here with the receiving address.

    5. Check

    In general, it will spend a few minutes to synchronize. After the testnet blockchain is downloaded, you can see it in your balance.

    You can also check it with command line by using JSON RPC:

    curl --user YOUR_RPC_USER --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/
    Enter host password for user 'root':
    {"result":{"version":90100,"protocolversion":70002,"walletversion":60000,"balance":37.38825610,"blocks":225803,"timeoffset":-1,"connections":12,"proxy":"","difficulty":1.00000000,"testnet":true,"keypoololdest":1397888847,"keypoolsize":101,"paytxfee":0.00000000,"errors":""},"error":null,"id":"curltest"}
    

    References:

    Deploy Rails app with Nginx in subdirectory

    #Rails #Nginx

    Follow my previous post, this article will try to figure out how to organize Rails app and Wordpress with different subdirectory under the same domain.

    For Wordpress, what we need is put it under the root directory of Apache, then we can directly visit it at example.com/blog.

    For Rails app, we use Unicorn as app server, Nginx as web server. When we deploy it with subdirectory, we need to care about two things.

    Make Rails routes work with subdirectory

    • configure root directory in config/application.rb

      config.root_directory = '/app'
      
    • configure Rails Routes under app scope in config/routes.rb

      require "rails/application"
      
      module RouteScope
        def self.root
          Rails.application.config.root_directory
        rescue NameError
          '/'
        end
      end
        
      RailsApp::Application.routes.draw do
        scope RouteScope.root do 
          root to: 'home#index'
          # other routes here ...
        end
      end
      
    • configure Rails app with Nginx in app subdirectory

      upstream rails_app_server {
        server unix:/path/to/railsapp/tmp/sockets/unicorn.sock fail_timeout=0;
      }
      
      server {
        listen 80;
        server_name example.com;
      
        location /app {
          proxy_pass  http://rails_app_server;
          proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
          proxy_redirect off;
          proxy_buffering off;
          proxy_set_header        Host            $host;
          proxy_set_header        X-Real-IP       $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          error_log /path/to/railsapp/log/nginx/error.log;
          access_log /path/to/railsapp/log/nginx/access.log;
          break;
        }
      }
      

    Make Assets work with subdirectory

    • configure prefix for assets in config/application.rb

      config.assets.prefix  = '/app/assets/'
      
    • make assets prefix work on Nginx

      location ~ ^/app/assets/  {
        root /path/to/railsapp/public;
        gzip_static on;
        expires max;
        add_header Cache-Control public;
        # access_log /dev/null;
      }
      

    Make Nginx As Front End Proxy For Apache

    #Nginx #Apache

    As requirements, after deploy a Rails app on Nginx, we need to setup a Wordpress as official blog, and make sure they share the same domain and work at different subdirectories, such as domain.com/app for rails, domain.com/blog for wordpress.

    On Wordpress official blog, Apache is the recommend web server, so we intend to use Apache and Nginx together, Nginx handle static files and proxy the dynamic requests to Apache.

    Why use Nginx in front of Apache

    As far as I known, Nginx is a non-blocking event based architecture while Apache is a blocking process based architecture. Nginx is more lightful and has better performance on handling static files than Apache, always used as a reverse proxy first, HTTP server second. It can be easily used as a load balancer by Upstream Module, powers high-performance and small memory footprinti. However, it is not better than Apache for handling dynamic requests, and if you want to make php work on Nginx, you may need intall php extension, such as php-fpm. Apache has built-in php module. So a common scenario is to use Nginx and Apache together and use Nginx as reverse proxy.

    How to configure Nginx in front of Apache

    Requirements:

    • Apache is listening on 8080 port
    • Nginx is running and listening on 80 port

    A good habit to add a nginx configuration is put it under /etc/nginx/sites-available and make a link to actviate it at /etc/nginx/sites-enable, here I use example.com as example:

      upstream apache_php {
        server example.com:8080;
      }
    
      server {
        listen 80;
        server_name example.com;
    
        location /blog {
          proxy_pass  http://apache_php;
          proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
          proxy_redirect off;
          proxy_buffering off;
          proxy_set_header        Host            $host;
          proxy_set_header        X-Real-IP       $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          break;
        }
    
        location / {
          rewrite ^(.*)$ /blog last;
        }
      }
    
      server {
        listen 80;
        server_name www.example.com;
        rewrite ^/(.*) http://example.com/$1 permanent;
      }
    

    As proxied backend, a VirtualHost configuration for Apache is required:

    <VirtualHost *:8080>
      ServerName example.com
      ServerAlias www.example.com
      DocumentRoot /var/www
      ErrorLog /var/www/blog/logs/error.log
      CustomLog /var/www/blog/logs/access.log combined
      <Directory />
        Options FollowSymLinks
        AllowOverride None
      </Directory>
      <Directory /var/www>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
      </Directory>
    </VirtualHost>
    

    After the configuration, we need restart Nginx and Apache to make the changes work.

    sudo /etc/init.d/nginx restart
    sudo /etc/init.d/apache2 restart
    

    Now we can directly visit wordpress blog at http://example.com.

    About how to organize Rails app and Wordpress with different subdirectory under the same domain, I will cover it in my next blog.

    Update a scheduled job in Sidekiq

    #Rails #Sidekiq

    In a recent rails project, I use Sidekiq for asynchronous processes.

    Sidekiq is a full-featured background processing framework for Ruby. It has much higher performance than other existing solutions. As it described in official website, 1 Sidekiq process could do the work of 20 Resque or DelayedJob processes.

    I used DelayedJob two yeas ago, It stores jobs in RDBMS and polls for jobs, which is a terribly unscalable idea. It uses processs instead of threads. Now it supports multiple backends for storing the job queue, such as ActiveRecord, Mongoid.

    I never use Resque, but as it described in README, it forks a new process for every worker, safe but easy memory hungry.

    Sidekiq runs thread per worker and has less forking, so it uses much less memory and works faster.

    To integrate Sidekiq in Rails application, it only need simple 3-steps to setup. But after starting a new job with Sidekiq, I find it is difficult to change it’s scheuled time. Below what I want to cover is how to update a scheduled job in Sidekiq.

    To change the scheduled job, the first thing is to locate it. The easiest way is using random generated jid, then update it with sidekiq-scheduler or sidekiq-status. But both of them are in favor of plugins and need to store job id in database.

    There is another way to change the scheduled job without plugins. From the source code of Sidekiq, a SortedEntry instance, which inherits from Job, are easy to be rescheduled. So what we need is to find and tell it running at another time.

    class DialWorker
      include Sidekiq::Worker
    
      def perform(schedule_id)
        schedule = Schedule.find schedule_id
        Tropo::Client.dial schedule
      end
    
      def self.reschedule_job(schedule_id, call_at)
        r = Sidekiq::ScheduledSet.new
        job = r.select do |s|
          s.item['args'].first == schedule_id
        end.pop # Sidekiq::SortedEntry
    
        job.reschedule call_at.to_i
      end
    end
    

    In reschedule_job method, use unique identity args to scan ScheduledSet and find the matched job to reschedule.

    Use CocoaPods to manage library dependencies in iOS projects

    #CocoaPods #iOS

    If you are a Ruby developer, you may heard of Bundler, an awesome tool to manage gem dependencies in Ruby Applications. Cocoapods built with Ruby and inspired from Bundler, which aims to improve discoverability of, and engagement in, third party open-source libraries, by creating a more centralized ecosystem.

    At past, if you use a lot of open source libraries with great quality that can help you build your application, you may need download the library first, and copy it into your project, then you can declare and use it. But there are still some disadvantages. The dependencies stored in your project will take in some space, and it is unconvinient to upgrade when there is an available update, because you can not specify an version of it.

    All of the problems mentioned above can be resolved with a dependencies manager. CocoaPods resolves dependencies between libraries, fetches source code for the dependencies, and creates and maintains an Xcode workspace to build your project.

    Installation

    To install it, you just need to run the below command in terminal, but make sure you have made Ruby installed first.

    gem install cocoapods
    

    Use the remote libraries

    In the root directory of your iOS projects, add a Podfile and edit it

    platform :ios
    
    pod 'AFNetworking', '1.1.0'
    pod 'JSONKit', '1.4'
    

    then run the command:

    pod install
    

    it will create Pods and APP.xcworkspace directories, and there is also a message followed:

    [!] From now on use `APP.xcworkspace'	    
    

    what you need is open APP.xcworkspace, remember always to open the Xcode workspace instead of the project file. After open it, keep Pods scheme selected, and build away both in iOS Simulator and Device mode by clicking Commond + B. Now, switch scheme to APP, you can run the application in your simulator or device.

    Use a local library

    You can find all the repositories are managed by CocoaPods/Specs, but if the open source library hosted on Github is not included in CocoaPods/Specs, how to install it to the project ? Below is a basic steps for setting up a local library with CocoaPods:

    1.choose a place wherever you like to store your third party library in your computer

    $ mkdir -p ~/code/Pods/NAME
    $ cd ~/code/Pods/NAME
    $ pod spec create NAME
    

    2.edit NAME.podspec, you can view the full document on podspec format here, and there are also some podspec examples.

    3.lint the pod against the files of its directory

    $ pod spec lint --local
    

    4.Now, you can add the library to Podfile

    platform :ios
    
    pod 'AFNetworking', '1.1.0'
    pod 'JSONKit', '1.4'    
    pod 'NAME', :local => '~/code/Pods/NAME'
    

    5.Run pod install again, and open APP.xcworkspace, you will find a Local Pods directory there. After compile it again, you can install the application to your simulator or device.

    Of course, you can share your code and contribute it to CocoaPods/Specs, just follow this guide.