lnd-tool 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29a0b231c3f98d1f7f9406ae0430144a2f6d9559df8ef7e320e88ce6ccaca60e
4
- data.tar.gz: 5c667e772a6f07445e1b80be330f00c8aedb870d2f68c1f269cb0afe210ab2bc
3
+ metadata.gz: a6d5e1b2aa6a45fc2bc9f9e1fc3d3b5ac739c8deb0708a03beb95d372b0d8ba9
4
+ data.tar.gz: cc803a39f5a7f95a98ba3b4dfd6a24fa79e863f1b6b74ddcd22dfa8134d74cb1
5
5
  SHA512:
6
- metadata.gz: 76090b33c85bc4bad788e397ba35480905c5801b5f07813eb238dc19946eb7bc7b7a4412df7c9811beb08b05909b209437520c9a5452c126aa94c5caedb48092
7
- data.tar.gz: fe4e971a7d10976797f34fb8c7093ae69e8c4bca542e57a32c74d13f61b4d0ed8e82e28d9397bd0b595d336456a15f344f84f8e5882a4a30c782aa802f0ddf30
6
+ metadata.gz: 381b5cdf5cc19157ab025feb2f2a4ec7c40ac5ca3b1af1c38e2786a470d52eadfe2f370cd707b826d0084e34061be18cdf3968936f51fbfa51760f56fbfa122d
7
+ data.tar.gz: 450bfd39f6963e3c49ea70420fafb48d9acea6b392b4b180a16ed510b573c22493f0b080cc043975a6add3b32f460f9b5dd3e4723767598fd7856742856e75a4
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Lnd::Tool
1
+ # LND::Tool [![Build Status](https://github.com/azuchi/lnd-tool/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/azuchi/lnd-tool/actions/workflows/main.yml) [![Gem Version](https://badge.fury.io/rb/lnd-tool.svg)](https://badge.fury.io/rb/lnd-tool) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lnd/tool`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ This is a tool for LND written in Ruby. Subscribe to htlc events in LND and save their contents to SQLite3.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,22 +20,67 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Configuration
24
+
25
+ First, create a configuration file for the gRPC connection to LND.
26
+
27
+ * config.yml
28
+
29
+ ```yaml
30
+ lnd:
31
+ credentials_path: 'Path to credential file like $LND_HOME/tls.cert.'
32
+ macaroon_path: 'Path to the macaroon file created by lnd like $LND_HOME/data/chain/bitcoin/mainnet/admin.macaroon'
33
+ host: 'lnd host'
34
+ port: 10009 # gRPC port
35
+ ```
36
+
37
+ ### Capture HTLC events
38
+
39
+ Run following command specifying the above configuration file.
40
+
41
+ $ lnd-tool capture_htlc --config "Path to configuration file"
42
+
43
+ Run process in the background as a daemon and starts capturing the HTLC event.
26
44
 
27
- ## Development
45
+ A directory `$HOME/.lnd-tool` will be created and a SQLite3 database named `storage.db`
46
+ will be created in it. You can access this database:
28
47
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+ $ sqlite3 $HOME/.lnd-tool
49
+ SQLite version 3.27.2 2019-02-25 16:06:06
50
+ Enter ".help" for usage hints.
51
+ sqlite> .tables
52
+ HtlcEvent
53
+ sqlite> .header on
54
+ sqlite> .mode column
55
+ sqlite> select * from HtlcEvent;
56
+ id incoming_channel_id outgoing_channel_id incoming_htlc_id outgoing_htlc_id timestamp_ns event_type forward_event forward_fail_event settle_event link_fail_event created_datetime
57
+ ---------- ------------------- ------------------- ---------------- ---------------- ------------------- ---------- ------------- ------------------ ------------ -------------------------------------------------------------------------------------------------------------------- -------------------
58
+ 1 759077539161571329 781080965883559937 201 0 1637643738317254952 FORWARD {"info":{"incomingTimelock":711047,"outgoingTimelock":711007,"incomingAmtMsat":"250004750","outgoingAmtMsat":"250001250"},"wireFailure":"TEMPORARY_CHANNEL_FAILURE","failureDetail":"INSUFFICIENT_BALANCE","failureString":"insufficient bandwidth to route htlc"} 2021-11-23 14:02:18
59
+ 2 759077539161571329 781080965883559937 202 0 1637643771224622997 FORWARD {"info":{"incomingTimelock":711047,"outgoingTimelock":711007,"incomingAmtMsat":"973389706","outgoingAmtMsat":"973378973"},"wireFailure":"TEMPORARY_CHANNEL_FAILURE","failureDetail":"INSUFFICIENT_BALANCE","failureString":"insufficient bandwidth to route htlc"} 2021-11-23 14:02:51
30
60
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+ If you stop the capturing, run following command:
32
62
 
