c3d 0.3.5 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module C3D
4
+ class Purger
5
+ include Celluloid
6
+
7
+ def initialize blob
8
+ begin
9
+ dn = blob[14..-1]
10
+ currently_downloading = $puller.all
11
+ downloading = currently_downloading.select{|t| t["name"] == dn}.first["id"]
12
+ $puller.destroy downloading if downloading
13
+ blob_file = File.join(ENV['BLOBS_DIR'], dn)
14
+ if File.exists? blob_file
15
+ File.delete blob_file
16
+ end
17
+ rescue
18
+ return
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module C3D
4
+ class SetupC3D
5
+ include Celluloid
6
+
7
+ def initialize
8
+ set_deps
9
+ config = get_config
10
+ set_the_env config
11
+ set_trans_config config
12
+ set_c3d_config config
13
+ start_processes config
14
+ end
15
+
16
+ private
17
+
18
+ def set_deps
19
+ dep_exist? 'transmission-daemon', 'sudo apt-get install transmission-daemon && sudo service transmission-daemon stop && sudo update-rc.d -f transmission-daemon remove'
20
+ end
21
+
22
+ def get_config
23
+ dir_exist? File.join(ENV['HOME'], '.epm')
24
+ config_file = File.join(ENV['HOME'], '.epm', 'c3d-config.json')
25
+ config_example = File.join(File.dirname(__FILE__), '..', '..', '..', 'settings', 'c3d-config.json')
26
+ unless File.exists? config_file
27
+ tmp = File.read config_example
28
+ until ! tmp[/(\{\{USERHOME\}\})/]
29
+ tmp.gsub!("{{USERHOME}}", ENV["HOME"])
30
+ p 'hello'
31
+ end
32
+ print "Before we begin, I need to ask you two personal questions:\n"
33
+ print "What is your primary account which I should be using to send transactions?\n\n"
34
+ account = STDIN.gets.chomp
35
+ print "Thanks. #{account} is what I will use.\n"
36
+ print "Thanks. Now what is the private key for that account which I should be using?\n\n"
37
+ secret = STDIN.gets.chomp
38
+ print "Thanks. #{secret} is what I will use.\n"
39
+ account = "0x#{account}" if account[0..1] != '0x'
40
+ secret = "0x#{secret}" if secret[0..1] != '0x'
41
+ tmp.gsub!("{{0xACCT}}", account)
42
+ tmp.gsub!("{{0xSEC}}", secret)
43
+ File.open(config_file, 'w'){|f| f.write(tmp)}
44
+ end
45
+ return JSON.load(File.read(config_file))
46
+ end
47
+
48
+ def set_the_env config
49
+ ENV['SWARM_DIR'] = config['swarm_dir']
50
+ ENV['TORRENTS_DIR'] = config['torrents_dir']
51
+ ENV['BLOBS_DIR'] = config['blobs_dir']
52
+ ENV['WATCH_FILE'] = config['watch_file']
53
+ ENV['IGNORE_FILE'] = config['ignore_file']
54
+ ENV['TORRENT_RPC'] = config['torrent_rpc']
55
+ ENV['TORRENT_USER'] = config['torrent_user']
56
+ ENV['TORRENT_PASS'] = config['torrent_pass']
57
+ ENV['UI_RESPOND'] = config['ui_respond']
58
+ ENV['UI_ANNOUNCE'] = config['ui_announce']
59
+ ENV['ETH_CONNECTOR'] = config['eth_connector']
60
+ ENV['ETH_ZMQ_ADDR'] = config['eth_zmq_addr']
61
+ ENV['ETH_HOST'] = config['eth_rpc_host']
62
+ ENV['ETH_PORT'] = config['eth_rpc_port']
63
+ ENV['ETH_KEY'] = config['primary_account_key']
64
+ end
65
+
66
+ def set_trans_config config
67
+ trans_file = File.join(ENV['HOME'], '.epm', 'settings.json')
68
+ trans_example = File.join(File.dirname(__FILE__), '..', '..', '..', 'settings', 'transmission.json')
69
+ unless File.exists? trans_file
70
+ FileUtils.cp trans_example, trans_file
71
+ end
72
+ trans_config = JSON.load(File.read(trans_file))
73
+ trans_config["incomplete-dir"] = config['download_dir']
74
+ trans_config["download-queue-size"] = config['download-queue-size'].to_i
75
+ trans_config["queue-stalled-minutes"] = config['queue-stalled-minutes'].to_i
76
+ trans_config["seed-queue-size"] = config['seed-queue-size'].to_i
77
+ File.open(trans_file, 'w'){|f| f.write(JSON.pretty_generate(trans_config))}
78
+ end
79
+
80
+ def set_c3d_config config
81
+ dir_exist? ENV['SWARM_DIR']
82
+ dir_exist? ENV['TORRENTS_DIR']
83
+ dir_exist? ENV['BLOBS_DIR']
84
+ dir_exist? config['download_dir']
85
+ file_exist? ENV['WATCH_FILE']
86
+ file_exist? ENV['IGNORE_FILE']
87
+ end
88
+
89
+ def start_processes config
90
+ C3D::TransmissionRunner.start_transmission
91
+ C3D::EthRunner.start_ethereum config
92
+ end
93
+
94
+ def dir_exist? directry
95
+ unless File.directory? directry
96
+ Dir.mkdir directry
97
+ end
98
+ end
99
+
100
+ def file_exist? fil
101
+ unless File.exists? fil
102
+ File.open(fil, "w") {}
103
+ end
104
+ end
105
+
106
+ def dep_exist? dependency, fixer
107
+ unless which dependency
108
+ `#{fixer}`
109
+ end
110
+ end
111
+
112
+ def which cmd
113
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
114
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
115
+ exts.each { |ext|
116
+ exe = File.join(path, "#{cmd}#{ext}")
117
+ return exe if File.executable? exe
118
+ }
119
+ end
120
+ return nil
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,226 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module C3D
4
+ class TreeBuilder
5
+ include Celluloid
6
+ attr_accessor :groups, :blobs, :parsed, :purged
7
+
8
+ def initialize parse=[], purge=[], debug=false
9
+ @debug = debug
10
+ @groups = 0
11
+ @blobs = 0
12
+ @parsed = 0
13
+ @purged = 0
14
+ @parse = parse || []
15
+ @purge = purge || []
16
+ assemble_and_perform_queries
17
+ print(@parsed.to_s + " <-Parsed Contracts || Purged Contracts-> " +
18
+ @purged.to_s + " || " + @groups.to_s + " <-Groups || Blobs-> " +
19
+ @blobs.to_s + "\n") if @debug
20
+ end
21
+
22
+ private
23
+
24
+ def assemble_and_perform_queries
25
+ puts "[C3D-EPM::#{Time.now.strftime( "%F %T" )}] Starting a Tree Parse." if @debug
26
+ until @parse.empty?
27
+ get_the_contract @parse.shift
28
+ @parse.uniq!
29
+ end
30
+ until @purge.empty?
31
+ purge_the_contract @purge.shift
32
+ @purge.uniq!
33
+ end
34
+ end
35
+
36
+ def get_the_contract contract
37
+ @parsed += 1
38
+ if ab? contract
39
+ get_ab_content contract
40
+ elsif ba? contract
41
+ get_ba_content contract
42
+ end
43
+ end
44
+
45
+ def purge_the_contract contract
46
+ @purged += 1
47
+ if ab? contract
48
+ purge_ab_content contract
49
+ elsif ba? contract
50
+ purge_ba_content contract
51
+ end
52
+ end
53
+
54
+ def get_ab_content contract
55
+ if can_we_get_the_k? contract
56
+ blob = send_query contract, '0x13'
57
+ get_the_blob blob unless do_i_have_it? blob
58
+ end
59
+ end
60
+
61
+ def get_ba_content contract
62
+ if can_we_get_the_k? contract
63
+ get_data_model contract
64
+ get_ui_files contract
65
+ group = {}
66
+ group[:prev] = send_query contract, '0x19'
67
+ until group[:prev] == '0x30'
68
+ group = get_the_group contract, group[:prev]
69
+ get_the_blob group[:cont] unless do_i_have_it? group[:cont]
70
+ end
71
+ end
72
+ end
73
+
74
+ def purge_ab_content contract
75
+ blob = send_query contract, '0x13'
76
+ purge_the_blob blob
77
+ end
78
+
79
+ def purge_ba_content contract
80
+ dm = send_query contract, '0x11'
81
+ purge_the_blob dm
82
+ ui = send_query contract, '0x12'
83
+ puts "[C3D-EPM::#{Time.now.strftime( "%F %T" )}] Pushing #{ui} to Parse Stack." if @debug
84
+ @purge.push ui
85
+ group = {}
86
+ group = send_query contract, '0x19'
87
+ until group == '0x30'
88
+ group = purge_the_group contract, group
89
+ end
90
+ end
91
+
92
+ def can_we_get_the_k? contract
93
+ behav = send_query contract, '0x18'
94
+ if behav == ('0x01' || '0x1')
95
+ return true
96
+ elsif behav == ('0x05' || '0x5')
97
+ puts "[C3D-EPM::#{Time.now.strftime( "%F %T" )}] Pushing #{contract} to Purge Stack." if @debug
98
+ @purge.push contract
99
+ end
100
+ return false
101
+ end
102
+
103
+ def get_the_group contract, group_id
104
+ @groups += 1
105
+ this_group = {}
106
+ if can_we_get_the_group? contract, group_id
107
+ if does_the_group_have_blobs? contract, group_id
108
+ this_group[:blob] = send_query(contract, content_slot(group_id))
109
+ end
110
+ this_group[:prev] = send_query(contract, prev_link_slot(group_id))
111
+ else
112
+ this_group[:prev] = send_query(contract, prev_link_slot(group_id))
113
+ end
114
+ return this_group
115
+ end
116
+
117
+ def purge_the_group contract, group_id
118
+ @groups += 1
119
+ blob = send_query(contract, content_slot(group_id))
120
+ purge_the_blob blob
121
+ send_query(contract, prev_link_slot(group_id))
122
+ end
123
+
124
+ def can_we_get_the_group? contract, group_id
125
+ behav = send_query(contract, behav_slot(group_id))
126
+ if behav == ('0x01' || '0x1' || '0x')
127
+ return true
128
+ elsif behav == ('0x05' || '0x5')
129
+ puts "[C3D-EPM::#{Time.now.strftime( "%F %T" )}] Pushing #{group_id} to Purge Stack." if @debug
130
+ @purge.push group_id
131
+ end
132
+ return false
133
+ end
134
+
135
+ def does_the_group_have_blobs? contract, group_id
136
+ type = send_query(contract, type_slot(group_id))
137
+ if type == '0x'
138
+ puts "[C3D-EPM::#{Time.now.strftime( "%F %T" )}] Pushing #{group_id} to Parse Stack." if @debug
139
+ @parse.push send_query contract, group_id
140
+ return false
141
+ elsif type == ('0x01' || '0x1')
142
+ puts "[C3D-EPM::#{Time.now.strftime( "%F %T" )}] Pushing #{group_id} to Parse Stack." if @debug
143
+ @parse.push group_id
144
+ return true
145
+ else
146
+ return false
147
+ end
148
+ end
149
+
150
+ def send_query contract, storage
151
+ $eth.get_storage_at contract, storage
152
+ end
153
+
154
+ def ab? contract
155
+ location = send_query contract, '0x10'
156
+ if location == '0x88554646ab'
157
+ return true
158
+ else
159
+ return false
160
+ end
161
+ end
162
+
163
+ def ba? contract
164
+ location = send_query contract, '0x10'
165
+ if location == '0x88554646ba'
166
+ return true
167
+ else
168
+ return false
169
+ end
170
+ end
171
+
172
+ def do_i_have_it? blob
173
+ begin
174
+ dn = blob[42..-1]
175
+ f = File.exists?(File.join(ENV['BLOBS_DIR'], dn))
176
+ @blobs += 1
177
+ rescue
178
+ return false
179
+ end
180
+ f
181
+ end
182
+
183
+ def get_the_blob blob
184
+ begin
185
+ btih = blob[2..41]
186
+ dn = blob[42..-1]
187
+ link = "magnet:?xt=urn:btih:" + btih + "&dn=" + dn
188
+ torrent = $puller.create link
189
+ return true
190
+ rescue
191
+ return false
192
+ end
193
+ end
194
+
195
+ def purge_the_blob blob
196
+ Purger.new blob
197
+ end
198
+
199
+ def get_data_model contract
200
+ blob = send_query contract, '0x11'
201
+ get_the_blob blob unless do_i_have_it? blob
202
+ end
203
+
204
+ def get_ui_files contract
205
+ contract = send_query contract, '0x12'
206
+ puts "[C3D-EPM::#{Time.now.strftime( "%F %T" )}] Pushing #{contract} to Parse Stack." if @debug
207
+ @parse.push contract
208
+ end
209
+
210
+ def prev_link_slot group_id
211
+ slot = "0x" + ((group_id.hex + 0x1).to_s(16))
212
+ end
213
+
214
+ def type_slot group_id
215
+ slot = "0x" + ((group_id.hex + 0x3).to_s(16))
216
+ end
217
+
218
+ def behav_slot group_id
219
+ slot = "0x" + ((group_id.hex + 0x4).to_s(16))
220
+ end
221
+
222
+ def content_slot group_id
223
+ slot = "0x" + ((group_id.hex + 0x5).to_s(16))
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module C3D
4
+ module Utility
5
+ extend self
6
+
7
+ def save_key
8
+ key = $eth.get_key
9
+ config_file = File.join(ENV['HOME'], '.epm', 'c3d-config.json')
10
+ config = JSON.load(File.read(config_file))
11
+ config["primary_account_key"] = key if config["primary_account_key"] != key
12
+ ENV['ETH_KEY'] = config["primary_account_key"]
13
+ File.open(config_file, 'w'){|f| f.write(JSON.pretty_generate(config))}
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module C3D
4
+ module EyeOfZorax
5
+ extend self
6
+
7
+ def subscribe contract
8
+ subscribe_file = ENV['WATCH_FILE']
9
+ subscribed = loader subscribe_file
10
+ adder subscribed, subscribe_file, contract
11
+ end
12
+
13
+ def unsubscribe contract
14
+ subscribe_file = ENV['WATCH_FILE']
15
+ subscribed = loader subscribe_file
16
+ remover subscribed, subscribe_file, contract
17
+ end
18
+
19
+ def ignore contract
20
+ ignore_file = ENV['IGNORE_FILE']
21
+ ignore = loader ignore_file
22
+ adder ignored, ignore_file, contract
23
+ end
24
+
25
+ def unignore contract
26
+ ignore_file = ENV['IGNORE_FILE']
27
+ ignore = loader ignore_file
28
+ remover ignored, ignore_file, contract
29
+ end
30
+
31
+ private
32
+ def loader file
33
+ begin
34
+ j = JSON.load File.read file
35
+ rescue
36
+ j = []
37
+ end
38
+ j = [] unless j
39
+ return j
40
+ end
41
+
42
+ def saver object, file
43
+ File.open(file, 'w'){ |f| f.write JSON.pretty_generate object }
44
+ end
45
+
46
+ def adder object, file, contract
47
+ unless object.include? contract
48
+ object << contract if contract.class == (String || Array)
49
+ object.flatten if contract.class == Array
50
+ saver object, file
51
+ return true
52
+ else
53
+ return false
54
+ end
55
+ end
56
+
57
+ def remover object, file, contract
58
+ if object.include? contract
59
+ if contract.class == String
60
+ object.delete contract
61
+ elsif contract.class == Array
62
+ contract.each{ |c| object.delete c }
63
+ end
64
+ saver object, file
65
+ return true
66
+ else
67
+ return false
68
+ end
69
+ end
70
+ end
71
+ end
data/lib/c3d/version.rb CHANGED
@@ -1 +1 @@
1
- VERSION = "0.3.5"
1
+ VERSION ||= "0.4.2"
data/lib/c3d.rb CHANGED
@@ -5,6 +5,8 @@ require 'digest/sha1'
5
5
  require 'json'
