lian / bitcoin-ruby

bitcoin utils and protocol in ruby.
Other
923 stars 323 forks source link

Toshi fails on Bitcoin::Protocol::Alert.parse #204

Closed astrolite closed 6 years ago

astrolite commented 7 years ago

Toshi (https://github.com/coinbase/toshi) uses library bitcoin-ruby fails in mainnet and testnet3 with such error:

/usr/local/share/gems/bundler/gems/bitcoin-ruby-69123208d45a/lib/bitcoin/protocol.rb:62:in `unpack_var_string': undefined method `>' for nil:NilClass (NoMethodError)
        from /usr/local/share/gems/bundler/gems/bitcoin-ruby-69123208d45a/lib/bitcoin/protocol/alert.rb:36:in `parse'
        from /usr/local/share/gems/bundler/gems/bitcoin-ruby-69123208d45a/lib/bitcoin/protocol/parser.rb:113:in `parse_alert'
        from /usr/local/share/gems/bundler/gems/bitcoin-ruby-69123208d45a/lib/bitcoin/protocol/parser.rb:94:in `process_pkt'
        from /usr/local/share/gems/bundler/gems/bitcoin-ruby-69123208d45a/lib/bitcoin/protocol/parser.rb:167:in `parse_buffer'
        from /usr/local/share/gems/bundler/gems/bitcoin-ruby-69123208d45a/lib/bitcoin/protocol/parser.rb:139:in `parse'
        from /home/urix/t/toshi/lib/toshi/connection_handler.rb:40:in `receive_data'
        from /usr/local/share/gems/gems/eventmachine-1.0.7/lib/eventmachine.rb:187:in `run_machine'
        from /usr/local/share/gems/gems/eventmachine-1.0.7/lib/eventmachine.rb:187:in `run'
        from /home/urix/t/toshi/lib/toshi/peer_manager.rb:95:in `run'
        from bin/peer_manager.rb:5:in `<main>'

To reproduce:

require 'bitcoin'
x="60010000000000000000000000ffffff7f00000000ffffff7ffeffff7f01ffffff7f00000000ffffff7f00ffffff7f002f555247454e543a20416c657274206b657920636f6d70726f6d697365642c2075706772616465207265717569726564004630440220653febd6410f470f6bae11cad19c48413becb1ac2c17f908fd0fd53bdc3abd5202206d0e9c96fe88d4a0f01ed9dedae2b6f9e00da94cad0fecaae66ecf689bf71b50"
print Bitcoin::Protocol::Alert.parse([x].pack('H*'))

I maked hot fix - https://github.com/astrolite/bitcoin-ruby/commit/00335850881679ad896bf630f36c056bae95cba1