33
- ## Contributing
63
+ $ lnd-tool stop_capture
34
64
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lnd-tool. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/lnd-tool/blob/master/CODE_OF_CONDUCT.md).
65
+ ### Search HTLC events
36
66
 
37
- ## License
67
+ You can use the `query_htlc` command to retrieve the data of HTLC events stored in SQLite3.
68
+ Since it is a search to the database, it can be run even if the capture process is not running.
38
69
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
70
+ $ lnd-tool query_htlc
71
+ +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
72
+ | HTLC Events |
73
+ +--------------------+---------------+--------------------+---------------+---------------------------+------------+--------------+------------------+---------------+---------------+
74
+ | incoming channel | incoming htlc | outgoing channel | outgoing htlc | timestamp | event type | result | detail | incoming msat | outgoing msat |
75
+ +--------------------+---------------+--------------------+---------------+---------------------------+------------+--------------+------------------+---------------+---------------+
76
+ | 759077539161571329 | 259 | 781080965883559937 | 0 | 2021-11-29 13:29:57 +0900 | FORWARD | LINK_FAIL | FEE_INSUFFICIENT | 75002825 | 75001075 |
77
+ | 759526139822866433 | 32 | 759077539161571329 | 441 | 2021-11-25 16:34:48 +0900 | FORWARD | FORWARD FAIL | | | |
78
+ | 759526139822866433 | 32 | 759077539161571329 | 441 | 2021-11-25 16:34:44 +0900 | FORWARD | FORWARD | | 100050900 | 100000000 |
79
+ | 759526139822866433 | 31 | 759077539161571329 | 440 | 2021-11-25 16:34:39 +0900 | FORWARD | FORWARD FAIL | | | |
80
+ | 759526139822866433 | 31 | 759077539161571329 | 440 | 2021-11-25 16:34:36 +0900 | FORWARD | FORWARD | | 11005 | 10000 |
81
+ +--------------------+---------------+--------------------+---------------+---------------------------+------------+--------------+------------------+---------------+---------------+
40
82
 
41
- ## Code of Conduct
83
+ The `query_htlc` command has the following options.
42
84
 
43
- Everyone interacting in the Lnd::Tool project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lnd-tool/blob/master/CODE_OF_CONDUCT.md).
85
+ * `event`: Option to specify the event type(`send`, `receive`, `forward`). e.g. `$ lnd-tool query_htlc --event=forward`
86
+ * `limit`: Option to narrows down the number of data. e.g. `$lnd-tool query_htlc --limit=30`
data/exe/lnd-tool CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'lnd/tool'
4
4
  require 'thor'
5
5
  require 'yaml'
6
+ require 'terminal-table'
6
7
 
7
8
  # CLI class of lnd-tool
8
9
  class CLI < Thor
@@ -33,6 +34,55 @@ class CLI < Thor
33
34
  LND::Tool::Daemon.stop
34
35
  end
35
36
 
