em-bitcoin 0.01
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/COPYING.txt +18 -0
- data/README.md +60 -0
- data/em-bitcoin.gemspec +19 -0
- data/lib/em_bitcoin.rb +113 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 536aed261caeaa49ca733e2bffb4b7dc985deaca
|
4
|
+
data.tar.gz: 8c42ad8cfa3df4c383cae0d74665ff5a4f6dfefd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2b93a8252b849a2f402cc5e13b1541ed79b94dd1890a7b7fd5e30e144402fbcae81ca64546a081395b5d1c92e0cd448107cad384cb36e3a8d5f281621dc17e04
|
7
|
+
data.tar.gz: ae4c80b187fe2dc0f2830c20cd5982571d74094696500088e0e289ccff4e3cf50e919bdb55f89bb79410cf1641a9d8c2dc7215417bb57d7a1851801654dbfc18
|
data/COPYING.txt
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Copyright (c) 2016 David H. Hoover <dave.hoover@gmail.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
5
|
+
deal in the Software without restriction, including without limitation the
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# EM-Bitcoin
|
2
|
+
|
3
|
+
The [Bitcoin protocol](https://en.bitcoin.it/wiki/Protocol_documentation) implemented using Ruby's [EventMachine](https://github.com/eventmachine/eventmachine). Initially extracted from [bitcoin-ruby](https://github.com/lian/bitcoin-ruby).
|
4
|
+
|
5
|
+
## Using `EventMachine::Bitcoin::Connection` to connect to the Bitcoin network
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'em_bitcoin'
|
9
|
+
```
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'em-bitcoin'
|
13
|
+
```
|
14
|
+
|
15
|
+
Remember that you always need to start the EventMachine event loop. You can't just do this:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
EventMachine::Bitcoin::Connection.connect_random_from_dns
|
19
|
+
```
|
20
|
+
|
21
|
+
You'll need to do this:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
EventMachine.run do
|
25
|
+
EventMachine::Bitcoin::Connection.connect_random_from_dns
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
You can override the [EventMachine::Connection](http://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine/Connection) methods to provide your application-specific logic. Such as:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class TxCounter < EventMachine::Bitcoin::Connection
|
33
|
+
def post_init
|
34
|
+
super
|
35
|
+
@tx_count = 0
|
36
|
+
end
|
37
|
+
|
38
|
+
def receive_data(data)
|
39
|
+
super
|
40
|
+
@tx_count += 1
|
41
|
+
end
|
42
|
+
|
43
|
+
def unbind
|
44
|
+
puts "Total transactions were: #{@tx_count}!"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
EM.run { TxCounter.connect_random_from_dns }
|
49
|
+
```
|
50
|
+
|
51
|
+
## TODO
|
52
|
+
|
53
|
+
* Put lisence in gemspec
|
54
|
+
* Rework the integration points to both EM and the Bitcoin parser
|
55
|
+
* Write some tests
|
56
|
+
* Write some examples
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
Available here: [COPYING](COPYING.txt)
|
data/em-bitcoin.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "em-bitcoin"
|
5
|
+
s.version = "0.01"
|
6
|
+
s.authors = ["redsquirrel"]
|
7
|
+
s.email = ["dave.hoover@gmail.com"]
|
8
|
+
s.summary = %q{Bitcoin Protocol using EventMachine}
|
9
|
+
s.description = %q{Combining Ruby's popular EventMachine library with the ruby-bitcoin implementation to provide an extensible connector to the Bitcoin network.}
|
10
|
+
s.homepage = "https://github.com/redsquirrel/em-bitcoin"
|
11
|
+
|
12
|
+
s.rubyforge_project = "em-bitcoin"
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.required_rubygems_version = ">= 1.3.6"
|
19
|
+
end
|
data/lib/em_bitcoin.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'eventmachine'
|
2
|
+
require 'bitcoin'
|
3
|
+
require 'resolv'
|
4
|
+
|
5
|
+
module EventMachine::Bitcoin
|
6
|
+
module ConnectionHandler
|
7
|
+
def on_inv_transaction(hash)
|
8
|
+
p ['inv transaction', hash.hth]
|
9
|
+
pkt = Bitcoin::Protocol.getdata_pkt(:tx, [hash])
|
10
|
+
send_data(pkt)
|
11
|
+
end
|
12
|
+
|
13
|
+
def on_inv_block(hash)
|
14
|
+
p ['inv block', hash.hth]
|
15
|
+
pkt = Bitcoin::Protocol.getdata_pkt(:block, [hash])
|
16
|
+
send_data(pkt)
|
17
|
+
end
|
18
|
+
|
19
|
+
def on_get_transaction(hash)
|
20
|
+
p ['get transaction', hash.hth]
|
21
|
+
end
|
22
|
+
|
23
|
+
def on_get_block(hash)
|
24
|
+
p ['get block', hash.hth]
|
25
|
+
end
|
26
|
+
|
27
|
+
def on_addr(addr)
|
28
|
+
p ['addr', addr, addr.alive?]
|
29
|
+
end
|
30
|
+
|
31
|
+
def on_tx(tx)
|
32
|
+
p ['tx', tx.hash]
|
33
|
+
end
|
34
|
+
|
35
|
+
def on_ping(nonce)
|
36
|
+
send_data(Bitcoin::Protocol.pong_pkt(nonce))
|
37
|
+
end
|
38
|
+
|
39
|
+
def on_block(block)
|
40
|
+
p ['block', block.hash]
|
41
|
+
end
|
42
|
+
|
43
|
+
def on_version(version)
|
44
|
+
p [@sockaddr, 'version', version, version.time - Time.now.to_i]
|
45
|
+
send_data(Bitcoin::Protocol.verack_pkt)
|
46
|
+
end
|
47
|
+
|
48
|
+
def on_verack
|
49
|
+
on_handshake_complete
|
50
|
+
end
|
51
|
+
|
52
|
+
def on_handshake_complete
|
53
|
+
p [@sockaddr, 'handshake complete']
|
54
|
+
@connected = true
|
55
|
+
|
56
|
+
query_blocks
|
57
|
+
end
|
58
|
+
|
59
|
+
def query_blocks
|
60
|
+
start = ("\x00"*32)
|
61
|
+
stop = ("\x00"*32)
|
62
|
+
pkt = Bitcoin::Protocol.pkt("getblocks", "\x00" + start + stop )
|
63
|
+
send_data(pkt)
|
64
|
+
end
|
65
|
+
|
66
|
+
def on_handshake_begin
|
67
|
+
pkt = Bitcoin::Protocol::Version.new(
|
68
|
+
from: "127.0.0.1:8333",
|
69
|
+
to: @sockaddr.join(":"),
|
70
|
+
).to_pkt
|
71
|
+
p ['sending version pkt', pkt]
|
72
|
+
send_data(pkt)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
class Connection < EM::Connection
|
78
|
+
include ConnectionHandler
|
79
|
+
|
80
|
+
def initialize(host, port)
|
81
|
+
@sockaddr = [host, port]
|
82
|
+
@parser = Bitcoin::Protocol::Parser.new( self )
|
83
|
+
end
|
84
|
+
|
85
|
+
def post_init
|
86
|
+
p ['connected', @sockaddr]
|
87
|
+
EM.schedule{ on_handshake_begin }
|
88
|
+
end
|
89
|
+
|
90
|
+
def receive_data(data)
|
91
|
+
@parser.parse(data)
|
92
|
+
end
|
93
|
+
|
94
|
+
def unbind
|
95
|
+
p ['disconnected', @sockaddr]
|
96
|
+
self.class.connect_random_from_dns
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.connect(host, port)
|
100
|
+
EM.connect(host, port, self, host, port)
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.connect_random_from_dns
|
104
|
+
seeds = Bitcoin.network[:dns_seeds]
|
105
|
+
if seeds.any?
|
106
|
+
host = Resolv::DNS.new.getaddresses(seeds.sample).map {|a| a.to_s}.sample
|
107
|
+
connect(host, Bitcoin::network[:default_port])
|
108
|
+
else
|
109
|
+
raise "No DNS seeds available. Provide IP, configure seeds, or use different network."
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-bitcoin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.01'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- redsquirrel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Combining Ruby's popular EventMachine library with the ruby-bitcoin implementation
|
14
|
+
to provide an extensible connector to the Bitcoin network.
|
15
|
+
email:
|
16
|
+
- dave.hoover@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- COPYING.txt
|
22
|
+
- README.md
|
23
|
+
- em-bitcoin.gemspec
|
24
|
+
- lib/em_bitcoin.rb
|
25
|
+
homepage: https://github.com/redsquirrel/em-bitcoin
|
26
|
+
licenses: []
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 1.3.6
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project: em-bitcoin
|
44
|
+
rubygems_version: 2.4.5.1
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Bitcoin Protocol using EventMachine
|
48
|
+
test_files: []
|