6
6
  require 'yaml'
7
7
  require 'base64'
8
+ require 'net/http'
9
+ require 'uri'
8
10
 
9
11
  # Gem Dependencies
10
12
  require 'httparty'
@@ -12,24 +14,46 @@ require 'bencode'
12
14
  require 'celluloid/autostart'
13
15
 
14
16
  # This Gem
15
- Dir[File.dirname(__FILE__) + '/c3d/*.rb'].each {|file| require file }
16
-
17
- SetupC3D.new
18
-
19
- TransmissionRunner.new.start_transmission
20
- sleep 5
21
-
22
- TorrentAPI.supervise_as :puller, {
23
- username: ENV['TORRENT_USER'],
24
- password: ENV['TORRENT_PASS'],
25
- url: ENV['TORRENT_RPC'] }
26
- @swarm_puller = Celluloid::Actor[:puller]
27
-
28
- @eth = ConnectEthZMQ.new
29
- # @ui = ConnectUI.new
30
- # @ui.async.run
17
+ Dir[File.dirname(__FILE__) + '/c3d/*.rb'].each {|file| require file }
18
+ Dir[File.dirname(__FILE__) + '/c3d/actors/*.rb'].each {|file| require file }
19
+ Dir[File.dirname(__FILE__) + '/c3d/bylaws/*.rb'].each {|file| require file }
20
+ Dir[File.dirname(__FILE__) + '/c3d/connectors/*.rb'].each {|file| require file }
21
+ Dir[File.dirname(__FILE__) + '/c3d/util/*.rb'].each {|file| require file }
22
+
23
+ module C3D
24
+ extend self
25
+
26
+ def start
27
+ C3D::SetupC3D.new
28
+
29
+ C3D::ConnectTorrent.supervise_as :puller, {
30
+ username: ENV['TORRENT_USER'],
31
+ password: ENV['TORRENT_PASS'],
32
+ url: ENV['TORRENT_RPC'] }
33
+ C3D::ConnectEth.supervise_as :eth, :cpp
34
+
35
+ # todo, need to trap_exit on these actors if they crash
36
+ $puller = Celluloid::Actor[:puller]
37
+ $eth = Celluloid::Actor[:eth]
38
+
39
+ C3D::Utility.save_key
40
+ sleep
41
+ end
42
+
43
+ def stop
44
+ exit 0
45
+ end
46
+
47
+ def restart
48
+ C3D.stop
49
+ C3D.start
50
+ end
51
+
52
+ def version
53
+ return VERSION
54
+ end
55
+ end
31
56
 
