Skip to content

Commit 3c796b7

Browse files
committed
isolating modules
1 parent 31fa1ff commit 3c796b7

12 files changed

+207
-185
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
> ⚠️ Warning: Early-stage, breaking changes are expected.
44
5+
This is a low-level client library. For a better experience, you may want to check out the [Lighstorm](https://github.com/icebaker/lighstorm) abstraction.
6+
57
- [Usage](#usage)
68
- [Channel Arguments](#channel-arguments)
79
- [Documentation](#documentation)

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ require_relative './controllers/grpc_generator'
55
namespace :grpc do
66
desc 'Upgrade lnd Protocol Buffers for gGRPC'
77
task :upgrade do
8-
GrpcGeneratorController.upgrade!
8+
LNDClientInternal::GrpcGeneratorController.upgrade!
99
end
1010
end

components/grpc.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
require_relative './grpc/lightning_services_pb'
44
require_relative './grpc/routerrpc/router_services_pb'
55

6-
module GRPC
7-
SERVICES = {
8-
lightning: Lnrpc::Lightning,
9-
router: Routerrpc::Router
10-
}.freeze
6+
module LNDClientInternal
7+
module GRPC
8+
SERVICES = {
9+
lightning: Lnrpc::Lightning,
10+
router: Routerrpc::Router
11+
}.freeze
12+
end
1113
end

controllers/client.rb

+22-16
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,34 @@
44
require_relative 'config'
55
require_relative 'service'
66

7-
class ClientController
8-
attr_reader :config, :doc
7+
module LNDClientInternal
8+
class ClientController
9+
attr_reader :config, :doc
910

10-
def initialize(options)
11-
@config = ConfigController.new(self, options)
12-
@services = {}
11+
def initialize(options)
12+
@config = LNDClientInternal::ConfigController.new(self, options)
13+
@services = {}
1314

14-
doc = Struct.new(:services)
15-
@doc = doc.new(GRPC::SERVICES.keys.map(&:to_s))
16-
end
15+
doc = Struct.new(:services)
16+
@doc = doc.new(LNDClientInternal::GRPC::SERVICES.keys.map(&:to_s))
17+
end
1718

18-
def respond_to_missing?(method_name, include_private = false)
19-
service_key = method_name.to_sym
19+
def respond_to_missing?(method_name, include_private = false)
20+
service_key = method_name.to_sym
2021

21-
GRPC::SERVICES.include?(service_key) || super
22-
end
22+
LNDClientInternal::GRPC::SERVICES.include?(service_key) || super
23+
end
2324

24-
def method_missing(method_name, *args)
25-
service_key = method_name.to_sym
25+
def method_missing(method_name, *args)
26+
service_key = method_name.to_sym
2627

27-
raise ArgumentError, "Method `#{method_name}` doesn't exist." unless GRPC::SERVICES.include?(service_key)
28+
unless LNDClientInternal::GRPC::SERVICES.include?(service_key)
29+
raise ArgumentError, "Method `#{method_name}` doesn't exist."
30+
end
2831

29-
@services[service_key] ||= ServiceController.new(self, GRPC::SERVICES[service_key], *args)
32+
@services[service_key] ||= LNDClientInternal::ServiceController.new(
33+
self, LNDClientInternal::GRPC::SERVICES[service_key], *args
34+
)
35+
end
3036
end
3137
end

controllers/config.rb

+22-20
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,36 @@
22

33
require 'grpc'
44

5-
class ConfigController
6-
attr_reader :socket_address, :credentials, :certificate, :macaroon
5+
module LNDClientInternal
6+
class ConfigController
7+
attr_reader :socket_address, :credentials, :certificate, :macaroon
78

8-
def initialize(client, options)
9-
@client = client
9+
def initialize(client, options)
10+
@client = client
1011

11-
@certificate_path = options[:certificate_path]
12-
@certificate = options[:certificate]
12+
@certificate_path = options[:certificate_path]
13+
@certificate = options[:certificate]
1314

14-
@macaroon_path = options[:macaroon_path]
15-
@macaroon = options[:macaroon]
15+
@macaroon_path = options[:macaroon_path]
16+
@macaroon = options[:macaroon]
1617

17-
@socket_address = options[:socket_address] || '127.0.0.1:10009'
18+
@socket_address = options[:socket_address] || '127.0.0.1:10009'
1819

19-
setup_certificate!
20-
setup_macaroon!
21-
end
20+
setup_certificate!
21+
setup_macaroon!
22+
end
2223

23-
def setup_certificate!
24-
raise 'conflicting options for certificate' if @certificate && @certificate_path
24+
def setup_certificate!
25+
raise 'conflicting options for certificate' if @certificate && @certificate_path
2526

26-
@certificate = File.read(@certificate_path) if @certificate_path
27-
@credentials = GRPC::Core::ChannelCredentials.new(@certificate)
28-
end
27+
@certificate = File.read(@certificate_path) if @certificate_path
28+
@credentials = ::GRPC::Core::ChannelCredentials.new(@certificate)
29+
end
2930

30-
def setup_macaroon!
31-
raise 'conflicting options for macaroon' if @macaroon && @macaroon_path
31+
def setup_macaroon!
32+
raise 'conflicting options for macaroon' if @macaroon && @macaroon_path
3233

33-
@macaroon = File.read(@macaroon_path).unpack('H*') if @macaroon_path
34+
@macaroon = File.read(@macaroon_path).unpack('H*') if @macaroon_path
35+
end
3436
end
3537
end

controllers/documentation.rb

+30-28
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,46 @@
22

33
require_relative '../logic/string'
44

5-
class DocumentationController
6-
attr_reader :available_methods
5+
module LNDClientInternal
6+
class DocumentationController
7+
attr_reader :available_methods
78

8-
def initialize(service)
9-
@service = service
10-
@descriptions = {}
11-
@grpc = {}
9+
def initialize(service)
10+
@service = service
11+
@descriptions = {}
12+
@grpc = {}
1213

13-
build!
14-
end
14+
build!
15+
end
1516

16-
def build!
17-
@available_methods = @service.service.rpc_descs.values.map do |desc|
18-
method_name = StringLogic.underscore(desc.name.to_s)
17+
def build!
18+
@available_methods = @service.service.rpc_descs.values.map do |desc|
19+
method_name = LNDClientInternal::StringLogic.underscore(desc.name.to_s)
1920

20-
build_description!(method_name, desc)
21+
build_description!(method_name, desc)
2122

22-
@grpc[method_name] = desc
23+
@grpc[method_name] = desc
2324

24-
method_name
25-
end.sort
26-
end
25+
method_name
26+
end.sort
27+
end
2728

28-
def build_description!(method_name, desc)
29-
input = desc.input.new.to_h if desc.input.respond_to?(:new)
30-
output = desc.output.new.to_h if desc.output.respond_to?(:new)
29+
def build_description!(method_name, desc)
30+
input = desc.input.new.to_h if desc.input.respond_to?(:new)
31+
output = desc.output.new.to_h if desc.output.respond_to?(:new)
3132

32-
@descriptions[method_name] = { method: method_name }
33+
@descriptions[method_name] = { method: method_name }
3334

34-
@descriptions[method_name][:input] = input if input
35-
@descriptions[method_name][:output] = output if output
36-
end
35+
@descriptions[method_name][:input] = input if input
36+
@descriptions[method_name][:output] = output if output
37+
end
3738

38-
def describe(method_name)
39-
@descriptions[method_name.to_s]
40-
end
39+
def describe(method_name)
40+
@descriptions[method_name.to_s]
41+
end
4142

42-
def grpc(method_name)
43-
@grpc[method_name.to_s]
43+
def grpc(method_name)
44+
@grpc[method_name.to_s]
45+
end
4446
end
4547
end

controllers/grpc_generator.rb

+56-54
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,80 @@
33
require 'fileutils'
44
require 'rainbow'
55

6-
module GrpcGeneratorController
7-
REQUIRE_REGEX = /^require\s+['"].*_pb['"]/
6+
module LNDClientInternal
7+
module GrpcGeneratorController
8+
REQUIRE_REGEX = /^require\s+['"].*_pb['"]/
89

9-
def self.upgrade!
10-
cleanup!
10+
def self.upgrade!
11+
cleanup!
1112

12-
download!
13+
download!
1314

14-
Dir['temp/grpc-upgrade/lnd/lnrpc/**/*.proto'].each do |proto_file|
15-
next if proto_file =~ /googleapis/
15+
Dir['temp/grpc-upgrade/lnd/lnrpc/**/*.proto'].each do |proto_file|
16+
next if proto_file =~ /googleapis/
1617

17-
run!(generate_command(proto_file.sub('temp/grpc-upgrade/lnd/lnrpc/', '')), print_output: true)
18-
end
18+
run!(generate_command(proto_file.sub('temp/grpc-upgrade/lnd/lnrpc/', '')), print_output: true)
19+
end
1920

20-
fix_requires!
21+
fix_requires!
2122

22-
remove_references!
23+
remove_references!
2324

24-
puts "\nDone!"
25-
end
25+
puts "\nDone!"
26+
end
2627

27-
def self.remove_references!
28-
run!('rm -rf temp/grpc-upgrade', print_output: false)
29-
end
28+
def self.remove_references!
29+
run!('rm -rf temp/grpc-upgrade', print_output: false)
30+
end
3031

31-
def self.cleanup!
32-
run!('rm -rf components/grpc', print_output: false)
33-
run!('rm -rf temp/grpc-upgrade', print_output: false)
32+
def self.cleanup!
33+
run!('rm -rf components/grpc', print_output: false)
34+
run!('rm -rf temp/grpc-upgrade', print_output: false)
3435

35-
FileUtils.mkdir_p('components/grpc')
36-
FileUtils.mkdir_p('temp/grpc-upgrade')
37-
end
36+
FileUtils.mkdir_p('components/grpc')
37+
FileUtils.mkdir_p('temp/grpc-upgrade')
38+
end
3839

39-
def self.download!
40-
run!('git clone https://github.com/lightningnetwork/lnd.git temp/grpc-upgrade/lnd', print_output: false)
41-
run!('git clone https://github.com/googleapis/googleapis.git temp/grpc-upgrade/lnd/lnrpc/googleapis',
42-
print_output: false)
43-
end
40+
def self.download!
41+
run!('git clone https://github.com/lightningnetwork/lnd.git temp/grpc-upgrade/lnd', print_output: false)
42+
run!('git clone https://github.com/googleapis/googleapis.git temp/grpc-upgrade/lnd/lnrpc/googleapis',
43+
print_output: false)
44+
end
4445

45-
def self.generate_command(proto_file)
46-
[
47-
'cd temp/grpc-upgrade/lnd/lnrpc/; grpc_tools_ruby_protoc',
48-
'--proto_path googleapis:.',
49-
'--ruby_out=../../../../components/grpc/',
50-
'--grpc_out=../../../../components/grpc/',
51-
proto_file
52-
].join(' ')
53-
end
46+
def self.generate_command(proto_file)
47+
[
48+
'cd temp/grpc-upgrade/lnd/lnrpc/; grpc_tools_ruby_protoc',
49+
'--proto_path googleapis:.',
50+
'--ruby_out=../../../../components/grpc/',
51+
'--grpc_out=../../../../components/grpc/',
52+
proto_file
53+
].join(' ')
54+
end
5455

55-
def self.fix_requires!
56-
5.times do
57-
Dir['components/grpc/**/*.rb'].each do |file|
58-
content = File.read(file)
59-
next unless content =~ REQUIRE_REGEX
56+
def self.fix_requires!
57+
5.times do
58+
Dir['components/grpc/**/*.rb'].each do |file|
59+
content = File.read(file)
60+
next unless content =~ REQUIRE_REGEX
6061

61-
apply!(file, content)
62+
apply!(file, content)
63+
end
6264
end
6365
end
64-
end
6566

66-
def self.apply!(file, content)
67-
old_code = content[REQUIRE_REGEX]
68-
new_code = content[REQUIRE_REGEX].sub('require', 'require_relative')
67+
def self.apply!(file, content)
68+
old_code = content[REQUIRE_REGEX]
69+
new_code = content[REQUIRE_REGEX].sub('require', 'require_relative')
6970

70-
puts "\n#{Rainbow('>').yellow} #{file}"
71-
puts " #{Rainbow(old_code).red} -> #{Rainbow(new_code).green}"
72-
File.write(file, content.gsub(old_code, new_code))
73-
end
71+
puts "\n#{Rainbow('>').yellow} #{file}"
72+
puts " #{Rainbow(old_code).red} -> #{Rainbow(new_code).green}"
73+
File.write(file, content.gsub(old_code, new_code))
74+
end
7475

75-
def self.run!(command, print_output: false)
76-
puts "\n#{Rainbow('>').yellow} #{command}"
77-
output = `#{command}`
78-
puts output if print_output
76+
def self.run!(command, print_output: false)
77+
puts "\n#{Rainbow('>').yellow} #{command}"
78+
output = `#{command}`
79+
puts output if print_output
80+
end
7981
end
8082
end

0 commit comments

Comments
 (0)