37
+ desc 'query_htlc', 'Query the captured data in SQLite DB.'
38
+ option :event, type: :string,
39
+ desc: 'Targets the specified type of event. Valid type are "send", "receive", "forward".'
40
+ option :limit, type: :numeric, desc: 'Maximum number of data'
41
+ def query_htlc
42
+ if options['event'] && !%w[send receive forward].include?(options['event'])
43
+ raise Thor::Error, 'event must be specified as send, receive, or forward.'
44
+ end
45
+
46
+ headers = [
47
+ 'incoming channel',
48
+ 'incoming htlc',
49
+ 'outgoing channel',
50
+ 'outgoing htlc',
51
+ 'timestamp',
52
+ 'event type',
53
+ 'result',
54
+ 'detail',
55
+ 'incoming msat',
56
+ 'outgoing msat'
57
+ ]
58
+ store = LND::Tool::Store::HTLCEvent.new
59
+ rows = store.query(event_type: options['event']&.upcase, limit: options['limit']).map do |r|
60
+ result, detail, htlc = if r.forward_event
61
+ ['FORWARD', nil, r.forward_event.info]
62
+ elsif r.forward_fail_event
63
+ ['FORWARD FAIL']
64
+ elsif r.settle_event
65
+ ['SETTLE']
66
+ elsif r.link_fail_event
67
+ ['LINK_FAIL', r.link_fail_event.wire_failure, r.link_fail_event.info]
68
+ end
69
+ [
70
+ { value: r.incoming_channel_id, alignment: :right },
71
+ { value: r.incoming_htlc_id, alignment: :right },
72
+ { value: r.outgoing_channel_id, alignment: :right },
73
+ { value: r.outgoing_htlc_id, alignment: :right },
74
+ Time.at(r.timestamp_ns / 1000000000),
75
+ r.event_type,
76
+ result,
77
+ detail,
78
+ { value: htlc&.incoming_amt_msat, alignment: :right },
79
+ { value: htlc&.outgoing_amt_msat, alignment: :right }
80
+ ]
81
+ end
82
+ table = Terminal::Table.new(title: 'HTLC Events', headings: headers, rows: rows)
83
+ puts table
84
+ end
85
+
36
86
  end
37
87
 
38
88
  CLI.start(ARGV)
@@ -47,7 +47,7 @@ module LND
47
47
  end
48
48
 
49
49
  # Start block program as daemon.
50
- def start()
50
+ def start
51
51
  base_dir.mkdir unless base_dir.exist?
52
52
  raise LND::Tool::Error, "process(#{pid_path.read.to_i}) already running." if running?
53
53
 
@@ -55,7 +55,35 @@ module LND
55
55
  incoming_htlc_id, outgoing_htlc_id, timestamp_ns, event_type, forward_event, forward_fail_event,
56
56
  settle_event, link_fail_event FROM HtlcEvent ORDER BY created_datetime DESC
57
57
  SQL
58
- db.query(query).lazy.map do |result|
58
+ convert(db.query(query))
59
+ end
60
+
61
+ # @param [String] event_type event type condition
62
+ # @param [Integer] limit limit for return value
63
+ # @return [Array]
64
+ def query(event_type: nil, limit: nil)
65
+ query = <<~SQL
66
+ SELECT incoming_channel_id, outgoing_channel_id,
67
+ incoming_htlc_id, outgoing_htlc_id, timestamp_ns, event_type, forward_event, forward_fail_event,
68
+ settle_event, link_fail_event FROM HtlcEvent
69
+ SQL
70
+ bind = []
71
+ if event_type
72
+ query << ' WHERE event_type = ?'
73
+ bind << event_type
74
+ end
75
+ query << ' ORDER BY created_datetime DESC'
76
+ if limit
77
+ query << ' LIMIT ?'
78
+ bind << limit
79
+ end
80
+ convert(db.query(query, bind))
81
+ end
82
+
83
+ private
84
+
85
+ def convert(recode_sets)
86
+ recode_sets.lazy.map do |result|
59
87
  forward_event = result[6] ? Routerrpc::ForwardEvent.decode_json(result[6]) : nil
60
88
  forward_fail_event = result[7] ? Routerrpc::ForwardFailEvent.decode_json(result[7]) : nil
61
89
  settle_event = result[8] ? Routerrpc::SettleEvent.decode_json(result[8]) : nil
@@ -2,6 +2,6 @@
2
2
 
3
3
  module LND
4
4
  module Tool
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
data/lnd-tool.gemspec CHANGED
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
30
30
  # Uncomment to register a new dependency of your gem
31
31
  spec.add_dependency 'lnrpc', '~> 0.13.0'
32
32
  spec.add_dependency 'sqlite3', '~> 1.4.2'
33
+ spec.add_dependency 'terminal-table', '~> 3.0.2'
33
34
  spec.add_dependency 'thor', '~> 1.1.0'
34
35
 
35
36
  spec.add_development_dependency 'rspec', '~> 3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lnd-tool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-23 00:00:00.000000000 Z
11
+ date: 2021-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lnrpc
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.4.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: terminal-table
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.2
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: thor
43
57
  requirement: !ruby/object:Gem::Requirement