32
57
  if __FILE__==$0
33
- blob1 = File.read(ARGV[0]) + "\n#{Time.now}"
34
- Publish.new @swarm_puller, @eth, blob1, 'a6cb63ec28c12929bee2d3567bf98f374a0b7167', '97d1f086800920e8fd2344be52fb22d7bf6036d2', '0x2E737671893D33BF53A5F00EDE9E839F9D86E3A391387CA9189CA79729D00000'
58
+ C3D.start
35
59
  end
@@ -1,17 +1,27 @@
1
1
  {
2
- "swarm_dir": "/home/coda/.cache/c3d",
3
- "torrents_dir": "/home/coda/.cache/c3d/torrents",
4
- "blobs_dir": "/home/coda/.cache/c3d/blobs",
5
- "watch_file": "/home/coda/.cache/c3d/watchers.json",
2
+ "swarm_dir": "{{USERHOME}}/.cache/c3d",
3
+ "torrents_dir": "{{USERHOME}}/.cache/c3d/torrents",
4
+ "blobs_dir": "{{USERHOME}}/.cache/c3d/blobs",
5
+ "watch_file": "{{USERHOME}}/.cache/c3d/watchers.json",
6
6
  "torrent_rpc": "http://127.0.0.1:9091/transmission/rpc",
7
7
  "torrent_user": "username",
8
8
  "torrent_pass": "password",
9
- "ui_address": "tcp://127.0.0.1:31314",
10
- "eth_zmq_addr": "tcp://127.0.0.1:31315",
11
- "eth_host": "localhost",
12
- "eth_port": "9090",
13
- "ignore_file": "/home/coda/.cache/c3d/ignored.json",
14
- "download_dir": "/home/coda/.cache/c3d/downloading",
9
+ "ui_respond": "tcp://127.0.0.1:31314",
10
+ "ui_announce": "tcp://127.0.0.1:31316",
11
+ "path-to-eth": "/opt/cpp-ethereum/build/eth/eth",
12
+ "path-to-lllc": "/opt/cpp-ethereum/build/lllc/lllc",
13
+ "eth_connector": "rpc",
14
+ "eth_rpc_host": "localhost",
15
+ "eth_rpc_port": "9090",
16
+ "eth_remote":"54.201.28.117",
17
+ "eth_peer_port":"30303",
18
+ "eth_mine":"off",
19
+ "eth_client_name":"c3d-headless",
20
+ "blockchain_dir":"{{USERHOME}}/.ethereum",
21
+ "ignore_file": "{{USERHOME}}/.cache/c3d/ignored.json",
22
+ "download_dir": "{{USERHOME}}/.cache/c3d/downloading",
15
23
  "download-queue-size": "50",
16
- "queue-stalled-minutes": "98"
17
- }
24
+ "queue-stalled-minutes": "98",
25
+ "primary_account":"{{0xACCT}}",
26
+ "primary_account_key":"{{0xSEC}}"
27
+ }